aboutsummaryrefslogtreecommitdiffstats
path: root/so-etsi-sol005-adapter-application/src/test/java/org/onap/so/adapters/vfc/util/RestfulUtilTest.java
blob: a3a3fd2b5ab055391652df5ea9681dafec3d7b61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (c) 2019 Samsung. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.so.adapters.vfc.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.Header;
import org.apache.http.message.BasicHeader;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.MockitoJUnitRunner;
import org.onap.so.adapters.vfc.model.RestfulResponse;
import org.springframework.http.HttpStatus;
import javax.ws.rs.HttpMethod;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class RestfulUtilTest {

    @InjectMocks
    @Spy
    private RestfulUtil restfulUtil;

    @Mock
    private HttpClient client;

    private HttpEntity httpEntity;
    private HttpResponse httpResponse;
    private StatusLine statusLine;
    private Header httpResponseHeader;

    @Before
    public void setUp() {
        httpEntity = mock(HttpEntity.class);
        httpResponse = mock(HttpResponse.class);
        statusLine = mock(StatusLine.class);
        httpResponseHeader = mock(Header.class);
    }

    private void sendInit() throws IOException {

        Header[] headerList = new BasicHeader[2];
        headerList[0] = new BasicHeader("Content-Type", "application/json");
        headerList[1] = new BasicHeader("cache-control", "no-cache");
        doReturn("https://testHost/").when(restfulUtil).getMsbHost();

        when(statusLine.getStatusCode()).thenReturn(HttpStatus.OK.value());
        when(httpResponse.getStatusLine()).thenReturn(statusLine);
        when(httpResponse.getEntity()).thenReturn(httpEntity);
        when(httpResponse.getAllHeaders()).thenReturn(headerList);
    }

    @Test
    public void sendGet() throws Exception {

        sendInit();

        ByteArrayInputStream responseStream = new ByteArrayInputStream(new String("GET").getBytes());
        when(client.execute(any(HttpGet.class))).thenReturn(httpResponse);
        when(httpEntity.getContent()).thenReturn(responseStream);

        RestfulResponse restfulResponse = restfulUtil.send("test", HttpMethod.GET, "some request content");

        assertEquals(HttpStatus.OK.value(), restfulResponse.getStatus());
        assertEquals("GET", restfulResponse.getResponseContent());

        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("a", "b");
        RestfulResponse restfulResponse1 =
                restfulUtil.send("test", HttpMethod.GET, "some request content", requestHeader);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), restfulResponse1.getStatus());

    }

    @Test
    public void sendPost() throws Exception {

        sendInit();


        ByteArrayInputStream responseStream = new ByteArrayInputStream(new String("POST").getBytes());
        when(client.execute(any(HttpPost.class))).thenReturn(httpResponse);
        when(httpEntity.getContent()).thenReturn(responseStream);

        RestfulResponse restfulResponse = restfulUtil.send("test", HttpMethod.POST, "some request content");

        assertEquals(HttpStatus.OK.value(), restfulResponse.getStatus());
        assertEquals("POST", restfulResponse.getResponseContent());

        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("a", "b");
        RestfulResponse restfulResponse1 =
                restfulUtil.send("test", HttpMethod.POST, "some request content", requestHeader);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), restfulResponse1.getStatus());

    }

    @Test
    public void sendPut() throws Exception {

        sendInit();

        ByteArrayInputStream responseStream = new ByteArrayInputStream(new String("PUT").getBytes());
        when(client.execute(any(HttpPut.class))).thenReturn(httpResponse);
        when(httpEntity.getContent()).thenReturn(responseStream);

        RestfulResponse restfulResponse = restfulUtil.send("test", HttpMethod.PUT, "some request content");

        assertEquals(HttpStatus.OK.value(), restfulResponse.getStatus());
        assertEquals("PUT", restfulResponse.getResponseContent());

        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("a", "b");
        RestfulResponse restfulResponse1 =
                restfulUtil.send("test", HttpMethod.PUT, "some request content", requestHeader);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), restfulResponse1.getStatus());

    }

    @Test
    public void sendDelete() throws Exception {

        sendInit();

        ByteArrayInputStream responseStream = new ByteArrayInputStream(new String("DELETE").getBytes());
        when(client.execute(any(HttpDelete.class))).thenReturn(httpResponse);
        when(httpEntity.getContent()).thenReturn(responseStream);

        RestfulResponse restfulResponse = restfulUtil.send("test", HttpMethod.DELETE, "some request content");

        assertEquals(HttpStatus.OK.value(), restfulResponse.getStatus());
        assertEquals("DELETE", restfulResponse.getResponseContent());

        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("a", "b");
        RestfulResponse restfulResponse1 =
                restfulUtil.send("test", HttpMethod.DELETE, "some request content", requestHeader);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), restfulResponse1.getStatus());

    }

    @Test
    public void sendOptions() throws Exception {

        doReturn("https://testHost/").when(restfulUtil).getMsbHost();

        RestfulResponse restfulResponse = restfulUtil.send("test", HttpMethod.OPTIONS, "some request content");

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), restfulResponse.getStatus());
        assertEquals("Error processing request to VFC", restfulResponse.getResponseContent());

        Map<String, String> requestHeader = new HashMap<>();
        requestHeader.put("a", "b");
        RestfulResponse restfulResponse1 =
                restfulUtil.send("test", HttpMethod.OPTIONS, "some request content", requestHeader);

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), restfulResponse1.getStatus());
    }

    @Test
    public void getNfvoFromAAITest() throws Exception {

        doReturn("https://testHost/").when(restfulUtil).getMsbHost();

        RestfulResponse restfulResponse = restfulUtil.getNfvoFromAAI("test");

        assertEquals(HttpStatus.INTERNAL_SERVER_ERROR.value(), restfulResponse.getStatus());
    }
}