Onboarding foundation changes
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / impl / NetworkManagerImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.openecomp.sdc.vendorsoftwareproduct.impl;
22
23
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.Spy;
28 import org.openecomp.sdc.common.errors.CoreException;
29 import org.openecomp.sdc.common.session.SessionContextProviderFactory;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDao;
33 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
35 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
36 import org.openecomp.sdc.vendorsoftwareproduct.CompositionEntityDataManager;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
43 import org.testng.Assert;
44 import org.testng.annotations.AfterMethod;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
47
48 import java.util.Arrays;
49 import java.util.Collection;
50
51 import static org.mockito.Matchers.anyObject;
52 import static org.mockito.Mockito.doReturn;
53 import static org.mockito.Mockito.never;
54 import static org.mockito.Mockito.verify;
55
56 public class NetworkManagerImplTest {
57
58   private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
59
60   private static final String VSP_ID = "vsp";
61   private static final String USER_ID = "test_user1";
62   private static final Version VERSION = new Version("version_id");
63   private static final String NETWORK1_ID = "network1";
64   private static final String NETWORK2_ID = "network2";
65   private static final String tenant = "dox";
66
67   @Mock
68   private NetworkDao networkDaoMock;
69   @Mock
70   private CompositionEntityDataManager compositionEntityDataManagerMock;
71   @Mock
72   private VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao;
73
74   @InjectMocks
75   @Spy
76   private NetworkManagerImpl networkManager;
77
78   static NetworkEntity createNetwork(String vspId, Version version, String networkId) {
79     NetworkEntity networkEntity = new NetworkEntity(vspId, version, networkId);
80     Network networkData = new Network();
81     networkData.setName(networkId + " name");
82     networkData.setDhcp(true);
83     networkEntity.setNetworkCompositionData(networkData);
84     return networkEntity;
85   }
86
87   @BeforeMethod
88   public void setUp() throws Exception {
89     MockitoAnnotations.initMocks(this);
90     SessionContextProviderFactory.getInstance().createInterface().create(USER_ID, tenant);
91   }
92
93   @AfterMethod
94   public void tearDown() {
95     networkManager = null;
96     SessionContextProviderFactory.getInstance().createInterface().close();
97   }
98
99   @Test
100   public void testListWhenNone() {
101     Collection<NetworkEntity> networks =
102         networkManager.listNetworks(VSP_ID, null);
103     Assert.assertEquals(networks.size(), 0);
104   }
105
106   @Test
107   public void testList() {
108     doReturn(Arrays.asList(
109         createNetwork(VSP_ID, VERSION, NETWORK1_ID),
110         createNetwork(VSP_ID, VERSION, NETWORK2_ID)))
111         .when(networkDaoMock).list(anyObject());
112
113     Collection<NetworkEntity> actual = networkManager.listNetworks(VSP_ID, VERSION);
114     Assert.assertEquals(actual.size(), 2);
115   }
116
117   @Test
118   public void testCreateOnUploadVsp_negative() {
119     testCreate_negative(new NetworkEntity(VSP_ID, VERSION, null),
120         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
121   }
122
123   @Test
124   public void testUpdateNonExistingNetworkId_negative() {
125     testUpdate_negative(VSP_ID, VERSION, NETWORK1_ID,
126         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
127   }
128
129   @Test
130   public void testIllegalUpdateOnUploadVsp() {
131     doReturn(createNetwork(VSP_ID, VERSION, NETWORK1_ID))
132         .when(networkDaoMock).get(anyObject());
133
134     CompositionEntityValidationData toBeReturned =
135         new CompositionEntityValidationData(CompositionEntityType.network, NETWORK1_ID);
136     toBeReturned.setErrors(Arrays.asList("error1", "error2"));
137     doReturn(toBeReturned)
138         .when(compositionEntityDataManagerMock)
139         .validateEntity(anyObject(), anyObject(), anyObject());
140     doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
141
142     NetworkEntity networkEntity = new NetworkEntity(VSP_ID, VERSION, NETWORK1_ID);
143     Network networkData = new Network();
144     networkData.setName(NETWORK1_ID + " name updated");
145     networkData.setDhcp(false);
146     networkEntity.setNetworkCompositionData(networkData);
147
148     CompositionEntityValidationData validationData =
149         networkManager.updateNetwork(networkEntity);
150     Assert.assertNotNull(validationData);
151     Assert.assertEquals(validationData.getErrors().size(), 2);
152
153     verify(networkDaoMock, never()).update(networkEntity);
154   }
155
156   @Test
157   public void testGetNonExistingNetworkId_negative() {
158     testGet_negative(VSP_ID, VERSION, NETWORK1_ID,
159         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
160   }
161
162   @Test
163   public void testGet() {
164     NetworkEntity network = createNetwork(VSP_ID, VERSION, NETWORK1_ID);
165     doReturn(network)
166         .when(networkDaoMock).get(anyObject());
167     doReturn("schema string").when(networkManager).getCompositionSchema(anyObject());
168     doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
169
170     CompositionEntityResponse<Network> response =
171         networkManager.getNetwork(VSP_ID, VERSION, NETWORK1_ID);
172     Assert.assertEquals(response.getId(), network.getId());
173     Assert.assertEquals(response.getData(), network.getNetworkCompositionData());
174     Assert.assertNotNull(response.getSchema());
175   }
176
177   @Test(dependsOnMethods = "testList")
178   public void testDeleteOnUploadVsp_negative() {
179     testDelete_negative(VSP_ID, VERSION, NETWORK1_ID,
180         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
181   }
182
183   private void testCreate_negative(NetworkEntity network, String expectedErrorCode) {
184     try {
185       doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
186       networkManager.createNetwork(network);
187       Assert.fail();
188     } catch (CoreException exception) {
189       log.debug("", exception);
190       Assert.assertEquals(exception.code().id(), expectedErrorCode);
191     }
192   }
193
194   private void testGet_negative(String vspId, Version version, String networkId,
195                                 String expectedErrorCode) {
196     try {
197       networkManager.getNetwork(vspId, version, networkId);
198       Assert.fail();
199     } catch (CoreException exception) {
200       log.debug("", exception);
201       Assert.assertEquals(exception.code().id(), expectedErrorCode);
202     }
203   }
204
205   private void testUpdate_negative(String vspId, Version version, String networkId,
206                                    String expectedErrorCode) {
207     try {
208       networkManager.updateNetwork(new NetworkEntity(vspId, version, networkId));
209       Assert.fail();
210     } catch (CoreException exception) {
211       log.debug("", exception);
212       Assert.assertEquals(exception.code().id(), expectedErrorCode);
213     }
214   }
215
216   private void testList_negative(String vspId, Version version, String expectedErrorCode) {
217     try {
218       networkManager.listNetworks(vspId, version);
219       Assert.fail();
220     } catch (CoreException exception) {
221       log.debug("", exception);
222       Assert.assertEquals(exception.code().id(), expectedErrorCode);
223     }
224   }
225
226   private void testDelete_negative(String vspId, Version version, String networkId,
227                                    String expectedErrorCode) {
228     try {
229       doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
230       networkManager.deleteNetwork(vspId, version, networkId);
231       Assert.fail();
232     } catch (CoreException exception) {
233       log.debug("", exception);
234       Assert.assertEquals(exception.code().id(), expectedErrorCode);
235     }
236   }
237 }