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 const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1"}, {"id": "type2", "kafkaInputTopic": "TestTopic"}]}`
118 const typeSchemaFileContent = `{
119 "$schema": "http://json-schema.org/draft-04/schema#",
126 "additionalProperties": false
129 func TestGetTypesFromConfiguration_fileOkShouldReturnSliceOfTypeDefinitions(t *testing.T) {
130 assertions := require.New(t)
131 typesDir, err := os.MkdirTemp("", "configs")
133 t.Errorf("Unable to create temporary directory for types due to: %v", err)
135 fname := filepath.Join(typesDir, "type_config.json")
137 os.RemoveAll(typesDir)
139 if err = os.WriteFile(fname, []byte(typeDefinition), 0666); err != nil {
140 t.Errorf("Unable to create temporary config file for types due to: %v", err)
142 fname = filepath.Join(typesDir, "typeSchemaDmaap.json")
143 if err = os.WriteFile(fname, []byte(typeSchemaFileContent), 0666); err != nil {
144 t.Errorf("Unable to create temporary schema file for DMaaP type due to: %v", err)
146 fname = filepath.Join(typesDir, "typeSchemaKafka.json")
147 if err = os.WriteFile(fname, []byte(typeSchemaFileContent), 0666); err != nil {
148 t.Errorf("Unable to create temporary schema file for Kafka type due to: %v", err)
150 var typeSchemaObj interface{}
151 json.Unmarshal([]byte(typeSchemaFileContent), &typeSchemaObj)
153 types, err := GetJobTypesFromConfiguration(typesDir)
155 wantedDMaaPType := TypeDefinition{
157 DMaaPTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1",
158 TypeSchema: typeSchemaObj,
160 wantedKafkaType := TypeDefinition{
162 KafkaInputTopic: "TestTopic",
163 TypeSchema: typeSchemaObj,
165 wantedTypes := []TypeDefinition{wantedDMaaPType, wantedKafkaType}
166 assertions.EqualValues(wantedTypes, types)