diff options
Diffstat (limited to 'vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java')
-rw-r--r-- | vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java | 75 |
1 files changed, 40 insertions, 35 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java index 7fad9019c..b5f8ff942 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java @@ -26,7 +26,11 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; +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 javax.servlet.http.HttpServletRequest; @@ -37,10 +41,14 @@ 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 javax.ws.rs.core.Response.Status.*; +import static junit.framework.TestCase.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -56,6 +64,8 @@ public class SingleAAIRestInterfaceTest { @Mock private Invocation.Builder builder; @Mock + private Invocation invocation; + @Mock private ServletRequestHelper servletRequestHelper; @Mock private HttpsAuthClient httpsAuthClient; @@ -76,6 +86,9 @@ public class SingleAAIRestInterfaceTest { 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()); } @@ -90,70 +103,67 @@ public class SingleAAIRestInterfaceTest { } @Test - public void testSetRestSrvrBaseURLWithNullValue() throws Exception { + public void testSetRestSrvrBaseURLWithNullValue() { testSubject.SetRestSrvrBaseURL(null); } @Test - public void testSetRestSrvrBaseURL() throws Exception { + public void testSetRestSrvrBaseURL() { String baseUrl = "anything"; testSubject.SetRestSrvrBaseURL(baseUrl); Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl); } @Test - public void testRestJsonPutWithResponse200() throws Exception { + public void testRestJsonPutWithResponse200() { // given String methodName = "RestPut"; String payload = "{\"id\": 1}"; Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON); // when - when(builder.put(Mockito.any(Entity.class))).thenReturn(response); when(response.getStatusInfo()).thenReturn(OK); - Response finalResponse = testSubject.RestPut("", PATH, payload, false); + Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse(); // then - verify(builder).put(entity); + verify(builder).build(HttpMethod.PUT.name(), entity); Assert.assertEquals(response, finalResponse); } - @Test - public void testFailedRestJsonPut() throws Exception { + @Test(expected = ExceptionWithRequestInfo.class) + public void testFailedRestJsonPut() { // given String methodName = "RestPut"; String payload = "{\"id\": 1}"; Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON); // when - when(builder.put(Mockito.any(Entity.class))).thenThrow(new RuntimeException()); - Response finalResponse = testSubject.RestPut("", PATH, payload, false); + when(builder.build(eq(HttpMethod.PUT.name()), any(Entity.class))).thenThrow(new GenericUncheckedException("msg")); + Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse(); // then - verify(builder).put(entity); - Assert.assertEquals(finalResponse, null); + fail("expected unreachable: exception to be thrown"); } @Test - public void testRestJsonPutWithResponse400() throws Exception { + public void testRestJsonPutWithResponse400() { // given String methodName = "RestPut"; String payload = "{\"id\": 1}"; Entity<String> entity = Entity.entity(payload, MediaType.APPLICATION_JSON); // when - when(builder.put(Mockito.any(Entity.class))).thenReturn(response); when(response.getStatusInfo()).thenReturn(BAD_REQUEST); when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode()); - Response finalResponse = testSubject.RestPut("", PATH, payload, false); + Response finalResponse = testSubject.RestPut("", PATH, payload, false, true).getResponse(); // then - verify(builder).put(entity); + verify(builder).build(HttpMethod.PUT.name(), entity); Assert.assertEquals(response, finalResponse); } @Test - public void testRestPostWithResponse200() throws Exception { + public void testRestPostWithResponse200() { // given String methodName = "RestPost"; String payload = "{\"id\": 1}"; @@ -170,7 +180,7 @@ public class SingleAAIRestInterfaceTest { } @Test - public void testRestPostWithResponse400() throws Exception { + public void testRestPostWithResponse400() { // given String methodName = "RestPost"; String payload = "{\"id\": 1}"; @@ -188,7 +198,7 @@ public class SingleAAIRestInterfaceTest { } @Test - public void testFailedRestPost() throws Exception { + public void testFailedRestPost() { // given String methodName = "RestPost"; String payload = "{\"id\": 1}"; @@ -204,7 +214,7 @@ public class SingleAAIRestInterfaceTest { } @Test - public void testRestDeleteWithResponse400() throws Exception { + public void testRestDeleteWithResponse400() { // given String methodName = "Delete"; @@ -222,7 +232,7 @@ public class SingleAAIRestInterfaceTest { } @Test - public void testRestDeleteWithResponse404() throws Exception { + public void testRestDeleteWithResponse404() { // given String methodName = "Delete"; @@ -240,7 +250,7 @@ public class SingleAAIRestInterfaceTest { } @Test - public void testFailedRestDelete() throws Exception { + public void testFailedRestDelete() { // given String methodName = "Delete"; @@ -254,54 +264,49 @@ public class SingleAAIRestInterfaceTest { } @Test - public void testRestJsonGetWithResponse200() throws Exception { + public void testRestJsonGetWithResponse200() { // given String methodName = "RestGet"; // when - when(builder.get()).thenReturn(response); when(response.getStatusInfo()).thenReturn(OK); - Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse(); + Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse(); // then Assert.assertEquals(response, finalResponse); } @Test - public void testRestJsonGetWithResponse400() throws Exception { + public void testRestJsonGetWithResponse400() { // given String methodName = "RestGet"; // when - when(builder.get()).thenReturn(response); when(response.getStatusInfo()).thenReturn(BAD_REQUEST); when(response.getStatus()).thenReturn(BAD_REQUEST.getStatusCode()); - Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse(); + Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse(); // then Assert.assertEquals(response, finalResponse); } @Test - public void testFailedRestGet() throws Exception { + public void testFailedRestGet() { // given String methodName = "RestGet"; // when - when(builder.get()).thenThrow(new RuntimeException()); - Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse(); + when(builder.build(HttpMethod.GET.name())).thenThrow(new RuntimeException()); + Response finalResponse = testSubject.RestGet("", "", Unchecked.toURI(PATH), false).getResponse(); // then Assert.assertEquals(finalResponse, null); } private void mockSystemProperties() throws UnsupportedEncodingException, InvalidPropertyException { - when(systemPropertyHelper.getAAIServerUrl()).thenReturn(Optional.of(HTTP_LOCALHOST)); - when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("cert")); - when(systemPropertyHelper.getAAIVIDPasswd()).thenReturn(Optional.of("passwd")); - when(systemPropertyHelper.getAAIVIDUsername()).thenReturn(Optional.of("user")); 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"); } |