use existing pnf - logging improvement
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / client / orchestration / AAIPnfResourcesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
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
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.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.io.File;
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;
53
54 @RunWith(MockitoJUnitRunner.Silent.class)
55 public class AAIPnfResourcesTest extends TestDataSetup {
56
57     private static final String PNF_NAME = "pnfTest";
58
59     private Pnf pnf;
60     private ServiceInstance serviceInstance;
61
62     @Mock
63     protected AAIObjectMapper aaiObjectMapperMock;
64
65     @Mock
66     protected InjectionHelper injectionHelperMock;
67
68     @Mock
69     protected AAIResourcesClient aaiResourcesClientMock;
70
71     @InjectMocks
72     AAIPnfResources testedObject = new AAIPnfResources();
73
74     @Before
75     public void setUp() {
76         pnf = buildPnf();
77         pnf.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
78         serviceInstance = buildServiceInstance();
79
80         doReturn(aaiResourcesClientMock).when(injectionHelperMock).getAaiClient();
81     }
82
83     @Test
84     public void createPnfAndConnectServiceInstanceShouldSetInventoriedStatusAndCallConnectMethod() {
85         org.onap.aai.domain.yang.Pnf pnfYang = new org.onap.aai.domain.yang.Pnf();
86
87         doReturn(pnfYang).when(aaiObjectMapperMock).mapPnf(pnf);
88         doReturn(aaiResourcesClientMock).when(aaiResourcesClientMock).createIfNotExists(any(AAIResourceUri.class),
89                 eq(Optional.of(pnfYang)));
90
91         testedObject.createPnfAndConnectServiceInstance(pnf, serviceInstance);
92
93         assertEquals(OrchestrationStatus.INVENTORIED, pnf.getOrchestrationStatus());
94         verify(aaiResourcesClientMock, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
95     }
96
97     @Test
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);
101
102         testedObject.updateOrchestrationStatusPnf(pnf, OrchestrationStatus.ACTIVE);
103
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));
108     }
109
110     @Test
111     public void existingPnfInAaiWithInventoriedStatusCanBeUsed() throws Exception {
112         // given
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));
116         // when
117         testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
118     }
119
120     @Test
121     public void existingPnfInAaiWithNullStatusCanBeUsed() throws Exception {
122         // given
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));
126         // when
127         testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
128     }
129
130     @Test
131     public void existingPnfInAaiWithEmptyStatusCanBeUsed() throws Exception {
132         // given
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));
136         // when
137         testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
138     }
139
140     @Test
141     public void existingPnfInAaiCanNotBeUsed() {
142         // given
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));
146         // when
147         try {
148             testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
149         } catch (Exception e) {
150             // then
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",
154                     PNF_NAME));
155         }
156     }
157
158     @Test
159     public void existingPnfInAaiIsRelatedToService() throws IOException {
160         // given
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));
168         // when
169         try {
170             testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
171         } catch (Exception e) {
172             // then
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));
176         }
177     }
178
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);
183         return pnfFromAai;
184     }
185 }