Poll MR and send messages to consumers
[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         "reflect"
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         os.Setenv("LOG_LEVEL", "Debug")
35         os.Setenv("INFO_PRODUCER_SUPERVISION_CALLBACK_HOST", "supervisionCallbackHost")
36         os.Setenv("INFO_PRODUCER_SUPERVISION_CALLBACK_PORT", "8095")
37         os.Setenv("INFO_JOB_CALLBACK_HOST", "jobCallbackHost")
38         os.Setenv("INFO_JOB_CALLBACK_PORT", "8096")
39         os.Setenv("INFO_COORD_ADDR", "infoCoordAddr")
40         os.Setenv("MR_HOST", "mrHost")
41         os.Setenv("MR_PORT", "3908")
42         t.Cleanup(func() {
43                 os.Clearenv()
44         })
45         wantConfig := Config{
46                 LogLevel:                            "Debug",
47                 InfoProducerSupervisionCallbackHost: "supervisionCallbackHost",
48                 InfoProducerSupervisionCallbackPort: 8095,
49                 InfoJobCallbackHost:                 "jobCallbackHost",
50                 InfoJobCallbackPort:                 8096,
51                 InfoCoordinatorAddress:              "infoCoordAddr",
52                 MRHost:                              "mrHost",
53                 MRPort:                              3908,
54         }
55         if got := New(); !reflect.DeepEqual(got, &wantConfig) {
56                 t.Errorf("New() = %v, want %v", got, &wantConfig)
57         }
58 }
59
60 func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
61         assertions := require.New(t)
62         var buf bytes.Buffer
63         log.SetOutput(&buf)
64
65         os.Setenv("INFO_PRODUCER_SUPERVISION_CALLBACK_PORT", "wrong")
66         t.Cleanup(func() {
67                 log.SetOutput(os.Stderr)
68                 os.Clearenv()
69         })
70         wantConfig := Config{
71                 LogLevel:                            "Info",
72                 InfoProducerSupervisionCallbackHost: "",
73                 InfoProducerSupervisionCallbackPort: 8085,
74                 InfoJobCallbackHost:                 "",
75                 InfoJobCallbackPort:                 8086,
76                 InfoCoordinatorAddress:              "http://enrichmentservice:8083",
77                 MRHost:                              "http://message-router.onap",
78                 MRPort:                              3904,
79         }
80         if got := New(); !reflect.DeepEqual(got, &wantConfig) {
81                 t.Errorf("New() = %v, want %v", got, &wantConfig)
82         }
83         logString := buf.String()
84         assertions.Contains(logString, "Invalid int value: wrong for variable: INFO_PRODUCER_SUPERVISION_CALLBACK_PORT. Default value: 8085 will be used")
85 }
86
87 func TestNew_envVarsNotSetConfigContainDefaultValues(t *testing.T) {
88         wantConfig := Config{
89                 LogLevel:                            "Info",
90                 InfoProducerSupervisionCallbackHost: "",
91                 InfoProducerSupervisionCallbackPort: 8085,
92                 InfoJobCallbackHost:                 "",
93                 InfoJobCallbackPort:                 8086,
94                 InfoCoordinatorAddress:              "http://enrichmentservice:8083",
95                 MRHost:                              "http://message-router.onap",
96                 MRPort:                              3904,
97         }
98         if got := New(); !reflect.DeepEqual(got, &wantConfig) {
99                 t.Errorf("New() = %v, want %v", got, &wantConfig)
100         }
101 }