43426fcfdef4bd79e42ddabf4ddea76e6bef61bb
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / client / orchestration / SDNCServiceInstanceResourcesTest.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.mockito.Matchers.any;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.runners.MockitoJUnitRunner;
36 import org.onap.so.bpmn.common.data.TestDataSetup;
37 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
38 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
39 import org.onap.so.bpmn.servicedecomposition.generalobjects.RequestContext;
40 import org.onap.so.client.exception.BadResponseException;
41 import org.onap.so.client.exception.MapperException;
42 import org.onap.so.client.sdnc.SDNCClient;
43 import org.onap.so.client.sdnc.beans.SDNCSvcAction;
44 import org.onap.so.client.sdnc.beans.SDNCSvcOperation;
45 import org.onap.so.client.sdnc.endpoint.SDNCTopology;
46 import org.onap.so.client.sdnc.mapper.ServiceTopologyOperationMapper;
47
48 import org.onap.sdnc.northbound.client.model.GenericResourceApiRequestActionEnumeration;
49 import org.onap.sdnc.northbound.client.model.GenericResourceApiServiceOperationInformation;
50
51 @RunWith(MockitoJUnitRunner.class)
52 public class SDNCServiceInstanceResourcesTest  extends TestDataSetup{
53         @InjectMocks
54     private SDNCServiceInstanceResources sdncServiceInstanceResources;
55         
56
57         @Mock
58         protected ServiceTopologyOperationMapper MOCK_serviceTopologyOperationMapper;
59         
60         private RequestContext requestContext;
61         private ServiceInstance serviceInstance;
62         private Customer customer;
63         
64         @Mock
65         protected SDNCClient MOCK_sdncClient;
66         
67         @Before
68         public void before() {
69                 requestContext = buildRequestContext();
70                 
71                 serviceInstance = buildServiceInstance();
72
73                 customer = buildCustomer();
74
75                 customer.getServiceSubscription().getServiceInstances().add(serviceInstance);
76         }
77         
78         @Test
79         public void assignServiceInstanceSuccessTest() throws Exception {
80                 doReturn(new GenericResourceApiServiceOperationInformation()).when(MOCK_serviceTopologyOperationMapper).reqMapper(eq(SDNCSvcOperation.SERVICE_TOPOLOGY_OPERATION), eq(SDNCSvcAction.ASSIGN), eq(GenericResourceApiRequestActionEnumeration.CREATESERVICEINSTANCE), any(ServiceInstance.class), any(Customer.class), any(RequestContext.class));
81                 doReturn("test").when(MOCK_sdncClient).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
82                 sdncServiceInstanceResources.assignServiceInstance(serviceInstance, customer, requestContext);
83                 verify(MOCK_sdncClient, times(1)).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
84         }
85         
86         @Test
87         public void assignServiceInstanceExceptionTest() throws Exception {
88                 expectedException.expect(Exception.class);
89                 doReturn(new GenericResourceApiServiceOperationInformation()).when(MOCK_serviceTopologyOperationMapper).reqMapper(eq(SDNCSvcOperation.SERVICE_TOPOLOGY_OPERATION), eq(SDNCSvcAction.ASSIGN), eq(GenericResourceApiRequestActionEnumeration.CREATESERVICEINSTANCE), any(ServiceInstance.class), any(Customer.class), any(RequestContext.class));
90                 doThrow(Exception.class).when(MOCK_sdncClient).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
91                 sdncServiceInstanceResources.assignServiceInstance(serviceInstance, customer, requestContext);
92         }
93         
94         @Test
95         public void deleteServiceInstanceSuccessTest() throws Exception {
96                 doReturn(new GenericResourceApiServiceOperationInformation()).when(MOCK_serviceTopologyOperationMapper).reqMapper(eq(SDNCSvcOperation.SERVICE_TOPOLOGY_OPERATION), eq(SDNCSvcAction.DELETE), eq(GenericResourceApiRequestActionEnumeration.DELETESERVICEINSTANCE), any(ServiceInstance.class), any(Customer.class), any(RequestContext.class));
97                 doReturn("test").when(MOCK_sdncClient).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
98                 sdncServiceInstanceResources.deleteServiceInstance(serviceInstance, customer, requestContext);
99                 verify(MOCK_sdncClient, times(1)).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
100         }
101         
102         @Test
103         public void deleteServiceInstanceExceptionTest() throws Exception {
104                 expectedException.expect(Exception.class);
105                 doReturn(new GenericResourceApiServiceOperationInformation()).when(MOCK_serviceTopologyOperationMapper).reqMapper(eq(SDNCSvcOperation.SERVICE_TOPOLOGY_OPERATION), eq(SDNCSvcAction.DELETE), eq(GenericResourceApiRequestActionEnumeration.DELETESERVICEINSTANCE), any(ServiceInstance.class), any(Customer.class), any(RequestContext.class));
106                 doThrow(Exception.class).when(MOCK_sdncClient).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
107                 sdncServiceInstanceResources.deleteServiceInstance(serviceInstance, customer, requestContext);
108         }
109         
110         @Test
111         public void unassignServiceInstanceSuccessTest() throws Exception {
112                 doReturn(new GenericResourceApiServiceOperationInformation()).when(MOCK_serviceTopologyOperationMapper).reqMapper(eq(SDNCSvcOperation.SERVICE_TOPOLOGY_OPERATION), eq(SDNCSvcAction.DELETE), eq(GenericResourceApiRequestActionEnumeration.DELETESERVICEINSTANCE), any(ServiceInstance.class), any(Customer.class), any(RequestContext.class));
113                 doReturn("test").when(MOCK_sdncClient).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
114                 sdncServiceInstanceResources.unassignServiceInstance(serviceInstance, customer, requestContext);
115                 verify(MOCK_sdncClient, times(1)).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
116         }
117         
118         @Test
119         public void unassignServiceInstanceExceptionTest() throws Exception {
120                 expectedException.expect(Exception.class);
121                 doReturn(new GenericResourceApiServiceOperationInformation()).when(MOCK_serviceTopologyOperationMapper).reqMapper(eq(SDNCSvcOperation.SERVICE_TOPOLOGY_OPERATION), eq(SDNCSvcAction.DELETE), eq(GenericResourceApiRequestActionEnumeration.DELETESERVICEINSTANCE), any(ServiceInstance.class), any(Customer.class), any(RequestContext.class));
122                 doThrow(Exception.class).when(MOCK_sdncClient).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
123                 sdncServiceInstanceResources.unassignServiceInstance(serviceInstance,customer, requestContext);
124         }
125
126         @Test
127         public void deactivateServiceInstanceSuccessTest() throws Exception {
128                 doReturn(new GenericResourceApiServiceOperationInformation()).when(MOCK_serviceTopologyOperationMapper).reqMapper(eq(SDNCSvcOperation.SERVICE_TOPOLOGY_OPERATION), eq(SDNCSvcAction.DEACTIVATE), eq(GenericResourceApiRequestActionEnumeration.CREATESERVICEINSTANCE), any(ServiceInstance.class), any(Customer.class), any(RequestContext.class));
129                 doReturn("test").when(MOCK_sdncClient).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
130                 sdncServiceInstanceResources.deactivateServiceInstance(serviceInstance, customer, requestContext);
131                 verify(MOCK_sdncClient, times(1)).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
132         }
133         
134         @Test
135         public void deactivateServiceInstanceExceptionTest() throws Exception {
136                 expectedException.expect(Exception.class);
137                 doReturn(new GenericResourceApiServiceOperationInformation()).when(MOCK_serviceTopologyOperationMapper).reqMapper(eq(SDNCSvcOperation.SERVICE_TOPOLOGY_OPERATION), eq(SDNCSvcAction.DEACTIVATE), eq(GenericResourceApiRequestActionEnumeration.CREATESERVICEINSTANCE), any(ServiceInstance.class), any(Customer.class), any(RequestContext.class));
138                 doThrow(Exception.class).when(MOCK_sdncClient).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
139                 sdncServiceInstanceResources.deactivateServiceInstance(serviceInstance, customer, requestContext);
140         }
141
142         @Test
143         public void test_changeModelServiceInstance() throws MapperException, BadResponseException {
144                 doReturn(new GenericResourceApiServiceOperationInformation()).when(MOCK_serviceTopologyOperationMapper).reqMapper(eq(SDNCSvcOperation.SERVICE_TOPOLOGY_OPERATION), eq(SDNCSvcAction.CHANGE_ASSIGN), eq(GenericResourceApiRequestActionEnumeration.CREATESERVICEINSTANCE), any(ServiceInstance.class), any(Customer.class), any(RequestContext.class));
145                 doReturn("test").when(MOCK_sdncClient).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
146                 sdncServiceInstanceResources.changeModelServiceInstance(serviceInstance, customer, requestContext);
147                 verify(MOCK_sdncClient, times(1)).post(any(GenericResourceApiServiceOperationInformation.class), eq(SDNCTopology.SERVICE));
148         }
149 }