215af9a19a0646431da6ca6ac17b0ed682e21612
[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.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.argThat;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.doReturn;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import java.util.Optional;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.so.bpmn.common.InjectionHelper;
38 import org.onap.so.bpmn.common.data.TestDataSetup;
39 import org.onap.so.bpmn.servicedecomposition.bbobjects.Pnf;
40 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
41 import org.onap.aaiclient.client.aai.AAIResourcesClient;
42 import org.onap.aaiclient.client.aai.entities.uri.AAIResourceUri;
43 import org.onap.so.client.aai.mapper.AAIObjectMapper;
44 import org.onap.so.db.catalog.beans.OrchestrationStatus;
45
46 @RunWith(MockitoJUnitRunner.Silent.class)
47 public class AAIPnfResourcesTest extends TestDataSetup {
48
49     private Pnf pnf;
50     private ServiceInstance serviceInstance;
51
52     @Mock
53     protected AAIObjectMapper aaiObjectMapperMock;
54
55     @Mock
56     protected InjectionHelper injectionHelperMock;
57
58     @Mock
59     protected AAIResourcesClient aaiResourcesClientMock;
60
61     @InjectMocks
62     AAIPnfResources aaiPnfResources = new AAIPnfResources();
63
64     @Before
65     public void setUp() {
66         pnf = buildPnf();
67         pnf.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
68         serviceInstance = buildServiceInstance();
69
70         doReturn(aaiResourcesClientMock).when(injectionHelperMock).getAaiClient();
71     }
72
73     @Test
74     public void createPnfAndConnectServiceInstanceShouldSetInventoriedStatusAndCallConnectMethod() {
75         org.onap.aai.domain.yang.Pnf pnfYang = new org.onap.aai.domain.yang.Pnf();
76
77         doReturn(pnfYang).when(aaiObjectMapperMock).mapPnf(pnf);
78         doReturn(aaiResourcesClientMock).when(aaiResourcesClientMock).createIfNotExists(any(AAIResourceUri.class),
79                 eq(Optional.of(pnfYang)));
80
81         aaiPnfResources.createPnfAndConnectServiceInstance(pnf, serviceInstance);
82
83         assertEquals(OrchestrationStatus.INVENTORIED, pnf.getOrchestrationStatus());
84         verify(aaiResourcesClientMock, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
85     }
86
87     @Test
88     public void updateOrchestrationStatusPnfShouldSetStatusAndUpdatePnfInAAI() {
89         org.onap.aai.domain.yang.Pnf pnfYang = new org.onap.aai.domain.yang.Pnf();
90         doReturn(pnfYang).when(aaiObjectMapperMock).mapPnf(pnf);
91
92         aaiPnfResources.updateOrchestrationStatusPnf(pnf, OrchestrationStatus.ACTIVE);
93
94         assertEquals(OrchestrationStatus.ACTIVE, pnf.getOrchestrationStatus());
95         verify(aaiObjectMapperMock, times(1))
96                 .mapPnf(argThat(arg -> OrchestrationStatus.ACTIVE.equals(arg.getOrchestrationStatus())));
97         verify(aaiResourcesClientMock, times(1)).update(any(AAIResourceUri.class), eq(pnfYang));
98     }
99
100 }