diff options
author | tgolabek <tomasz.golabek@nokia.com> | 2018-05-23 11:40:17 +0200 |
---|---|---|
committer | golabek <tomasz.golabek@nokia.com> | 2018-08-06 13:40:06 +0200 |
commit | f07fc1a586328d2b4cef02bd1d8f9e791130bf1b (patch) | |
tree | c842e8bce5b2acd3c17c494c15e9ad2a6d157a82 /vid-app-common/src/test/java/org/onap | |
parent | ff76b5ed0aa91d5fdf9dc4f95e8b20f91ed9d072 (diff) |
Refactor of an AAIRestInterface
Refactor and some additional tests added
(cherry picked from commit 6d8fa7d179b8de802ae386b317ddd1214eac1c47)
Change-Id: Ibe7583353499352aa81d100b9995b9c74133c447
Issue-ID: VID-229
Signed-off-by: Stern, Ittay (is9613) <is9613@att.com>
[Added proper headers to modified and created files]
Signed-off-by: golabek <tomasz.golabek@nokia.com>
Diffstat (limited to 'vid-app-common/src/test/java/org/onap')
8 files changed, 641 insertions, 23 deletions
diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/AaiClientTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/AaiClientTest.java index 32e894d0..76d1d4af 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/AaiClientTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/AaiClientTest.java @@ -1,3 +1,24 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 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. + * 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.vid.aai; import com.fasterxml.jackson.databind.ObjectMapper; @@ -8,21 +29,23 @@ import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.commons.lang3.reflect.FieldUtils; import org.apache.commons.lang3.tuple.Pair; import org.mockito.Mockito; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.aai.model.AaiGetTenatns.GetTenantsResponse; import org.onap.vid.aai.model.AaiNodeQueryResponse; import org.onap.vid.aai.model.ResourceType; import org.onap.vid.aai.util.AAIRestInterface; import org.onap.vid.aai.util.HttpsAuthClient; +import org.onap.vid.aai.util.ServletRequestHelper; +import org.onap.vid.aai.util.SystemPropertyHelper; +import org.onap.vid.controllers.LocalWebConfig; import org.onap.vid.exceptions.GenericUncheckedException; import org.onap.vid.model.Subscriber; import org.onap.vid.model.SubscriberList; import org.onap.vid.model.probes.ExternalComponentStatus; import org.onap.vid.model.probes.HttpRequestMetadata; import org.onap.vid.model.probes.StatusMetadata; -import org.onap.vid.controllers.LocalWebConfig; import org.onap.vid.testUtils.TestUtils; -import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; -import org.onap.portalsdk.core.util.SystemProperties; import org.springframework.http.HttpMethod; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.web.WebAppConfiguration; @@ -398,7 +421,7 @@ public class AaiClientTest { Response responseMock = mocks.getFakeResponse(); // prepare real AAIRestInterface and AaiClient, and wire mocks - AAIRestInterface aaiRestInterface = new AAIRestInterface(httpsAuthClientMock); + AAIRestInterface aaiRestInterface = new AAIRestInterface(httpsAuthClientMock, new ServletRequestHelper(), new SystemPropertyHelper()); final AaiClient aaiClient = new AaiClient(aaiRestInterface, null); when(httpsAuthClientMock.getClient(any())).thenReturn(javaxClientMock); diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/util/CustomJacksonJaxBJsonProviderTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/util/CustomJacksonJaxBJsonProviderTest.java index dcd962f1..f2b01f0c 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/util/CustomJacksonJaxBJsonProviderTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/util/CustomJacksonJaxBJsonProviderTest.java @@ -1,5 +1,32 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 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. + * 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.vid.aai.util; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.databind.DeserializationConfig; +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.SerializationConfig; +import com.fasterxml.jackson.databind.SerializationFeature; +import org.junit.Assert; import org.junit.Test; public class CustomJacksonJaxBJsonProviderTest { @@ -9,11 +36,19 @@ public class CustomJacksonJaxBJsonProviderTest { } @Test - public void testGetMapper() throws Exception { - CustomJacksonJaxBJsonProvider testSubject; + public void testMapperHasCorrectConfig() throws Exception { + CustomJacksonJaxBJsonProvider testSubject = createTestSubject(); + DeserializationConfig deserializationConfig = testSubject.getMapper().getDeserializationConfig(); + SerializationConfig serializationConfig = testSubject.getMapper().getSerializationConfig(); + + Assert.assertFalse(serializationConfig.hasSerializationFeatures(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS.getMask())); + Assert.assertFalse(serializationConfig.hasSerializationFeatures(SerializationFeature.INDENT_OUTPUT.getMask())); + Assert.assertFalse(serializationConfig.hasSerializationFeatures(SerializationFeature.WRAP_ROOT_VALUE.getMask())); + Assert.assertFalse(serializationConfig.hasSerializationFeatures(SerializationFeature.CLOSE_CLOSEABLE.getMask())); + + Assert.assertFalse(deserializationConfig.hasDeserializationFeatures(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES.getMask())); + Assert.assertFalse(deserializationConfig.hasDeserializationFeatures(DeserializationFeature.UNWRAP_ROOT_VALUE.getMask())); - // default test - testSubject = createTestSubject(); - testSubject.getMapper(); + Assert.assertEquals(serializationConfig.getSerializationInclusion(), JsonInclude.Include.NON_NULL); } }
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/util/HttpsAuthClientTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/util/HttpsAuthClientTest.java index a26c431e..f000dc80 100644 --- a/vid-app-common/src/test/java/org/onap/vid/aai/util/HttpsAuthClientTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/aai/util/HttpsAuthClientTest.java @@ -1,22 +1,94 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 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. + * 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.vid.aai.util; +import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.vid.aai.exceptions.HttpClientBuilderException; + +import javax.net.ssl.SSLContext; +import java.util.Optional; + +import static org.mockito.Matchers.anyString; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +@RunWith(MockitoJUnitRunner.class) public class HttpsAuthClientTest { + @Mock + private SystemPropertyHelper systemPropertyHelper; + @Mock + private SSLContextProvider sslContextProvider; + @Mock + private SSLContext sslContext; + + public static final String CERT_FILE_PATH = "any_path"; /* TO BE IMPLEMENTED private HttpsAuthClient createTestSubject() { - return new HttpsAuthClient(); + return new HttpsAuthClient(systemPropertyHelper, sslContextProvider); + } + + @Before + public void setUp() throws Exception { + when(systemPropertyHelper.getAAITruststoreFilename()).thenReturn(Optional.of("filename")); + when(systemPropertyHelper.getEncodedTruststorePassword()).thenReturn("password"); + } + + @Test(expected = HttpClientBuilderException.class) + public void testHttpClientBuilderExceptionOnGetClient() throws HttpClientBuilderException { + //when + when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("true")); + when(sslContextProvider.getSslContext(anyString(), anyString())).thenThrow(new HttpClientBuilderException()); + createTestSubject().getClient("nonExistingFile"); + } + + @Test + public void testGetSecuredClient() throws Exception { + // when + when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("true")); + when(sslContextProvider.getSslContext(anyString(), anyString())).thenReturn(sslContext); + createTestSubject().getClient(CERT_FILE_PATH); + + //then + verify(sslContextProvider).getSslContext(anyString(), anyString()); } @Test - public void testGetClient() throws Exception { - String certFilePath = ""; + public void testGetUnsecuredClient() throws Exception { + // when + when(systemPropertyHelper.getAAIUseClientCert()).thenReturn(Optional.of("false")); + when(sslContextProvider.getSslContext(anyString(), anyString())).thenReturn(sslContext); + createTestSubject().getClient(CERT_FILE_PATH); - // default test - HttpsAuthClient.getClient(certFilePath); + //then + verify(sslContextProvider, never()).getSslContext(anyString(), anyString()); } */ }
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/aai/util/ParametrizedAAIRestInterfaceTest.java b/vid-app-common/src/test/java/org/onap/vid/aai/util/ParametrizedAAIRestInterfaceTest.java new file mode 100644 index 00000000..94e749ac --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/aai/util/ParametrizedAAIRestInterfaceTest.java @@ -0,0 +1,123 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2018 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. + * 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.vid.aai.util; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.onap.vid.aai.exceptions.InvalidPropertyException; +import org.testng.Assert; + +import javax.servlet.http.HttpServletRequest; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.Response; +import java.io.UnsupportedEncodingException; +import java.util.Arrays; +import java.util.Collection; +import java.util.Optional; +import java.util.UUID; + +import static javax.ws.rs.core.Response.Status.NO_CONTENT; +import static javax.ws.rs.core.Response.Status.OK; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(Parameterized.class) +public class ParametrizedAAIRestInterfaceTest { + + 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 ServletRequestHelper servletRequestHelper; + @Mock + private HttpsAuthClient httpsAuthClient; + @Mock + private HttpServletRequest httpServletRequest; + @Mock + private Response response; + @Mock + private SystemPropertyHelper systemPropertyHelper; + + private AAIRestInterface testSubject; + private Response.Status status; + + @Parameterized.Parameters + public static Collection<Object> data() { + return Arrays.asList(OK, NO_CONTENT); + } + + @Before + public void setUp() throws Exception { + MockitoAnnotations.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(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString()); + } + + public ParametrizedAAIRestInterfaceTest(Response.Status status) { + this.status = status; + } + + private AAIRestInterface createTestSubject() { + return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper); + } + + @Test + public void testRestDeleteWithValidResponse() throws Exception { + // given + String methodName = "Delete"; + + // when + when(builder.delete()).thenReturn(response); + when(response.getStatusInfo()).thenReturn(status); + boolean finalResponse = testSubject.Delete("", "", PATH); + + // then + verify(builder).delete(); + Assert.assertTrue(finalResponse); + } + + 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"); + } + +} 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 new file mode 100644 index 00000000..288b891c --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/aai/util/SingleAAIRestInterfaceTest.java @@ -0,0 +1,307 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2018 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. + * 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.vid.aai.util; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.vid.aai.exceptions.InvalidPropertyException; +import org.testng.Assert; + +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.util.Optional; +import java.util.UUID; + +import static javax.ws.rs.core.Response.Status.*; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@RunWith(MockitoJUnitRunner.class) +public class SingleAAIRestInterfaceTest { + + 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 ServletRequestHelper servletRequestHelper; + @Mock + private HttpsAuthClient httpsAuthClient; + @Mock + private HttpServletRequest httpServletRequest; + @Mock + private Response response; + @Mock + private SystemPropertyHelper systemPropertyHelper; + + private AAIRestInterface testSubject; + + @Before + public void setUp() throws Exception { + 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(servletRequestHelper.extractOrGenerateRequestId()).thenReturn(UUID.randomUUID().toString()); + } + + private AAIRestInterface createTestSubject() { + return new AAIRestInterface(Optional.of(client), httpsAuthClient, servletRequestHelper, systemPropertyHelper); + } + + @Test + public void testEncodeURL() throws Exception { + String nodeKey = "some unusual uri"; + Assert.assertEquals(testSubject.encodeURL(nodeKey), "some%20unusual%20uri"); + } + + @Test + public void testSetRestSrvrBaseURLWithNullValue() throws Exception { + testSubject.SetRestSrvrBaseURL(null); + } + + @Test + public void testSetRestSrvrBaseURL() throws Exception { + String baseUrl = "anything"; + testSubject.SetRestSrvrBaseURL(baseUrl); + Assert.assertEquals(testSubject.getRestSrvrBaseURL(), baseUrl); + } + + @Test + public void testRestJsonPutWithResponse200() throws Exception { + // 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); + + // then + verify(builder).put(entity); + Assert.assertEquals(response, finalResponse); + } + + @Test + public void testFailedRestJsonPut() throws Exception { + // 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); + + // then + verify(builder).put(entity); + Assert.assertEquals(finalResponse, null); + } + + @Test + public void testRestJsonPutWithResponse400() throws Exception { + // 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); + + // then + verify(builder).put(entity); + Assert.assertEquals(response, finalResponse); + } + + @Test + public void testRestPostWithResponse200() throws Exception { + // given + String methodName = "RestPost"; + 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 testRestPostWithResponse400() throws Exception { + // given + String methodName = "RestPost"; + 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 testFailedRestPost() throws Exception { + // given + String methodName = "RestPost"; + 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.assertEquals(finalResponse, null); + } + + @Test + public void testRestDeleteWithResponse400() throws Exception { + // given + String methodName = "Delete"; + + // 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 testRestDeleteWithResponse404() throws Exception { + // given + String methodName = "Delete"; + + // 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 testFailedRestDelete() throws Exception { + // given + String methodName = "Delete"; + + // when + when(builder.delete()).thenThrow(new RuntimeException()); + boolean finalResponse = testSubject.Delete("", "", PATH); + + // then + verify(builder).delete(); + Assert.assertFalse(finalResponse); + } + + @Test + public void testRestJsonGetWithResponse200() throws Exception { + // given + String methodName = "RestGet"; + + // when + when(builder.get()).thenReturn(response); + when(response.getStatusInfo()).thenReturn(OK); + Response finalResponse = testSubject.RestGet("", "", PATH, false).getResponse(); + + // then + Assert.assertEquals(response, finalResponse); + } + + @Test + public void testRestJsonGetWithResponse400() throws Exception { + // 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(); + + // then + Assert.assertEquals(response, finalResponse); + } + + @Test + public void testFailedRestGet() throws Exception { + // given + String methodName = "RestGet"; + + // when + when(builder.get()).thenThrow(new RuntimeException()); + Response finalResponse = testSubject.RestGet("", "", 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"); + } + +}
\ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/config/JobCommandsConfigWithMockedMso.java b/vid-app-common/src/test/java/org/onap/vid/config/JobCommandsConfigWithMockedMso.java index 245623aa..56ac28d2 100644 --- a/vid-app-common/src/test/java/org/onap/vid/config/JobCommandsConfigWithMockedMso.java +++ b/vid-app-common/src/test/java/org/onap/vid/config/JobCommandsConfigWithMockedMso.java @@ -1,9 +1,33 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 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. + * 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.vid.config; import org.hibernate.SessionFactory; import org.mockito.Mockito; +import org.onap.portalsdk.core.service.DataAccessService; import org.onap.vid.aai.AaiClientInterface; import org.onap.vid.aai.util.HttpsAuthClient; +import org.onap.vid.aai.util.SSLContextProvider; +import org.onap.vid.aai.util.SystemPropertyHelper; import org.onap.vid.job.JobAdapter; import org.onap.vid.job.JobsBrokerService; import org.onap.vid.job.command.InProgressStatusCommand; @@ -17,13 +41,11 @@ import org.onap.vid.services.AsyncInstantiationBusinessLogic; import org.onap.vid.services.AsyncInstantiationBusinessLogicImpl; import org.onap.vid.services.AuditService; import org.onap.vid.services.AuditServiceImpl; -import org.onap.portalsdk.core.service.DataAccessService; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; -import org.togglz.core.manager.FeatureManager; @Configuration public class JobCommandsConfigWithMockedMso { @@ -40,7 +62,7 @@ public class JobCommandsConfigWithMockedMso { @Bean public HttpsAuthClient httpsAuthClientFactory(){ - return new HttpsAuthClient("some random path"); + return new HttpsAuthClient("some random path", new SystemPropertyHelper(), new SSLContextProvider()); } @Bean diff --git a/vid-app-common/src/test/java/org/onap/vid/controller/LocalWebConfig.java b/vid-app-common/src/test/java/org/onap/vid/controller/LocalWebConfig.java index 54d0d77b..f63580d2 100644 --- a/vid-app-common/src/test/java/org/onap/vid/controller/LocalWebConfig.java +++ b/vid-app-common/src/test/java/org/onap/vid/controller/LocalWebConfig.java @@ -1,3 +1,24 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * Modifications Copyright (C) 2018 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. + * 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.vid.controller; import com.fasterxml.jackson.databind.ObjectMapper; @@ -5,8 +26,7 @@ import org.onap.vid.aai.AaiClient; import org.onap.vid.aai.AaiClientInterface; import org.onap.vid.aai.AaiResponseTranslator; import org.onap.vid.aai.model.PortDetailsTranslator; -import org.onap.vid.aai.util.AAIRestInterface; -import org.onap.vid.aai.util.HttpsAuthClient; +import org.onap.vid.aai.util.*; import org.onap.vid.asdc.AsdcClient; import org.onap.vid.asdc.parser.ToscaParserImpl2; import org.onap.vid.services.AaiService; @@ -46,14 +66,29 @@ public class LocalWebConfig { } @Bean - public HttpsAuthClient httpsAuthClientFactory(ServletContext servletContext) { + public SSLContextProvider sslContextProvider() { + return new SSLContextProvider(); + } + + @Bean + public SystemPropertyHelper systemPropertyHelper() { + return new SystemPropertyHelper(); + } + + @Bean + public ServletRequestHelper servletRequestHelper() { + return new ServletRequestHelper(); + } + + @Bean + public HttpsAuthClient httpsAuthClientFactory(ServletContext servletContext, SystemPropertyHelper systemPropertyHelper, SSLContextProvider sslContextProvider) { final String certFilePath = new File(servletContext.getRealPath("/WEB-INF/cert/")).getAbsolutePath(); - return new HttpsAuthClient(certFilePath); + return new HttpsAuthClient(certFilePath, systemPropertyHelper, sslContextProvider); } @Bean(name = "aaiRestInterface") - public AAIRestInterface aaiRestInterface(HttpsAuthClient httpsAuthClientFactory) { - return new AAIRestInterface(httpsAuthClientFactory); + public AAIRestInterface aaiRestInterface(HttpsAuthClient httpsAuthClientFactory, ServletRequestHelper servletRequestHelper, SystemPropertyHelper systemPropertyHelper) { + return new AAIRestInterface(httpsAuthClientFactory, servletRequestHelper, systemPropertyHelper); } @Bean diff --git a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java index 175f87f2..0460f532 100644 --- a/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java +++ b/vid-app-common/src/test/java/org/onap/vid/testUtils/TestUtils.java @@ -133,6 +133,7 @@ public class TestUtils { Mockito.when(fakeBuilder.get(any(GenericType.class))).thenReturn(null); Mockito.when(fakeResponse.getStatus()).thenReturn(200); + Mockito.when(fakeResponse.getStatusInfo()).thenReturn(Response.Status.OK); Mockito.when(fakeResponse.readEntity(Service.class)).thenReturn(null); } } |