Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 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 |
|
| 22 |
|
| 23 | package org.onap.osam.aai.util;
|
| 24 |
|
| 25 | import org.junit.Before;
|
| 26 | import org.junit.Test;
|
| 27 | import org.junit.runner.RunWith;
|
| 28 | import org.mockito.Mock;
|
| 29 | import org.mockito.Mockito;
|
| 30 | import org.mockito.runners.MockitoJUnitRunner;
|
| 31 | import org.onap.osam.aai.exceptions.InvalidPropertyException;
|
| 32 | import org.testng.Assert;
|
| 33 |
|
| 34 | import javax.servlet.http.HttpServletRequest;
|
| 35 | import javax.ws.rs.client.Client;
|
| 36 | import javax.ws.rs.client.Entity;
|
| 37 | import javax.ws.rs.client.Invocation;
|
| 38 | import javax.ws.rs.client.WebTarget;
|
| 39 | import javax.ws.rs.core.MediaType;
|
| 40 | import javax.ws.rs.core.Response;
|
| 41 | import java.io.UnsupportedEncodingException;
|
| 42 | import java.util.Optional;
|
| 43 | import java.util.UUID;
|
| 44 |
|
| 45 | import static javax.ws.rs.core.Response.Status.*;
|
| 46 | import static org.mockito.Mockito.verify;
|
| 47 | import static org.mockito.Mockito.when;
|
| 48 |
|
| 49 | @RunWith(MockitoJUnitRunner.class)
|
| 50 | public class SingleAAIRestInterfaceTest {
|
| 51 |
|
| 52 | private static final String PATH = "path";
|
| 53 | private static final String HTTP_LOCALHOST = "http://localhost/";
|
| 54 | @Mock
|
| 55 | private Client client;
|
| 56 | @Mock
|
| 57 | private WebTarget webTarget;
|
| 58 | @Mock
|
| 59 | private Invocation.Builder builder;
|
| 60 | @Mock
|
| 61 | private ServletRequestHelper servletRequestHelper;
|
| 62 | @Mock
|
| 63 | private HttpsAuthClient httpsAuthClient;
|
| 64 | @Mock
|
| 65 | private HttpServletRequest httpServletRequest;
|
| 66 | @Mock
|
| 67 | private Response response;
|
| 68 | @Mock
|
| 69 | private SystemPropertyHelper systemPropertyHelper;
|
| 70 |
|
| 71 | private AAIRestInterface testSubject;
|
| 72 |
|
| 73 | @Before
|
| 74 | public void setUp() throws Exception {
|
| 75 | mockSystemProperties();
|
| 76 | testSubject = createTestSubject();
|
| 77 | when(client.target(HTTP_LOCALHOST+PATH)).thenReturn(webTarget);
|
| 78 | when(webTarget.request()).thenReturn(builder);
|
| 79 | when(builder.accept(Mockito.anyString())).thenReturn(builder);
|
| 80 | when(builder.header(Mockito.anyString(), Mockito.anyString())).thenReturn(builder);
|
| 81 | when(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
|
| 82 | }
|
| 83 |
|
| 84 | private AAIRestInterface createTestSubject() {
|
| 85 | return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper);
|
| 86 | }
|
| 87 |
|
| 88 | @Test
|
| 89 | public void testEncodeURL() throws Exception {
|
| 90 | String nodeKey = "some unusual uri";
|
| 91 | Assert.assertEquals(testSubject.encodeURL(nodeKey), "some%20unusual%20uri");
|
| 92 | }
|
| 93 |
|
| 94 | @Test
|
| 95 | public void testSetRestSrvrBaseURLWithNullValue() throws Exception {
|
| 96 | testSubject.SetRestSrvrBaseURL(null);
|
| 97 | }
|
| 98 |
|
| 99 | @Test
|
| 100 | public void testSetRestSrvrBaseURL() throws Exception {
|
| 101 | String baseUrl = "anything";
|
| 102 | testSubject.SetRestSrvrBaseURL(baseUrl);
|
| 103 | Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl);
|
| 104 | }
|
| 105 |
|
| 106 | @Test
|
| 107 | public void testRestJsonPutWithResponse200() throws Exception {
|
| 108 | // given
|
| 109 | String methodName = "RestPut";
|
| 110 | String payload = "{\"id\": 1}";
|
| 111 | Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
|
| 112 |
|
| 113 | // when
|
| 114 | when(builder.put(Mockito.any(Entity.class))).thenReturn(response);
|
| 115 | when(response.getStatusInfo()).thenReturn(OK);
|
| 116 | Response finalResponse = testSubject.RestPut("", PATH, payload, false);
|
| 117 |
|
| 118 | // then
|
| 119 | verify(builder).put(entity);
|
| 120 | Assert.assertEquals(response, finalResponse);
|
| 121 | }
|
| 122 |
|
| 123 | @Test
|
| 124 | public void testFailedRestJsonPut() throws Exception {
|
| 125 | // given
|
| 126 | String methodName = "RestPut";
|
| 127 | String payload = "{\"id\": 1}";
|
| 128 | Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
|
| 129 |
|
| 130 | // when
|
| 131 | when(builder.put(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
|
| 132 | Response finalResponse = testSubject.RestPut("", PATH, payload, false);
|
| 133 |
|
| 134 | // then
|
| 135 | verify(builder).put(entity);
|
| 136 | Assert.assertEquals(finalResponse, null);
|
| 137 | }
|
| 138 |
|
| 139 | @Test
|
| 140 | public void testRestJsonPutWithResponse400() throws Exception {
|
| 141 | // given
|
| 142 | String methodName = "RestPut";
|
| 143 | String payload = "{\"id\": 1}";
|
| 144 | Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
|
| 145 |
|
| 146 | // when
|
| 147 | when(builder.put(Mockito.any(Entity.class))).thenReturn(response);
|
| 148 | when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
|
| 149 | when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
|
| 150 | Response finalResponse = testSubject.RestPut("", PATH, payload, false);
|
| 151 |
|
| 152 | // then
|
| 153 | verify(builder).put(entity);
|
| 154 | Assert.assertEquals(response, finalResponse);
|
| 155 | }
|
| 156 |
|
| 157 | @Test
|
| 158 | public void testRestPostWithResponse200() throws Exception {
|
| 159 | // given
|
| 160 | String methodName = "RestPost";
|
| 161 | String payload = "{\"id\": 1}";
|
| 162 | Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
|
| 163 |
|
| 164 | // when
|
| 165 | when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
|
| 166 | when(response.getStatusInfo()).thenReturn(OK);
|
| 167 | Response finalResponse = testSubject.RestPost("", PATH, payload, false);
|
| 168 |
|
| 169 | // then
|
| 170 | verify(builder).post(entity);
|
| 171 | Assert.assertEquals(response, finalResponse);
|
| 172 | }
|
| 173 |
|
| 174 | @Test
|
| 175 | public void testRestPostWithResponse400() throws Exception {
|
| 176 | // given
|
| 177 | String methodName = "RestPost";
|
| 178 | String payload = "{\"id\": 1}";
|
| 179 | Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
|
| 180 |
|
| 181 | // when
|
| 182 | when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
|
| 183 | when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
|
| 184 | when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
|
| 185 | Response finalResponse = testSubject.RestPost("", PATH, payload, false);
|
| 186 |
|
| 187 | // then
|
| 188 | verify(builder).post(entity);
|
| 189 | Assert.assertEquals(response, finalResponse);
|
| 190 | }
|
| 191 |
|
| 192 | @Test
|
| 193 | public void testFailedRestPost() throws Exception {
|
| 194 | // given
|
| 195 | String methodName = "RestPost";
|
| 196 | String payload = "{\"id\": 1}";
|
| 197 | Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
|
| 198 |
|
| 199 | // when
|
| 200 | when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
|
| 201 | Response finalResponse = testSubject.RestPost("", PATH, payload, false);
|
| 202 |
|
| 203 | // then
|
| 204 | verify(builder).post(entity);
|
| 205 | Assert.assertEquals(finalResponse, null);
|
| 206 | }
|
| 207 |
|
| 208 | @Test
|
| 209 | public void testRestDeleteWithResponse400() throws Exception {
|
| 210 | // given
|
| 211 | String methodName = "Delete";
|
| 212 |
|
| 213 | // when
|
| 214 | when(builder.delete()).thenReturn(response);
|
| 215 | when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
|
| 216 | String reason = "Any reason";
|
| 217 | when(response.readEntity(String.class)).thenReturn(reason);
|
| 218 | when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
|
| 219 | boolean finalResponse = testSubject.Delete("", "", PATH);
|
| 220 |
|
| 221 | // then
|
| 222 | verify(builder).delete();
|
| 223 | Assert.assertFalse(finalResponse);
|
| 224 | }
|
| 225 |
|
| 226 | @Test
|
| 227 | public void testRestDeleteWithResponse404() throws Exception {
|
| 228 | // given
|
| 229 | String methodName = "Delete";
|
| 230 |
|
| 231 | // when
|
| 232 | when(builder.delete()).thenReturn(response);
|
| 233 | when(response.getStatusInfo()).thenReturn(NOT_FOUND);
|
| 234 | String reason = "Any reason";
|
| 235 | when(response.readEntity(String.class)).thenReturn(reason);
|
| 236 | when(response.getStatus()).thenReturn(NOT_FOUND.getStatusCode());
|
| 237 | boolean finalResponse = testSubject.Delete("", "", PATH);
|
| 238 |
|
| 239 | // then
|
| 240 | verify(builder).delete();
|
| 241 | Assert.assertFalse(finalResponse);
|
| 242 | }
|
| 243 |
|
| 244 | @Test
|
| 245 | public void testFailedRestDelete() throws Exception {
|
| 246 | // given
|
| 247 | String methodName = "Delete";
|
| 248 |
|
| 249 | // when
|
| 250 | when(builder.delete()).thenThrow(new RuntimeException());
|
| 251 | boolean finalResponse = testSubject.Delete("", "", PATH);
|
| 252 |
|
| 253 | // then
|
| 254 | verify(builder).delete();
|
| 255 | Assert.assertFalse(finalResponse);
|
| 256 | }
|
| 257 |
|
| 258 | @Test
|
| 259 | public void testRestJsonGetWithResponse200() throws Exception {
|
| 260 | // given
|
| 261 | String methodName = "RestGet";
|
| 262 |
|
| 263 | // when
|
| 264 | when(builder.get()).thenReturn(response);
|
| 265 | when(response.getStatusInfo()).thenReturn(OK);
|
| 266 | Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse();
|
| 267 |
|
| 268 | // then
|
| 269 | Assert.assertEquals(response, finalResponse);
|
| 270 | }
|
| 271 |
|
| 272 | @Test
|
| 273 | public void testRestJsonGetWithResponse400() throws Exception {
|
| 274 | // given
|
| 275 | String methodName = "RestGet";
|
| 276 |
|
| 277 | // when
|
| 278 | when(builder.get()).thenReturn(response);
|
| 279 | when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
|
| 280 | when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
|
| 281 | Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse();
|
| 282 |
|
| 283 | // then
|
| 284 | Assert.assertEquals(response, finalResponse);
|
| 285 | }
|
| 286 |
|
| 287 | @Test
|
| 288 | public void testFailedRestGet() throws Exception {
|
| 289 | // given
|
| 290 | String methodName = "RestGet";
|
| 291 |
|
| 292 | // when
|
| 293 | when(builder.get()).thenThrow(new RuntimeException());
|
| 294 | Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse();
|
| 295 |
|
| 296 | // then
|
| 297 | Assert.assertEquals(finalResponse, null);
|
| 298 | }
|
| 299 |
|
| 300 | private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException {
|
| 301 | when(systemPropertyHelper.getAAIServerUrl()).thenReturn(Optional.of(HTTP_LOCALHOST));
|
| 302 | when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("cert"));
|
| 303 | when(systemPropertyHelper.getAAIVIDPasswd()).thenReturn(Optional.of("passwd"));
|
| 304 | when(systemPropertyHelper.getAAIVIDUsername()).thenReturn(Optional.of("user"));
|
| 305 | when(systemPropertyHelper.getEncodedCredentials()).thenReturn("someCredentials");
|
| 306 | when(systemPropertyHelper.getFullServicePath(Mockito.anyString())).thenReturn("http://localhost/path");
|
| 307 | when(systemPropertyHelper.getServiceBasePath(Mockito.anyString())).thenReturn("http://localhost/path");
|
| 308 | }
|
| 309 |
|
| 310 | }
|