X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Fjobs%2Fjobs_test.go;h=8533719ee3fb05f52b70b2b562fcc8bd8411f7de;hb=cc74808f1f5835a0b42c77ced5a585648d6d434c;hp=6f2922724d04f4b1d70ecf60d54d656b6d6c779a;hpb=fe1aa1e639246094905f6a9f09a051084bf431ed;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/jobs/jobs_test.go b/dmaap-mediator-producer/internal/jobs/jobs_test.go index 6f292272..8533719e 100644 --- a/dmaap-mediator-producer/internal/jobs/jobs_test.go +++ b/dmaap-mediator-producer/internal/jobs/jobs_test.go @@ -31,7 +31,7 @@ import ( "time" "github.com/stretchr/testify/require" - "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient" + "oransc.org/nonrtric/dmaapmediatorproducer/internal/config" ) const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1"}]}` @@ -42,31 +42,31 @@ func TestGetTypes_filesOkShouldReturnSliceOfTypesAndProvideSupportedTypes(t *tes if err != nil { t.Errorf("Unable to create temporary directory for types due to: %v", err) } + fname := filepath.Join(typesDir, "type_config.json") + handlerUnderTest := NewJobHandlerImpl(fname, nil, nil) t.Cleanup(func() { os.RemoveAll(typesDir) - clearAll() + handlerUnderTest.clearAll() }) - fname := filepath.Join(typesDir, "type_config.json") - configFile = fname 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 := GetTypes() - wantedType := TypeData{ - TypeId: "type1", - DMaaPTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1", - Jobs: make(map[string]JobInfo), + types, err := handlerUnderTest.GetTypes() + wantedType := config.TypeDefinition{ + Id: "type1", + DmaapTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1", } - wantedTypes := []TypeData{wantedType} + wantedTypes := []config.TypeDefinition{wantedType} assertions.EqualValues(wantedTypes, types) assertions.Nil(err) - supportedTypes := GetSupportedTypes() + supportedTypes := handlerUnderTest.GetSupportedTypes() assertions.EqualValues([]string{"type1"}, supportedTypes) } func TestAddJobWhenTypeIsSupported_shouldAddJobToAllJobsMap(t *testing.T) { assertions := require.New(t) + handlerUnderTest := NewJobHandlerImpl("", nil, nil) wantedJob := JobInfo{ Owner: "owner", LastUpdated: "now", @@ -75,66 +75,72 @@ func TestAddJobWhenTypeIsSupported_shouldAddJobToAllJobsMap(t *testing.T) { InfoJobData: "{}", InfoTypeIdentity: "type1", } - allTypes["type1"] = TypeData{ + handlerUnderTest.allTypes["type1"] = TypeData{ TypeId: "type1", Jobs: map[string]JobInfo{"job1": wantedJob}, } t.Cleanup(func() { - clearAll() + handlerUnderTest.clearAll() }) - err := AddJob(wantedJob) + err := handlerUnderTest.AddJob(wantedJob) assertions.Nil(err) - assertions.Equal(1, len(allTypes["type1"].Jobs)) - assertions.Equal(wantedJob, allTypes["type1"].Jobs["job1"]) + assertions.Equal(1, len(handlerUnderTest.allTypes["type1"].Jobs)) + assertions.Equal(wantedJob, handlerUnderTest.allTypes["type1"].Jobs["job1"]) } func TestAddJobWhenTypeIsNotSupported_shouldReturnError(t *testing.T) { assertions := require.New(t) + handlerUnderTest := NewJobHandlerImpl("", nil, nil) jobInfo := JobInfo{ InfoTypeIdentity: "type1", } - err := AddJob(jobInfo) + err := handlerUnderTest.AddJob(jobInfo) assertions.NotNil(err) assertions.Equal("type not supported: type1", err.Error()) } func TestAddJobWhenJobIdMissing_shouldReturnError(t *testing.T) { assertions := require.New(t) - allTypes["type1"] = TypeData{ + handlerUnderTest := NewJobHandlerImpl("", nil, nil) + handlerUnderTest.allTypes["type1"] = TypeData{ TypeId: "type1", } t.Cleanup(func() { - clearAll() + handlerUnderTest.clearAll() }) + jobInfo := JobInfo{ InfoTypeIdentity: "type1", } - - err := AddJob(jobInfo) + err := handlerUnderTest.AddJob(jobInfo) assertions.NotNil(err) assertions.Equal("missing required job identity: { type1}", err.Error()) } func TestAddJobWhenTargetUriMissing_shouldReturnError(t *testing.T) { assertions := require.New(t) - allTypes["type1"] = TypeData{ + handlerUnderTest := NewJobHandlerImpl("", nil, nil) + handlerUnderTest.allTypes["type1"] = TypeData{ TypeId: "type1", } + t.Cleanup(func() { + handlerUnderTest.clearAll() + }) + jobInfo := JobInfo{ InfoTypeIdentity: "type1", InfoJobIdentity: "job1", } - - err := AddJob(jobInfo) + err := handlerUnderTest.AddJob(jobInfo) assertions.NotNil(err) assertions.Equal("missing required target URI: { job1 type1}", err.Error()) - clearAll() } func TestDeleteJob(t *testing.T) { assertions := require.New(t) + handlerUnderTest := NewJobHandlerImpl("", nil, nil) jobToKeep := JobInfo{ InfoJobIdentity: "job1", InfoTypeIdentity: "type1", @@ -143,52 +149,44 @@ func TestDeleteJob(t *testing.T) { InfoJobIdentity: "job2", InfoTypeIdentity: "type1", } - allTypes["type1"] = TypeData{ + handlerUnderTest.allTypes["type1"] = TypeData{ TypeId: "type1", Jobs: map[string]JobInfo{"job1": jobToKeep, "job2": jobToDelete}, } t.Cleanup(func() { - clearAll() + handlerUnderTest.clearAll() }) - DeleteJob("job2") - assertions.Equal(1, len(allTypes["type1"].Jobs)) - assertions.Equal(jobToKeep, allTypes["type1"].Jobs["job1"]) + handlerUnderTest.DeleteJob("job2") + assertions.Equal(1, len(handlerUnderTest.allTypes["type1"].Jobs)) + assertions.Equal(jobToKeep, handlerUnderTest.allTypes["type1"].Jobs["job1"]) } func TestPollAndDistributeMessages(t *testing.T) { assertions := require.New(t) - jobInfo := JobInfo{ - InfoTypeIdentity: "type1", - InfoJobIdentity: "job1", - TargetUri: "http://consumerHost/target", - } - allTypes["type1"] = TypeData{ - TypeId: "type1", - DMaaPTopicURL: "topicUrl", - Jobs: map[string]JobInfo{"job1": jobInfo}, - } - t.Cleanup(func() { - clearAll() - }) wg := sync.WaitGroup{} - wg.Add(2) // Two calls should be made to the server, one to poll and one to distribute messages := `[{"message": {"data": "data"}}]` - clientMock := NewTestClient(func(req *http.Request) *http.Response { + pollClientMock := NewTestClient(func(req *http.Request) *http.Response { if req.URL.String() == "http://mrAddr/topicUrl" { assertions.Equal(req.Method, "GET") - wg.Done() + wg.Done() // Signal that the poll call has been made return &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewReader([]byte(messages))), Header: make(http.Header), // Must be set to non-nil value or it panics } - } else if req.URL.String() == "http://consumerHost/target" { + } + t.Error("Wrong call to client: ", req) + t.Fail() + return nil + }) + 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("application/json; charset=utf-8", req.Header.Get("Content-Type")) - wg.Done() + wg.Done() // Signal that the distribution call has been made return &http.Response{ StatusCode: 200, Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)), @@ -200,9 +198,24 @@ func TestPollAndDistributeMessages(t *testing.T) { return nil }) - restclient.Client = clientMock + handlerUnderTest := NewJobHandlerImpl("", pollClientMock, distributeClientMock) - pollAndDistributeMessages("http://mrAddr") + jobInfo := JobInfo{ + InfoTypeIdentity: "type1", + InfoJobIdentity: "job1", + TargetUri: "http://consumerHost/target", + } + handlerUnderTest.allTypes["type1"] = TypeData{ + TypeId: "type1", + DMaaPTopicURL: "topicUrl", + Jobs: map[string]JobInfo{"job1": jobInfo}, + } + t.Cleanup(func() { + handlerUnderTest.clearAll() + }) + + wg.Add(2) // Two calls should be made to the server, one to poll and one to distribute + handlerUnderTest.pollAndDistributeMessages("http://mrAddr") if waitTimeout(&wg, 100*time.Millisecond) { t.Error("Not all calls to server were made")