X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Fconfig%2Fconfig_test.go;h=0e081a82379477e2d8beb8b70ffbdeb9d5565452;hb=6b45b053fe289bf0b01b40e4939832fa636e1617;hp=0fcbdd3f5c243de0d6dbd578fc79864faf41cf14;hpb=47d0ee37691eddc290a1f9e34091dfd2020db07f;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/config/config_test.go b/dmaap-mediator-producer/internal/config/config_test.go index 0fcbdd3f..0e081a82 100644 --- a/dmaap-mediator-producer/internal/config/config_test.go +++ b/dmaap-mediator-producer/internal/config/config_test.go @@ -22,8 +22,9 @@ package config import ( "bytes" + "encoding/json" "os" - "reflect" + "path/filepath" "testing" log "github.com/sirupsen/logrus" @@ -31,26 +32,31 @@ import ( ) 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") os.Setenv("INFO_COORD_ADDR", "infoCoordAddr") - os.Setenv("MR_HOST", "mrHost") - os.Setenv("MR_PORT", "3908") + os.Setenv("DMAAP_MR_ADDR", "mrHost:3908") + os.Setenv("KAFKA_BOOTSTRAP_SERVERS", "localhost:9093") + os.Setenv("PRODUCER_CERT_PATH", "cert") + os.Setenv("PRODUCER_KEY_PATH", "key") t.Cleanup(func() { 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) + DMaaPMRAddress: "mrHost:3908", + KafkaBootstrapServers: "localhost:9093", + ProducerCertPath: "cert", + ProducerKeyPath: "key", } + got := New() + + assertions.Equal(&wantConfig, got) } func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T) { @@ -64,30 +70,105 @@ func TestNew_faultyIntValueSetConfigContainDefaultValueAndWarnInLog(t *testing.T 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) + InfoCoordinatorAddress: "https://informationservice:8434", + DMaaPMRAddress: "https://message-router.onap:3905", + KafkaBootstrapServers: "localhost:9092", + 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", + LogLevel: log.InfoLevel, InfoProducerHost: "", InfoProducerPort: 8085, - InfoCoordinatorAddress: "http://enrichmentservice:8083", - MRHost: "http://message-router.onap", - MRPort: 3904, + InfoCoordinatorAddress: "https://informationservice:8434", + DMaaPMRAddress: "https://message-router.onap:3905", + KafkaBootstrapServers: "localhost:9092", + ProducerCertPath: "security/producer.crt", + ProducerKeyPath: "security/producer.key", + } + + got := New() + + assertions.Equal(&wantConfig, got) + logString := buf.String() + assertions.Contains(logString, "Invalid log level: wrong. Log level will be Info!") +} + +func TestGetJobTypesFromConfiguration_fileOkShouldReturnSliceOfTypeDefinitions(t *testing.T) { + assertions := require.New(t) + typesDir := CreateTypeConfigFiles(t) + t.Cleanup(func() { + os.RemoveAll(typesDir) + }) + + var typeSchemaObj interface{} + json.Unmarshal([]byte(typeSchemaFileContent), &typeSchemaObj) + + types, err := GetJobTypesFromConfiguration(typesDir) + + wantedDMaaPType := TypeDefinition{ + Identity: "type1", + DMaaPTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1", + TypeSchema: typeSchemaObj, + } + wantedKafkaType := TypeDefinition{ + Identity: "type2", + KafkaInputTopic: "TestTopic", + TypeSchema: typeSchemaObj, + } + wantedTypes := []TypeDefinition{wantedDMaaPType, wantedKafkaType} + assertions.EqualValues(wantedTypes, types) + assertions.Nil(err) +} + +const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1"}, {"id": "type2", "kafkaInputTopic": "TestTopic"}]}` +const typeSchemaFileContent = `{ + "$schema": "http://json-schema.org/draft-04/schema#", + "type": "object", + "properties": { + "filter": { + "type": "string" + } + }, + "additionalProperties": false + }` + +func CreateTypeConfigFiles(t *testing.T) string { + 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") + if err = os.WriteFile(fname, []byte(typeDefinition), 0666); err != nil { + t.Errorf("Unable to create temporary config file for types due to: %v", err) + } + fname = filepath.Join(typesDir, "typeSchemaDmaap.json") + if err = os.WriteFile(fname, []byte(typeSchemaFileContent), 0666); err != nil { + t.Errorf("Unable to create temporary schema file for DMaaP type due to: %v", err) } - if got := New(); !reflect.DeepEqual(got, &wantConfig) { - t.Errorf("New() = %v, want %v", got, &wantConfig) + fname = filepath.Join(typesDir, "typeSchemaKafka.json") + if err = os.WriteFile(fname, []byte(typeSchemaFileContent), 0666); err != nil { + t.Errorf("Unable to create temporary schema file for Kafka type due to: %v", err) } + return typesDir }