From addf93c4f22bc4a2c4edcabeea3f36e0065cbb6c Mon Sep 17 00:00:00 2001 From: Bartosz Gardziejewski Date: Mon, 4 Mar 2019 14:57:03 +0100 Subject: Increasing test coverage for vid.mso.rest Change-Id: Ie672d561c5f1c674cce979e0f640027bb6fc0fa7 Issue-ID: VID-387 Signed-off-by: Bartosz Gardziejewski --- .../java/org/onap/vid/controller/MsoConfig.java | 5 +- .../org/onap/vid/mso/RestMsoImplementation.java | 18 +- .../org/onap/vid/mso/rest/MsoRestClientNew.java | 4 +- .../src/main/java/org/onap/vid/mso/rest/Task.java | 3 - .../onap/vid/mso/RestMsoImplementationTest.java | 421 +++++++++++ .../onap/vid/mso/rest/MsoRestClientNewTest.java | 4 +- .../org/onap/vid/mso/rest/MsoRestClientTest.java | 822 ++++++++++++++++++++- .../services/ChangeManagementServiceUnitTest.java | 6 +- 8 files changed, 1242 insertions(+), 41 deletions(-) create mode 100644 vid-app-common/src/test/java/org/onap/vid/mso/RestMsoImplementationTest.java (limited to 'vid-app-common/src') diff --git a/vid-app-common/src/main/java/org/onap/vid/controller/MsoConfig.java b/vid-app-common/src/main/java/org/onap/vid/controller/MsoConfig.java index 563e798df..a472268a3 100644 --- a/vid-app-common/src/main/java/org/onap/vid/controller/MsoConfig.java +++ b/vid-app-common/src/main/java/org/onap/vid/controller/MsoConfig.java @@ -23,6 +23,7 @@ package org.onap.vid.controller; import io.joshworks.restclient.http.mapper.ObjectMapper; import org.onap.portalsdk.core.util.SystemProperties; import org.onap.vid.aai.AaiClientInterface; +import org.onap.vid.aai.util.HttpsAuthClient; import org.onap.vid.client.SyncRestClient; import org.onap.vid.factories.MsoRequestFactory; import org.onap.vid.mso.MsoBusinessLogic; @@ -47,10 +48,10 @@ public class MsoConfig { } @Bean - public MsoRestClientNew msoClient(ObjectMapper unirestObjectMapper){ + public MsoRestClientNew msoClient(ObjectMapper unirestObjectMapper, HttpsAuthClient httpsAuthClient){ // Satisfy both interfaces -- MsoInterface and RestMsoImplementation return new MsoRestClientNew(new SyncRestClient(unirestObjectMapper), SystemProperties.getProperty( - MsoProperties.MSO_SERVER_URL)); + MsoProperties.MSO_SERVER_URL),httpsAuthClient); } @Bean diff --git a/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java b/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java index ff12bb8bc..e14ac0e11 100644 --- a/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java +++ b/vid-app-common/src/main/java/org/onap/vid/mso/RestMsoImplementation.java @@ -51,7 +51,7 @@ import static org.onap.vid.utils.Logging.*; /** * Created by pickjonathan on 26/06/2017. */ -public abstract class RestMsoImplementation implements RestInterface { +public class RestMsoImplementation implements RestInterface { /** * The logger. @@ -62,7 +62,7 @@ public abstract class RestMsoImplementation implements RestInterface { /** The client. */ private Client client = null; - @Autowired + protected HttpsAuthClient httpsAuthClient; private static final String START_LOG = " start"; @@ -79,6 +79,11 @@ public abstract class RestMsoImplementation implements RestInterface { * Instantiates a new mso rest interface. */ + @Autowired + protected RestMsoImplementation(HttpsAuthClient httpsAuthClient){ + this.httpsAuthClient=httpsAuthClient; + } + @SuppressWarnings("Duplicates") protected MultivaluedHashMap initMsoClient() { @@ -139,7 +144,7 @@ public abstract class RestMsoImplementation implements RestInterface { MultivaluedHashMap commonHeaders = initMsoClient(); Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); - final Response cres = client.target(url) + final Response cres = client.target(url) .request() .accept(APPLICATION_JSON) .headers(commonHeaders) @@ -219,7 +224,8 @@ public abstract class RestMsoImplementation implements RestInterface { .accept(APPLICATION_JSON) .headers(commonHeaders) //.entity(r) - .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)).invoke(); + .build("DELETE", Entity.entity(r, MediaType.APPLICATION_JSON)) + .invoke(); Logging.logResponse(outgoingRequestsLogger, HttpMethod.DELETE, url, cres); int status = cres.getStatus(); restObject.setStatusCode (status); @@ -245,6 +251,7 @@ public abstract class RestMsoImplementation implements RestInterface { catch ( Exception e ) { logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + NO_RESPONSE_ENTITY_LOG + e.getMessage()); + throw e; } } @@ -252,7 +259,6 @@ public abstract class RestMsoImplementation implements RestInterface { { logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + WITH_URL_LOG +url+ EXCEPTION_LOG + e.toString()); throw e; - } } @@ -349,7 +355,6 @@ public abstract class RestMsoImplementation implements RestInterface { restObject.setStatusCode (status); return restObject; - } @Override @@ -383,6 +388,7 @@ public abstract class RestMsoImplementation implements RestInterface { catch ( Exception e ) { logger.debug(EELFLoggerDelegate.debugLogger,"<== " + methodName + NO_RESPONSE_ENTITY_LOG + e.getMessage()); + throw e; } int status = cres.getStatus(); diff --git a/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java b/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java index 76e61b35f..b47981ecf 100644 --- a/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java +++ b/vid-app-common/src/main/java/org/onap/vid/mso/rest/MsoRestClientNew.java @@ -26,6 +26,7 @@ import org.apache.commons.codec.binary.Base64; import org.eclipse.jetty.util.security.Password; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.aai.util.HttpsAuthClient; import org.onap.vid.changeManagement.MsoRequestDetails; import org.onap.vid.changeManagement.RequestDetailsWrapper; import org.onap.vid.client.SyncRestClient; @@ -61,7 +62,8 @@ public class MsoRestClientNew extends RestMsoImplementation implements MsoInterf */ EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(MsoRestClientNew.class); - public MsoRestClientNew(SyncRestClient client, String baseUrl) { + public MsoRestClientNew(SyncRestClient client, String baseUrl, HttpsAuthClient authClient) { + super(authClient); this.client = client; this.baseUrl = baseUrl; this.commonHeaders = initCommonHeaders(); diff --git a/vid-app-common/src/main/java/org/onap/vid/mso/rest/Task.java b/vid-app-common/src/main/java/org/onap/vid/mso/rest/Task.java index 23a5119e9..1956d8a95 100644 --- a/vid-app-common/src/main/java/org/onap/vid/mso/rest/Task.java +++ b/vid-app-common/src/main/java/org/onap/vid/mso/rest/Task.java @@ -133,7 +133,4 @@ public class Task { public void setValidResponses(List validResponses) { this.validResponses = validResponses; } - - - } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/RestMsoImplementationTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/RestMsoImplementationTest.java new file mode 100644 index 000000000..4cba53785 --- /dev/null +++ b/vid-app-common/src/test/java/org/onap/vid/mso/RestMsoImplementationTest.java @@ -0,0 +1,421 @@ +/*- + * ============LICENSE_START======================================================= + * VID + * ================================================================================ + * Copyright (C) 2019 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.mso; + +import io.joshworks.restclient.request.HttpRequest; +import org.glassfish.jersey.client.JerseyInvocation; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.aai.util.HttpsAuthClient; +import org.onap.vid.changeManagement.RequestDetailsWrapper; +import org.onap.vid.exceptions.GenericUncheckedException; +import org.onap.vid.mso.rest.RequestDetails; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.core.Response; + +import java.util.Optional; + +import static org.assertj.core.api.Java6Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + +public class RestMsoImplementationTest { + + @Mock + private HttpRequest httpRequest; + + @Mock + private Client mockClient; + + @Mock + private HttpsAuthClient mockHttpsAuthClient; + + @Mock + private WebTarget webTarget; + + @Mock + private javax.ws.rs.client.Invocation.Builder builder; + + @Mock + private Response response; + + @Mock + private JerseyInvocation jerseyInvocation; + + @InjectMocks + private RestMsoImplementation restMsoImplementation = new RestMsoImplementation(mockHttpsAuthClient); + + String path = "/test_path/"; + String rawData = "test-row-data"; + + @BeforeClass + public void setUp(){ + initMocks(this); + } + + @Test + public void shouldProperlyInitMsoClient() { + // when + MultivaluedHashMap result = restMsoImplementation.initMsoClient(); + + // then + assertThat(result).containsKeys("Authorization","X-ONAP-PartnerName"); + assertThat(result).doesNotContainKey("notExistingKey"); + } + + @Test + public void shouldProperlyGetRestObjectWithRequestInfo() { + // given + RestObject restObject = new RestObject<>(); + + prepareMocks(rawData, HttpStatus.ACCEPTED.value(),""); + + // when + RestObjectWithRequestInfo response = restMsoImplementation.Get(httpRequest, path, restObject,false); + + // then + assertThat(response.getRequestedUrl()).contains(path); + assertThat(response.getRawData()).isEqualTo(rawData); + assertThat(response.getHttpCode()).isEqualTo(HttpStatus.ACCEPTED.value()); + assertThat(response.getHttpMethod()).isEqualTo(HttpMethod.GET); + } + + @Test( expectedExceptions = GenericUncheckedException.class) + public void shouldThrowExceptionWhenGetRestObjectWithRequestInfoGetsWrongStatus() { + // given + RestObject restObject = new RestObject<>(); + + prepareMocks("",HttpStatus.BAD_REQUEST.value(),""); + + // when + restMsoImplementation.Get(httpRequest, "", restObject,false); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenGetRestObjectWithRequestInfoGetsWrongParameters() { + // given + RestObject restObject = new RestObject<>(); + + prepareMocks("",HttpStatus.ACCEPTED.value(),""); + when(mockClient.target(SystemProperties.getProperty(MsoProperties.MSO_SERVER_URL))).thenThrow(new MsoTestException("test-target-exception")); + + // when + restMsoImplementation.Get(httpRequest, "", restObject,false); + } + + @Test() + public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndAcceptCode() { + // given + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),""); + + // when + RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class); + + // then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED.value()); + assertThat(response.getRaw()).isEqualTo(rawData); + } + + @Test() + public void shouldProperlyGetRestObjectForObjectWithRequestInfoAndBadRequestCode() { + // given + prepareMocks(rawData,HttpStatus.BAD_REQUEST.value(),""); + + // when + RestObject response = restMsoImplementation.GetForObject(path, HttpRequest.class); + + // then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + assertThat(response.getRaw()).isEqualTo(rawData); + } + + @Test() + public void shouldProperlyDeleteRestObjectWithStatusHttpAccepted() { + // given + RestObject restObject = new RestObject<>(); + + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE"); + + // when + restMsoImplementation.Delete(httpRequest, "testObject", path, restObject); + + // then + assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED.value()); + } + + @Test() + public void shouldProperlyDeleteRestObjectWithStatusOK() { + // given + RestObject restObject = new RestObject<>(); + + prepareMocks(rawData,HttpStatus.OK.value(),"DELETE"); + + // when + restMsoImplementation.Delete(httpRequest, "testObject", path, restObject); + + // then + assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.OK.value()); + } + + @Test() + public void shouldProperlyReturnFromDeleteWithStatusBadRequest() { + // given + RestObject restObject = new RestObject<>(); + + prepareMocks(rawData,HttpStatus.BAD_REQUEST.value(),"DELETE"); + + // when + restMsoImplementation.Delete(httpRequest, "testObject", path, restObject); + + // then + assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.BAD_REQUEST.value()); + } + + @Test() + public void shouldProperlyReturnFromDeleteWithStatusOtherThenAbove() { + // given + RestObject restObject = new RestObject<>(); + prepareMocks(rawData,HttpStatus.NOT_EXTENDED.value(),"DELETE"); + + // when + restMsoImplementation.Delete(httpRequest, "testObject", path, restObject); + + // then + assertThat(restObject.getStatusCode()).isEqualTo(HttpStatus.NOT_EXTENDED.value()); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenCallsDeleteWithWrongParameters() { + // given + when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testDeleteException")); + + // when + restMsoImplementation.Delete(httpRequest, "testObject", "", null); + } + + @Test( expectedExceptions = NullPointerException.class) + public void shouldThrowExceptionWhenCallsDeleteWithWrongObjectType() { + // given + RestObject restObject = new RestObject<>(); + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE"); + + // when + restMsoImplementation.Delete(null, "testObject", path, restObject); + } + + @Test + public void shouldProperlyPostForObject() { + // given + RequestDetails requestDetails = new RequestDetails(); + + RestObject expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value()); + expectedResponse.setRaw(rawData); + + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"POST"); + + // when + RestObject response = restMsoImplementation.PostForObject(requestDetails, path, HttpRequest.class); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteForObject() { + // given + RequestDetails requestDetails = new RequestDetails(); + + RestObject expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value()); + expectedResponse.setRaw(rawData); + + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE"); + + // when + RestObject response = restMsoImplementation.DeleteForObject(requestDetails, path, HttpRequest.class); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyPost() { + // given + RequestDetails requestDetails = new RequestDetails(); + RestObject response = new RestObject<>(); + + RestObject expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value()); + expectedResponse.setRaw(rawData); + + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"POST"); + + // when + restMsoImplementation.Post(rawData,requestDetails, path, response); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyPrepareClient() { + // given + String method = "POST"; + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method); + + // when + javax.ws.rs.client.Invocation.Builder response = restMsoImplementation.prepareClient(path, method); + + // then + assertThat(response).isEqualTo(builder); + } + + @Test + public void shouldCreatRestObjectOnlyWithHttpMethod() { + // given + String method = "GET"; + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method); + + RestObject expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value()); + expectedResponse.setRaw(rawData); + + // when + RestObject response = restMsoImplementation.restCall(HttpMethod.GET, String.class, null, path, Optional.empty()); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenCreateRestObjectIsCalledWithoutDefinedClient() { + // given + when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testNoClientException")); + + // when + restMsoImplementation.restCall(HttpMethod.GET, String.class, null, "", Optional.empty()); + } + + @Test + public void shouldProperlyPutRestObjectWithProperParametersAndStatusAccepted() { + // given + String method = "PUT"; + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),method); + + org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); + RestObject response = new RestObject<>(); + + RestObject expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.ACCEPTED.value()); + expectedResponse.set(rawData); + + // when + restMsoImplementation.Put("testPutBody", requestDetailsWrapper , path, response); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyPutRestObjectWithProperParametersAndStatusMultipleChoices() { + // given + String method = "PUT"; + prepareMocks(rawData,HttpStatus.MULTIPLE_CHOICES.value(),method); + + org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); + RestObject response = new RestObject<>(); + + RestObject expectedResponse = new RestObject<>(); + expectedResponse.setStatusCode(HttpStatus.MULTIPLE_CHOICES.value()); + expectedResponse.set(rawData); + + // when + restMsoImplementation.Put("testPutBody", requestDetailsWrapper , path, response); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenCallsPutWithWrongParameters() { + // given + when(mockClient.target(any(String.class))).thenThrow(new MsoTestException("testDeleteException")); + org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); + + // when + restMsoImplementation.Put(null, requestDetailsWrapper, "", null); + } + + @Test( expectedExceptions = NullPointerException.class) + public void shouldThrowExceptionWhenCallsPutWithWrongObjectType() { + // given + RestObject restObject = new RestObject<>(); + org.onap.vid.changeManagement.RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper(); + + prepareMocks(rawData,HttpStatus.ACCEPTED.value(),"DELETE"); + + // when + restMsoImplementation.Put(null, requestDetailsWrapper, path, restObject); + } + + + + private void prepareMocks(String rawData,int status,String httpMethod) { + + when(mockClient.target(any(String.class))).thenReturn(webTarget); + when(webTarget.request()).thenReturn(builder); + + + when(builder.accept(any(String.class))).thenReturn(builder); + when(builder.headers(any(MultivaluedMap.class))).thenReturn(builder); + when(builder.get()).thenReturn(response); + + when(builder.build( eq(httpMethod), any(Entity.class))).thenReturn(jerseyInvocation); + when(builder.build( eq(httpMethod))).thenReturn(jerseyInvocation); + + when(builder.put( any(Entity.class))).thenReturn(response); + when(jerseyInvocation.invoke()).thenReturn(response); + + + when(response.getStatus()).thenReturn(status); + when(response.readEntity(String.class)).thenReturn(rawData); + } + + private class MsoTestException extends RuntimeException{ + MsoTestException(String testException) { + super(testException); + } + } + +} \ No newline at end of file diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java index 6114552e6..f58462dd6 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientNewTest.java @@ -468,10 +468,10 @@ public class MsoRestClientNewTest { private MsoRestClientNew msoRestClient() { final WebConfig webConfig = new WebConfig(); - return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl()); + return new MsoRestClientNew(new SyncRestClient(webConfig.unirestFasterxmlObjectMapper(new ObjectMapper())), baseUrl(),null); } private MsoRestClientNew createTestSubject() { - return new MsoRestClientNew(null, ""); + return new MsoRestClientNew(null, "",null); } } diff --git a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java index 5a0f93b45..39d633f70 100644 --- a/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/mso/rest/MsoRestClientTest.java @@ -20,65 +20,795 @@ */ package org.onap.vid.mso.rest; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.json.JSONObject; -import org.junit.Assert; +import io.joshworks.restclient.http.HttpResponse; +import io.joshworks.restclient.http.JsonMapper; +import org.apache.http.ProtocolVersion; +import org.apache.http.StatusLine; +import org.apache.http.message.BasicHttpResponse; +import org.apache.http.message.BasicStatusLine; +import org.mockito.Mock; import org.onap.portalsdk.core.util.SystemProperties; -import org.onap.vid.changeManagement.RequestDetails; +import org.onap.vid.changeManagement.RelatedInstanceList; +import org.onap.vid.changeManagement.RequestDetailsWrapper; import org.onap.vid.client.SyncRestClient; import org.onap.vid.controller.LocalWebConfig; -import org.onap.vid.mso.MsoBusinessLogic; -import org.onap.vid.mso.MsoBusinessLogicImpl; +import org.onap.vid.model.RequestReferencesContainer; +import org.onap.vid.mso.MsoResponseWrapper; +import org.onap.vid.mso.MsoResponseWrapperInterface; +import org.onap.vid.mso.MsoUtil; +import org.onap.vid.mso.RestObject; import org.onap.vid.mso.model.CloudConfiguration; import org.onap.vid.mso.model.ModelInfo; import org.onap.vid.mso.model.RequestInfo; import org.onap.vid.mso.model.RequestParameters; +import org.onap.vid.mso.model.RequestReferences; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.web.WebAppConfiguration; +import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; +import static org.mockito.ArgumentMatchers.any; + +import java.util.LinkedList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyMap; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.when; +import static org.mockito.MockitoAnnotations.initMocks; + @ContextConfiguration(classes = {LocalWebConfig.class, SystemProperties.class}) @WebAppConfiguration public class MsoRestClientTest { - private MsoBusinessLogic msoBusinessLogic = new MsoBusinessLogicImpl(new MsoRestClientNew(new SyncRestClient(), ""), null); - private ObjectMapper om = new ObjectMapper(); + private final String baseUrl = "http://testURL/"; + + @Mock + private SyncRestClient client; + + private MsoRestClientNew restClient; + + + @BeforeClass + private void setUp(){ + initMocks(this); + restClient = new MsoRestClientNew(client,baseUrl,null); + + } @Test - public void createInPlaceMsoRequest() { - String result = null; - try { - RequestDetails requestDetails = generateMockMsoRequest(); - result = om.writeValueAsString(msoBusinessLogic.generateInPlaceMsoRequest(requestDetails)); - } catch (Exception e) { - e.printStackTrace(); - } - if (result == null) { - Assert.fail("Failed to create mso request"); + public void shouldProperlyCreateServiceInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createSvcInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenCreateSvcInstanceGetsWrongParameters() { + // given + String endpoint = ""; + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(null),eq(String.class) ) ). + thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.createSvcInstance(null,endpoint); + } + + @Test + public void shouldProperlyCreateE2eSvcInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createE2eSvcInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyCreateVnf() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createVnf(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyCreateNwInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createNwInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyCreateVolumeGroupInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createVolumeGroupInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyCreateVfModuleInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createVfModuleInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyScaleOutVFModuleInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + RequestDetailsWrapper wrappedRequestDetails = new RequestDetailsWrapper<>(requestDetails); + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.scaleOutVFModuleInstance(wrappedRequestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyCreateConfigurationInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + org.onap.vid.mso.rest.RequestDetailsWrapper wrappedRequestDetails = new org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails); + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.createConfigurationInstance(wrappedRequestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteE2eSvcInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + org.onap.vid.mso.rest.RequestDetailsWrapper wrappedRequestDetails = new org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails); + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(wrappedRequestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteE2eSvcInstance(wrappedRequestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteSvcInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteSvcInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyUnassignSvcInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.unassignSvcInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteVnf() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteVnf(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteVfModule() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteVfModule(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteVolumeGroupInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteVolumeGroupInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyDeleteNwInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint),anyMap(),eq(requestDetails),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteNwInstance(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyGetOrchestrationRequest() { + // given + RestObject restObject = generateMockMsoRestObject(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.getOrchestrationRequest(null,null,endpoint,restObject,true); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyGetOrchestrationRequestWithOnlyEndpoint() { + // given + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.getOrchestrationRequest(endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyGetManualTasks() { + // given + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.get( eq(baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.getManualTasks(endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyGetManualTasksByRequestId() { + // given + RestObject restObject = generateMockMsoRestObject(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + /// WUT 'baseUrl+baseUrl+endpoint' + when( client.get( eq(baseUrl+baseUrl+endpoint),anyMap(),anyMap(),eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.getManualTasksByRequestId(null,null,endpoint,restObject); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test(expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenGetManualTasksByRequestIdGetsWrongParameter() { + // given + when( client.get( eq(baseUrl+baseUrl),anyMap(),anyMap(),eq(String.class) ) ).thenThrow(new MsoTestException("testsException")); + + // when + restClient.getManualTasksByRequestId(null,null,"",null); + } + + @Test + public void shouldProperlyCompleteManualTask() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + RestObject restObject = generateMockMsoRestObject(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint), anyMap(), eq(requestDetails), eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.completeManualTask(requestDetails,null,null,endpoint,restObject); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test(expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenCompleteManualTaskWrongParameter() { + // given + when( client.post( eq(baseUrl),anyMap(),eq(null), eq(String.class) ) ).thenThrow(new MsoTestException("testsException")); + + // when + restClient.completeManualTask(null, null,null,"",null); + } + + @Test + public void shouldProperlyReplaceVnf() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = generateChangeManagementMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test + public void shouldProperlyReplaceVnfWithStatus202() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = generateChangeManagementMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = createOkResponse(); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.post( eq(baseUrl+endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.replaceVnf(requestDetails,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenReplaceVnfGetsWrongParameters() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = generateChangeManagementMockMsoRequest(); + + when( client.post( eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class) ) ).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.replaceVnf(requestDetails, ""); + } + + @Test + public void shouldProperlyDeleteConfiguration() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = generateChangeManagementMockMsoRequest(); + org.onap.vid.mso.rest.RequestDetailsWrapper requestDetailsWrapper = new org.onap.vid.mso.rest.RequestDetailsWrapper(requestDetails); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when( client.delete( eq(baseUrl+endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class) ) ).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.deleteConfiguration(requestDetailsWrapper,endpoint); + + // then + assertThat(response).isEqualToComparingFieldByField(expectedResponse); + } + + @Test( expectedExceptions = MsoTestException.class ) + public void shouldThrowExceptionWhenProperlyDeleteConfigurationGetsWrongParameters() { + // given + when( client.delete( eq(baseUrl), anyMap(), eq(null), eq(String.class) ) ).thenThrow(new MsoTestException("test-delete-exception")); + + // when + restClient.deleteConfiguration(null,""); + } + + @Test + public void shouldProperlySetConfigurationActiveStatus() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.setConfigurationActiveStatus(requestDetails, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test(expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenSetConfigurationActiveStatusGetsWrongParameters() { + // given + + when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.setConfigurationActiveStatus(null, ""); + } + + @Test + public void shouldProperlySetPortOnConfigurationStatus() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.setPortOnConfigurationStatus(requestDetails, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test(expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenSetPortOnConfigurationStatusGetsWrongParameters() { + // given + String endpoint = ""; + + when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.setPortOnConfigurationStatus(null, endpoint); + } + + @Test + public void shouldProperlyChangeManagementUpdate() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper<>(requestDetails); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback( + new RequestReferencesContainer( + new RequestReferences())); + + MsoResponseWrapperInterface expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(RequestReferencesContainer.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapperInterface response = restClient.changeManagementUpdate(requestDetailsWrapper, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test + public void shouldProperlyUpdateVnfAndUpdateInstance() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = generateChangeManagementMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapperInterface expectedResponse = MsoUtil.wrapResponse(httpResponse); + + + when(client.put(eq(baseUrl + endpoint), anyMap(), any(RequestDetailsWrapper.class), eq(String.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapperInterface response = restClient.updateVnf(requestDetails, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test( expectedExceptions = MsoTestException.class ) + public void shouldThrowExceptionWhenUpdateVnfAndUpdateInstanceGetsWrongParameter() { + // given + org.onap.vid.changeManagement.RequestDetails requestDetails = generateChangeManagementMockMsoRequest(); + String endpoint = ""; + + when(client.put(eq(baseUrl), anyMap(), any(RequestDetailsWrapper.class), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.updateVnf(requestDetails, endpoint); + + // then + } + + @Test + public void shouldProperlySetServiceInstanceStatus() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + RestObject restObject = generateMockMsoRestObject(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); + + // when + restClient.setServiceInstanceStatus(requestDetails,"", "", endpoint, restObject); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenSetServiceInstanceStatusGetsWrongParameter() { + // given + String endpoint = ""; + + when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.setServiceInstanceStatus(null,"", "", endpoint, null); + } + + @Test + public void shouldProperlyRemoveRelationshipFromServiceInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.removeRelationshipFromServiceInstance(requestDetails, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenRemoveRelationshipFromServiceInstanceGetsWrongParameter() { + // given + String endpoint = ""; + + when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.removeRelationshipFromServiceInstance(null,endpoint); + } + + @Test + public void shouldProperlyAddRelationshipToServiceInstance() { + // given + RequestDetails requestDetails = generateMockMsoRequest(); + + String endpoint = "testEndpoint"; + HttpResponse httpResponse = HttpResponse.fallback("testOkResponse"); + MsoResponseWrapper expectedResponse = MsoUtil.wrapResponse(httpResponse); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetails), eq(String.class))).thenReturn(httpResponse); + + // when + MsoResponseWrapper response = restClient.addRelationshipToServiceInstance(requestDetails, endpoint); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test( expectedExceptions = MsoTestException.class) + public void shouldThrowExceptionWhenAddRelationshipToServiceInstanceGetsWrongParameter() { + // given + String endpoint = ""; + + when(client.post(eq(baseUrl), anyMap(), eq(null), eq(String.class))).thenThrow(new MsoTestException("test-post-exception")); + + // when + restClient.addRelationshipToServiceInstance(null,endpoint); + } + + @Test + public void shouldProperlyPerformGetRequest() { + // given + String endpoint = "testEndpoint"; + HttpResponse expectedResponse = HttpResponse.fallback("testOkResponse"); + + when(client.get(eq(baseUrl + endpoint), anyMap(), anyMap(), eq(String.class))).thenReturn(expectedResponse); + + // when + HttpResponse response = restClient.get(endpoint, String.class); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + @Test + public void shouldProperlyPerformPostRequest() { + // given + + RequestDetailsWrapper requestDetailsWrapper = new RequestDetailsWrapper<>(generateMockMsoRequest()); + + String endpoint = "testEndpoint"; + HttpResponse expectedResponse = HttpResponse.fallback("testOkResponse"); + + when(client.post(eq(baseUrl + endpoint), anyMap(), eq(requestDetailsWrapper), eq(String.class))).thenReturn(expectedResponse); + + // when + HttpResponse response = restClient.post(endpoint,requestDetailsWrapper, String.class); + + // then + assertThat(expectedResponse).isEqualToComparingFieldByField(response); + } + + private class MsoTestException extends RuntimeException{ + MsoTestException(String testException) { + super(testException); } - JSONObject jsonObj = new JSONObject(result); - Assert.assertNotNull(jsonObj.getJSONObject("requestDetails")); + } + + private HttpResponse createOkResponse() { + StatusLine statusline = new BasicStatusLine( + new ProtocolVersion("http",1,1), 202, "acceptResponse"); + + org.apache.http.HttpResponse responseBase = new BasicHttpResponse(statusline); + + return new HttpResponse<>(responseBase ,String.class, new JsonMapper()); } private RequestDetails generateMockMsoRequest() { RequestDetails requestDetails = new RequestDetails(); - requestDetails.setVnfInstanceId("vnf-instance-id"); - requestDetails.setVnfName("vnf-name"); + CloudConfiguration cloudConfiguration = new CloudConfiguration(); cloudConfiguration.setTenantId("tenant-id"); cloudConfiguration.setLcpCloudRegionId("lcp-region"); requestDetails.setCloudConfiguration(cloudConfiguration); + ModelInfo modelInfo = new ModelInfo(); modelInfo.setModelInvariantId("model-invarient-id"); modelInfo.setModelCustomizationName("modelCustomizationName"); + modelInfo.setModelType("test-model-type"); requestDetails.setModelInfo(modelInfo); + RequestInfo requestInfo = new RequestInfo(); requestInfo.setRequestorId("ok883e"); requestInfo.setSource("VID"); requestDetails.setRequestInfo(requestInfo); RequestParameters requestParameters = new RequestParameters(); + requestParameters.setSubscriptionServiceType("subscriber-service-type"); requestParameters.setAdditionalProperty("a", 1); requestParameters.setAdditionalProperty("b", 2); @@ -90,4 +820,52 @@ public class MsoRestClientTest { requestDetails.setRequestParameters(requestParameters); return requestDetails; } + + private org.onap.vid.changeManagement.RequestDetails generateChangeManagementMockMsoRequest() { + List relatedInstances = new LinkedList<>(); + relatedInstances.add(new RelatedInstanceList()); + + org.onap.vid.changeManagement.RequestDetails requestDetails = new org.onap.vid.changeManagement.RequestDetails(); + + requestDetails.setVnfName("test-vnf-name"); + requestDetails.setVnfInstanceId("test-vnf-instance_id"); + requestDetails.setRelatedInstList(relatedInstances); + + CloudConfiguration cloudConfiguration = new CloudConfiguration(); + cloudConfiguration.setTenantId("tenant-id"); + cloudConfiguration.setLcpCloudRegionId("lcp-region"); + requestDetails.setCloudConfiguration(cloudConfiguration); + + ModelInfo modelInfo = new ModelInfo(); + modelInfo.setModelInvariantId("model-invarient-id"); + modelInfo.setModelCustomizationName("modelCustomizationName"); + modelInfo.setModelType("test-model-type"); + requestDetails.setModelInfo(modelInfo); + + RequestInfo requestInfo = new RequestInfo(); + requestInfo.setRequestorId("ok883e"); + requestInfo.setSource("VID"); + requestDetails.setRequestInfo(requestInfo); + + RequestParameters requestParameters = new RequestParameters(); + requestParameters.setSubscriptionServiceType("subscriber-service-type"); + requestParameters.setAdditionalProperty("a", 1); + requestParameters.setAdditionalProperty("b", 2); + requestParameters.setAdditionalProperty("c", 3); + requestParameters.setAdditionalProperty("d", 4); + String payload = "{\"existing_software_version\": \"3.1\",\"new_software_version\": \"3.2\", \"operations_timeout\": \"3600\"}"; + requestParameters.setAdditionalProperty("payload", payload); + + requestDetails.setRequestParameters(requestParameters); + return requestDetails; + } + + private RestObject generateMockMsoRestObject() { + RestObject restObject = new RestObject<>(); + + restObject.set("test-rest-object-body"); + restObject.setRaw("test-rest-object-raw-string"); + restObject.setStatusCode(202); + return restObject; + } } diff --git a/vid-app-common/src/test/java/org/onap/vid/services/ChangeManagementServiceUnitTest.java b/vid-app-common/src/test/java/org/onap/vid/services/ChangeManagementServiceUnitTest.java index b66899c31..e638605c1 100644 --- a/vid-app-common/src/test/java/org/onap/vid/services/ChangeManagementServiceUnitTest.java +++ b/vid-app-common/src/test/java/org/onap/vid/services/ChangeManagementServiceUnitTest.java @@ -26,6 +26,7 @@ import org.apache.commons.io.IOUtils; import org.mockito.ArgumentCaptor; import org.onap.portalsdk.core.service.DataAccessService; import org.onap.portalsdk.core.util.SystemProperties; +import org.onap.vid.aai.util.HttpsAuthClient; import org.onap.vid.changeManagement.ChangeManagementRequest; import org.onap.vid.changeManagement.RequestDetailsWrapper; import org.onap.vid.client.SyncRestClient; @@ -116,11 +117,6 @@ public class ChangeManagementServiceUnitTest extends AbstractTestNGSpringContext @Configuration public static class TestMsoConfig extends MsoConfig { - public MsoRestClientNew getMsoClient() { - MsoRestClientNew spyClient = spy(new MsoRestClientNew(new SyncRestClient(), "")); - return spyClient; - } - @Bean public ChangeManagementService getChangeManagementService(DataAccessService dataAccessService, MsoBusinessLogic msoInterface, SchedulerRestInterfaceIfc schedulerRestInterface, CloudOwnerService cloudOwnerService) { return new ChangeManagementServiceImpl(dataAccessService, msoInterface, schedulerRestInterface, cloudOwnerService); -- cgit 1.2.3-korg