Replaced all tabs with spaces in java and pom.xml
[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 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;
52
53 public class NetworkAdapterRestV1Test extends BaseTaskTest {
54
55     @InjectMocks
56     NetworkAdapterRestV1 networkAdapterRestV1Tasks = new NetworkAdapterRestV1();
57     @Mock
58     ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
59
60     @Before
61     public void setup() {
62         delegateExecution = new DelegateExecutionFake();
63     }
64
65     @Test
66     public void testUnmarshalXml() throws IOException, JAXBException {
67         String xml =
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);
73     }
74
75     @Test
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);
83         String xml =
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));
88     }
89
90     @Test
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());
98
99         networkAdapterRestV1Tasks.processCallback(delegateExecution);
100
101         assertThat(updateNetworkResponse, sameBeanAs(delegateExecution.getVariable("updateNetworkResponse")));
102     }
103
104     @Test
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());
112
113         doThrow(new BpmnError("MSOWorkflowException")).when(exceptionBuilder)
114                 .buildAndThrowWorkflowException(any(DelegateExecution.class), anyInt(), any(String.class));
115         try {
116             networkAdapterRestV1Tasks.processCallback(delegateExecution);
117         } catch (BpmnError be) {
118             assertEquals("MSOWorkflowException", be.getErrorCode());
119         }
120         assertNull(delegateExecution.getVariable("updateNetworkResponse"));
121         verify(exceptionBuilder, times(1)).buildAndThrowWorkflowException(any(DelegateExecution.class), eq(7000),
122                 eq("test error message"));
123     }
124 }