Use env varialbes to replace image urls & tags
[nonrtric.git] / test / usecases / oruclosedlooprecovery / goversion / internal / restclient / client_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 //        http://www.apache.org/licenses/LICENSE-2.0
11 //
12 //   Unless required by applicable law or agreed to in writing, software
13 //   distributed under the License is distributed on an "AS IS" BASIS,
14 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 //   See the License for the specific language governing permissions and
16 //   limitations under the License.
17 //   ========================LICENSE_END===================================
18 //
19
20 package restclient
21
22 import (
23         "bytes"
24         "fmt"
25         "io/ioutil"
26         "net/http"
27         "testing"
28
29         "github.com/stretchr/testify/mock"
30         "github.com/stretchr/testify/require"
31         "oransc.org/usecase/oruclosedloop/mocks"
32 )
33
34 func TestRequestError_Error(t *testing.T) {
35         assertions := require.New(t)
36
37         actualError := RequestError{
38                 StatusCode: http.StatusBadRequest,
39                 Body:       []byte("error"),
40         }
41         assertions.Equal("Request failed due to error response with status: 400 and body: error", actualError.Error())
42 }
43
44 func TestPutWithoutAuth(t *testing.T) {
45         assertions := require.New(t)
46
47         clientMock := mocks.HTTPClient{}
48         clientMock.On("Do", mock.Anything).Return(&http.Response{
49                 StatusCode: http.StatusOK,
50         }, nil)
51
52         error := PutWithoutAuth("url", []byte("body"), &clientMock)
53
54         assertions.Nil(error)
55         var actualRequest *http.Request
56         clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
57                 actualRequest = req
58                 return true
59         }))
60         assertions.Equal(http.MethodPut, actualRequest.Method)
61         assertions.Equal("url", actualRequest.URL.Path)
62         assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
63         assertions.Empty(actualRequest.Header.Get("Authorization"))
64         body, _ := ioutil.ReadAll(actualRequest.Body)
65         expectedBody := []byte("body")
66         assertions.Equal(expectedBody, body)
67         clientMock.AssertNumberOfCalls(t, "Do", 1)
68 }
69
70 func TestPut(t *testing.T) {
71         assertions := require.New(t)
72
73         clientMock := mocks.HTTPClient{}
74         clientMock.On("Do", mock.Anything).Return(&http.Response{
75                 StatusCode: http.StatusOK,
76         }, nil)
77
78         error := Put("url", "body", &clientMock, "admin", "pwd")
79
80         assertions.Nil(error)
81         var actualRequest *http.Request
82         clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
83                 actualRequest = req
84                 return true
85         }))
86         assertions.Equal(http.MethodPut, actualRequest.Method)
87         assertions.Equal("url", actualRequest.URL.Path)
88         assertions.Equal("application/json; charset=utf-8", actualRequest.Header.Get("Content-Type"))
89         tempRequest, _ := http.NewRequest("", "", nil)
90         tempRequest.SetBasicAuth("admin", "pwd")
91         assertions.Equal(tempRequest.Header.Get("Authorization"), actualRequest.Header.Get("Authorization"))
92         body, _ := ioutil.ReadAll(actualRequest.Body)
93         expectedBody := []byte("body")
94         assertions.Equal(expectedBody, body)
95         clientMock.AssertNumberOfCalls(t, "Do", 1)
96 }
97
98 func TestDelete(t *testing.T) {
99         assertions := require.New(t)
100
101         clientMock := mocks.HTTPClient{}
102         clientMock.On("Do", mock.Anything).Return(&http.Response{
103                 StatusCode: http.StatusOK,
104         }, nil)
105
106         error := Delete("url", &clientMock)
107
108         assertions.Nil(error)
109         var actualRequest *http.Request
110         clientMock.AssertCalled(t, "Do", mock.MatchedBy(func(req *http.Request) bool {
111                 actualRequest = req
112                 return true
113         }))
114         assertions.Equal(http.MethodDelete, actualRequest.Method)
115         assertions.Equal("url", actualRequest.URL.Path)
116         assertions.Empty(actualRequest.Header.Get("Content-Type"))
117         assertions.Empty(actualRequest.Header.Get("Authorization"))
118         assertions.Equal(http.NoBody, actualRequest.Body)
119         clientMock.AssertNumberOfCalls(t, "Do", 1)
120 }
121
122 func Test_doErrorCases(t *testing.T) {
123         assertions := require.New(t)
124
125         type args struct {
126                 url              string
127                 mockReturnStatus int
128                 mockReturnBody   []byte
129                 mockReturnError  error
130         }
131         tests := []struct {
132                 name    string
133                 args    args
134                 wantErr error
135         }{
136                 {
137                         name: "Bad request should get RequestError",
138                         args: args{
139                                 url:              "badRequest",
140                                 mockReturnStatus: http.StatusBadRequest,
141                                 mockReturnBody:   []byte("bad request"),
142                         },
143                         wantErr: RequestError{
144                                 StatusCode: http.StatusBadRequest,
145                                 Body:       []byte("bad request"),
146                         },
147                 },
148                 {
149                         name: "Server unavailable should get error",
150                         args: args{
151                                 url:             "serverUnavailable",
152                                 mockReturnError: fmt.Errorf("Server unavailable"),
153                         },
154                         wantErr: fmt.Errorf("Server unavailable"),
155                 },
156         }
157         for _, tt := range tests {
158                 t.Run(tt.name, func(t *testing.T) {
159                         clientMock := mocks.HTTPClient{}
160                         clientMock.On("Do", mock.Anything).Return(&http.Response{
161                                 StatusCode: tt.args.mockReturnStatus,
162                                 Body:       ioutil.NopCloser(bytes.NewReader(tt.args.mockReturnBody)),
163                         }, tt.args.mockReturnError)
164
165                         err := do("PUT", tt.args.url, nil, &clientMock)
166                         assertions.Equal(tt.wantErr, err, tt.name)
167                 })
168         }
169 }