Poll MR and send messages to consumers
[nonrtric.git] / dmaap-mediator-producer / internal / server / server_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2021: Nordix Foundation
6 //   %%
7 //   Licensed under the Apache License, Version 2.0 (the "License");
8 //   you may not use this file except in compliance with the License.
9 //   You may obtain a copy of the License at
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
13 //   Unless required by applicable law or agreed to in writing, software
14 //   distributed under the License is distributed on an "AS IS" BASIS,
15 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 //   See the License for the specific language governing permissions and
17 //   limitations under the License.
18 //   ========================LICENSE_END===================================
19 //
20
21 package server
22
23 import (
24         "bytes"
25         "encoding/json"
26         "errors"
27         "io"
28         "io/ioutil"
29         "net/http"
30         "net/http/httptest"
31         "testing"
32
33         "github.com/stretchr/testify/require"
34         "oransc.org/nonrtric/dmaapmediatorproducer/internal/jobs"
35         "oransc.org/nonrtric/dmaapmediatorproducer/mocks/jobhandler"
36 )
37
38 func TestStatusHandler(t *testing.T) {
39         assertions := require.New(t)
40         type args struct {
41                 responseRecorder *httptest.ResponseRecorder
42                 r                *http.Request
43         }
44         tests := []struct {
45                 name         string
46                 args         args
47                 wantedStatus int
48                 wantedBody   string
49         }{
50                 {
51                         name: "StatusHandler with correct path and method, should return OK",
52                         args: args{
53                                 responseRecorder: httptest.NewRecorder(),
54                                 r:                newRequest("GET", "/", nil, t),
55                         },
56                         wantedStatus: http.StatusOK,
57                         wantedBody:   "All is well!",
58                 },
59                 {
60                         name: "StatusHandler with incorrect path, should return NotFound",
61                         args: args{
62                                 responseRecorder: httptest.NewRecorder(),
63                                 r:                newRequest("GET", "/wrong", nil, t),
64                         },
65                         wantedStatus: http.StatusNotFound,
66                         wantedBody:   "404 not found.\n",
67                 },
68                 {
69                         name: "StatusHandler with incorrect method, should return MethodNotAllowed",
70                         args: args{
71                                 responseRecorder: httptest.NewRecorder(),
72                                 r:                newRequest("PUT", "/", nil, t),
73                         },
74                         wantedStatus: http.StatusMethodNotAllowed,
75                         wantedBody:   "Method is not supported.\n",
76                 },
77         }
78         for _, tt := range tests {
79                 t.Run(tt.name, func(t *testing.T) {
80                         handler := http.HandlerFunc(StatusHandler)
81                         handler.ServeHTTP(tt.args.responseRecorder, tt.args.r)
82                         assertions.Equal(tt.wantedStatus, tt.args.responseRecorder.Code)
83
84                         assertions.Equal(tt.wantedBody, tt.args.responseRecorder.Body.String())
85                 })
86         }
87 }
88
89 func TestCreateInfoJobHandler(t *testing.T) {
90         assertions := require.New(t)
91         jobHandlerMock := jobhandler.JobHandler{}
92
93         goodJobInfo := jobs.JobInfo{
94                 Owner:            "owner",
95                 LastUpdated:      "now",
96                 InfoJobIdentity:  "jobId",
97                 TargetUri:        "target",
98                 InfoJobData:      "{}",
99                 InfoTypeIdentity: "type",
100         }
101         badJobInfo := jobs.JobInfo{
102                 Owner: "bad",
103         }
104         jobHandlerMock.On("AddJob", goodJobInfo).Return(nil)
105         jobHandlerMock.On("AddJob", badJobInfo).Return(errors.New("error"))
106         jobs.Handler = &jobHandlerMock
107
108         type args struct {
109                 responseRecorder *httptest.ResponseRecorder
110                 r                *http.Request
111         }
112         tests := []struct {
113                 name         string
114                 args         args
115                 wantedStatus int
116                 wantedBody   string
117         }{
118                 {
119                         name: "CreateInfoJobHandler with correct path and method, should return OK",
120                         args: args{
121                                 responseRecorder: httptest.NewRecorder(),
122                                 r:                newRequest("POST", "/producer_simulator/info_job", &goodJobInfo, t),
123                         },
124                         wantedStatus: http.StatusOK,
125                         wantedBody:   "",
126                 },
127                 {
128                         name: "CreateInfoJobHandler with incorrect job info, should return BadRequest",
129                         args: args{
130                                 responseRecorder: httptest.NewRecorder(),
131                                 r:                newRequest("POST", "/producer_simulator/info_job", &badJobInfo, t),
132                         },
133                         wantedStatus: http.StatusBadRequest,
134                         wantedBody:   "Invalid job info. Cause: error",
135                 },
136                 {
137                         name: "CreateInfoJobHandler with incorrect path, should return NotFound",
138                         args: args{
139                                 responseRecorder: httptest.NewRecorder(),
140                                 r:                newRequest("GET", "/wrong", nil, t),
141                         },
142                         wantedStatus: http.StatusNotFound,
143                         wantedBody:   "404 not found.",
144                 },
145                 {
146                         name: "CreateInfoJobHandler with incorrect method, should return MethodNotAllowed",
147                         args: args{
148                                 responseRecorder: httptest.NewRecorder(),
149                                 r:                newRequest("PUT", "/producer_simulator/info_job", nil, t),
150                         },
151                         wantedStatus: http.StatusMethodNotAllowed,
152                         wantedBody:   "Method is not supported.",
153                 },
154         }
155         for _, tt := range tests {
156                 t.Run(tt.name, func(t *testing.T) {
157                         handler := http.HandlerFunc(CreateInfoJobHandler)
158                         handler.ServeHTTP(tt.args.responseRecorder, tt.args.r)
159                         assertions.Equal(tt.wantedStatus, tt.args.responseRecorder.Code)
160
161                         assertions.Contains(tt.args.responseRecorder.Body.String(), tt.wantedBody)
162                 })
163         }
164 }
165
166 func newRequest(method string, url string, jobInfo *jobs.JobInfo, t *testing.T) *http.Request {
167         var body io.Reader
168         if jobInfo != nil {
169                 bodyAsBytes, _ := json.Marshal(jobInfo)
170                 body = ioutil.NopCloser(bytes.NewReader(bodyAsBytes))
171         }
172         if req, err := http.NewRequest(method, url, body); err == nil {
173                 return req
174         } else {
175                 t.Fatalf("Could not create request due to: %v", err)
176                 return nil
177         }
178 }