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 / TestL3NetworkManager.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.google.gson.JsonObject;
19 import com.nokia.cbam.lcm.v32.model.AffectedVirtualLink;
20 import com.nokia.cbam.lcm.v32.model.ResourceHandle;
21 import java.util.ArrayList;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.Mock;
26 import org.onap.aai.api.NetworkApi;
27 import org.onap.aai.model.L3Network;
28 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.AAIRestApiProvider;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
30
31 import static junit.framework.TestCase.assertEquals;
32 import static junit.framework.TestCase.assertFalse;
33 import static org.mockito.Matchers.eq;
34 import static org.mockito.Mockito.when;
35 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.AbstractManager.buildRelationshipData;
36 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.direct.notification.TestGenericVnfManager.assertRelation;
37 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getCloudOwner;
38 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.LifecycleManager.getRegionName;
39 import static org.springframework.test.util.ReflectionTestUtils.setField;
40
41 public class TestL3NetworkManager extends TestBase {
42     private ArgumentCaptor<L3Network> payload = ArgumentCaptor.forClass(L3Network.class);
43     private AffectedVirtualLink affectedVirtualLink = new AffectedVirtualLink();
44     @Mock
45     private AAIRestApiProvider aaiRestApiProvider;
46     private L3NetworkManager l3NetworkManager;
47     @Mock
48     private NetworkApi networkApi;
49
50     @Before
51     public void init() {
52         l3NetworkManager = new L3NetworkManager(aaiRestApiProvider, cbamRestApiProvider, driverProperties);
53         setField(L3NetworkManager.class, "logger", logger);
54         when(aaiRestApiProvider.getNetworkApi()).thenReturn(networkApi);
55     }
56
57     /**
58      * test L3 network creation
59      */
60     @Test
61     public void testUpdate() throws Exception {
62         affectedVirtualLink.setId("vlId");
63         JsonObject additionalData = new JsonObject();
64         additionalData.addProperty("name", "netName");
65         additionalData.addProperty("tenantId", "myTenantId");
66         affectedVirtualLink.setResource(new ResourceHandle());
67         affectedVirtualLink.getResource().setAdditionalData(additionalData);
68         affectedVirtualLink.getResource().setResourceId("netProviderId");
69         L3Network existingNetwork = new L3Network();
70         when(networkApi.getNetworkL3NetworksL3Network("myVnfId_vlId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(existingNetwork));
71         when(networkApi.createOrUpdateNetworkL3NetworksL3Network(eq("myVnfId_vlId"), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
72         //when
73         l3NetworkManager.update(VIM_ID, VNF_ID, affectedVirtualLink);
74         //verify
75         assertEquals("myVnfId_vlId", payload.getValue().getNetworkId());
76         assertEquals("netName", payload.getValue().getNetworkName());
77         assertEquals("netProviderId", payload.getValue().getNeutronNetworkId());
78         assertFalse(payload.getValue().isIsBoundToVpn());
79         assertFalse(payload.getValue().isIsProviderNetwork());
80         assertFalse(payload.getValue().isIsSharedNetwork());
81         assertFalse(payload.getValue().isIsExternalNetwork());
82         assertEquals("active", payload.getValue().getOperationalStatus());
83         assertRelation(payload.getValue().getRelationshipList(), "cloud-region", buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)), buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)));
84         assertRelation(payload.getValue().getRelationshipList(), "tenant", buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)), buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)), buildRelationshipData("tenant.tenant-id", "myTenantId"));
85         assertRelation(payload.getValue().getRelationshipList(), "generic-vnf", buildRelationshipData("generic-vnf.vnf-id", VNF_ID));
86         VOID_OBSERVABLE.assertCalled();
87     }
88
89     /**
90      * Test existing resource update
91      */
92     @Test
93     public void testExistingUpdate() throws Exception {
94         affectedVirtualLink.setId("vlId");
95         JsonObject additionalData = new JsonObject();
96         additionalData.addProperty("name", "netName");
97         additionalData.addProperty("tenantId", "myTenantId");
98         affectedVirtualLink.setResource(new ResourceHandle());
99         affectedVirtualLink.getResource().setAdditionalData(additionalData);
100         affectedVirtualLink.getResource().setResourceId("netProviderId");
101         L3Network l3Network = new L3Network();
102         l3Network.setResourceVersion("v3");
103         l3Network.setRelationshipList(new ArrayList<>());
104         when(networkApi.getNetworkL3NetworksL3Network("myVnfId_vlId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(l3Network));
105         when(networkApi.createOrUpdateNetworkL3NetworksL3Network(eq("myVnfId_vlId"), payload.capture())).thenReturn(VOID_OBSERVABLE.value());
106         //when
107         l3NetworkManager.update(VIM_ID, VNF_ID, affectedVirtualLink);
108         //verify
109         assertEquals("myVnfId_vlId", payload.getValue().getNetworkId());
110         assertEquals("netName", payload.getValue().getNetworkName());
111         assertEquals("netProviderId", payload.getValue().getNeutronNetworkId());
112         assertFalse(payload.getValue().isIsBoundToVpn());
113         assertFalse(payload.getValue().isIsProviderNetwork());
114         assertFalse(payload.getValue().isIsSharedNetwork());
115         assertFalse(payload.getValue().isIsExternalNetwork());
116         assertEquals("active", payload.getValue().getOperationalStatus());
117         assertEquals("v3", payload.getValue().getResourceVersion());
118         assertRelation(payload.getValue().getRelationshipList(), "cloud-region", buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)), buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)));
119         assertRelation(payload.getValue().getRelationshipList(), "tenant", buildRelationshipData("cloud-region.cloud-owner", getCloudOwner(VIM_ID)), buildRelationshipData("cloud-region.cloud-region-id", getRegionName(VIM_ID)), buildRelationshipData("tenant.tenant-id", "myTenantId"));
120         assertRelation(payload.getValue().getRelationshipList(), "generic-vnf", buildRelationshipData("generic-vnf.vnf-id", VNF_ID));
121         VOID_OBSERVABLE.assertCalled();
122     }
123
124     /**
125      * test L3 network deletion
126      */
127     @Test
128     public void testDelete() throws Exception {
129         affectedVirtualLink.setId("vlId");
130         L3Network l3Network = new L3Network();
131         l3Network.setResourceVersion("v3");
132         l3Network.setNetworkId("myVnfId_vlId");
133         when(networkApi.getNetworkL3NetworksL3Network("myVnfId_vlId", null, null, null, null, null, null, null, null, null)).thenReturn(buildObservable(l3Network));
134         when(networkApi.deleteNetworkL3NetworksL3Network("myVnfId_vlId", "v3")).thenReturn(VOID_OBSERVABLE.value());
135         //when
136         l3NetworkManager.delete(VNF_ID, affectedVirtualLink);
137         //verify
138         networkApi.deleteNetworkL3NetworksL3Network("myVnfId_vlId", "v3");
139         VOID_OBSERVABLE.assertCalled();
140     }
141
142     /**
143      * test inheritence
144      */
145     @Test
146     public void testInheritence() {
147         assertEquals(logger, l3NetworkManager.getLogger());
148     }
149
150
151 }