NONRTRIC - Implement DMaaP mediator producer service in Java
[nonrtric.git] / dmaap-mediator-producer / internal / restclient / HTTPClient_test.go
index 3727e6a..3ded9be 100644 (file)
@@ -23,58 +23,55 @@ package restclient
 import (
        "bytes"
        "errors"
+       "fmt"
        "io/ioutil"
        "net/http"
-       "reflect"
        "testing"
 
        "github.com/stretchr/testify/mock"
        "github.com/stretchr/testify/require"
-       "oransc.org/nonrtric/dmaapmediatorproducer/mocks"
+       "oransc.org/nonrtric/dmaapmediatorproducer/mocks/httpclient"
 )
 
-func TestGet(t *testing.T) {
-       clientMock := mocks.HTTPClient{}
-
-       clientMock.On("Get", "http://testOk").Return(&http.Response{
-               StatusCode: http.StatusOK,
-               Body:       ioutil.NopCloser(bytes.NewReader([]byte("Response"))),
-       }, nil)
-
-       clientMock.On("Get", "http://testNotOk").Return(&http.Response{
+func TestRequestError_Error(t *testing.T) {
+       assertions := require.New(t)
+       actualError := RequestError{
                StatusCode: http.StatusBadRequest,
-               Body:       ioutil.NopCloser(bytes.NewReader([]byte("Bad Response"))),
-       }, nil)
-
-       clientMock.On("Get", "http://testError").Return(nil, errors.New("Failed Request"))
-
-       Client = &clientMock
-
+               Body:       []byte("error"),
+       }
+       assertions.Equal("Request failed due to error response with status: 400 and body: error", actualError.Error())
+}
+func TestGet(t *testing.T) {
+       assertions := require.New(t)
        type args struct {
-               url string
+               url              string
+               mockReturnStatus int
+               mockReturnBody   string
+               mockReturnError  error
        }
        tests := []struct {
                name        string
                args        args
                want        []byte
-               wantErr     bool
                wantedError error
        }{
                {
                        name: "Test Get with OK response",
                        args: args{
-                               url: "http://testOk",
+                               url:              "http://testOk",
+                               mockReturnStatus: http.StatusOK,
+                               mockReturnBody:   "Response",
                        },
-                       want:    []byte("Response"),
-                       wantErr: false,
+                       want: []byte("Response"),
                },
                {
                        name: "Test Get with Not OK response",
                        args: args{
-                               url: "http://testNotOk",
+                               url:              "http://testNotOk",
+                               mockReturnStatus: http.StatusBadRequest,
+                               mockReturnBody:   "Bad Response",
                        },
-                       want:    nil,
-                       wantErr: true,
+                       want: nil,
                        wantedError: RequestError{
                                StatusCode: http.StatusBadRequest,
                                Body:       []byte("Bad Response"),
@@ -83,40 +80,38 @@ func TestGet(t *testing.T) {
                {
                        name: "Test Get with error",
                        args: args{
-                               url: "http://testError",
+                               url:             "http://testError",
+                               mockReturnError: errors.New("Failed Request"),
                        },
                        want:        nil,
-                       wantErr:     true,
                        wantedError: errors.New("Failed Request"),
                },
        }
        for _, tt := range tests {
                t.Run(tt.name, func(t *testing.T) {
-                       got, err := Get(tt.args.url)
-                       if (err != nil) != tt.wantErr {
-                               t.Errorf("Get() error = %v, wantErr %v", err, tt.wantErr)
-                               return
-                       }
-                       if !reflect.DeepEqual(got, tt.want) {
-                               t.Errorf("Get() = %v, want %v", got, tt.want)
-                       }
-                       if tt.wantErr && err.Error() != tt.wantedError.Error() {
-                               t.Errorf("Get() error = %v, wantedError % v", err, tt.wantedError.Error())
-                       }
+                       clientMock := httpclient.HTTPClient{}
+                       clientMock.On("Get", tt.args.url).Return(&http.Response{
+                               StatusCode: tt.args.mockReturnStatus,
+                               Body:       ioutil.NopCloser(bytes.NewReader([]byte(tt.args.mockReturnBody))),
+                       }, tt.args.mockReturnError)
+
+                       got, err := Get(tt.args.url, &clientMock)
+                       assertions.Equal(tt.wantedError, err, tt.name)
+                       assertions.Equal(tt.want, got, tt.name)
+                       clientMock.AssertCalled(t, "Get", tt.args.url)
                })
        }
 }
 
 func TestPutOk(t *testing.T) {
        assertions := require.New(t)
-       clientMock := mocks.HTTPClient{}
+       clientMock := httpclient.HTTPClient{}
 
        clientMock.On("Do", mock.Anything).Return(&http.Response{
                StatusCode: http.StatusOK,
        }, nil)
 
-       Client = &clientMock
-       if err := Put("http://localhost:9990", []byte("body")); err != nil {
+       if err := Put("http://localhost:9990", []byte("body"), &clientMock); err != nil {
                t.Errorf("Put() error = %v, did not want error", err)
        }
        var actualRequest *http.Request
@@ -127,38 +122,83 @@ func TestPutOk(t *testing.T) {
        assertions.Equal(http.MethodPut, actualRequest.Method)
        assertions.Equal("http", actualRequest.URL.Scheme)
        assertions.Equal("localhost:9990", actualRequest.URL.Host)
-       assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
+       assertions.Equal("application/json", actualRequest.Header.Get("Content-Type"))
        body, _ := ioutil.ReadAll(actualRequest.Body)
        expectedBody := []byte("body")
        assertions.Equal(expectedBody, body)
        clientMock.AssertNumberOfCalls(t, "Do", 1)
 }
 
-func TestPutBadResponse(t *testing.T) {
+func TestPostOk(t *testing.T) {
        assertions := require.New(t)
-       clientMock := mocks.HTTPClient{}
+       clientMock := httpclient.HTTPClient{}
 
        clientMock.On("Do", mock.Anything).Return(&http.Response{
-               StatusCode: http.StatusBadRequest,
-               Body:       ioutil.NopCloser(bytes.NewReader([]byte("Bad Request"))),
+               StatusCode: http.StatusOK,
        }, nil)
 
-       Client = &clientMock
-       err := Put("url", []byte("body"))
-       assertions.NotNil("Put() error = %v, wanted error", err)
-       expectedErrorMessage := "Request failed due to error response with status: 400 and body: Bad Request"
-       assertions.Equal(expectedErrorMessage, err.Error())
+       if err := Post("http://localhost:9990", []byte("body"), &clientMock); err != nil {
+               t.Errorf("Put() error = %v, did not want error", err)
+       }
+       var actualRequest *http.Request
+       clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
+               actualRequest = req
+               return true
+       }))
+       assertions.Equal(http.MethodPost, actualRequest.Method)
+       assertions.Equal("http", actualRequest.URL.Scheme)
+       assertions.Equal("localhost:9990", actualRequest.URL.Host)
+       assertions.Equal("application/json", actualRequest.Header.Get("Content-Type"))
+       body, _ := ioutil.ReadAll(actualRequest.Body)
+       expectedBody := []byte("body")
+       assertions.Equal(expectedBody, body)
+       clientMock.AssertNumberOfCalls(t, "Do", 1)
 }
 
-func TestPutError(t *testing.T) {
+func Test_doErrorCases(t *testing.T) {
        assertions := require.New(t)
-       clientMock := mocks.HTTPClient{}
-
-       clientMock.On("Do", mock.Anything).Return(nil, errors.New("Failed Request"))
-
-       Client = &clientMock
-       err := Put("url", []byte("body"))
-       assertions.NotNil("Put() error = %v, wanted error", err)
-       expectedErrorMessage := "Failed Request"
-       assertions.Equal(expectedErrorMessage, err.Error())
+       type args struct {
+               url              string
+               mockReturnStatus int
+               mockReturnBody   []byte
+               mockReturnError  error
+       }
+       tests := []struct {
+               name    string
+               args    args
+               wantErr error
+       }{
+               {
+                       name: "Bad request should get RequestError",
+                       args: args{
+                               url:              "badRequest",
+                               mockReturnStatus: http.StatusBadRequest,
+                               mockReturnBody:   []byte("bad request"),
+                               mockReturnError:  nil,
+                       },
+                       wantErr: RequestError{
+                               StatusCode: http.StatusBadRequest,
+                               Body:       []byte("bad request"),
+                       },
+               },
+               {
+                       name: "Server unavailable should get error",
+                       args: args{
+                               url:             "serverUnavailable",
+                               mockReturnError: fmt.Errorf("Server unavailable"),
+                       },
+                       wantErr: fmt.Errorf("Server unavailable"),
+               },
+       }
+       for _, tt := range tests {
+               t.Run(tt.name, func(t *testing.T) {
+                       clientMock := httpclient.HTTPClient{}
+                       clientMock.On("Do", mock.Anything).Return(&http.Response{
+                               StatusCode: tt.args.mockReturnStatus,
+                               Body:       ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
+                       }, tt.args.mockReturnError)
+                       err := do("PUT", tt.args.url, nil, &clientMock)
+                       assertions.Equal(tt.wantErr, err, tt.name)
+               })
+       }
 }