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;
32 import java.util.Optional;
33 import joptsimple.internal.Strings;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.junit.MockitoJUnitRunner;
40 import org.onap.aaiclient.client.aai.AAIObjectType;
41 import org.onap.aaiclient.client.aai.entities.uri.AAIUriFactory;
42 import org.onap.so.bpmn.common.InjectionHelper;
43 import org.onap.so.bpmn.common.data.TestDataSetup;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
46 import org.onap.aaiclient.client.aai.AAIResourcesClient;
47 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
48 import org.onap.so.client.aai.mapper.AAIObjectMapper;
49 import org.onap.so.db.catalog.beans.OrchestrationStatus;
51 @RunWith(MockitoJUnitRunner.Silent.class)
52 public class AAIPnfResourcesTest extends TestDataSetup {
54 private static final String PNF_NAME = "pnfTest";
57 private ServiceInstance serviceInstance;
60 protected AAIObjectMapper aaiObjectMapperMock;
63 protected InjectionHelper injectionHelperMock;
66 protected AAIResourcesClient aaiResourcesClientMock;
69 AAIPnfResources testedObject = new AAIPnfResources();
74 pnf.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
75 serviceInstance = buildServiceInstance();
77 doReturn(aaiResourcesClientMock).when(injectionHelperMock).getAaiClient();
81 public void createPnfAndConnectServiceInstanceShouldSetInventoriedStatusAndCallConnectMethod() {
82 org.onap.aai.domain.yang.Pnf pnfYang = new org.onap.aai.domain.yang.Pnf();
84 doReturn(pnfYang).when(aaiObjectMapperMock).mapPnf(pnf);
85 doReturn(aaiResourcesClientMock).when(aaiResourcesClientMock).createIfNotExists(any(AAIResourceUri.class),
86 eq(Optional.of(pnfYang)));
88 testedObject.createPnfAndConnectServiceInstance(pnf, serviceInstance);
90 assertEquals(OrchestrationStatus.INVENTORIED, pnf.getOrchestrationStatus());
91 verify(aaiResourcesClientMock, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
95 public void updateOrchestrationStatusPnfShouldSetStatusAndUpdatePnfInAAI() {
96 org.onap.aai.domain.yang.Pnf pnfYang = new org.onap.aai.domain.yang.Pnf();
97 doReturn(pnfYang).when(aaiObjectMapperMock).mapPnf(pnf);
99 testedObject.updateOrchestrationStatusPnf(pnf, OrchestrationStatus.ACTIVE);
101 assertEquals(OrchestrationStatus.ACTIVE, pnf.getOrchestrationStatus());
102 verify(aaiObjectMapperMock, times(1))
103 .mapPnf(argThat(arg -> OrchestrationStatus.ACTIVE.equals(arg.getOrchestrationStatus())));
104 verify(aaiResourcesClientMock, times(1)).update(any(AAIResourceUri.class), eq(pnfYang));
108 public void existingPnfInAaiWithInventoriedStatusCanBeUsed() throws Exception {
110 org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(OrchestrationStatus.INVENTORIED.toString());
111 when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
112 AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
114 testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
118 public void existingPnfInAaiWithNullStatusCanBeUsed() throws Exception {
120 org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(null);
121 when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
122 AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
124 testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
128 public void existingPnfInAaiWithEmptyStatusCanBeUsed() throws Exception {
130 org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(Strings.EMPTY);
131 when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
132 AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
134 testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
138 public void existingPnfInAaiCanNotBeUsed() {
140 org.onap.aai.domain.yang.Pnf pnfFromAai = createPnf(OrchestrationStatus.ACTIVE.toString());
141 when(injectionHelperMock.getAaiClient().get(org.onap.aai.domain.yang.Pnf.class,
142 AAIUriFactory.createResourceUri(AAIObjectType.PNF, PNF_NAME))).thenReturn(Optional.of(pnfFromAai));
145 testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
146 } catch (Exception e) {
148 assertThat(e.getMessage()).isEqualTo(String.format(
149 "pnf with name %s already exists with orchestration status Active, only status Inventoried allows to use existing pnf",
154 private org.onap.aai.domain.yang.Pnf createPnf(String orchestrationStatus) {
155 org.onap.aai.domain.yang.Pnf pnfFromAai = new org.onap.aai.domain.yang.Pnf();
156 pnfFromAai.setPnfName(PNF_NAME);
157 pnfFromAai.setOrchestrationStatus(orchestrationStatus);