X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Fconfig%2Fconfig_test.go;h=faf5900dbb7e00fcf38923f0737bc80baabfcb92;hb=5feecd881172a3b22041d35443c1f946e7d5f63e;hp=f0106d0f914cc707b8c540aa13eba3a39f3cfc1e;hpb=7ea5ec4f81f6e8c07b890f3d7567ce699c8d8d1d;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/config/config_test.go b/dmaap-mediator-producer/internal/config/config_test.go index f0106d0f..faf5900d 100644 --- a/dmaap-mediator-producer/internal/config/config_test.go +++ b/dmaap-mediator-producer/internal/config/config_test.go @@ -21,33 +21,117 @@ package config import ( + "bytes" "os" - "reflect" + "path/filepath" "testing" + + log "github.com/sirupsen/logrus" + "github.com/stretchr/testify/require" ) func TestNew_envVarsSetConfigContainSetValues(t *testing.T) { + assertions := require.New(t) os.Setenv("LOG_LEVEL", "Debug") - os.Setenv("JOB_RESULT_URI", "testUrl") - os.Setenv("INFO_COORD_ADDR", "testAddr") - defer os.Clearenv() + os.Setenv("INFO_PRODUCER_HOST", "producerHost") + os.Setenv("INFO_PRODUCER_PORT", "8095") + os.Setenv("INFO_COORD_ADDR", "infoCoordAddr") + os.Setenv("DMAAP_MR_ADDR", "mrHost:3908") + os.Setenv("PRODUCER_CERT_PATH", "cert") + os.Setenv("PRODUCER_KEY_PATH", "key") + t.Cleanup(func() { + os.Clearenv() + }) wantConfig := Config{ - LogLevel: "Debug", - JobResultUri: "testUrl", - InfoCoordinatorAddress: "testAddr", + LogLevel: log.DebugLevel, + InfoProducerHost: "producerHost", + InfoProducerPort: 8095, + InfoCoordinatorAddress: "infoCoordAddr", + DMaaPMRAddress: "mrHost:3908", + ProducerCertPath: "cert", + ProducerKeyPath: "key", } - 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) { + assertions := require.New(t) + var buf bytes.Buffer + log.SetOutput(&buf) + + os.Setenv("INFO_PRODUCER_PORT", "wrong") + t.Cleanup(func() { + log.SetOutput(os.Stderr) + os.Clearenv() + }) + wantConfig := Config{ + LogLevel: log.InfoLevel, + InfoProducerHost: "", + InfoProducerPort: 8085, + InfoCoordinatorAddress: "https://informationservice:8434", + DMaaPMRAddress: "https://message-router.onap:3905", + ProducerCertPath: "security/producer.crt", + ProducerKeyPath: "security/producer.key", } + got := New() + assertions.Equal(&wantConfig, got) + logString := buf.String() + 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", - JobResultUri: "", - InfoCoordinatorAddress: "http://enrichmentservice:8083", + LogLevel: log.InfoLevel, + InfoProducerHost: "", + InfoProducerPort: 8085, + InfoCoordinatorAddress: "https://informationservice:8434", + DMaaPMRAddress: "https://message-router.onap:3905", + ProducerCertPath: "security/producer.crt", + ProducerKeyPath: "security/producer.key", } - 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!") +} + +const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1"}]}` + +func TestGetTypesFromConfiguration_fileOkShouldReturnSliceOfTypeDefinitions(t *testing.T) { + assertions := require.New(t) + typesDir, err := os.MkdirTemp("", "configs") + if err != nil { + t.Errorf("Unable to create temporary directory for types due to: %v", err) + } + fname := filepath.Join(typesDir, "type_config.json") + t.Cleanup(func() { + os.RemoveAll(typesDir) + }) + if err = os.WriteFile(fname, []byte(typeDefinition), 0666); err != nil { + t.Errorf("Unable to create temporary config file for types due to: %v", err) + } + + types, err := GetJobTypesFromConfiguration(fname) + + wantedType := TypeDefinition{ + Id: "type1", + DmaapTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1", } + wantedTypes := []TypeDefinition{wantedType} + assertions.EqualValues(wantedTypes, types) + assertions.Nil(err) }