db719d3151299ef167852dffe60e76b74e530a90
[so.git] / bpmn / so-bpmn-tasks / src / test / java / org / onap / so / client / orchestration / AAIVnfResourcesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 AT&T 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.Matchers.any;
25 import static org.mockito.Matchers.eq;
26 import static org.mockito.Matchers.isA;
27 import static org.mockito.Mockito.doNothing;
28 import static org.mockito.Mockito.doReturn;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31
32 import java.util.Optional;
33
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.runners.MockitoJUnitRunner;
40 import org.onap.so.bpmn.common.data.TestDataSetup;
41 import org.onap.so.bpmn.common.InjectionHelper;
42 import org.onap.so.bpmn.servicedecomposition.bbobjects.GenericVnf;
43 import org.onap.so.bpmn.servicedecomposition.bbobjects.LineOfBusiness;
44 import org.onap.so.bpmn.servicedecomposition.bbobjects.Platform;
45 import org.onap.so.bpmn.servicedecomposition.bbobjects.ServiceInstance;
46 import org.onap.so.client.aai.AAIResourcesClient;
47 import org.onap.so.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.class)
52 public class AAIVnfResourcesTest extends TestDataSetup {
53
54         private GenericVnf genericVnf;
55
56         private ServiceInstance serviceInstance;
57         @Mock
58         protected AAIResourcesClient MOCK_aaiResourcesClient;
59
60         @Mock
61         protected AAIObjectMapper MOCK_aaiObjectMapper;
62
63         @Mock
64         protected InjectionHelper MOCK_injectionHelper;
65         
66         @InjectMocks
67         AAIVnfResources aaiVnfResources = new AAIVnfResources();
68
69         @Before
70         public void before() {
71                 serviceInstance = buildServiceInstance();
72                 genericVnf = buildGenericVnf();
73                  doReturn(MOCK_aaiResourcesClient).when(MOCK_injectionHelper).getAaiClient();
74         }
75
76         @Test
77         public void createVnfandConnectServiceInstanceTest() {
78                 doReturn(new org.onap.aai.domain.yang.GenericVnf()).when(MOCK_aaiObjectMapper).mapVnf(genericVnf);
79                 doReturn(MOCK_aaiResourcesClient).when(MOCK_aaiResourcesClient).createIfNotExists(isA(AAIResourceUri.class), any(Optional.class));
80                 doNothing().when(MOCK_aaiResourcesClient).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
81                 genericVnf.setOrchestrationStatus(OrchestrationStatus.PRECREATED);
82
83                 aaiVnfResources.createVnfandConnectServiceInstance(genericVnf, serviceInstance);
84
85                 assertEquals(OrchestrationStatus.INVENTORIED, genericVnf.getOrchestrationStatus());
86                 verify(MOCK_aaiObjectMapper, times(1)).mapVnf(genericVnf);
87                 verify(MOCK_aaiResourcesClient, times(1)).createIfNotExists(any(AAIResourceUri.class), any(Optional.class));
88                 verify(MOCK_aaiResourcesClient, times(1)).connect(any(AAIResourceUri.class), any(AAIResourceUri.class));
89         }
90
91         @Test
92         public void createPlatformandConnectVnfTest() {
93                 Platform platform = new Platform();
94                 platform.setPlatformName("a123");
95                 doNothing().when(MOCK_aaiResourcesClient).connect(isA(AAIResourceUri.class),isA(AAIResourceUri.class));
96                 doReturn(MOCK_aaiResourcesClient).when(MOCK_aaiResourcesClient).createIfNotExists(isA(AAIResourceUri.class), any(Optional.class));
97                 aaiVnfResources.createPlatformandConnectVnf(platform, genericVnf);
98                 verify(MOCK_aaiResourcesClient, times(1)).connect(any(AAIResourceUri.class),isA(AAIResourceUri.class));
99         }
100         
101         @Test
102         public void createLineOfBusinessandConnectVnfTest() {
103                 LineOfBusiness lob = new LineOfBusiness();
104                 lob.setLineOfBusinessName("a123");
105                 doNothing().when(MOCK_aaiResourcesClient).connect(isA(AAIResourceUri.class),isA(AAIResourceUri.class));
106                 doReturn(MOCK_aaiResourcesClient).when(MOCK_aaiResourcesClient).createIfNotExists(isA(AAIResourceUri.class), any(Optional.class));
107                 aaiVnfResources.createLineOfBusinessandConnectVnf(lob, genericVnf);
108                 verify(MOCK_aaiResourcesClient, times(1)).connect(any(AAIResourceUri.class),isA(AAIResourceUri.class));
109         }
110         
111         @Test
112         public void deleteVnfTest() {
113                 doNothing().when(MOCK_aaiResourcesClient).delete(isA(AAIResourceUri.class));
114
115                 aaiVnfResources.deleteVnf(genericVnf);
116
117                 verify(MOCK_aaiResourcesClient, times(1)).delete(any(AAIResourceUri.class));
118         }
119
120         @Test
121         public void updateOrchestrationStatusVnfTest() {
122                 doNothing().when(MOCK_aaiResourcesClient).update(isA(AAIResourceUri.class), isA(org.onap.aai.domain.yang.Vnf.class));
123
124                 aaiVnfResources.updateOrchestrationStatusVnf(genericVnf, OrchestrationStatus.ACTIVE);
125
126                 verify(MOCK_aaiResourcesClient, times(1)).update(any(AAIResourceUri.class), any(org.onap.aai.domain.yang.Vnf.class));
127
128                 assertEquals(OrchestrationStatus.ACTIVE, genericVnf.getOrchestrationStatus());
129         }
130
131         @Test
132         public void updateObjectVnfTest() {
133                 doReturn(new org.onap.aai.domain.yang.GenericVnf()).when(MOCK_aaiObjectMapper).mapVnf(genericVnf);
134                 doNothing().when(MOCK_aaiResourcesClient).update(isA(AAIResourceUri.class), isA(org.onap.aai.domain.yang.GenericVnf.class));
135
136                 aaiVnfResources.updateObjectVnf(genericVnf);
137
138                 verify(MOCK_aaiObjectMapper, times(1)).mapVnf(genericVnf);
139                 verify(MOCK_aaiResourcesClient, times(1)).update(isA(AAIResourceUri.class), isA(org.onap.aai.domain.yang.GenericVnf.class));
140         }
141
142         @Test
143         public void getGenericVnfTest () {
144                 Optional<org.onap.aai.domain.yang.GenericVnf> vnf = Optional.of(new org.onap.aai.domain.yang.GenericVnf());
145                 vnf.get().setVnfId("vnfId");
146                 doReturn(vnf).when(MOCK_aaiResourcesClient).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
147                 aaiVnfResources.getGenericVnf("vnfId");
148                 verify(MOCK_aaiResourcesClient, times(1)).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
149         }
150
151         @Test
152         public void checkInMaintFlagTest () {
153                 Optional<org.onap.aai.domain.yang.GenericVnf> vnf = Optional.of(new org.onap.aai.domain.yang.GenericVnf());
154                 vnf.get().setVnfId("vnfId");
155                 vnf.get().setInMaint(true);
156                 doReturn(vnf).when(MOCK_aaiResourcesClient).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
157                 boolean inMaintFlag = aaiVnfResources.checkInMaintFlag("vnfId");
158                 verify(MOCK_aaiResourcesClient, times(1)).get(eq(org.onap.aai.domain.yang.GenericVnf.class),isA(AAIResourceUri.class));
159                 assertEquals(inMaintFlag, true);
160         }
161 }