2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.so.bpmn.infrastructure.adapter.network.tasks;
23 import java.io.IOException;
24 import java.util.HashMap;
26 import javax.xml.bind.JAXBException;
27 import org.camunda.bpm.engine.delegate.BpmnError;
28 import org.camunda.bpm.engine.delegate.DelegateExecution;
29 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.onap.so.adapters.nwrest.CreateNetworkResponse;
35 import org.onap.so.adapters.nwrest.UpdateNetworkError;
36 import org.onap.so.adapters.nwrest.UpdateNetworkRequest;
37 import org.onap.so.adapters.nwrest.UpdateNetworkResponse;
38 import org.onap.so.bpmn.BaseTaskTest;
39 import org.onap.so.client.exception.BadResponseException;
40 import org.onap.so.client.exception.ExceptionBuilder;
41 import org.onap.so.client.exception.MapperException;
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.ArgumentMatchers.anyInt;
44 import static org.mockito.ArgumentMatchers.eq;
45 import static org.junit.Assert.assertEquals;
46 import static org.junit.Assert.assertNull;
47 import static org.junit.Assert.assertThat;
48 import static org.mockito.Mockito.doThrow;
49 import static org.mockito.Mockito.times;
50 import static org.mockito.Mockito.verify;
51 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
53 public class NetworkAdapterRestV1Test extends BaseTaskTest {
56 NetworkAdapterRestV1 networkAdapterRestV1Tasks = new NetworkAdapterRestV1();
58 ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
62 delegateExecution = new DelegateExecutionFake();
66 public void testUnmarshalXml() throws IOException, JAXBException {
68 "<?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>";
69 CreateNetworkResponse response =
70 (CreateNetworkResponse) new NetworkAdapterRestV1().unmarshalXml(xml, CreateNetworkResponse.class);
71 String returnedXml = response.toXmlString();
72 System.out.println(returnedXml);
76 public void testUnmarshalXmlUpdate() throws IOException, JAXBException {
77 UpdateNetworkResponse expectedResponse = new UpdateNetworkResponse();
78 expectedResponse.setMessageId("ec100bcc-2659-4aa4-b4d8-3255715c2a51");
79 expectedResponse.setNetworkId("80de31e3-cc78-4111-a9d3-5b92bf0a39eb");
80 Map<String, String> subnetMap = new HashMap<String, String>();
81 subnetMap.put("95cd8437-25f1-4238-8720-cbfe7fa81476", "d8d16606-5d01-4822-b160-9a0d257303e0");
82 expectedResponse.setSubnetMap(subnetMap);
84 "<?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>";
85 UpdateNetworkResponse response =
86 (UpdateNetworkResponse) new NetworkAdapterRestV1().unmarshalXml(xml, UpdateNetworkResponse.class);
87 assertThat(expectedResponse, sameBeanAs(response));
91 public void processCallbackTest() throws MapperException, BadResponseException, IOException {
92 UpdateNetworkRequest updateNetworkRequest = new UpdateNetworkRequest();
93 UpdateNetworkResponse updateNetworkResponse = new UpdateNetworkResponse();
94 updateNetworkResponse.setMessageId("messageId");
95 updateNetworkResponse.setNetworkId("networkId");
96 delegateExecution.setVariable("networkAdapterRequest", updateNetworkRequest);
97 delegateExecution.setVariable("NetworkAResponse_MESSAGE", updateNetworkResponse.toXmlString());
99 networkAdapterRestV1Tasks.processCallback(delegateExecution);
101 assertThat(updateNetworkResponse, sameBeanAs(delegateExecution.getVariable("updateNetworkResponse")));
105 public void processCallbackErrorTest() throws MapperException, BadResponseException, IOException {
106 UpdateNetworkRequest updateNetworkRequest = new UpdateNetworkRequest();
107 UpdateNetworkError updateNetworkResponse = new UpdateNetworkError();
108 updateNetworkResponse.setMessageId("messageId");
109 updateNetworkResponse.setMessage("test error message");
110 delegateExecution.setVariable("networkAdapterRequest", updateNetworkRequest);
111 delegateExecution.setVariable("NetworkAResponse_MESSAGE", updateNetworkResponse.toXmlString());
113 doThrow(new BpmnError("MSOWorkflowException")).when(exceptionBuilder)
114 .buildAndThrowWorkflowException(any(DelegateExecution.class), anyInt(), any(String.class));
116 networkAdapterRestV1Tasks.processCallback(delegateExecution);
117 } catch (BpmnError be) {
118 assertEquals("MSOWorkflowException", be.getErrorCode());
120 assertNull(delegateExecution.getVariable("updateNetworkResponse"));
121 verify(exceptionBuilder, times(1)).buildAndThrowWorkflowException(any(DelegateExecution.class), eq(7000),
122 eq("test error message"));