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