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===================================
29 log "github.com/sirupsen/logrus"
30 "github.com/stretchr/testify/require"
33 func TestNew_envVarsSetConfigContainSetValues(t *testing.T) {
34 assertions := require.New(t)
35 os.Setenv("LOG_LEVEL", "Debug")
36 os.Setenv("INFO_PRODUCER_HOST", "producerHost")
37 os.Setenv("INFO_PRODUCER_PORT", "8095")
38 os.Setenv("INFO_COORD_ADDR", "infoCoordAddr")
39 os.Setenv("DMAAP_MR_ADDR", "mrHost:3908")
40 os.Setenv("PRODUCER_CERT_PATH", "cert")
41 os.Setenv("PRODUCER_KEY_PATH", "key")
46 LogLevel: log.DebugLevel,
47 InfoProducerHost: "producerHost",
48 InfoProducerPort: 8095,
49 InfoCoordinatorAddress: "infoCoordAddr",
50 DMaaPMRAddress: "mrHost:3908",
51 ProducerCertPath: "cert",
52 ProducerKeyPath: "key",
56 assertions.Equal(&wantConfig, got)
59 func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
60 assertions := require.New(t)
64 os.Setenv("INFO_PRODUCER_PORT", "wrong")
66 log.SetOutput(os.Stderr)
70 LogLevel: log.InfoLevel,
72 InfoProducerPort: 8085,
73 InfoCoordinatorAddress: "https://enrichmentservice:8434",
74 DMaaPMRAddress: "https://message-router.onap:3905",
75 ProducerCertPath: "security/producer.crt",
76 ProducerKeyPath: "security/producer.key",
79 assertions.Equal(&wantConfig, got)
80 logString := buf.String()
81 assertions.Contains(logString, "Invalid int value: wrong for variable: INFO_PRODUCER_PORT. Default value: 8085 will be used")
84 func TestNew_envFaultyLogLevelConfigContainDefaultValues(t *testing.T) {
85 assertions := require.New(t)
89 os.Setenv("LOG_LEVEL", "wrong")
91 log.SetOutput(os.Stderr)
96 LogLevel: log.InfoLevel,
98 InfoProducerPort: 8085,
99 InfoCoordinatorAddress: "https://enrichmentservice:8434",
100 DMaaPMRAddress: "https://message-router.onap:3905",
101 ProducerCertPath: "security/producer.crt",
102 ProducerKeyPath: "security/producer.key",
107 assertions.Equal(&wantConfig, got)
108 logString := buf.String()
109 assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
112 const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1"}]}`
114 func TestGetTypesFromConfiguration_fileOkShouldReturnSliceOfTypeDefinitions(t *testing.T) {
115 assertions := require.New(t)
116 typesDir, err := os.MkdirTemp("", "configs")
118 t.Errorf("Unable to create temporary directory for types due to: %v", err)
120 fname := filepath.Join(typesDir, "type_config.json")
122 os.RemoveAll(typesDir)
124 if err = os.WriteFile(fname, []byte(typeDefinition), 0666); err != nil {
125 t.Errorf("Unable to create temporary config file for types due to: %v", err)
128 types, err := GetJobTypesFromConfiguration(fname)
130 wantedType := TypeDefinition{
132 DmaapTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1",
134 wantedTypes := []TypeDefinition{wantedType}
135 assertions.EqualValues(wantedTypes, types)