X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Fjobs%2Fjobs_test.go;h=552b5fa103b7096b1d639f5958ff59dc5c078c13;hb=46a0fd717e5f49ebae6cb2c4fbcf54f0e329dc86;hp=b53d85e66612e0174f7dc8b471abb6ff9d541867;hpb=280385634f160bb78a8944998e9106a2f6549eb0;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/jobs/jobs_test.go b/dmaap-mediator-producer/internal/jobs/jobs_test.go index b53d85e6..552b5fa1 100644 --- a/dmaap-mediator-producer/internal/jobs/jobs_test.go +++ b/dmaap-mediator-producer/internal/jobs/jobs_test.go @@ -26,49 +26,46 @@ import ( "net/http" "os" "path/filepath" + "sync" "testing" "time" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "oransc.org/nonrtric/dmaapmediatorproducer/internal/restclient" - "oransc.org/nonrtric/dmaapmediatorproducer/mocks" + "oransc.org/nonrtric/dmaapmediatorproducer/internal/config" ) -const typeDefinition = `{"id": "type1", "dmaapTopic": "unauthenticated.SEC_FAULT_OUTPUT", "schema": {"title": "Type 1"}}` +const typeDefinition = `{"types": [{"id": "type1", "dmaapTopicUrl": "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1"}]}` -func TestGetTypes_filesOkShouldReturnSliceOfTypesAndProvideSupportedTypes(t *testing.T) { +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) - clearAll() }) - typeDir = typesDir - fname := filepath.Join(typesDir, "type1.json") if err = os.WriteFile(fname, []byte(typeDefinition), 0666); err != nil { - t.Errorf("Unable to create temporary files for types due to: %v", err) + t.Errorf("Unable to create temporary config file for types due to: %v", err) } - types, err := GetTypes() - wantedType := Type{ - TypeId: "type1", - DMaaPTopic: "unauthenticated.SEC_FAULT_OUTPUT", - Schema: `{"title":"Type 1"}`, - Jobs: make(map[string]JobInfo), + types, err := managerUnderTest.LoadTypesFromConfiguration() + wantedType := config.TypeDefinition{ + Id: "type1", + DmaapTopicURL: "events/unauthenticated.SEC_FAULT_OUTPUT/dmaapmediatorproducer/type1", } - wantedTypes := []*Type{&wantedType} + wantedTypes := []config.TypeDefinition{wantedType} assertions.EqualValues(wantedTypes, types) assertions.Nil(err) - supportedTypes := GetSupportedTypes() + supportedTypes := managerUnderTest.GetSupportedTypes() assertions.EqualValues([]string{"type1"}, supportedTypes) } -func TestAddJobWhenTypeIsSupported_shouldAddJobToAllJobsMap(t *testing.T) { +func TestJobsManagerAddJobWhenTypeIsSupported_shouldAddJobToChannel(t *testing.T) { assertions := require.New(t) + managerUnderTest := NewJobsManagerImpl("", nil, "", nil) wantedJob := JobInfo{ Owner: "owner", LastUpdated: "now", @@ -77,110 +74,224 @@ func TestAddJobWhenTypeIsSupported_shouldAddJobToAllJobsMap(t *testing.T) { InfoJobData: "{}", InfoTypeIdentity: "type1", } - allJobs["type1"] = Type{ - TypeId: "type1", - Jobs: map[string]JobInfo{"job1": wantedJob}, + jobsHandler := jobsHandler{ + addJobCh: make(chan JobInfo)} + managerUnderTest.allTypes["type1"] = TypeData{ + TypeId: "type1", + jobsHandler: &jobsHandler, } - t.Cleanup(func() { - clearAll() - }) - err := AddJob(wantedJob) + var err error + go func() { + err = managerUnderTest.AddJobFromRESTCall(wantedJob) + }() + assertions.Nil(err) - assertions.Equal(1, len(allJobs["type1"].Jobs)) - assertions.Equal(wantedJob, allJobs["type1"].Jobs["job1"]) + addedJob := <-jobsHandler.addJobCh + assertions.Equal(wantedJob, addedJob) } -func TestAddJobWhenTypeIsNotSupported_shouldReturnError(t *testing.T) { +func TestJobsManagerAddJobWhenTypeIsNotSupported_shouldReturnError(t *testing.T) { assertions := require.New(t) + managerUnderTest := NewJobsManagerImpl("", nil, "", nil) jobInfo := JobInfo{ InfoTypeIdentity: "type1", } - err := AddJob(jobInfo) + err := managerUnderTest.AddJobFromRESTCall(jobInfo) assertions.NotNil(err) assertions.Equal("type not supported: type1", err.Error()) } -func TestAddJobWhenJobIdMissing_shouldReturnError(t *testing.T) { +func TestJobsManagerAddJobWhenJobIdMissing_shouldReturnError(t *testing.T) { assertions := require.New(t) - allJobs["type1"] = Type{ + managerUnderTest := NewJobsManagerImpl("", nil, "", nil) + managerUnderTest.allTypes["type1"] = TypeData{ TypeId: "type1", } - t.Cleanup(func() { - clearAll() - }) + jobInfo := JobInfo{ InfoTypeIdentity: "type1", } - - err := AddJob(jobInfo) + err := managerUnderTest.AddJobFromRESTCall(jobInfo) assertions.NotNil(err) - assertions.Equal("missing required job identity: { type1}", err.Error()) + assertions.Equal("missing required job identity: { type1}", err.Error()) } -func TestAddJobWhenTargetUriMissing_shouldReturnError(t *testing.T) { +func TestJobsManagerAddJobWhenTargetUriMissing_shouldReturnError(t *testing.T) { assertions := require.New(t) - allJobs["type1"] = Type{ + managerUnderTest := NewJobsManagerImpl("", nil, "", nil) + managerUnderTest.allTypes["type1"] = TypeData{ TypeId: "type1", } + jobInfo := JobInfo{ InfoTypeIdentity: "type1", InfoJobIdentity: "job1", } - - err := AddJob(jobInfo) + err := managerUnderTest.AddJobFromRESTCall(jobInfo) assertions.NotNil(err) - assertions.Equal("missing required target URI: { job1 type1}", err.Error()) - clearAll() + assertions.Equal("missing required target URI: { job1 type1}", err.Error()) } -func TestPollAndDistributeMessages(t *testing.T) { +func TestJobsManagerDeleteJob_shouldSendDeleteToChannel(t *testing.T) { assertions := require.New(t) + managerUnderTest := NewJobsManagerImpl("", nil, "", nil) + jobsHandler := jobsHandler{ + deleteJobCh: make(chan string)} + managerUnderTest.allTypes["type1"] = TypeData{ + TypeId: "type1", + jobsHandler: &jobsHandler, + } + + go managerUnderTest.DeleteJobFromRESTCall("job2") + + assertions.Equal("job2", <-jobsHandler.deleteJobCh) +} + +func TestAddJobToJobsManager_shouldStartPollAndDistributeMessages(t *testing.T) { + assertions := require.New(t) + + called := false + messages := `[{"message": {"data": "data"}}]` + pollClientMock := NewTestClient(func(req *http.Request) *http.Response { + if req.URL.String() == "http://mrAddr/topicUrl" { + assertions.Equal(req.Method, "GET") + body := "[]" + if !called { + called = true + body = messages + } + return &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewReader([]byte(body))), + Header: make(http.Header), // Must be set to non-nil value or it panics + } + } + t.Error("Wrong call to client: ", req) + t.Fail() + return nil + }) + + wg := sync.WaitGroup{} + 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, t)) + assertions.Equal("application/json", req.Header.Get("Content-Type")) + wg.Done() + return &http.Response{ + StatusCode: 200, + Body: ioutil.NopCloser(bytes.NewBufferString(`OK`)), + Header: make(http.Header), // Must be set to non-nil value or it panics + } + } + t.Error("Wrong call to client: ", req) + t.Fail() + return nil + }) + jobsHandler := newJobsHandler("type1", "/topicUrl", pollClientMock, distributeClientMock) + + jobsManager := NewJobsManagerImpl("", pollClientMock, "http://mrAddr", distributeClientMock) + jobsManager.allTypes["type1"] = TypeData{ + DMaaPTopicURL: "/topicUrl", + TypeId: "type1", + jobsHandler: jobsHandler, + } + + jobsManager.StartJobsForAllTypes() + jobInfo := JobInfo{ InfoTypeIdentity: "type1", InfoJobIdentity: "job1", TargetUri: "http://consumerHost/target", } - allJobs["type1"] = Type{ - TypeId: "type1", - DMaaPTopic: "topic", - Jobs: map[string]JobInfo{"job1": jobInfo}, + + wg.Add(1) // Wait till the distribution has happened + err := jobsManager.AddJobFromRESTCall(jobInfo) + assertions.Nil(err) + + if waitTimeout(&wg, 2*time.Second) { + t.Error("Not all calls to server were made") + t.Fail() + } +} + +func TestJobsHandlerDeleteJob_shouldDeleteJobFromJobsMap(t *testing.T) { + jobToDelete := newJob(JobInfo{}, nil) + go jobToDelete.start() + jobsHandler := newJobsHandler("type1", "/topicUrl", nil, nil) + jobsHandler.jobs["job1"] = jobToDelete + + go jobsHandler.monitorManagementChannels() + + jobsHandler.deleteJobCh <- "job1" + + deleted := false + for i := 0; i < 100; i++ { + if len(jobsHandler.jobs) == 0 { + deleted = true + break + } + time.Sleep(time.Microsecond) // Need to drop control to let the job's goroutine do the job } - t.Cleanup(func() { - clearAll() - }) + require.New(t).True(deleted, "Job not deleted") +} - body := ioutil.NopCloser(bytes.NewReader([]byte(`[{"message": {"data": "data"}}]`))) - clientMock := mocks.HTTPClient{} - clientMock.On("Get", mock.Anything).Return(&http.Response{ - StatusCode: http.StatusOK, - Body: body, +func TestJobsHandlerEmptyJobMessageBufferWhenItIsFull(t *testing.T) { + job := newJob(JobInfo{ + InfoJobIdentity: "job", }, nil) - clientMock.On("Do", mock.Anything).Return(&http.Response{ - StatusCode: http.StatusOK, - }, nil) + jobsHandler := newJobsHandler("type1", "/topicUrl", nil, nil) + jobsHandler.jobs["job1"] = job - restclient.Client = &clientMock + fillMessagesBuffer(job.messagesChannel) - pollAndDistributeMessages("http://mrAddr") + jobsHandler.distributeMessages([]byte("sent msg")) - time.Sleep(100 * time.Millisecond) + require.New(t).Len(job.messagesChannel, 0) +} - var actualRequest *http.Request - clientMock.AssertCalled(t, "Get", "http://mrAddr/events/topic/users/dmaapmediatorproducer") - clientMock.AssertNumberOfCalls(t, "Get", 1) +func fillMessagesBuffer(mc chan []byte) { + for i := 0; i < cap(mc); i++ { + mc <- []byte("msg") + } +} + +type RoundTripFunc func(req *http.Request) *http.Response + +func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { + return f(req), nil +} + +//NewTestClient returns *http.Client with Transport replaced to avoid making real calls +func NewTestClient(fn RoundTripFunc) *http.Client { + return &http.Client{ + Transport: RoundTripFunc(fn), + } +} + +// waitTimeout waits for the waitgroup for the specified max timeout. +// Returns true if waiting timed out. +func waitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool { + c := make(chan struct{}) + go func() { + defer close(c) + wg.Wait() + }() + select { + case <-c: + return false // completed normally + case <-time.After(timeout): + return true // timed out + } +} - clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool { - actualRequest = req - return true - })) - assertions.Equal(http.MethodPost, actualRequest.Method) - assertions.Equal("consumerHost", actualRequest.URL.Host) - assertions.Equal("/target", actualRequest.URL.Path) - assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type")) - actualBody, _ := ioutil.ReadAll(actualRequest.Body) - assertions.Equal([]byte(`[{"message": {"data": "data"}}]`), actualBody) - clientMock.AssertNumberOfCalls(t, "Do", 1) +func getBodyAsString(req *http.Request, t *testing.T) string { + buf := new(bytes.Buffer) + if _, err := buf.ReadFrom(req.Body); err != nil { + t.Fail() + } + return buf.String() }