possibility to use inventoried PNF instance in new service instance
[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.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;
50
51 @RunWith(MockitoJUnitRunner.Silent.class)
52 public class AAIPnfResourcesTest extends TestDataSetup {
53
54     private static final String PNF_NAME = "pnfTest";
55
56     private Pnf pnf;
57     private ServiceInstance serviceInstance;
58
59     @Mock
60     protected AAIObjectMapper aaiObjectMapperMock;
61
62     @Mock
63     protected InjectionHelper injectionHelperMock;
64
65     @Mock
66     protected AAIResourcesClient aaiResourcesClientMock;
67
68     @InjectMocks
69     AAIPnfResources testedObject = new AAIPnfResources();
70
71     @Before
72     public void setUp() {
73         pnf = buildPnf();
74         pnf.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
75         serviceInstance = buildServiceInstance();
76
77         doReturn(aaiResourcesClientMock).when(injectionHelperMock).getAaiClient();
78     }
79
80     @Test
81     public void createPnfAndConnectServiceInstanceShouldSetInventoriedStatusAndCallConnectMethod() {
82         org.onap.aai.domain.yang.Pnf pnfYang = new org.onap.aai.domain.yang.Pnf();
83
84         doReturn(pnfYang).when(aaiObjectMapperMock).mapPnf(pnf);
85         doReturn(aaiResourcesClientMock).when(aaiResourcesClientMock).createIfNotExists(any(AAIResourceUri.class),
86                 eq(Optional.of(pnfYang)));
87
88         testedObject.createPnfAndConnectServiceInstance(pnf, serviceInstance);
89
90         assertEquals(OrchestrationStatus.INVENTORIED, pnf.getOrchestrationStatus());
91         verify(aaiResourcesClientMock, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
92     }
93
94     @Test
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);
98
99         testedObject.updateOrchestrationStatusPnf(pnf, OrchestrationStatus.ACTIVE);
100
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));
105     }
106
107     @Test
108     public void existingPnfInAaiWithInventoriedStatusCanBeUsed() throws Exception {
109         // given
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));
113         // when
114         testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
115     }
116
117     @Test
118     public void existingPnfInAaiWithNullStatusCanBeUsed() throws Exception {
119         // given
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));
123         // when
124         testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
125     }
126
127     @Test
128     public void existingPnfInAaiWithEmptyStatusCanBeUsed() throws Exception {
129         // given
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));
133         // when
134         testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
135     }
136
137     @Test
138     public void existingPnfInAaiCanNotBeUsed() {
139         // given
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));
143         // when
144         try {
145             testedObject.checkIfPnfExistsInAaiAndCanBeUsed(PNF_NAME);
146         } catch (Exception e) {
147             // then
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",
150                     PNF_NAME));
151         }
152     }
153
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);
158         return pnfFromAai;
159     }
160 }