Rename enrichment coordinator service to information coordinator service
[nonrtric.git] / dmaap-mediator-producer / internal / config / config_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
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
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
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===================================
19 //
20
21 package config
22
23 import (
24         "bytes"
25         "os"
26         "path/filepath"
27         "testing"
28
29         log "github.com/sirupsen/logrus"
30         "github.com/stretchr/testify/require"
31 )
32
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")
42         t.Cleanup(func() {
43                 os.Clearenv()
44         })
45         wantConfig := Config{
46                 LogLevel:               log.DebugLevel,
47                 InfoProducerHost:       "producerHost",
48                 InfoProducerPort:       8095,
49                 InfoCoordinatorAddress: "infoCoordAddr",
50                 DMaaPMRAddress:         "mrHost:3908",
51                 ProducerCertPath:       "cert",
52                 ProducerKeyPath:        "key",
53         }
54         got := New()
55
56         assertions.Equal(&wantConfig, got)
57 }
58
59 func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
60         assertions := require.New(t)
61         var buf bytes.Buffer
62         log.SetOutput(&buf)
63
64         os.Setenv("INFO_PRODUCER_PORT", "wrong")
65         t.Cleanup(func() {
66                 log.SetOutput(os.Stderr)
67                 os.Clearenv()
68         })
69         wantConfig := Config{
70                 LogLevel:               log.InfoLevel,
71                 InfoProducerHost:       "",
72                 InfoProducerPort:       8085,
73                 InfoCoordinatorAddress: "https://informationservice:8434",
74                 DMaaPMRAddress:         "https://message-router.onap:3905",
75                 ProducerCertPath:       "security/producer.crt",
76                 ProducerKeyPath:        "security/producer.key",
77         }
78         got := New()
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")
82 }
83
84 func TestNew_envFaultyLogLevelConfigContainDefaultValues(t *testing.T) {
85         assertions := require.New(t)
86         var buf bytes.Buffer
87         log.SetOutput(&buf)
88
89         os.Setenv("LOG_LEVEL", "wrong")
90         t.Cleanup(func() {
91                 log.SetOutput(os.Stderr)
92                 os.Clearenv()
93         })
94
95         wantConfig := Config{
96                 LogLevel:               log.InfoLevel,
97                 InfoProducerHost:       "",
98                 InfoProducerPort:       8085,
99                 InfoCoordinatorAddress: "https://informationservice:8434",
100                 DMaaPMRAddress:         "https://message-router.onap:3905",
101                 ProducerCertPath:       "security/producer.crt",
102                 ProducerKeyPath:        "security/producer.key",
103         }
104
105         got := New()
106
107         assertions.Equal(&wantConfig, got)
108         logString := buf.String()
109         assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
110 }
111
112 const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1"}]}`
113
114 func TestGetTypesFromConfiguration_fileOkShouldReturnSliceOfTypeDefinitions(t *testing.T) {
115         assertions := require.New(t)
116         typesDir, err := os.MkdirTemp("", "configs")
117         if err != nil {
118                 t.Errorf("Unable to create temporary directory for types due to: %v", err)
119         }
120         fname := filepath.Join(typesDir, "type_config.json")
121         t.Cleanup(func() {
122                 os.RemoveAll(typesDir)
123         })
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)
126         }
127
128         types, err := GetJobTypesFromConfiguration(fname)
129
130         wantedType := TypeDefinition{
131                 Id:            "type1",
132                 DmaapTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1",
133         }
134         wantedTypes := []TypeDefinition{wantedType}
135         assertions.EqualValues(wantedTypes, types)
136         assertions.Nil(err)
137 }