Merge "Enhanced List Level flow with backward support"
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / network / tasks / NetworkAdapterRestV1Test.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.onap.so.bpmn.infrastructure.adapter.network.tasks;
22
23 import java.io.IOException;
24 import java.util.HashMap;
25 import java.util.Map;
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 org.onap.so.utils.TargetEntity;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.ArgumentMatchers.anyInt;
45 import static org.mockito.ArgumentMatchers.eq;
46 import static org.junit.Assert.assertEquals;
47 import static org.junit.Assert.assertNull;
48 import static org.junit.Assert.assertThat;
49 import static org.mockito.Mockito.doNothing;
50 import static org.mockito.Mockito.doThrow;
51 import static org.mockito.Mockito.times;
52 import static org.mockito.Mockito.verify;
53 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
54
55 public class NetworkAdapterRestV1Test extends BaseTaskTest {
56
57     @InjectMocks
58     NetworkAdapterRestV1 networkAdapterRestV1Tasks = new NetworkAdapterRestV1();
59     @Mock
60     ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
61
62     @Before
63     public void setup() {
64         delegateExecution = new DelegateExecutionFake();
65     }
66
67     @Test
68     public void testUnmarshalXml() throws IOException, JAXBException {
69         String xml =
70                 "<?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>";
71         CreateNetworkResponse response =
72                 (CreateNetworkResponse) new NetworkAdapterRestV1().unmarshalXml(xml, CreateNetworkResponse.class);
73         String returnedXml = response.toXmlString();
74         System.out.println(returnedXml);
75     }
76
77     @Test
78     public void testUnmarshalXmlUpdate() throws IOException, JAXBException {
79         UpdateNetworkResponse expectedResponse = new UpdateNetworkResponse();
80         expectedResponse.setMessageId("ec100bcc-2659-4aa4-b4d8-3255715c2a51");
81         expectedResponse.setNetworkId("80de31e3-cc78-4111-a9d3-5b92bf0a39eb");
82         Map<String, String> subnetMap = new HashMap<String, String>();
83         subnetMap.put("95cd8437-25f1-4238-8720-cbfe7fa81476", "d8d16606-5d01-4822-b160-9a0d257303e0");
84         expectedResponse.setSubnetMap(subnetMap);
85         String xml =
86                 "<?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>";
87         UpdateNetworkResponse response =
88                 (UpdateNetworkResponse) new NetworkAdapterRestV1().unmarshalXml(xml, UpdateNetworkResponse.class);
89         assertThat(expectedResponse, sameBeanAs(response));
90     }
91
92     @Test
93     public void processCallbackTest() throws MapperException, BadResponseException, IOException {
94         UpdateNetworkRequest updateNetworkRequest = new UpdateNetworkRequest();
95         UpdateNetworkResponse updateNetworkResponse = new UpdateNetworkResponse();
96         updateNetworkResponse.setMessageId("messageId");
97         updateNetworkResponse.setNetworkId("networkId");
98         delegateExecution.setVariable("networkAdapterRequest", updateNetworkRequest);
99         delegateExecution.setVariable("NetworkAResponse_MESSAGE", updateNetworkResponse.toXmlString());
100
101         networkAdapterRestV1Tasks.processCallback(delegateExecution);
102
103         assertThat(updateNetworkResponse, sameBeanAs(delegateExecution.getVariable("updateNetworkResponse")));
104     }
105
106     @Test
107     public void processCallbackErrorTest() throws MapperException, BadResponseException, IOException {
108         UpdateNetworkRequest updateNetworkRequest = new UpdateNetworkRequest();
109         UpdateNetworkError updateNetworkResponse = new UpdateNetworkError();
110         updateNetworkResponse.setMessageId("messageId");
111         updateNetworkResponse.setMessage("test error message");
112         delegateExecution.setVariable("networkAdapterRequest", updateNetworkRequest);
113         delegateExecution.setVariable("NetworkAResponse_MESSAGE", updateNetworkResponse.toXmlString());
114
115         doThrow(new BpmnError("MSOWorkflowException")).when(exceptionBuilder).buildAndThrowWorkflowException(
116                 any(DelegateExecution.class), anyInt(), any(String.class), any(TargetEntity.class));
117
118         try {
119             networkAdapterRestV1Tasks.processCallback(delegateExecution);
120         } catch (BpmnError be) {
121             assertEquals("MSOWorkflowException", be.getErrorCode());
122         }
123         assertNull(delegateExecution.getVariable("updateNetworkResponse"));
124         verify(exceptionBuilder, times(1)).buildAndThrowWorkflowException(any(DelegateExecution.class), eq(7000),
125                 eq("test error message"), eq(TargetEntity.OPENSTACK));
126     }
127 }