2  * ============LICENSE_START=======================================================
 
   4  * ================================================================================
 
   5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 
   6  * ================================================================================
 
   7  * Copyright (C) 2018 Nokia.
 
   8  * ================================================================================
 
   9  * Licensed under the Apache License, Version 2.0 (the "License");
 
  10  * you may not use this file except in compliance with the License.
 
  11  * You may obtain a copy of the License at
 
  13  *      http://www.apache.org/licenses/LICENSE-2.0
 
  15  * Unless required by applicable law or agreed to in writing, software
 
  16  * distributed under the License is distributed on an "AS IS" BASIS,
 
  17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
  18  * See the License for the specific language governing permissions and
 
  19  * limitations under the License.
 
  20  * ============LICENSE_END=========================================================
 
  23 package org.onap.so.bpmn.infrastructure.adapter.network.tasks;
 
  25 import java.util.HashMap;
 
  27 import java.util.Optional;
 
  28 import javax.ws.rs.core.Response;
 
  29 import javax.ws.rs.core.Response.Status;
 
  30 import javax.xml.bind.JAXBException;
 
  31 import org.camunda.bpm.engine.delegate.BpmnError;
 
  32 import org.camunda.bpm.engine.delegate.DelegateExecution;
 
  33 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
 
  34 import org.glassfish.jersey.message.internal.OutboundJaxrsResponse;
 
  35 import org.glassfish.jersey.message.internal.OutboundMessageContext;
 
  36 import org.junit.Before;
 
  37 import org.junit.Test;
 
  38 import org.mockito.InjectMocks;
 
  39 import org.mockito.Mock;
 
  40 import org.onap.so.adapters.nwrest.CreateNetworkRequest;
 
  41 import org.onap.so.adapters.nwrest.CreateNetworkResponse;
 
  42 import org.onap.so.adapters.nwrest.DeleteNetworkRequest;
 
  43 import org.onap.so.adapters.nwrest.DeleteNetworkResponse;
 
  44 import org.onap.so.adapters.nwrest.NetworkRequestCommon;
 
  45 import org.onap.so.adapters.nwrest.UpdateNetworkError;
 
  46 import org.onap.so.adapters.nwrest.UpdateNetworkRequest;
 
  47 import org.onap.so.adapters.nwrest.UpdateNetworkResponse;
 
  48 import org.onap.so.client.exception.ExceptionBuilder;
 
  49 import org.onap.so.client.orchestration.NetworkAdapterResources;
 
  50 import org.onap.so.utils.Components;
 
  51 import org.onap.logging.filter.base.ONAPComponents;
 
  52 import static org.junit.Assert.assertTrue;
 
  53 import static org.mockito.ArgumentMatchers.any;
 
  54 import static org.mockito.ArgumentMatchers.anyInt;
 
  55 import static org.mockito.ArgumentMatchers.eq;
 
  56 import static org.junit.Assert.assertEquals;
 
  57 import static org.junit.Assert.assertNull;
 
  58 import static org.junit.Assert.assertThat;
 
  59 import static org.mockito.Mockito.doThrow;
 
  60 import static org.mockito.Mockito.times;
 
  61 import static org.mockito.Mockito.verify;
 
  62 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
 
  63 import static org.mockito.Mockito.when;
 
  64 import static org.mockito.MockitoAnnotations.initMocks;
 
  66 public class NetworkAdapterRestV1Test {
 
  69     private ExceptionBuilder exceptionBuilder;
 
  71     private NetworkAdapterResources networkAdapterResources;
 
  73     private NetworkAdapterRestV1 networkAdapterRestV1Tasks;
 
  75     private DelegateExecution delegateExecution;
 
  77     private static final String CREATE_NETWORK_RESPONSE = "createNetworkResponse";
 
  78     private static final String DELETE_NETWORK_RESPONSE = "deleteNetworkResponse";
 
  79     private static final String CREATE_NETWORK_ERROR = "createNetworkError";
 
  80     private static final String DELETE_NETWORK_ERROR = "deleteNetworkError";
 
  81     private static final String NET_ID_FOR_CREATE_NETWORK_RESPONSE = "netIdForCreateNetworkResponse";
 
  82     private static final String NET_ID_FOR_DELETE_NETWORK_RESPONSE = "netIdForDeleteNetworkResponse";
 
  83     private static final String CREATE_NETWORK_ERROR_MESSAGE = "createNetErrorMessage";
 
  84     private static final String DELETE_NETWORK_ERROR_MESSAGE = "deleteNetErrorMessage";
 
  89         delegateExecution = new DelegateExecutionFake();
 
  93     public void testUnmarshalXml() throws JAXBException {
 
  95                 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><createNetworkResponse><messageId>ec37c121-e3ec-4697-8adf-2d7dca7044fc</messageId><networkCreated>true</networkCreated><networkFqdn>someNetworkFqdn</networkFqdn><networkId>991ec7bf-c9c4-4ac1-bb9c-4b61645bddb3</networkId><networkStackId>someStackId</networkStackId><neutronNetworkId>9c47521a-2916-4018-b2bc-71ab767497e3</neutronNetworkId><rollback><cloudId>someCloudId</cloudId><modelCustomizationUuid>b7171cdd-8b05-459b-80ef-2093150e8983</modelCustomizationUuid><msoRequest><requestId>90b32315-176e-4dab-bcf1-80eb97a1c4f4</requestId><serviceInstanceId>71e7db22-7907-4d78-8fcc-8d89d28e90be</serviceInstanceId></msoRequest><networkCreated>true</networkCreated><networkStackId>someStackId</networkStackId><networkType>SomeNetworkType</networkType><neutronNetworkId>9c47521a-2916-4018-b2bc-71ab767497e3</neutronNetworkId><tenantId>b60da4f71c1d4b35b8113d4eca6deaa1</tenantId></rollback><subnetMap><entry><key>6b381fa9-48ce-4e16-9978-d75309565bb6</key><value>bc1d5537-860b-4894-8eba-6faff41e648c</value></entry></subnetMap></createNetworkResponse>";
 
  96         CreateNetworkResponse response =
 
  97                 (CreateNetworkResponse) new NetworkAdapterRestV1().unmarshalXml(xml, CreateNetworkResponse.class);
 
  98         String returnedXml = response.toXmlString();
 
  99         System.out.println(returnedXml);
 
 103     public void testUnmarshalXmlUpdate() throws JAXBException {
 
 104         UpdateNetworkResponse expectedResponse = new UpdateNetworkResponse();
 
 105         expectedResponse.setMessageId("ec100bcc-2659-4aa4-b4d8-3255715c2a51");
 
 106         expectedResponse.setNetworkId("80de31e3-cc78-4111-a9d3-5b92bf0a39eb");
 
 107         Map<String, String> subnetMap = new HashMap<>();
 
 108         subnetMap.put("95cd8437-25f1-4238-8720-cbfe7fa81476", "d8d16606-5d01-4822-b160-9a0d257303e0");
 
 109         expectedResponse.setSubnetMap(subnetMap);
 
 111                 "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><updateNetworkResponse><messageId>ec100bcc-2659-4aa4-b4d8-3255715c2a51</messageId><networkId>80de31e3-cc78-4111-a9d3-5b92bf0a39eb</networkId><subnetMap><entry><key>95cd8437-25f1-4238-8720-cbfe7fa81476</key><value>d8d16606-5d01-4822-b160-9a0d257303e0</value></entry></subnetMap></updateNetworkResponse>";
 
 112         UpdateNetworkResponse response =
 
 113                 (UpdateNetworkResponse) new NetworkAdapterRestV1().unmarshalXml(xml, UpdateNetworkResponse.class);
 
 114         assertThat(expectedResponse, sameBeanAs(response));
 
 118     public void processCallbackTest() {
 
 119         UpdateNetworkRequest updateNetworkRequest = new UpdateNetworkRequest();
 
 120         UpdateNetworkResponse updateNetworkResponse = new UpdateNetworkResponse();
 
 121         updateNetworkResponse.setMessageId("messageId");
 
 122         updateNetworkResponse.setNetworkId("networkId");
 
 123         delegateExecution.setVariable("networkAdapterRequest", updateNetworkRequest);
 
 124         delegateExecution.setVariable("NetworkAResponse_MESSAGE", updateNetworkResponse.toXmlString());
 
 126         networkAdapterRestV1Tasks.processCallback(delegateExecution);
 
 128         assertThat(updateNetworkResponse, sameBeanAs(delegateExecution.getVariable("updateNetworkResponse")));
 
 132     public void processCallbackErrorTest() {
 
 133         UpdateNetworkRequest updateNetworkRequest = new UpdateNetworkRequest();
 
 134         UpdateNetworkError updateNetworkResponse = new UpdateNetworkError();
 
 135         updateNetworkResponse.setMessageId("messageId");
 
 136         updateNetworkResponse.setMessage("test error message");
 
 137         delegateExecution.setVariable("networkAdapterRequest", updateNetworkRequest);
 
 138         delegateExecution.setVariable("NetworkAResponse_MESSAGE", updateNetworkResponse.toXmlString());
 
 140         doThrow(new BpmnError("MSOWorkflowException")).when(exceptionBuilder).buildAndThrowWorkflowException(
 
 141                 any(DelegateExecution.class), anyInt(), any(String.class), any(ONAPComponents.class));
 
 144             networkAdapterRestV1Tasks.processCallback(delegateExecution);
 
 145         } catch (BpmnError be) {
 
 146             assertEquals("MSOWorkflowException", be.getErrorCode());
 
 148         assertNull(delegateExecution.getVariable("updateNetworkResponse"));
 
 149         verify(exceptionBuilder, times(1)).buildAndThrowWorkflowException(any(DelegateExecution.class), eq(7000),
 
 150                 eq("test error message"), eq(Components.OPENSTACK));
 
 154     public void processCallback_createNetworkResponse() {
 
 155         delegateExecution.setVariable("networkAdapterRequest", new CreateNetworkRequest());
 
 156         delegateExecution.setVariable("NetworkAResponse_MESSAGE",
 
 157                 createNetworkResponse(CREATE_NETWORK_RESPONSE, NET_ID_FOR_CREATE_NETWORK_RESPONSE));
 
 158         networkAdapterRestV1Tasks.processCallback(delegateExecution);
 
 160         Object result = delegateExecution.getVariable("createNetworkResponse");
 
 161         assertTrue(result instanceof CreateNetworkResponse);
 
 162         CreateNetworkResponse createNetworkResponse = (CreateNetworkResponse) result;
 
 163         assertEquals(createNetworkResponse.getNetworkId(), NET_ID_FOR_CREATE_NETWORK_RESPONSE);
 
 167     public void processCallback_deleteNetworkResponse() {
 
 168         delegateExecution.setVariable("networkAdapterRequest", new DeleteNetworkRequest());
 
 169         delegateExecution.setVariable("NetworkAResponse_MESSAGE",
 
 170                 createNetworkResponse(DELETE_NETWORK_RESPONSE, NET_ID_FOR_DELETE_NETWORK_RESPONSE));
 
 171         networkAdapterRestV1Tasks.processCallback(delegateExecution);
 
 173         Object result = delegateExecution.getVariable("deleteNetworkResponse");
 
 174         assertTrue(result instanceof DeleteNetworkResponse);
 
 175         DeleteNetworkResponse deleteNetworkResponse = (DeleteNetworkResponse) result;
 
 176         assertEquals(deleteNetworkResponse.getNetworkId(), NET_ID_FOR_DELETE_NETWORK_RESPONSE);
 
 180     public void processCallback_createNetworkError() {
 
 182             delegateExecution.setVariable("networkAdapterRequest", new CreateNetworkRequest());
 
 183             delegateExecution.setVariable("NetworkAResponse_MESSAGE",
 
 184                     createNetworkError(CREATE_NETWORK_ERROR, CREATE_NETWORK_ERROR_MESSAGE));
 
 185             networkAdapterRestV1Tasks.processCallback(delegateExecution);
 
 186         } catch (Exception e) {
 
 187             assertEquals(e.getMessage(), CREATE_NETWORK_ERROR_MESSAGE);
 
 192     public void processCallback_deleteNetworkError() {
 
 194             delegateExecution.setVariable("networkAdapterRequest", new DeleteNetworkRequest());
 
 195             delegateExecution.setVariable("NetworkAResponse_MESSAGE",
 
 196                     createNetworkError(DELETE_NETWORK_ERROR, DELETE_NETWORK_ERROR_MESSAGE));
 
 197             networkAdapterRestV1Tasks.processCallback(delegateExecution);
 
 198         } catch (Exception e) {
 
 199             assertEquals(e.getMessage(), DELETE_NETWORK_ERROR_MESSAGE);
 
 204     public void callNetworkAdapter_CreateNetworkRequestSuccess() throws Exception {
 
 206         String messageId = "createNetReqMessageId";
 
 207         CreateNetworkRequest createNetworkRequest = new CreateNetworkRequest();
 
 208         createNetworkRequest.setMessageId(messageId);
 
 209         delegateExecution.setVariable("networkAdapterRequest", createNetworkRequest);
 
 210         Status status = Status.OK;
 
 211         String responseEntity = "createNetworkResponse";
 
 212         Optional<Response> response = Optional.of(createResponse(status, responseEntity));
 
 213         when(networkAdapterResources.createNetworkAsync(createNetworkRequest)).thenReturn(response);
 
 215         networkAdapterRestV1Tasks.callNetworkAdapter(delegateExecution);
 
 217         verifyExecutionContent(status, responseEntity, messageId);
 
 221     public void callNetworkAdapter_DeleteNetworkRequestSuccess() throws Exception {
 
 223         String messageId = "DeleteNetReqMessageId";
 
 224         DeleteNetworkRequest deleteNetworkRequest = new DeleteNetworkRequest();
 
 225         deleteNetworkRequest.setMessageId(messageId);
 
 226         delegateExecution.setVariable("networkAdapterRequest", deleteNetworkRequest);
 
 227         Status status = Status.OK;
 
 228         String responseEntity = "deleteNetworkResponse";
 
 229         Optional<Response> response = Optional.of(createResponse(status, responseEntity));
 
 230         when(networkAdapterResources.deleteNetworkAsync(deleteNetworkRequest)).thenReturn(response);
 
 232         networkAdapterRestV1Tasks.callNetworkAdapter(delegateExecution);
 
 234         verifyExecutionContent(status, responseEntity, messageId);
 
 238     public void callNetworkAdapter_UpdateNetworkRequestSuccess() throws Exception {
 
 240         String messageId = "UpdateNetReqMessageId";
 
 241         UpdateNetworkRequest updateNetworkRequest = new UpdateNetworkRequest();
 
 242         updateNetworkRequest.setMessageId(messageId);
 
 243         delegateExecution.setVariable("networkAdapterRequest", updateNetworkRequest);
 
 244         Status status = Status.OK;
 
 245         String responseEntity = "updateNetworkResponse";
 
 246         Optional<Response> response = Optional.of(createResponse(status, responseEntity));
 
 247         when(networkAdapterResources.updateNetworkAsync(updateNetworkRequest)).thenReturn(response);
 
 249         networkAdapterRestV1Tasks.callNetworkAdapter(delegateExecution);
 
 251         verifyExecutionContent(status, responseEntity, messageId);
 
 255     public void callNetworkAdapterError_networkAdapterRequestIsNull() {
 
 257         networkAdapterRestV1Tasks.callNetworkAdapter(delegateExecution);
 
 259         verify(exceptionBuilder, times(1)).buildAndThrowWorkflowException(any(DelegateExecution.class), eq(7000),
 
 260                 any(Exception.class), eq(ONAPComponents.SO));
 
 264     public void callNetworkAdapterError_noResponse() throws Exception {
 
 266         String messageId = "UpdateNetReqMessageId";
 
 267         UpdateNetworkRequest updateNetworkRequest = new UpdateNetworkRequest();
 
 268         updateNetworkRequest.setMessageId(messageId);
 
 269         delegateExecution.setVariable("networkAdapterRequest", updateNetworkRequest);
 
 270         when(networkAdapterResources.updateNetworkAsync(updateNetworkRequest)).thenReturn(Optional.empty());
 
 272         networkAdapterRestV1Tasks.callNetworkAdapter(delegateExecution);
 
 274         verify(exceptionBuilder, times(1)).buildAndThrowWorkflowException(any(DelegateExecution.class), eq(7000),
 
 275                 any(Exception.class), eq(ONAPComponents.SO));
 
 278     private void verifyExecutionContent(Status status, String responseEntity, String messageId) {
 
 279         assertEquals(delegateExecution.getVariable("NETWORKREST_networkAdapterStatusCode"),
 
 280                 Integer.toString(status.getStatusCode()));
 
 281         assertEquals(delegateExecution.getVariable("NETWORKREST_networkAdapterResponse"), responseEntity);
 
 282         assertEquals(delegateExecution.getVariable("NetworkAResponse_CORRELATOR"), messageId);
 
 285     private Response createResponse(Status status, String responseEntity) {
 
 286         OutboundMessageContext outboundMessageContext = new OutboundMessageContext();
 
 287         outboundMessageContext.setEntity(responseEntity);
 
 288         return new OutboundJaxrsResponse(status, outboundMessageContext);
 
 291     private String createNetworkResponse(String networkResponseType, String networkId) {
 
 293         return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><" + networkResponseType + "><networkId>"
 
 294                 + networkId + "</networkId></" + networkResponseType + ">";
 
 297     private String createNetworkError(String networkErrorType, String message) {
 
 299         return "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><" + networkErrorType + "><message>"
 
 300                 + message + "</message></" + networkErrorType + ">";