4c38211324bda866922120b1d4f15a77a418793c
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / direct / notification / TestVnfcManager.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification;
17
18 import com.nokia.cbam.lcm.v32.model.AffectedVnfc;
19 import com.nokia.cbam.lcm.v32.model.ResourceHandle;
20 import io.reactivex.Observable;
21 import java.util.ArrayList;
22 import java.util.NoSuchElementException;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.mockito.Mock;
27 import org.onap.aai.api.NetworkApi;
28 import org.onap.aai.model.Vnfc;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
31
32 import static junit.framework.TestCase.assertEquals;
33 import static org.mockito.Matchers.eq;
34 import static org.mockito.Mockito.verify;
35 import static org.mockito.Mockito.when;
36 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.AbstractManager.buildRelationshipData;
37 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.TestGenericVnfManager.assertRelation;
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getCloudOwner;
39 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getRegionName;
40 import static org.springframework.test.util.ReflectionTestUtils.setField;
41
42 public class TestVnfcManager extends TestBase {
43     private ArgumentCaptor<Vnfc> payload = ArgumentCaptor.forClass(Vnfc.class);
44
45     @Mock
46     private AAIRestApiProvider aaiRestApiProvider;
47     private VnfcManager vnfcManager;
48     @Mock
49     private NetworkApi networkApi;
50
51     @Before
52     public void init() {
53         vnfcManager = new VnfcManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
54         setField(VnfcManager.class, "logger", logger);
55         when(aaiRestApiProvider.getNetworkApi()).thenReturn(networkApi);
56     }
57
58     /**
59      * test create
60      */
61     @Test
62     public void testCreate() throws Exception {
63         AffectedVnfc affectedVnfc = new AffectedVnfc();
64         affectedVnfc.setComputeResource(new ResourceHandle());
65         affectedVnfc.getComputeResource().setResourceId("serverProviderId");
66         affectedVnfc.setId("vnfcId");
67         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(Observable.error(new NoSuchElementException()));
68         when(networkApi.createOrUpdateNetworkVnfcsVnfc(eq("myVnfId_vnfcId"), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
69         //when
70         vnfcManager.update(VIM_ID, "myTenantPrivderId", VNF_ID, affectedVnfc, true);
71         //verify
72         Vnfc vnfc = payload.getValue();
73         assertEquals("myVnfId_vnfcId", vnfc.getVnfcName());
74         assertEquals("vnfcId", vnfc.getNfcFunction());
75         assertEquals("vnfcId", vnfc.getNfcNamingCode());
76         assertRelation(payload.getValue().getRelationshipList(), "generic-vnf", buildRelationshipData("generic-vnf.vnf-id", VNF_ID));
77
78         assertRelation(vnfc.getRelationshipList(), "vserver",
79                 buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)),
80                 buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)),
81                 buildRelationshipData("tenant.tenant-id", "myTenantPrivderId"),
82                 buildRelationshipData("vserver.vserver-id", "serverProviderId"));
83         assertEquals(2, vnfc.getRelationshipList().size());
84         VOID_OBSERVABLE.assertCalled();
85     }
86
87     /**
88      * test update
89      */
90     @Test
91     public void testUpdate() throws Exception {
92         AffectedVnfc affectedVnfc = new AffectedVnfc();
93         affectedVnfc.setComputeResource(new ResourceHandle());
94         affectedVnfc.getComputeResource().setResourceId("serverProviderId");
95         affectedVnfc.setId("vnfcId");
96         Vnfc existingVnfc = new Vnfc();
97         existingVnfc.setRelationshipList(new ArrayList<>());
98         existingVnfc.getRelationshipList().add(GenericVnfManager.linkTo("any"));
99         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(existingVnfc));
100         when(networkApi.createOrUpdateNetworkVnfcsVnfc(eq("myVnfId_vnfcId"), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
101         //when
102         vnfcManager.update(VIM_ID, "myTenantPrivderId", VNF_ID, affectedVnfc, true);
103         //verify
104         Vnfc vnfc = payload.getValue();
105         assertEquals("myVnfId_vnfcId", vnfc.getVnfcName());
106         assertEquals("vnfcId", vnfc.getNfcFunction());
107         assertEquals("vnfcId", vnfc.getNfcNamingCode());
108         assertRelation(payload.getValue().getRelationshipList(), "generic-vnf", buildRelationshipData("generic-vnf.vnf-id", VNF_ID));
109
110         assertRelation(vnfc.getRelationshipList(), "vserver",
111                 buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)),
112                 buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)),
113                 buildRelationshipData("tenant.tenant-id", "myTenantPrivderId"),
114                 buildRelationshipData("vserver.vserver-id", "serverProviderId"));
115         assertEquals(2, vnfc.getRelationshipList().size());
116         VOID_OBSERVABLE.assertCalled();
117     }
118
119     /**
120      * test delete
121      */
122     @Test
123     public void testDelete() throws Exception {
124         AffectedVnfc affectedVnfc = new AffectedVnfc();
125         affectedVnfc.setComputeResource(new ResourceHandle());
126         affectedVnfc.getComputeResource().setResourceId("serverProviderId");
127         affectedVnfc.setId("vnfcId");
128         Vnfc existingVnfc = new Vnfc();
129         existingVnfc.setResourceVersion("v3");
130         existingVnfc.setVnfcName("myVnfId_vnfcId");
131         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(existingVnfc));
132         when(networkApi.deleteNetworkVnfcsVnfc("myVnfId_vnfcId", "v3")).thenReturn(VOID_OBSERVABLE.value());
133         //when
134         vnfcManager.delete(VNF_ID, affectedVnfc);
135         //verify
136         verify(networkApi).deleteNetworkVnfcsVnfc("myVnfId_vnfcId", "v3");
137         VOID_OBSERVABLE.assertCalled();
138     }
139
140     /**
141      * test VNFC id conversion
142      */
143     @Test
144     public void testCbamId() {
145         assertEquals("b", VnfcManager.buildCbamId("a_b"));
146     }
147
148
149     /**
150      * test inheritence
151      */
152     @Test
153     public void testInheritence() {
154         assertEquals(logger, vnfcManager.getLogger());
155     }
156
157
158 }