3cec3a656a5ddfc806e8cda0600194bed35329ba
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / client / orchestration / SDNCNetworkResourcesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.client.orchestration;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Matchers.isA;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.runners.MockitoJUnitRunner;
37 import org.onap.so.bpmn.common.data.TestDataSetup;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.L3Network;
41 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceSubscription;
43 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
44 import org.onap.so.client.exception.BadResponseException;
45 import org.onap.so.client.exception.MapperException;
46 import org.onap.so.client.sdnc.SDNCClient;
47 import org.onap.so.client.sdnc.beans.SDNCSvcAction;
48 import org.onap.so.client.sdnc.beans.SDNCSvcOperation;
49 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
50 import org.onap.so.client.sdnc.mapper.NetworkTopologyOperationRequestMapper;
51 import org.onap.so.db.catalog.beans.OrchestrationStatus;
52
53 import org.onap.sdnc.northbound.client.model.GenericResourceApiNetworkOperationInformation;
54 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration;
55
56 @RunWith(MockitoJUnitRunner.class)
57 public class SDNCNetworkResourcesTest extends TestDataSetup{
58         
59         @InjectMocks
60         private SDNCNetworkResources sdncNetworkResources;
61         
62         @Mock
63         protected SDNCClient MOCK_sdncClient;
64         
65         @Mock
66         protected NetworkTopologyOperationRequestMapper MOCK_networkTopologyOperationRequestMapper;     
67         
68         private L3Network network;
69         private ServiceInstance serviceInstance;
70         private Customer customer;
71         private RequestContext requestContext;
72         private CloudRegion cloudRegion;
73         
74         @Before
75         public void before() {
76                 network = buildL3Network();
77
78                 customer = buildCustomer();
79
80                 serviceInstance = buildServiceInstance();
81
82                 requestContext = buildRequestContext();
83                 
84                 cloudRegion = new CloudRegion();
85         }
86
87         @Test
88         public void assignNetworkTest() throws Exception {
89                 network.setOrchestrationStatus(OrchestrationStatus.ASSIGNED);
90                 
91                 doReturn("test").when(MOCK_sdncClient).post(isA(GenericResourceApiNetworkOperationInformation.class), eq(SDNCTopology.NETWORK));
92                 
93                 sdncNetworkResources.assignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
94
95                 verify(MOCK_sdncClient, times(1)).post(any(GenericResourceApiNetworkOperationInformation.class), eq(SDNCTopology.NETWORK));
96                 
97                 assertEquals(OrchestrationStatus.ASSIGNED, network.getOrchestrationStatus());
98         }
99         
100         @Test
101         public void rollbackAssignNetworkTest() throws Exception {
102                 network.setOrchestrationStatus(OrchestrationStatus.ASSIGNED);
103
104                 doReturn("test").when(MOCK_sdncClient).post(isA(GenericResourceApiNetworkOperationInformation.class), eq(SDNCTopology.NETWORK));
105                 
106                 sdncNetworkResources.rollbackAssignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
107
108                 verify(MOCK_sdncClient, times(1)).post(any(GenericResourceApiNetworkOperationInformation.class), eq(SDNCTopology.NETWORK));
109                 
110                 assertEquals(OrchestrationStatus.ASSIGNED, network.getOrchestrationStatus());
111         }
112         
113         @Test
114         public void activateNetworkTest() throws Exception {
115                 network.setOrchestrationStatus(OrchestrationStatus.ASSIGNED);
116
117                 doReturn("test").when(MOCK_sdncClient).post(isA(GenericResourceApiNetworkOperationInformation.class), eq(SDNCTopology.NETWORK));
118                 
119                 sdncNetworkResources.activateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
120
121                 verify(MOCK_sdncClient, times(1)).post(any(GenericResourceApiNetworkOperationInformation.class), eq(SDNCTopology.NETWORK));
122                 
123                 assertEquals(OrchestrationStatus.ASSIGNED, network.getOrchestrationStatus());
124         }
125         
126         @Test
127         public void deleteNetworkTest() throws Exception {
128                 network.setOrchestrationStatus(OrchestrationStatus.ASSIGNED);
129
130                 doReturn("test").when(MOCK_sdncClient).post(isA(GenericResourceApiNetworkOperationInformation.class), eq(SDNCTopology.NETWORK));
131                 
132                 sdncNetworkResources.deleteNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
133
134                 verify(MOCK_sdncClient, times(1)).post(any(GenericResourceApiNetworkOperationInformation.class), eq(SDNCTopology.NETWORK));
135                 
136                 assertEquals(OrchestrationStatus.ASSIGNED, network.getOrchestrationStatus());
137         }
138         
139         @Test
140         public void test_deactivateNetwork() throws MapperException, BadResponseException {
141                 serviceInstance.getNetworks().add(network);
142
143                 Customer customer = new Customer();
144                 customer.setGlobalCustomerId("gcustId");
145                 customer.setServiceSubscription(new ServiceSubscription());
146                 // set Customer on service instance
147                 customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
148
149                 GenericResourceApiNetworkOperationInformation expectedGenericResourceApiNetworkOperationInformation = new GenericResourceApiNetworkOperationInformation();
150                 
151                 String expectedResponse = "response";
152                 
153                 doReturn(expectedResponse).when(MOCK_sdncClient).post(expectedGenericResourceApiNetworkOperationInformation, SDNCTopology.NETWORK);
154                 
155                 doReturn(expectedGenericResourceApiNetworkOperationInformation).when(MOCK_networkTopologyOperationRequestMapper).reqMapper(SDNCSvcOperation.NETWORK_TOPOLOGY_OPERATION, SDNCSvcAction.DEACTIVATE, GenericResourceApiRequestActionEnumeration.DELETENETWORKINSTANCE, network, serviceInstance, customer, requestContext, cloudRegion);
156                 
157                 String actualResponse = sdncNetworkResources.deactivateNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
158                 
159                 verify(MOCK_networkTopologyOperationRequestMapper, times(1)).reqMapper(SDNCSvcOperation.NETWORK_TOPOLOGY_OPERATION, SDNCSvcAction.DEACTIVATE, GenericResourceApiRequestActionEnumeration.DELETENETWORKINSTANCE, network, serviceInstance, customer, requestContext, cloudRegion);
160                 
161                 verify(MOCK_sdncClient).post(expectedGenericResourceApiNetworkOperationInformation, SDNCTopology.NETWORK);
162                 
163                 assertEquals(expectedResponse, actualResponse);
164         }
165         
166         @Test
167         public void changeAssignNetworkTest() throws MapperException, BadResponseException {
168                 String expectedSdncResponse = "SDNCChangeAssignNetworkResponse";
169
170                 serviceInstance.getNetworks().add(network);
171
172                 Customer customer = new Customer();
173                 customer.setGlobalCustomerId("globalCustomerId");
174                 customer.setServiceSubscription(new ServiceSubscription());
175                 // set Customer on service instance
176                 customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
177                 
178                 GenericResourceApiNetworkOperationInformation sdncReq = new GenericResourceApiNetworkOperationInformation();
179                 
180                 doReturn(sdncReq).when(MOCK_networkTopologyOperationRequestMapper).reqMapper(isA(SDNCSvcOperation.class), isA(SDNCSvcAction.class), isA(GenericResourceApiRequestActionEnumeration.class), isA(L3Network.class), isA(ServiceInstance.class), isA(Customer.class), isA(RequestContext.class), isA(CloudRegion.class));
181                 
182                 doReturn(expectedSdncResponse).when(MOCK_sdncClient).post(isA(GenericResourceApiNetworkOperationInformation.class), isA(SDNCTopology.class));
183                 
184                 String actualSdncResponse = sdncNetworkResources.changeAssignNetwork(network, serviceInstance, customer, requestContext, cloudRegion);
185                 
186                 verify(MOCK_networkTopologyOperationRequestMapper, times(1)).reqMapper(SDNCSvcOperation.NETWORK_TOPOLOGY_OPERATION, SDNCSvcAction.CHANGE_ASSIGN, GenericResourceApiRequestActionEnumeration.CREATENETWORKINSTANCE, network, serviceInstance, customer, requestContext, cloudRegion);
187                 verify(MOCK_sdncClient, times(1)).post(sdncReq, SDNCTopology.NETWORK);
188                 assertEquals(actualSdncResponse, expectedSdncResponse);
189         }
190         
191         @Test
192         public void unassignNetwork_Test() throws Exception {
193                 network.setOrchestrationStatus(OrchestrationStatus.CREATED);
194
195                 doReturn("test").when(MOCK_sdncClient).post(isA(GenericResourceApiNetworkOperationInformation.class), eq(SDNCTopology.NETWORK));
196                 
197                 sdncNetworkResources.unassignNetwork(network, serviceInstance, customer,
198                                 requestContext, cloudRegion);
199  
200                 verify(MOCK_sdncClient, times(1)).post(any(GenericResourceApiNetworkOperationInformation.class), eq(SDNCTopology.NETWORK));
201                 
202                 assertEquals(OrchestrationStatus.CREATED, network.getOrchestrationStatus());            
203         }               
204 }