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