d499bcd36e2b6e8dd3a9e60479846f5610b6447b
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / client / orchestration / AAIVnfResourcesTest.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.junit.Assert.assertTrue;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.ArgumentMatchers.isA;
28 import static org.mockito.Mockito.doNothing;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32
33 import java.io.IOException;
34 import java.util.Optional;
35
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.ArgumentMatchers;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.onap.so.bpmn.common.data.TestDataSetup;
44 import org.onap.so.bpmn.common.InjectionHelper;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.CloudRegion;
46 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.LineOfBusiness;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.Platform;
49 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
50 import org.onap.so.client.aai.AAIObjectType;
51 import org.onap.so.client.aai.AAIResourcesClient;
52 import org.onap.so.client.aai.AAIValidatorImpl;
53 import org.onap.so.client.aai.entities.uri.AAIResourceUri;
54 import org.onap.so.client.aai.entities.uri.AAIUriFactory;
55 import org.onap.so.client.aai.mapper.AAIObjectMapper;
56 import org.onap.so.db.catalog.beans.OrchestrationStatus;
57
58 @RunWith(MockitoJUnitRunner.Silent.class)
59 public class AAIVnfResourcesTest extends TestDataSetup {
60
61         private GenericVnf genericVnf;
62
63         private ServiceInstance serviceInstance;
64         
65         private CloudRegion cloudRegion;
66         
67         @Mock
68         protected AAIResourcesClient MOCK_aaiResourcesClient;
69
70         @Mock
71         protected AAIObjectMapper MOCK_aaiObjectMapper;
72
73         @Mock
74         protected InjectionHelper MOCK_injectionHelper;
75         
76         @Mock
77         protected AAIValidatorImpl MOCK_aaiValidatorImpl;
78         
79         @InjectMocks
80         AAIVnfResources aaiVnfResources = new AAIVnfResources();
81
82         @Before
83         public void before() {
84                 serviceInstance = buildServiceInstance();
85                 genericVnf = buildGenericVnf();
86                 cloudRegion = buildCloudRegion();
87                  doReturn(MOCK_aaiResourcesClient).when(MOCK_injectionHelper).getAaiClient();
88         }
89
90         @Test
91         public void createVnfandConnectServiceInstanceTest() {
92                 doReturn(new org.onap.aai.domain.yang.GenericVnf()).when(MOCK_aaiObjectMapper).mapVnf(genericVnf);
93                 doReturn(MOCK_aaiResourcesClient).when(MOCK_aaiResourcesClient).createIfNotExists(isA(AAIResourceUri.class), any(Optional.class));
94                 doNothing().when(MOCK_aaiResourcesClient).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
95                 genericVnf.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
96
97                 aaiVnfResources.createVnfandConnectServiceInstance(genericVnf, serviceInstance);
98
99                 assertEquals(OrchestrationStatus.INVENTORIED, genericVnf.getOrchestrationStatus());
100                 verify(MOCK_aaiObjectMapper, times(1)).mapVnf(genericVnf);
101                 verify(MOCK_aaiResourcesClient, times(1)).createIfNotExists(any(AAIResourceUri.class), any(Optional.class));
102                 verify(MOCK_aaiResourcesClient, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
103         }
104
105         @Test
106         public void createPlatformandConnectVnfTest() {
107                 Platform platform = new Platform();
108                 platform.setPlatformName("a123");
109                 doNothing().when(MOCK_aaiResourcesClient).connect(isA(AAIResourceUri.class),isA(AAIResourceUri.class));
110                 doReturn(MOCK_aaiResourcesClient).when(MOCK_aaiResourcesClient).createIfNotExists(isA(AAIResourceUri.class), any(Optional.class));
111                 aaiVnfResources.createPlatformandConnectVnf(platform, genericVnf);
112                 verify(MOCK_aaiResourcesClient, times(1)).connect(any(AAIResourceUri.class),isA(AAIResourceUri.class));
113         }
114         
115         @Test
116         public void createLineOfBusinessandConnectVnfTest() {
117                 LineOfBusiness lob = new LineOfBusiness();
118                 lob.setLineOfBusinessName("a123");
119                 doNothing().when(MOCK_aaiResourcesClient).connect(isA(AAIResourceUri.class),isA(AAIResourceUri.class));
120                 doReturn(MOCK_aaiResourcesClient).when(MOCK_aaiResourcesClient).createIfNotExists(isA(AAIResourceUri.class), any(Optional.class));
121                 aaiVnfResources.createLineOfBusinessandConnectVnf(lob, genericVnf);
122                 verify(MOCK_aaiResourcesClient, times(1)).connect(any(AAIResourceUri.class),isA(AAIResourceUri.class));
123         }
124         
125         @Test
126         public void deleteVnfTest() {
127                 doNothing().when(MOCK_aaiResourcesClient).delete(isA(AAIResourceUri.class));
128
129                 aaiVnfResources.deleteVnf(genericVnf);
130
131                 verify(MOCK_aaiResourcesClient, times(1)).delete(any(AAIResourceUri.class));
132         }
133
134         @Test
135         public void updateOrchestrationStatusVnfTest() {
136                 doNothing().when(MOCK_aaiResourcesClient).update(isA(AAIResourceUri.class), isA(org.onap.aai.domain.yang.Vnf.class));
137
138                 aaiVnfResources.updateOrchestrationStatusVnf(genericVnf, OrchestrationStatus.ACTIVE);
139
140                 verify(MOCK_aaiResourcesClient, times(1)).update(any(AAIResourceUri.class), ArgumentMatchers.isNull());
141
142                 assertEquals(OrchestrationStatus.ACTIVE, genericVnf.getOrchestrationStatus());
143         }
144
145         @Test
146         public void updateObjectVnfTest() {
147                 doReturn(new org.onap.aai.domain.yang.GenericVnf()).when(MOCK_aaiObjectMapper).mapVnf(genericVnf);
148                 doNothing().when(MOCK_aaiResourcesClient).update(isA(AAIResourceUri.class), isA(org.onap.aai.domain.yang.GenericVnf.class));
149
150                 aaiVnfResources.updateObjectVnf(genericVnf);
151
152                 verify(MOCK_aaiObjectMapper, times(1)).mapVnf(genericVnf);
153                 verify(MOCK_aaiResourcesClient, times(1)).update(isA(AAIResourceUri.class), isA(org.onap.aai.domain.yang.GenericVnf.class));
154         }
155
156         @Test
157         public void getGenericVnfTest () {
158                 Optional<org.onap.aai.domain.yang.GenericVnf> vnf = Optional.of(new org.onap.aai.domain.yang.GenericVnf());
159                 vnf.get().setVnfId("vnfId");
160                 doReturn(vnf).when(MOCK_aaiResourcesClient).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
161                 aaiVnfResources.getGenericVnf("vnfId");
162                 verify(MOCK_aaiResourcesClient, times(1)).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
163         }
164
165         @Test
166         public void checkInMaintFlagTest () {
167                 Optional<org.onap.aai.domain.yang.GenericVnf> vnf = Optional.of(new org.onap.aai.domain.yang.GenericVnf());
168                 vnf.get().setVnfId("vnfId");
169                 vnf.get().setInMaint(true);
170                 doReturn(vnf).when(MOCK_aaiResourcesClient).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
171                 boolean inMaintFlag = aaiVnfResources.checkInMaintFlag("vnfId");
172                 verify(MOCK_aaiResourcesClient, times(1)).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
173                 assertEquals(inMaintFlag, true);
174         }
175         
176         @Test
177         public void connectVnfToTenantTest() throws Exception {
178                 aaiVnfResources.connectVnfToTenant(genericVnf, cloudRegion);
179                 verify(MOCK_aaiResourcesClient, times(1)).connect(eq(AAIUriFactory.createResourceUri(AAIObjectType.TENANT, 
180                                 cloudRegion.getCloudOwner(), cloudRegion.getLcpCloudRegionId(), cloudRegion.getTenantId())), 
181                                 eq(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, genericVnf.getVnfId())));
182         }
183         
184         @Test
185         public void connectVnfToCloudRegionTest() throws Exception {
186                 aaiVnfResources.connectVnfToCloudRegion(genericVnf, cloudRegion);
187                 verify(MOCK_aaiResourcesClient, times(1)).connect(eq(AAIUriFactory.createResourceUri(AAIObjectType.GENERIC_VNF, genericVnf.getVnfId())),
188                                 eq(AAIUriFactory.createResourceUri(AAIObjectType.CLOUD_REGION, 
189                                                 cloudRegion.getCloudOwner(), cloudRegion.getLcpCloudRegionId())));
190         }
191         
192
193         @Test
194         public void checkVnfClosedLoopDisabledFlagTest () {
195                 Optional<org.onap.aai.domain.yang.GenericVnf> vnf = Optional.of(new org.onap.aai.domain.yang.GenericVnf());
196                 vnf.get().setVnfId("vnfId");
197                 vnf.get().setIsClosedLoopDisabled(true);
198                 doReturn(vnf).when(MOCK_aaiResourcesClient).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
199                 boolean isCheckVnfClosedLoopDisabledFlag = aaiVnfResources.checkVnfClosedLoopDisabledFlag("vnfId");
200                 verify(MOCK_aaiResourcesClient, times(1)).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
201                 assertEquals(isCheckVnfClosedLoopDisabledFlag, true);
202         }
203         
204         @Test
205         public void checkVnfPserversLockedFlagTest () throws IOException {
206                 
207                  Optional<org.onap.aai.domain.yang.GenericVnf> vnf = Optional.of(new org.onap.aai.domain.yang.GenericVnf());
208          vnf.get().setVnfId("vnfId");      
209          doReturn(vnf).when(MOCK_aaiResourcesClient).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
210          doReturn(true).when(MOCK_aaiValidatorImpl).isPhysicalServerLocked("vnfId");       
211          boolean isVnfPserversLockedFlag = aaiVnfResources.checkVnfPserversLockedFlag("vnfId");
212          verify(MOCK_aaiResourcesClient, times(1)).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
213          verify(MOCK_aaiValidatorImpl, times(1)).isPhysicalServerLocked(isA(String.class));        
214          assertTrue(isVnfPserversLockedFlag);
215         }
216 }