2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.client.orchestration;
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;
33 import java.io.IOException;
34 import java.util.Optional;
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;
58 @RunWith(MockitoJUnitRunner.Silent.class)
59 public class AAIVnfResourcesTest extends TestDataSetup {
61 private GenericVnf genericVnf;
63 private ServiceInstance serviceInstance;
65 private CloudRegion cloudRegion;
68 protected AAIResourcesClient MOCK_aaiResourcesClient;
71 protected AAIObjectMapper MOCK_aaiObjectMapper;
74 protected InjectionHelper MOCK_injectionHelper;
77 protected AAIValidatorImpl MOCK_aaiValidatorImpl;
80 AAIVnfResources aaiVnfResources = new AAIVnfResources();
83 public void before() {
84 serviceInstance = buildServiceInstance();
85 genericVnf = buildGenericVnf();
86 cloudRegion = buildCloudRegion();
87 doReturn(MOCK_aaiResourcesClient).when(MOCK_injectionHelper).getAaiClient();
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);
97 aaiVnfResources.createVnfandConnectServiceInstance(genericVnf, serviceInstance);
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));
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));
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));
126 public void deleteVnfTest() {
127 doNothing().when(MOCK_aaiResourcesClient).delete(isA(AAIResourceUri.class));
129 aaiVnfResources.deleteVnf(genericVnf);
131 verify(MOCK_aaiResourcesClient, times(1)).delete(any(AAIResourceUri.class));
135 public void updateOrchestrationStatusVnfTest() {
136 doNothing().when(MOCK_aaiResourcesClient).update(isA(AAIResourceUri.class), isA(org.onap.aai.domain.yang.Vnf.class));
138 aaiVnfResources.updateOrchestrationStatusVnf(genericVnf, OrchestrationStatus.ACTIVE);
140 verify(MOCK_aaiResourcesClient, times(1)).update(any(AAIResourceUri.class), ArgumentMatchers.isNull());
142 assertEquals(OrchestrationStatus.ACTIVE, genericVnf.getOrchestrationStatus());
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));
150 aaiVnfResources.updateObjectVnf(genericVnf);
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));
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));
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);
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())));
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())));
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);
205 public void checkVnfPserversLockedFlagTest () throws IOException {
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);