X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Fjobs%2Fjobs_test.go;h=30b4ffd9f3aa867fc52e5e3b224a45e7fd84a357;hb=f4969908aee0d89693e127a242005f88cdabc586;hp=e48e68d1f9229bd7fca47b60c1328b34fdf064cd;hpb=036962c60e1cfa18a689dffa1a8b92d335e6063e;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/jobs/jobs_test.go b/dmaap-mediator-producer/internal/jobs/jobs_test.go index e48e68d1..30b4ffd9 100644 --- a/dmaap-mediator-producer/internal/jobs/jobs_test.go +++ b/dmaap-mediator-producer/internal/jobs/jobs_test.go @@ -24,8 +24,6 @@ import ( "bytes" "io/ioutil" "net/http" - "os" - "path/filepath" "sync" "testing" "time" @@ -38,26 +36,18 @@ const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unau func TestJobsManagerGetTypes_filesOkShouldReturnSliceOfTypesAndProvideSupportedTypes(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") - managerUnderTest := NewJobsManagerImpl(fname, nil, "", nil) - 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 := managerUnderTest.LoadTypesFromConfiguration() + + managerUnderTest := NewJobsManagerImpl(nil, "", nil) + wantedType := config.TypeDefinition{ Id: "type1", DmaapTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1", } wantedTypes := []config.TypeDefinition{wantedType} + + types := managerUnderTest.LoadTypesFromConfiguration(wantedTypes) + assertions.EqualValues(wantedTypes, types) - assertions.Nil(err) supportedTypes := managerUnderTest.GetSupportedTypes() assertions.EqualValues([]string{"type1"}, supportedTypes) @@ -65,7 +55,7 @@ func TestJobsManagerGetTypes_filesOkShouldReturnSliceOfTypesAndProvideSupportedT func TestJobsManagerAddJobWhenTypeIsSupported_shouldAddJobToChannel(t *testing.T) { assertions := require.New(t) - managerUnderTest := NewJobsManagerImpl("", nil, "", nil) + managerUnderTest := NewJobsManagerImpl(nil, "", nil) wantedJob := JobInfo{ Owner: "owner", LastUpdated: "now", @@ -93,7 +83,7 @@ func TestJobsManagerAddJobWhenTypeIsSupported_shouldAddJobToChannel(t *testing.T func TestJobsManagerAddJobWhenTypeIsNotSupported_shouldReturnError(t *testing.T) { assertions := require.New(t) - managerUnderTest := NewJobsManagerImpl("", nil, "", nil) + managerUnderTest := NewJobsManagerImpl(nil, "", nil) jobInfo := JobInfo{ InfoTypeIdentity: "type1", } @@ -105,7 +95,7 @@ func TestJobsManagerAddJobWhenTypeIsNotSupported_shouldReturnError(t *testing.T) func TestJobsManagerAddJobWhenJobIdMissing_shouldReturnError(t *testing.T) { assertions := require.New(t) - managerUnderTest := NewJobsManagerImpl("", nil, "", nil) + managerUnderTest := NewJobsManagerImpl(nil, "", nil) managerUnderTest.allTypes["type1"] = TypeData{ TypeId: "type1", } @@ -120,7 +110,7 @@ func TestJobsManagerAddJobWhenJobIdMissing_shouldReturnError(t *testing.T) { func TestJobsManagerAddJobWhenTargetUriMissing_shouldReturnError(t *testing.T) { assertions := require.New(t) - managerUnderTest := NewJobsManagerImpl("", nil, "", nil) + managerUnderTest := NewJobsManagerImpl(nil, "", nil) managerUnderTest.allTypes["type1"] = TypeData{ TypeId: "type1", } @@ -136,7 +126,7 @@ func TestJobsManagerAddJobWhenTargetUriMissing_shouldReturnError(t *testing.T) { func TestJobsManagerDeleteJob_shouldSendDeleteToChannel(t *testing.T) { assertions := require.New(t) - managerUnderTest := NewJobsManagerImpl("", nil, "", nil) + managerUnderTest := NewJobsManagerImpl(nil, "", nil) jobsHandler := jobsHandler{ deleteJobCh: make(chan string)} managerUnderTest.allTypes["type1"] = TypeData{ @@ -177,7 +167,7 @@ func TestAddJobToJobsManager_shouldStartPollAndDistributeMessages(t *testing.T) distributeClientMock := NewTestClient(func(req *http.Request) *http.Response { if req.URL.String() == "http://consumerHost/target" { assertions.Equal(req.Method, "POST") - assertions.Equal(messages, getBodyAsString(req)) + assertions.Equal(messages, getBodyAsString(req, t)) assertions.Equal("application/json", req.Header.Get("Content-Type")) wg.Done() return &http.Response{ @@ -192,7 +182,7 @@ func TestAddJobToJobsManager_shouldStartPollAndDistributeMessages(t *testing.T) }) jobsHandler := newJobsHandler("type1", "/topicUrl", pollClientMock, distributeClientMock) - jobsManager := NewJobsManagerImpl("", pollClientMock, "http://mrAddr", distributeClientMock) + jobsManager := NewJobsManagerImpl(pollClientMock, "http://mrAddr", distributeClientMock) jobsManager.allTypes["type1"] = TypeData{ DMaaPTopicURL: "/topicUrl", TypeId: "type1", @@ -208,7 +198,8 @@ func TestAddJobToJobsManager_shouldStartPollAndDistributeMessages(t *testing.T) } wg.Add(1) // Wait till the distribution has happened - jobsManager.AddJobFromRESTCall(jobInfo) + err := jobsManager.AddJobFromRESTCall(jobInfo) + assertions.Nil(err) if waitTimeout(&wg, 2*time.Second) { t.Error("Not all calls to server were made") @@ -287,8 +278,10 @@ func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool { } } -func getBodyAsString(req *http.Request) string { +func getBodyAsString(req *http.Request, t *testing.T) string { buf := new(bytes.Buffer) - buf.ReadFrom(req.Body) + if _, err := buf.ReadFrom(req.Body); err != nil { + t.Fail() + } return buf.String() }