ea98ee016933e3a123bc6d8f3065f6f401360a66
[so.git] /
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.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.isA;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.doNothing;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.doThrow;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.verify;
34 import java.util.Optional;
35 import org.junit.Before;
36 import org.junit.Ignore;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.so.bpmn.common.data.TestDataSetup;
43 import org.onap.so.bpmn.common.InjectionHelper;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.Customer;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.OwningEntity;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.Project;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceSubscription;
49 import org.onap.so.client.aai.AAIObjectPlurals;
50 import org.onap.so.client.aai.AAIResourcesClient;
51 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
52 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
53 import org.onap.so.client.aai.mapper.AAIObjectMapper;
54 import org.onap.so.db.catalog.beans.OrchestrationStatus;
55
56 @RunWith(MockitoJUnitRunner.Silent.class)
57 public class AAIServiceInstanceResourcesTest extends TestDataSetup {
58
59     @InjectMocks
60     private AAIServiceInstanceResources aaiServiceInstanceResources = new AAIServiceInstanceResources();
61
62     @Mock
63     protected AAIResourcesClient MOCK_aaiResourcesClient;
64
65     @Mock
66     protected AAIObjectMapper MOCK_aaiObjectMapper;
67
68     @Mock
69     protected InjectionHelper MOCK_injectionHelper;
70
71     private ServiceInstance serviceInstance;
72     private ServiceSubscription serviceSubscription;
73     private Customer customer;
74     private Project project;
75     private OwningEntity owningEntity;
76
77     @Before
78     public void before() {
79         serviceInstance = buildServiceInstance();
80         serviceSubscription = buildServiceSubscription();
81         customer = buildCustomer();
82         project = buildProject();
83         owningEntity = buildOwningEntity();
84         doReturn(MOCK_aaiResourcesClient).when(MOCK_injectionHelper).getAaiClient();
85     }
86
87     @Test
88     public void deleteServiceInstanceSuccessTest() throws Exception {
89         aaiServiceInstanceResources.deleteServiceInstance(serviceInstance);
90         verify(MOCK_aaiResourcesClient, times(1)).delete(any(AAIResourceUri.class));
91     }
92
93     @Test
94     public void deleteServiceInstanceExceptionTest() throws Exception {
95         expectedException.expect(Exception.class);
96         doThrow(Exception.class).when(MOCK_aaiResourcesClient).delete(isA(AAIResourceUri.class));
97         aaiServiceInstanceResources.deleteServiceInstance(serviceInstance);
98     }
99
100     @Test
101     public void existsServiceInstanceTest() {
102         aaiServiceInstanceResources.existsServiceInstance(serviceInstance);
103         verify(MOCK_aaiResourcesClient, times(1)).exists(any(AAIResourceUri.class));
104     }
105
106     @Test
107     public void createServiceSubscriptionTest() {
108         serviceSubscription.setServiceType("IP-FLEX");
109         customer.setServiceSubscription(serviceSubscription);
110         doReturn(new org.onap.aai.domain.yang.ServiceSubscription()).when(MOCK_aaiObjectMapper)
111                 .mapServiceSubscription(customer.getServiceSubscription());
112         aaiServiceInstanceResources.createServiceSubscription(customer);
113         verify(MOCK_aaiResourcesClient, times(1)).createIfNotExists(any(AAIResourceUri.class), any(Optional.class));
114     }
115
116     @Test
117     public void createServiceInstanceTest() {
118         serviceSubscription.setServiceType("testSubscriberType");
119         customer.setServiceSubscription(serviceSubscription);
120         doReturn(new org.onap.aai.domain.yang.ServiceInstance()).when(MOCK_aaiObjectMapper)
121                 .mapServiceInstance(serviceInstance);
122         serviceInstance.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
123
124         aaiServiceInstanceResources.createServiceInstance(serviceInstance, customer);
125
126         assertEquals(OrchestrationStatus.INVENTORIED, serviceInstance.getOrchestrationStatus());
127         verify(MOCK_aaiResourcesClient, times(1)).createIfNotExists(any(AAIResourceUri.class), any(Optional.class));
128     }
129
130     @Test
131     public void createProjectTest() {
132         doReturn(new org.onap.aai.domain.yang.Project()).when(MOCK_aaiObjectMapper).mapProject(project);
133         aaiServiceInstanceResources.createProject(project);
134         verify(MOCK_aaiResourcesClient, times(1)).createIfNotExists(any(AAIResourceUri.class), any(Optional.class));
135     }
136
137     @Test
138     public void createProjectandConnectServiceInstanceTest() {
139         doReturn(MOCK_aaiResourcesClient).when(MOCK_aaiResourcesClient).createIfNotExists(any(AAIResourceUri.class),
140                 any(Optional.class));
141         doNothing().when(MOCK_aaiResourcesClient).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
142         doReturn(new org.onap.aai.domain.yang.Project()).when(MOCK_aaiObjectMapper).mapProject(project);
143         aaiServiceInstanceResources.createProjectandConnectServiceInstance(project, serviceInstance);
144         verify(MOCK_aaiResourcesClient, times(1)).createIfNotExists(any(AAIResourceUri.class), any(Optional.class));
145         verify(MOCK_aaiResourcesClient, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
146     }
147
148     @Test
149     public void createOwningEntityTest() {
150         doReturn(new org.onap.aai.domain.yang.OwningEntity()).when(MOCK_aaiObjectMapper).mapOwningEntity(owningEntity);
151         aaiServiceInstanceResources.createOwningEntity(owningEntity);
152         verify(MOCK_aaiResourcesClient, times(1)).createIfNotExists(any(AAIResourceUri.class), any(Optional.class));
153     }
154
155     @Test
156     public void existsOwningEntityTest() {
157         aaiServiceInstanceResources.existsOwningEntity(owningEntity);
158         verify(MOCK_aaiResourcesClient, times(1)).exists(any(AAIResourceUri.class));
159     }
160
161     @Test
162     public void connectOwningEntityandServiceInstanceTest() {
163         aaiServiceInstanceResources.connectOwningEntityandServiceInstance(owningEntity, serviceInstance);
164         verify(MOCK_aaiResourcesClient, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
165     }
166
167     @Test
168     public void createOwningEntityandConnectServiceInstanceTest() {
169         doReturn(MOCK_aaiResourcesClient).when(MOCK_aaiResourcesClient).createIfNotExists(any(AAIResourceUri.class),
170                 any(Optional.class));
171         doNothing().when(MOCK_aaiResourcesClient).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
172         doReturn(new org.onap.aai.domain.yang.OwningEntity()).when(MOCK_aaiObjectMapper).mapOwningEntity(owningEntity);
173         aaiServiceInstanceResources.createOwningEntityandConnectServiceInstance(owningEntity, serviceInstance);
174         verify(MOCK_aaiResourcesClient, times(1)).createIfNotExists(any(AAIResourceUri.class), any(Optional.class));
175         verify(MOCK_aaiResourcesClient, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
176     }
177
178     @Test
179     @Ignore
180     public void updateOrchestrationStatusServiceInstanceTest() {
181         aaiServiceInstanceResources.updateOrchestrationStatusServiceInstance(serviceInstance,
182                 OrchestrationStatus.ACTIVE);
183         verify(MOCK_aaiResourcesClient, times(1)).update(any(AAIResourceUri.class),
184                 any(org.onap.aai.domain.yang.ServiceInstance.class));
185     }
186
187     @Test
188     @Ignore
189     public void test_updateServiceInstance() {
190         aaiServiceInstanceResources.updateServiceInstance(serviceInstance);
191         verify(MOCK_aaiResourcesClient, times(1)).update(any(AAIResourceUri.class),
192                 any(org.onap.aai.domain.yang.ServiceInstance.class));
193     }
194
195     @Test
196     public void checkInstanceServiceNameInUseTrueTest() throws Exception {
197         AAIResourceUri uri = AAIUriFactory.createNodesUri(AAIObjectPlurals.SERVICE_INSTANCE)
198                 .queryParam("service-instance-name", serviceInstance.getServiceInstanceName());
199         doReturn(true).when(MOCK_aaiResourcesClient).exists(eq(uri));
200         boolean nameInUse = aaiServiceInstanceResources.checkInstanceServiceNameInUse(serviceInstance);
201         assertTrue(nameInUse);
202     }
203
204     @Test
205     public void checkInstanceServiceNameInUseFalseTest() throws Exception {
206         AAIResourceUri uri = AAIUriFactory.createNodesUri(AAIObjectPlurals.SERVICE_INSTANCE)
207                 .queryParam("service-instance-name", serviceInstance.getServiceInstanceName());
208         doReturn(false).when(MOCK_aaiResourcesClient).exists(eq(uri));
209         boolean nameInUse = aaiServiceInstanceResources.checkInstanceServiceNameInUse(serviceInstance);
210         assertFalse(nameInUse);
211     }
212
213 }