Refactor Go code
Issue-ID: NONRTRIC-606
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: Icbd97b8dbf0c3b015e2c864d4d4dd3581d5ade9b
diff --git a/dmaap-mediator-producer/internal/config/config_test.go b/dmaap-mediator-producer/internal/config/config_test.go
index 0fcbdd3..fc64e57 100644
--- a/dmaap-mediator-producer/internal/config/config_test.go
+++ b/dmaap-mediator-producer/internal/config/config_test.go
@@ -31,6 +31,7 @@
)
func TestNew_envVarsSetConfigContainSetValues(t *testing.T) {
+ assertions := require.New(t)
os.Setenv("LOG_LEVEL", "Debug")
os.Setenv("INFO_PRODUCER_HOST", "producerHost")
os.Setenv("INFO_PRODUCER_PORT", "8095")
@@ -41,16 +42,16 @@
os.Clearenv()
})
wantConfig := Config{
- LogLevel: "Debug",
+ LogLevel: log.DebugLevel,
InfoProducerHost: "producerHost",
InfoProducerPort: 8095,
InfoCoordinatorAddress: "infoCoordAddr",
MRHost: "mrHost",
MRPort: 3908,
}
- if got := New(); !reflect.DeepEqual(got, &wantConfig) {
- t.Errorf("New() = %v, want %v", got, &wantConfig)
- }
+ got := New()
+
+ assertions.Equal(&wantConfig, got)
}
func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) {
@@ -64,7 +65,7 @@
os.Clearenv()
})
wantConfig := Config{
- LogLevel: "Info",
+ LogLevel: log.InfoLevel,
InfoProducerHost: "",
InfoProducerPort: 8085,
InfoCoordinatorAddress: "http://enrichmentservice:8083",
@@ -78,16 +79,29 @@
assertions.Contains(logString, "Invalid int value: wrong for variable: INFO_PRODUCER_PORT. Default value: 8085 will be used")
}
-func TestNew_envVarsNotSetConfigContainDefaultValues(t *testing.T) {
+func TestNew_envFaultyLogLevelConfigContainDefaultValues(t *testing.T) {
+ assertions := require.New(t)
+ var buf bytes.Buffer
+ log.SetOutput(&buf)
+
+ os.Setenv("LOG_LEVEL", "wrong")
+ t.Cleanup(func() {
+ log.SetOutput(os.Stderr)
+ os.Clearenv()
+ })
+
wantConfig := Config{
- LogLevel: "Info",
+ LogLevel: log.InfoLevel,
InfoProducerHost: "",
InfoProducerPort: 8085,
InfoCoordinatorAddress: "http://enrichmentservice:8083",
MRHost: "http://message-router.onap",
MRPort: 3904,
}
- if got := New(); !reflect.DeepEqual(got, &wantConfig) {
- t.Errorf("New() = %v, want %v", got, &wantConfig)
- }
+
+ got := New()
+
+ assertions.Equal(&wantConfig, got)
+ logString := buf.String()
+ assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!")
}