2 // ========================LICENSE_START=================================
5 // Copyright (C) 2021: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
30 log "github.com/sirupsen/logrus"
31 "github.com/stretchr/testify/require"
34 func TestNew_envVarsSetConfigContainSetValues(t *testing.T) {
35 assertions := require.New(t)
36 os.Setenv("LOG_LEVEL", "Debug")
37 os.Setenv("INFO_PRODUCER_HOST", "producerHost")
38 os.Setenv("INFO_PRODUCER_PORT", "8095")
39 os.Setenv("INFO_COORD_ADDR", "infoCoordAddr")
40 os.Setenv("DMAAP_MR_ADDR", "mrHost:3908")
41 os.Setenv("KAFKA_BOOTSTRAP_SERVERS", "localhost:9093")
42 os.Setenv("PRODUCER_CERT_PATH", "cert")
43 os.Setenv("PRODUCER_KEY_PATH", "key")
48 LogLevel: log.DebugLevel,
49 InfoProducerHost: "producerHost",
50 InfoProducerPort: 8095,
51 InfoCoordinatorAddress: "infoCoordAddr",
52 DMaaPMRAddress: "mrHost:3908",
53 KafkaBootstrapServers: "localhost:9093",
54 ProducerCertPath: "cert",
55 ProducerKeyPath: "key",
59 assertions.Equal(&wantConfig, got)
62 func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
63 assertions := require.New(t)
67 os.Setenv("INFO_PRODUCER_PORT", "wrong")
69 log.SetOutput(os.Stderr)
73 LogLevel: log.InfoLevel,
75 InfoProducerPort: 8085,
76 InfoCoordinatorAddress: "https://informationservice:8434",
77 DMaaPMRAddress: "https://message-router.onap:3905",
78 KafkaBootstrapServers: "localhost:9092",
79 ProducerCertPath: "security/producer.crt",
80 ProducerKeyPath: "security/producer.key",
83 assertions.Equal(&wantConfig, got)
84 logString := buf.String()
85 assertions.Contains(logString, "Invalid int value: wrong for variable: INFO_PRODUCER_PORT. Default value: 8085 will be used")
88 func TestNew_envFaultyLogLevelConfigContainDefaultValues(t *testing.T) {
89 assertions := require.New(t)
93 os.Setenv("LOG_LEVEL", "wrong")
95 log.SetOutput(os.Stderr)
100 LogLevel: log.InfoLevel,
101 InfoProducerHost: "",
102 InfoProducerPort: 8085,
103 InfoCoordinatorAddress: "https://informationservice:8434",
104 DMaaPMRAddress: "https://message-router.onap:3905",
105 KafkaBootstrapServers: "localhost:9092",
106 ProducerCertPath: "security/producer.crt",
107 ProducerKeyPath: "security/producer.key",
112 assertions.Equal(&wantConfig, got)
113 logString := buf.String()
114 assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
117 func TestGetJobTypesFromConfiguration_fileOkShouldReturnSliceOfTypeDefinitions(t *testing.T) {
118 assertions := require.New(t)
119 typesDir := CreateTypeConfigFiles(t)
121 os.RemoveAll(typesDir)
124 var typeSchemaObj interface{}
125 json.Unmarshal([]byte(typeSchemaFileContent), &typeSchemaObj)
127 types, err := GetJobTypesFromConfiguration(typesDir)
129 wantedDMaaPType := TypeDefinition{
131 DMaaPTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1",
132 TypeSchema: typeSchemaObj,
134 wantedKafkaType := TypeDefinition{
136 KafkaInputTopic: "TestTopic",
137 TypeSchema: typeSchemaObj,
139 wantedTypes := []TypeDefinition{wantedDMaaPType, wantedKafkaType}
140 assertions.EqualValues(wantedTypes, types)
144 const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1"}, {"id": "type2", "kafkaInputTopic": "TestTopic"}]}`
145 const typeSchemaFileContent = `{
146 "$schema": "http://json-schema.org/draft-04/schema#",
153 "additionalProperties": false
156 func CreateTypeConfigFiles(t *testing.T) string {
157 typesDir, err := os.MkdirTemp("", "configs")
159 t.Errorf("Unable to create temporary directory for types due to: %v", err)
161 fname := filepath.Join(typesDir, "type_config.json")
162 if err = os.WriteFile(fname, []byte(typeDefinition), 0666); err != nil {
163 t.Errorf("Unable to create temporary config file for types due to: %v", err)
165 fname = filepath.Join(typesDir, "typeSchemaDmaap.json")
166 if err = os.WriteFile(fname, []byte(typeSchemaFileContent), 0666); err != nil {
167 t.Errorf("Unable to create temporary schema file for DMaaP type due to: %v", err)
169 fname = filepath.Join(typesDir, "typeSchemaKafka.json")
170 if err = os.WriteFile(fname, []byte(typeSchemaFileContent), 0666); err != nil {
171 t.Errorf("Unable to create temporary schema file for Kafka type due to: %v", err)