X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=dmaap-mediator-producer%2Finternal%2Fserver%2Fserver_test.go;h=a4b19c43708bb686d1bb5843f76fc9cbb09c5eec;hb=47d0ee37691eddc290a1f9e34091dfd2020db07f;hp=921321651386a648309312d92fe2d1fea16ad5f5;hpb=ba96d847ab72a882bfe3acd39bc70c8f544f094a;p=nonrtric.git diff --git a/dmaap-mediator-producer/internal/server/server_test.go b/dmaap-mediator-producer/internal/server/server_test.go index 92132165..a4b19c43 100644 --- a/dmaap-mediator-producer/internal/server/server_test.go +++ b/dmaap-mediator-producer/internal/server/server_test.go @@ -21,12 +21,18 @@ package server import ( + "bytes" + "encoding/json" + "errors" "io" + "io/ioutil" "net/http" "net/http/httptest" "testing" "github.com/stretchr/testify/require" + "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs" + "oransc.org/nonrtric/dmaapmediatorproducer/mocks/jobhandler" ) func TestStatusHandler(t *testing.T) { @@ -45,7 +51,7 @@ func TestStatusHandler(t *testing.T) { name: "StatusHandler with correct path and method, should return OK", args: args{ responseRecorder: httptest.NewRecorder(), - r: newRequest("GET", "/", nil, t), + r: newRequest("GET", "/status", nil, t), }, wantedStatus: http.StatusOK, wantedBody: "All is well!", @@ -60,12 +66,12 @@ func TestStatusHandler(t *testing.T) { wantedBody: "404 not found.\n", }, { - name: "StatusHandler with incorrect method, should return NotFound", + name: "StatusHandler with incorrect method, should return MethodNotAllowed", args: args{ responseRecorder: httptest.NewRecorder(), - r: newRequest("PUT", "/", nil, t), + r: newRequest("PUT", "/status", nil, t), }, - wantedStatus: http.StatusNotFound, + wantedStatus: http.StatusMethodNotAllowed, wantedBody: "Method is not supported.\n", }, } @@ -80,7 +86,89 @@ func TestStatusHandler(t *testing.T) { } } -func newRequest(method string, url string, body io.Reader, t *testing.T) *http.Request { +func TestCreateInfoJobHandler(t *testing.T) { + assertions := require.New(t) + jobHandlerMock := jobhandler.JobHandler{} + + goodJobInfo := jobs.JobInfo{ + Owner: "owner", + LastUpdated: "now", + InfoJobIdentity: "jobId", + TargetUri: "target", + InfoJobData: "{}", + InfoTypeIdentity: "type", + } + badJobInfo := jobs.JobInfo{ + Owner: "bad", + } + jobHandlerMock.On("AddJob", goodJobInfo).Return(nil) + jobHandlerMock.On("AddJob", badJobInfo).Return(errors.New("error")) + jobs.Handler = &jobHandlerMock + + type args struct { + responseRecorder *httptest.ResponseRecorder + r *http.Request + } + tests := []struct { + name string + args args + wantedStatus int + wantedBody string + }{ + { + name: "CreateInfoJobHandler with correct path and method, should return OK", + args: args{ + responseRecorder: httptest.NewRecorder(), + r: newRequest("POST", "/jobs", &goodJobInfo, t), + }, + wantedStatus: http.StatusOK, + wantedBody: "", + }, + { + name: "CreateInfoJobHandler with incorrect job info, should return BadRequest", + args: args{ + responseRecorder: httptest.NewRecorder(), + r: newRequest("POST", "/jobs", &badJobInfo, t), + }, + wantedStatus: http.StatusBadRequest, + wantedBody: "Invalid job info. Cause: error", + }, + { + name: "CreateInfoJobHandler with incorrect path, should return NotFound", + args: args{ + responseRecorder: httptest.NewRecorder(), + r: newRequest("GET", "/wrong", nil, t), + }, + wantedStatus: http.StatusNotFound, + wantedBody: "404 not found.", + }, + { + name: "CreateInfoJobHandler with incorrect method, should return MethodNotAllowed", + args: args{ + responseRecorder: httptest.NewRecorder(), + r: newRequest("PUT", "/jobs", nil, t), + }, + wantedStatus: http.StatusMethodNotAllowed, + wantedBody: "Method is not supported.", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + handler := http.HandlerFunc(CreateInfoJobHandler) + handler.ServeHTTP(tt.args.responseRecorder, tt.args.r) + assertions.Equal(tt.wantedStatus, tt.args.responseRecorder.Code) + + assertions.Contains(tt.args.responseRecorder.Body.String(), tt.wantedBody) + }) + } +} + +func newRequest(method string, url string, jobInfo *jobs.JobInfo, t *testing.T) *http.Request { + var body io.Reader + if jobInfo != nil { + bodyAsBytes, _ := json.Marshal(jobInfo) + body = ioutil.NopCloser(bytes.NewReader(bodyAsBytes)) + } if req, err := http.NewRequest(method, url, body); err == nil { return req } else {