Springboot 2.0 upgrade
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / bpmn / infrastructure / adapter / network / tasks / NetworkAdapterUpdateTasksTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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 static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.doThrow;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import java.io.UnsupportedEncodingException;
34 import java.util.Map;
35 import java.util.Optional;
36
37 import org.camunda.bpm.engine.delegate.BpmnError;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.mockito.ArgumentMatchers;
41 import org.mockito.InjectMocks;
42 import org.onap.so.adapters.nwrest.UpdateNetworkResponse;
43 import org.onap.so.bpmn.BaseTaskTest;
44 import org.onap.so.bpmn.common.BuildingBlockExecution;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
49 import org.onap.so.bpmn.servicedecomposition.entities.ResourceKey;
50 import org.onap.so.bpmn.servicedecomposition.generalobjects.OrchestrationContext;
51 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
52 import org.onap.so.client.adapter.network.NetworkAdapterClientException;
53 import org.onap.so.client.exception.BBObjectNotFoundException;
54 import org.springframework.beans.factory.annotation.Autowired;
55
56 public class NetworkAdapterUpdateTasksTest extends BaseTaskTest{
57         @InjectMocks
58         private NetworkAdapterUpdateTasks networkAdapterUpdateTasks = new NetworkAdapterUpdateTasks();
59         
60         private ServiceInstance serviceInstance;
61         private L3Network network;
62         private RequestContext requestContext;
63         private CloudRegion cloudRegion;
64         private OrchestrationContext orchestrationContext;
65         private Map<String, String> userInput;
66         private Customer customer;
67         
68         @Before
69         public void before() throws BBObjectNotFoundException {
70                 customer = setCustomer();
71                 serviceInstance = setServiceInstance();
72                 network = setL3Network();
73                 requestContext = setRequestContext();
74                 cloudRegion = setCloudRegion();
75                 orchestrationContext = setOrchestrationContext();
76                 userInput = setUserInput();
77                 userInput.put("userInputKey1", "userInputValue1");
78                 
79                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.NETWORK_ID), any())).thenReturn(network);
80                 when(extractPojosForBB.extractByKey(any(),ArgumentMatchers.eq(ResourceKey.SERVICE_INSTANCE_ID), any())).thenReturn(serviceInstance);
81                 
82         }
83         
84         @Test
85         public void updateNetworkTest() throws Exception {
86                 UpdateNetworkResponse updateNetworkResponse = new UpdateNetworkResponse();
87                 updateNetworkResponse.setMessageId("messageId");
88                 updateNetworkResponse.setNetworkId("networkId");
89                 Optional<UpdateNetworkResponse> oUpdateNetworkResponse = Optional.of(updateNetworkResponse);
90                 
91                 doReturn(oUpdateNetworkResponse).when(networkAdapterResources).updateNetwork(requestContext, cloudRegion, orchestrationContext, serviceInstance, network, userInput, customer);
92                 
93                 networkAdapterUpdateTasks.updateNetwork(execution);
94                 
95                 verify(networkAdapterResources, times(1)).updateNetwork(requestContext, cloudRegion, orchestrationContext, serviceInstance, network, userInput, customer);
96                 assertEquals(updateNetworkResponse, execution.getVariable("NetworkAdapterUpdateNetworkResponse"));
97         }
98         
99         @Test
100         public void updateNetworkNoResponseTest() throws Exception {
101                 doReturn(Optional.empty()).when(networkAdapterResources).updateNetwork(requestContext, cloudRegion, orchestrationContext, serviceInstance, network, userInput, customer);
102                 
103                 networkAdapterUpdateTasks.updateNetwork(execution);
104                 
105                 verify(networkAdapterResources, times(1)).updateNetwork(requestContext, cloudRegion, orchestrationContext, serviceInstance, network, userInput, customer);
106                 assertNull(execution.getVariable("NetworkAdapterUpdateNetworkResponse"));
107         }
108         
109         @Test
110         public void updateNetworkExceptionTest() throws UnsupportedEncodingException, NetworkAdapterClientException {
111                 expectedException.expect(BpmnError.class);
112                 doThrow(new NetworkAdapterClientException("ERROR")).when(networkAdapterResources).updateNetwork(any(RequestContext.class),any(CloudRegion.class), 
113                                 any(OrchestrationContext.class),eq(serviceInstance),eq(network),any(Map.class),any(Customer.class));
114                 doThrow(new BpmnError("BPMN Error")).when(exceptionUtil).buildAndThrowWorkflowException(any(BuildingBlockExecution.class), eq(7000), any(Exception.class));
115                 networkAdapterUpdateTasks.updateNetwork(execution);
116         }
117 }