aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java')
-rw-r--r--vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java302
1 files changed, 249 insertions, 53 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java
index 3ed59ed38..e64b2ac55 100644
--- a/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java
+++ b/vid-app-common/src/test/java/org/onap/vid/aai/util/AAIRestInterfaceTest.java
@@ -2,7 +2,7 @@
* ============LICENSE_START=======================================================
* VID
* ================================================================================
- * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2018 - 2019 Nokia. 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.
@@ -20,80 +20,276 @@
package org.onap.vid.aai.util;
-import org.junit.Test;
+
+import org.mockito.Mock;
+import org.mockito.Mockito;
+
+import org.onap.vid.aai.ExceptionWithRequestInfo;
+import org.onap.vid.aai.exceptions.InvalidPropertyException;
+import org.onap.vid.exceptions.GenericUncheckedException;
+import org.onap.vid.utils.Unchecked;
+import org.springframework.http.HttpMethod;
+import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.ws.rs.client.Client;
+import javax.ws.rs.client.Entity;
+import javax.ws.rs.client.Invocation;
+import javax.ws.rs.client.WebTarget;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.Response;
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.util.Optional;
+import java.util.UUID;
+
+import static com.google.code.beanmatchers.BeanMatchers.hasValidBeanConstructor;
+import static com.google.code.beanmatchers.BeanMatchers.hasValidGettersAndSetters;
+import static javax.ws.rs.core.Response.Status.*;
+import static junit.framework.TestCase.assertSame;
+import static junit.framework.TestCase.fail;
+import static org.junit.Assert.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+import static org.mockito.MockitoAnnotations.initMocks;
+
public class AAIRestInterfaceTest {
- /*
- TO BE IMPLEMENTED
-
+ private static final String PATH = "path";
+ private static final String HTTP_LOCALHOST = "http://localhost/";
+ @Mock
+ private Client client;
+ @Mock
+ private WebTarget webTarget;
+ @Mock
+ private Invocation.Builder builder;
+ @Mock
+ private Invocation invocation;
+ @Mock
+ private ServletRequestHelper servletRequestHelper;
+ @Mock
+ private HttpsAuthClient httpsAuthClient;
+ @Mock
+ private HttpServletRequest httpServletRequest;
+ @Mock
+ private Response response;
+ @Mock
+ private SystemPropertyHelper systemPropertyHelper;
+
+ private AAIRestInterface testSubject;
+
+ @BeforeMethod
+ public void setUp() throws Exception {
+ initMocks(this);
+ mockSystemProperties();
+ testSubject = createTestSubject();
+ when(client.target(HTTP_LOCALHOST+PATH)).thenReturn(webTarget);
+ when(webTarget.request()).thenReturn(builder);
+ when(builder.accept(Mockito.anyString())).thenReturn(builder);
+ when(builder.header(Mockito.anyString(), Mockito.anyString())).thenReturn(builder);
+ when(builder.build(Mockito.anyString())).thenReturn(invocation);
+ when(builder.build(Mockito.anyString(), any(Entity.class))).thenReturn(invocation);
+ when(invocation.invoke()).thenReturn(response);
+ when(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString());
+ }
+
private AAIRestInterface createTestSubject() {
- return new AAIRestInterface("");
+ return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper);
}
@Test
- public void testEncodeURL() throws Exception {
- AAIRestInterface testSubject;
- String nodeKey = "";
- String result;
+ public void shouldEncodeURL() throws Exception {
+ String nodeKey = "some unusual uri";
+ String nodeKey2 = "some/usual/uri";
+ Assert.assertEquals(testSubject.encodeURL(nodeKey), "some%20unusual%20uri");
+ Assert.assertEquals(testSubject.encodeURL(nodeKey2), "some%2Fusual%2Furi");
+ }
- // default test
- testSubject = createTestSubject();
- result = testSubject.encodeURL(nodeKey);
+ @Test
+ public void shouldSetRestSrvrBaseURL() {
+ String baseUrl = "anything";
+ testSubject.setRestSrvrBaseURL(baseUrl);
+ Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl);
}
@Test
- public void testSetRestSrvrBaseURL() throws Exception {
- AAIRestInterface testSubject;
- String baseURL = "";
+ public void shouldExecuteRestJsonPutMethodWithResponse200() {
+ // given
+ String payload = "{\"id\": 1}";
+ Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
- // test 1
- testSubject = createTestSubject();
- baseURL = null;
- testSubject.SetRestSrvrBaseURL(baseURL);
+ // when
+ when(response.getStatusInfo()).thenReturn(OK);
+ Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse();
- // test 2
- testSubject = createTestSubject();
- baseURL = "";
- testSubject.SetRestSrvrBaseURL(baseURL);
+ // then
+ verify(builder).build(HttpMethod.PUT.name(), entity);
+ Assert.assertEquals(response, finalResponse);
+ }
+
+ @Test(expectedExceptions = {ExceptionWithRequestInfo.class})
+ public void shouldFailWhenRestJsonPutMethodExecuted() {
+ // given
+ String payload = "{\"id\": 1}";
+
+ // when
+ when(builder.build(eq(HttpMethod.PUT.name()), any(Entity.class))).thenThrow(new GenericUncheckedException("msg"));
+ testSubject.RestPut("", PATH, payload, false, true);
+
+ //then
}
@Test
- public void testGetRestSrvrBaseURL() throws Exception {
- AAIRestInterface testSubject;
- String result;
+ public void shouldExecuteRestJsonPutMethodWithResponse400() {
+ // given
+ String payload = "{\"id\": 1}";
+ Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
- // default test
- testSubject = createTestSubject();
- result = testSubject.getRestSrvrBaseURL();
+ // when
+ when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
+ when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
+ Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse();
+
+ // then
+ verify(builder).build(HttpMethod.PUT.name(), entity);
+ Assert.assertEquals(response, finalResponse);
}
+ @Test
+ public void shouldExecuteRestPostMethodWithResponse200() {
+ // given
+ String payload = "{\"id\": 1}";
+ Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
+
+ // when
+ when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
+ when(response.getStatusInfo()).thenReturn(OK);
+ Response finalResponse = testSubject.RestPost("", PATH, payload, false);
+
+ // then
+ verify(builder).post(entity);
+ Assert.assertEquals(response, finalResponse);
+ }
@Test
- public void testRestPut() throws Exception {
- AAIRestInterface testSubject;
- String fromAppId = "";
- String transId = "";
- String path = "";
- String payload = "";
- boolean xml = false;
-
- // default test
- testSubject = createTestSubject();
- testSubject.RestPut(fromAppId, transId, path, payload, xml);
+ public void shouldExecuteRestPostMethodWithResponse400() {
+ // given
+ String payload = "{\"id\": 1}";
+ Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
+
+ // when
+ when(builder.post(Mockito.any(Entity.class))).thenReturn(response);
+ when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
+ when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
+ Response finalResponse = testSubject.RestPost("", PATH, payload, false);
+
+ // then
+ verify(builder).post(entity);
+ Assert.assertEquals(response, finalResponse);
}
@Test
- public void testRestPost() throws Exception {
- AAIRestInterface testSubject;
- String fromAppId = "";
- String transId = "";
- String path = "";
- String payload = "";
- boolean xml = false;
-
- // default test
- testSubject = createTestSubject();
- testSubject.RestPost(fromAppId, transId, path, payload, xml);
- }*/
+ public void shouldFailWhenRestPostMethodExecuted() {
+ // given
+ String payload = "{\"id\": 1}";
+ Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON);
+
+ // when
+ when(builder.post(Mockito.any(Entity.class))).thenThrow(new RuntimeException());
+ Response finalResponse = testSubject.RestPost("", PATH, payload, false);
+
+ // then
+ verify(builder).post(entity);
+ Assert.assertNull(finalResponse);
+ }
+
+ @Test
+ public void shouldExecuteRestDeleteMethodWithResponse400() {
+ // given
+ // when
+ when(builder.delete()).thenReturn(response);
+ when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
+ String reason = "Any reason";
+ when(response.readEntity(String.class)).thenReturn(reason);
+ when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
+ boolean finalResponse = testSubject.Delete("", "", PATH);
+
+ // then
+ verify(builder).delete();
+ Assert.assertFalse(finalResponse);
+ }
+
+ @Test
+ public void shouldExecuteRestDeleteMethodWithResponse404() {
+ // given
+ // when
+ when(builder.delete()).thenReturn(response);
+ when(response.getStatusInfo()).thenReturn(NOT_FOUND);
+ String reason = "Any reason";
+ when(response.readEntity(String.class)).thenReturn(reason);
+ when(response.getStatus()).thenReturn(NOT_FOUND.getStatusCode());
+ boolean finalResponse = testSubject.Delete("", "", PATH);
+
+ // then
+ verify(builder).delete();
+ Assert.assertFalse(finalResponse);
+ }
+
+ @Test
+ public void shouldFailWhenRestDeleteExecuted() {
+ // given
+ // when
+ when(builder.delete()).thenThrow(new RuntimeException());
+ boolean finalResponse = testSubject.Delete("", "", PATH);
+ // then
+ verify(builder).delete();
+ Assert.assertFalse(finalResponse);
+ }
+
+ @Test
+ public void shouldExecuteRestGetMethodWithResponse200() {
+ // given
+ // when
+ when(response.getStatusInfo()).thenReturn(OK);
+ Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
+
+ // then
+ Assert.assertEquals(response, finalResponse);
+ }
+
+ @Test
+ public void shouldExecuteRestGetMethodWithResponse400() {
+ // given
+ // when
+ when(response.getStatusInfo()).thenReturn(BAD_REQUEST);
+ when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode());
+ Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
+
+ // then
+ Assert.assertEquals(response, finalResponse);
+ }
+
+ @Test
+ public void shouldFailWhenRestGetMethodExecuted() {
+ // given
+ // when
+ when(builder.build(HttpMethod.GET.name())).thenThrow(new RuntimeException());
+ Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse();
+
+ // then
+ Assert.assertNull(finalResponse);
+ }
+
+ private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException {
+ when(systemPropertyHelper.getEncodedCredentials()).thenReturn("someCredentials");
+ when(systemPropertyHelper.getFullServicePath(Mockito.anyString())).thenReturn("http://localhost/path");
+ when(systemPropertyHelper.getFullServicePath(Mockito.any(URI.class))).thenReturn("http://localhost/path");
+ when(systemPropertyHelper.getServiceBasePath(Mockito.anyString())).thenReturn("http://localhost/path");
+ }
+
}