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 java.util.ArrayList;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Mock;
25 import org.onap.aai.api.NetworkApi;
26 import org.onap.aai.model.Vnfc;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
29
30 import static junit.framework.TestCase.assertEquals;
31 import static org.mockito.Matchers.eq;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.AbstractManager.buildRelationshipData;
35 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.TestGenericVnfManager.assertRelation;
36 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getCloudOwner;
37 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getRegionName;
38 import static org.springframework.test.util.ReflectionTestUtils.setField;
39
40 public class TestVnfcManager extends TestBase {
41     private ArgumentCaptor<Vnfc> payload = ArgumentCaptor.forClass(Vnfc.class);
42
43     @Mock
44     private AAIRestApiProvider aaiRestApiProvider;
45     private VnfcManager vnfcManager;
46     @Mock
47     private NetworkApi networkApi;
48
49     @Before
50     public void init() {
51         vnfcManager = new VnfcManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
52         setField(VnfcManager.class, "logger", logger);
53         when(aaiRestApiProvider.getNetworkApi()).thenReturn(networkApi);
54     }
55
56     /**
57      * test update
58      */
59     @Test
60     public void testUpdate() throws Exception {
61         AffectedVnfc affectedVnfc = new AffectedVnfc();
62         affectedVnfc.setComputeResource(new ResourceHandle());
63         affectedVnfc.getComputeResource().setResourceId("serverProviderId");
64         affectedVnfc.setId("vnfcId");
65         Vnfc existingVnfc = new Vnfc();
66         existingVnfc.setRelationshipList(new ArrayList<>());
67         existingVnfc.getRelationshipList().add(GenericVnfManager.linkTo("any"));
68         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(existingVnfc));
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 delete
90      */
91     @Test
92     public void testDelete() 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.setResourceVersion("v3");
99         existingVnfc.setVnfcName("myVnfId_vnfcId");
100         when(networkApi.getNetworkVnfcsVnfc("myVnfId_vnfcId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(existingVnfc));
101         when(networkApi.deleteNetworkVnfcsVnfc("myVnfId_vnfcId", "v3")).thenReturn(VOID_OBSERVABLE.value());
102         //when
103         vnfcManager.delete(VNF_ID, affectedVnfc);
104         //verify
105         verify(networkApi).deleteNetworkVnfcsVnfc("myVnfId_vnfcId", "v3");
106         VOID_OBSERVABLE.assertCalled();
107     }
108
109     /**
110      * test inheritence
111      */
112     @Test
113     public void testInheritence() {
114         assertEquals(logger, vnfcManager.getLogger());
115     }
116
117
118 }