c11dc822b420bc94b31aefd24fde655abc39e510
[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
27 import javax.xml.bind.JAXBException;
28
29 import org.camunda.bpm.engine.delegate.BpmnError;
30 import org.camunda.bpm.engine.delegate.DelegateExecution;
31 import org.camunda.bpm.extension.mockito.delegate.DelegateExecutionFake;
32 import org.junit.Before;
33 import org.junit.Test;
34
35 import org.mockito.InjectMocks;
36 import org.mockito.Mock;
37 import org.onap.so.adapters.nwrest.CreateNetworkResponse;
38 import org.onap.so.adapters.nwrest.UpdateNetworkError;
39 import org.onap.so.adapters.nwrest.UpdateNetworkRequest;
40 import org.onap.so.adapters.nwrest.UpdateNetworkResponse;
41 import org.onap.so.bpmn.BaseTaskTest;
42 import org.onap.so.client.exception.BadResponseException;
43 import org.onap.so.client.exception.ExceptionBuilder;
44 import org.onap.so.client.exception.MapperException;
45
46 import static org.mockito.ArgumentMatchers.any;
47 import static org.mockito.ArgumentMatchers.anyInt;
48 import static org.mockito.ArgumentMatchers.eq;
49 import static org.junit.Assert.assertEquals;
50 import static org.junit.Assert.assertNull;
51 import static org.junit.Assert.assertThat;
52 import static org.mockito.Mockito.doThrow;
53 import static org.mockito.Mockito.times;
54 import static org.mockito.Mockito.verify;
55 import static com.shazam.shazamcrest.matcher.Matchers.sameBeanAs;
56
57 public class NetworkAdapterRestV1Test extends BaseTaskTest {
58         
59         @InjectMocks
60         NetworkAdapterRestV1 networkAdapterRestV1Tasks = new NetworkAdapterRestV1();
61         @Mock
62         ExceptionBuilder exceptionBuilder = new ExceptionBuilder();
63
64         @Before
65         public void setup(){            
66         delegateExecution = new DelegateExecutionFake();        
67         }
68         
69         @Test
70         public void testUnmarshalXml() throws IOException, JAXBException {
71                 String xml = "<?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 = (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 = "<?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>";
86                 UpdateNetworkResponse response = (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).buildAndThrowWorkflowException(any(DelegateExecution.class), anyInt(), any(String.class));
114                 try {
115                         networkAdapterRestV1Tasks.processCallback(delegateExecution);
116                 } 
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), eq("test error message"));
122         }
123 }