Fix sonar issues
[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     /**
60      * test create
61      */
62     @Test
63     public void testCreate() throws Exception {
64         AffectedVnfc affectedVnfc = new AffectedVnfc();
65         affectedVnfc.setComputeResource(new ResourceHandle());
66         affectedVnfc.getComputeResource().setResourceId("serverProviderId");
67         affectedVnfc.setId("vnfcId");
68         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(Observable.error(new NoSuchElementException()));
69         when(networkApi.createOrUpdateNetworkVnfcsVnfc(eq("myVnfId_vnfcId"), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
70         //when
71         vnfcManager.update(VIM_ID, "myTenantPrivderId", VNF_ID, affectedVnfc, true);
72         //verify
73         Vnfc vnfc = payload.getValue();
74         assertEquals("myVnfId_vnfcId", vnfc.getVnfcName());
75         assertEquals("vnfcId", vnfc.getNfcFunction());
76         assertEquals("vnfcId", vnfc.getNfcNamingCode());
77         assertRelation(payload.getValue().getRelationshipList(), "generic-vnf", buildRelationshipData("generic-vnf.vnf-id", VNF_ID));
78
79         assertRelation(vnfc.getRelationshipList(), "vserver",
80                 buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)),
81                 buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)),
82                 buildRelationshipData("tenant.tenant-id", "myTenantPrivderId"),
83                 buildRelationshipData("vserver.vserver-id", "serverProviderId"));
84         assertEquals(2, vnfc.getRelationshipList().size());
85         VOID_OBSERVABLE.assertCalled();
86     }
87
88     /**
89      * test update
90      */
91     @Test
92     public void testUpdate() throws Exception {
93         AffectedVnfc affectedVnfc = new AffectedVnfc();
94         affectedVnfc.setComputeResource(new ResourceHandle());
95         affectedVnfc.getComputeResource().setResourceId("serverProviderId");
96         affectedVnfc.setId("vnfcId");
97         Vnfc existingVnfc = new Vnfc();
98         existingVnfc.setRelationshipList(new ArrayList<>());
99         existingVnfc.getRelationshipList().add(GenericVnfManager.linkTo("any"));
100         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(existingVnfc));
101         when(networkApi.createOrUpdateNetworkVnfcsVnfc(eq("myVnfId_vnfcId"), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
102         //when
103         vnfcManager.update(VIM_ID, "myTenantPrivderId", VNF_ID, affectedVnfc, true);
104         //verify
105         Vnfc vnfc = payload.getValue();
106         assertEquals("myVnfId_vnfcId", vnfc.getVnfcName());
107         assertEquals("vnfcId", vnfc.getNfcFunction());
108         assertEquals("vnfcId", vnfc.getNfcNamingCode());
109         assertRelation(payload.getValue().getRelationshipList(), "generic-vnf", buildRelationshipData("generic-vnf.vnf-id", VNF_ID));
110
111         assertRelation(vnfc.getRelationshipList(), "vserver",
112                 buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)),
113                 buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)),
114                 buildRelationshipData("tenant.tenant-id", "myTenantPrivderId"),
115                 buildRelationshipData("vserver.vserver-id", "serverProviderId"));
116         assertEquals(2, vnfc.getRelationshipList().size());
117         VOID_OBSERVABLE.assertCalled();
118     }
119
120     /**
121      * test delete
122      */
123     @Test
124     public void testDelete() throws Exception {
125         AffectedVnfc affectedVnfc = new AffectedVnfc();
126         affectedVnfc.setComputeResource(new ResourceHandle());
127         affectedVnfc.getComputeResource().setResourceId("serverProviderId");
128         affectedVnfc.setId("vnfcId");
129         Vnfc existingVnfc = new Vnfc();
130         existingVnfc.setResourceVersion("v3");
131         existingVnfc.setVnfcName("myVnfId_vnfcId");
132         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(existingVnfc));
133         when(networkApi.deleteNetworkVnfcsVnfc("myVnfId_vnfcId", "v3")).thenReturn(VOID_OBSERVABLE.value());
134         //when
135         vnfcManager.delete(VNF_ID, affectedVnfc);
136         //verify
137         verify(networkApi).deleteNetworkVnfcsVnfc("myVnfId_vnfcId", "v3");
138         VOID_OBSERVABLE.assertCalled();
139     }
140
141     /**
142      * test inheritence
143      */
144     @Test
145     public void testInheritence() {
146         assertEquals(logger, vnfcManager.getLogger());
147     }
148
149
150 }