From d47fd3bfc9aa456091c356122d149166e853f953 Mon Sep 17 00:00:00 2001 From: koblosz Date: Mon, 20 Aug 2018 16:42:57 +0200 Subject: VID-266 added stub server for testing Change-Id: I39b8fca2df4c45123e2308142dfbb43c737f7707 Issue-ID: VID-266 Signed-off-by: Koblosz, Sandra --- .../vid/scheduler/SchedulerRestInterfaceTest.java | 158 +++++++++++++++++++++ .../org/onap/vid/testUtils/StubServerUtil.java | 90 ++++++++++++ 2 files changed, 248 insertions(+) create mode 100644 vid-app-common/src/test/java/org/onap/vid/scheduler/SchedulerRestInterfaceTest.java create mode 100644 vid-app-common/src/test/java/org/onap/vid/testUtils/StubServerUtil.java (limited to 'vid-app-common/src/test/java') diff --git a/vid-app-common/src/test/java/org/onap/vid/scheduler/SchedulerRestInterfaceTest.java b/vid-app-common/src/test/java/org/onap/vid/scheduler/SchedulerRestInterfaceTest.java new file mode 100644 index 00000000..7becf8b7 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/scheduler/SchedulerRestInterfaceTest.java @@ -0,0 +1,158 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2018 Nokia Intellectual Property. 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.scheduler; + +import com.xebialabs.restito.semantics.Action; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.*; +import org.mockito.runners.MockitoJUnitRunner; +import org.onap.vid.aai.util.HttpClientMode; +import org.onap.vid.aai.util.HttpsAuthClient; +import org.onap.vid.testUtils.StubServerUtil; +import org.testng.annotations.BeforeMethod; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.Response; +import java.io.IOException; +import java.security.GeneralSecurityException; +import java.util.Collections; +import java.util.function.Function; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + + +@RunWith(MockitoJUnitRunner.class) +public class SchedulerRestInterfaceTest { + + private static final String USR_PWD_AUTH_STRING = "c2FtcGxlOnBhUyR3MFJk"; + private static final String APPLICATION_JSON = "application/json"; + private static MultivaluedHashMap commonHeaders = new MultivaluedHashMap<>(); + private static StubServerUtil serverUtil; + private String sampleBaseUrl; + @Mock + private HttpsAuthClient mockedHttpsAuthClient; + @Mock + private Client mockedClient; + @Mock + private Invocation.Builder mockedBuilder; + @Mock + private Response mockedResponse; + @Mock + private WebTarget mockedWebTarget; + + @Mock + private Function propertyGetter; + + @InjectMocks + private SchedulerRestInterface schedulerInterface = new SchedulerRestInterface(); + + @BeforeClass + public static void setUpClass() { + serverUtil = new StubServerUtil(); + serverUtil.runServer(); + commonHeaders.put("Authorization", Collections.singletonList("Basic " + USR_PWD_AUTH_STRING)); + } + + @AfterClass + public static void tearDownClass() { + serverUtil.stopServer(); + } + + @BeforeMethod + public void setUp() { + MockitoAnnotations.initMocks(this); + + sampleBaseUrl = serverUtil.constructTargetUrl("http", ""); + } + + @Test + public void testShouldGetOKWhenStringIsExpected() throws IOException, GeneralSecurityException { + String sampleSourceId = "AAI"; + RestObject sampleRestObj = new RestObject<>(); + String resultHolder = ""; + + String responseContent = "sample : SAMPLE RESULT STRING"; + Mockito.doReturn(mockedClient).when(mockedHttpsAuthClient).getClient(HttpClientMode.WITHOUT_KEYSTORE); + Mockito.doReturn("sample").when(propertyGetter).apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL); + Mockito.doReturn("paS$w0Rd").when(propertyGetter).apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL); + Mockito.doReturn(sampleBaseUrl).when(propertyGetter).apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL); + Mockito.doReturn(200).when(mockedResponse).getStatus(); + Mockito.doReturn(responseContent).when(mockedResponse).readEntity(String.class); + Mockito.doReturn(mockedResponse).when(mockedBuilder).get(); + Mockito.when(mockedBuilder.header(Matchers.any(), Matchers.any())).thenReturn(mockedBuilder); + Mockito.doReturn(mockedBuilder).when(mockedBuilder).headers(commonHeaders); + Mockito.doReturn(mockedBuilder).when(mockedBuilder).accept(APPLICATION_JSON); + Mockito.doReturn(mockedBuilder).when(mockedWebTarget).request(); + Mockito.doReturn(mockedWebTarget).when(mockedClient).target(sampleBaseUrl + "test"); + + serverUtil.prepareGetCall("/test", responseContent, Action.ok()); + + schedulerInterface.Get(resultHolder, sampleSourceId, "test", sampleRestObj); + + assertResponseData(sampleRestObj, responseContent, 200); + } + + + @Test + public void testShouldDeleteSuccessfully() throws IOException, GeneralSecurityException { + String sampleTargetUrl = serverUtil.constructTargetUrl("http", ""); + String sampleSourceId = "AAI"; + RestObject sampleRestObj = new RestObject<>(); + String resultHolder = ""; + + String responseContent = "sample : SAMPLE RESULT STRING"; + Mockito.doReturn(mockedClient).when(mockedHttpsAuthClient).getClient(HttpClientMode.WITHOUT_KEYSTORE); + Mockito.doReturn("sample").when(propertyGetter).apply(SchedulerProperties.SCHEDULER_USER_NAME_VAL); + Mockito.doReturn("paS$w0Rd").when(propertyGetter).apply(SchedulerProperties.SCHEDULER_PASSWORD_VAL); + Mockito.doReturn(sampleTargetUrl).when(propertyGetter).apply(SchedulerProperties.SCHEDULER_SERVER_URL_VAL); + Mockito.doReturn(200).when(mockedResponse).getStatus(); + Mockito.doReturn(responseContent).when(mockedResponse).readEntity(String.class); + Mockito.doReturn(mockedResponse).when(mockedBuilder).delete(); + Mockito.when(mockedBuilder.header(Matchers.any(), Matchers.any())).thenReturn(mockedBuilder); + Mockito.doReturn(mockedBuilder).when(mockedBuilder).headers(commonHeaders); + Mockito.doReturn(mockedBuilder).when(mockedBuilder).accept(APPLICATION_JSON); + Mockito.doReturn(mockedBuilder).when(mockedWebTarget).request(); + Mockito.doReturn(mockedWebTarget).when(mockedClient).target(sampleTargetUrl + "test"); + + serverUtil.prepareDeleteCall("/test", responseContent, Action.ok()); + + schedulerInterface.Delete(resultHolder, sampleSourceId, "test", sampleRestObj); + + assertResponseData(sampleRestObj, responseContent, 200); + } + + + private void assertResponseData(RestObject sampleRestObj, String expectedResponse, int expectedStatusCode) { + + assertThat(sampleRestObj.getStatusCode()).isEqualTo(expectedStatusCode); + assertThat(sampleRestObj.get()).isInstanceOf(String.class).isEqualTo(expectedResponse); + assertThat(sampleRestObj.getUUID()).isNull(); + + } + +} diff --git a/vid-app-common/src/test/java/org/onap/vid/testUtils/StubServerUtil.java b/vid-app-common/src/test/java/org/onap/vid/testUtils/StubServerUtil.java new file mode 100644 index 00000000..e84655f7 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/testUtils/StubServerUtil.java @@ -0,0 +1,90 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2018 Nokia Intellectual Property. 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.testUtils; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.xebialabs.restito.semantics.Action; +import com.xebialabs.restito.semantics.Condition; +import com.xebialabs.restito.server.StubServer; + +import static com.xebialabs.restito.builder.stub.StubHttp.whenHttp; +import static com.xebialabs.restito.semantics.Action.contentType; +import static com.xebialabs.restito.semantics.Action.stringContent; + +public class StubServerUtil { + + private static final String APPLICATION_JSON = "application/json"; + private ObjectMapper objectMapper; + + + private StubServer stubServer; + + public StubServerUtil() { + this.objectMapper = new ObjectMapper(); + this.stubServer = new StubServer(); + } + + + public void runServer() { + stubServer.run(); + } + + public void stopServer() { + stubServer.stop(); + } + + + public String constructTargetUrl(String protocol, String relativePath) { + return String.format("%s://localhost:%s/%s", protocol, stubServer.getPort(), relativePath); + } + + public void prepareGetCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException { + whenHttp(stubServer) + .match(Condition.get(path)) + .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON)); + } + + public void prepareDeleteCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException { + whenHttp(stubServer) + .match(Condition.delete(path)) + .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON)); + } + + public void preparePostCall(String path, Object returnObj, Action expectedAction) throws JsonProcessingException { + whenHttp(stubServer) + .match(Condition.post(path), + Condition.withPostBodyContaining(objectMapper.writeValueAsString(returnObj))) + .then(expectedAction, jsonContent(returnObj), contentType(APPLICATION_JSON)); + } + + public void preparePutCall(String path, Object returnObj, Action expectedStatus) throws JsonProcessingException { + whenHttp(stubServer) + .match(Condition.put(path), + Condition.withPostBodyContaining(objectMapper.writeValueAsString(returnObj))) + .then(expectedStatus, jsonContent(returnObj), contentType(APPLICATION_JSON)); + } + + private Action jsonContent(Object returnObj) throws JsonProcessingException { + return stringContent(objectMapper.writeValueAsString(returnObj)); + } + +} -- cgit 1.2.3-korg