2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 Nokia 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.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.argThat;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.io.IOException;
34 import java.util.Optional;
35 import com.fasterxml.jackson.databind.ObjectMapper;
36 import joptsimple.internal.Strings;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.InjectMocks;
41 import org.mockito.Mock;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.onap.aaiclient.client.aai.AAIObjectType;
44 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
45 import org.onap.so.bpmn.common.InjectionHelper;
46 import org.onap.so.bpmn.common.data.TestDataSetup;
47 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
48 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
49 import org.onap.aaiclient.client.aai.AAIResourcesClient;
50 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
51 import org.onap.so.client.aai.mapper.AAIObjectMapper;
52 import org.onap.so.db.catalog.beans.OrchestrationStatus;
54 @RunWith(MockitoJUnitRunner.Silent.class)
55 public class AAIPnfResourcesTest extends TestDataSetup {
57 private static final String PNF_NAME = "pnfTest";
60 private ServiceInstance serviceInstance;
63 protected AAIObjectMapper aaiObjectMapperMock;
66 protected InjectionHelper injectionHelperMock;
69 protected AAIResourcesClient aaiResourcesClientMock;
72 AAIPnfResources testedObject = new AAIPnfResources();
77 pnf.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
78 serviceInstance = buildServiceInstance();
80 doReturn(aaiResourcesClientMock).when(injectionHelperMock).getAaiClient();
84 public void createPnfAndConnectServiceInstanceShouldSetInventoriedStatusAndCallConnectMethod() {
85 org.onap.aai.domain.yang.Pnf pnfYang = new org.onap.aai.domain.yang.Pnf();
87 doReturn(pnfYang).when(aaiObjectMapperMock).mapPnf(pnf);
88 doReturn(aaiResourcesClientMock).when(aaiResourcesClientMock).createIfNotExists(any(AAIResourceUri.class),
89 eq(Optional.of(pnfYang)));
91 testedObject.createPnfAndConnectServiceInstance(pnf, serviceInstance);
93 assertEquals(OrchestrationStatus.INVENTORIED, pnf.getOrchestrationStatus());
94 verify(aaiResourcesClientMock, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
98 public void updateOrchestrationStatusPnfShouldSetStatusAndUpdatePnfInAAI() {
99 org.onap.aai.domain.yang.Pnf pnfYang = new org.onap.aai.domain.yang.Pnf();
100 doReturn(pnfYang).when(aaiObjectMapperMock).mapPnf(pnf);
102 testedObject.updateOrchestrationStatusPnf(pnf, OrchestrationStatus.ACTIVE);
104 assertEquals(OrchestrationStatus.ACTIVE, pnf.getOrchestrationStatus());
105 verify(aaiObjectMapperMock, times(1))
106 .mapPnf(argThat(arg -> OrchestrationStatus.ACTIVE.equals(arg.getOrchestrationStatus())));
107 verify(aaiResourcesClientMock, times(1)).update(any(AAIResourceUri.class), eq(pnfYang));
111 public void existingPnfInAaiWithInventoriedStatusCanBeUsed() throws Exception {
113 org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(OrchestrationStatus.INVENTORIED.toString());
114 when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
115 AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
117 testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
121 public void existingPnfInAaiWithNullStatusCanBeUsed() throws Exception {
123 org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(null);
124 when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
125 AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
127 testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
131 public void existingPnfInAaiWithEmptyStatusCanBeUsed() throws Exception {
133 org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(Strings.EMPTY);
134 when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
135 AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
137 testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
141 public void existingPnfInAaiCanNotBeUsed() {
143 org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(OrchestrationStatus.ACTIVE.toString());
144 when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
145 AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
148 testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
149 } catch (Exception e) {
151 assertThat(e.getMessage()).isEqualTo(String.format(
152 "pnf with name %s already exists with orchestration status Active, existing pnf can be used only "
153 + "if status is not set or set as Inventoried",
159 public void existingPnfInAaiIsRelatedToService() throws IOException {
161 final String relatedTo = "service-instance";
162 final String serviceInstanceId = "service-instance-id";
163 final String path = "src/test/resources/__files/BuildingBlocks/aaiPnf.json";
164 org.onap.aai.domain.yang.Pnf pnfFromAai =
165 new ObjectMapper().readValue(new File(path), org.onap.aai.domain.yang.Pnf.class);
166 when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
167 AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
170 testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
171 } catch (Exception e) {
173 assertThat(e.getMessage()).isEqualTo(String.format(
174 "Pnf with name %s exist with orchestration status %s and is related to %s service with certain service-instance-id: %s",
175 PNF_NAME, OrchestrationStatus.ACTIVE, relatedTo, serviceInstanceId));
179 private org.onap.aai.domain.yang.Pnf createPnf(String orchestrationStatus) {
180 org.onap.aai.domain.yang.Pnf pnfFromAai = new org.onap.aai.domain.yang.Pnf();
181 pnfFromAai.setPnfName(PNF_NAME);
182 pnfFromAai.setOrchestrationStatus(orchestrationStatus);