2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct.impl;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.MockitoAnnotations;
26 import org.mockito.Spy;
27 import org.openecomp.sdc.common.errors.CoreException;
28 import org.openecomp.sdc.logging.api.Logger;
29 import org.openecomp.sdc.logging.api.LoggerFactory;
30 import org.openecomp.sdc.vendorsoftwareproduct.dao.NetworkDao;
31 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
32 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
33 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
38 import org.openecomp.sdc.versioning.dao.types.Version;
39 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
40 import org.testng.Assert;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
44 import java.util.Arrays;
45 import java.util.Collection;
47 import static org.mockito.Matchers.anyObject;
48 import static org.mockito.Mockito.doReturn;
49 import static org.mockito.Mockito.never;
50 import static org.mockito.Mockito.verify;
52 public class NetworkManagerImplTest {
54 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
56 private static final String VSP_ID = "vsp";
57 private static final Version VERSION = new Version("version_id");
58 private static final String NETWORK1_ID = "network1";
59 private static final String NETWORK2_ID = "network2";
62 private NetworkDao networkDaoMock;
64 private CompositionEntityDataManager compositionEntityDataManagerMock;
67 private NetworkManagerImpl networkManager;
69 static NetworkEntity createNetwork(String vspId, Version version, String networkId) {
70 NetworkEntity networkEntity = new NetworkEntity(vspId, version, networkId);
71 Network networkData = new Network();
72 networkData.setName(networkId + " name");
73 networkData.setDhcp(true);
74 networkEntity.setNetworkCompositionData(networkData);
79 public void setUp() throws Exception {
80 MockitoAnnotations.initMocks(this);
84 public void testListWhenNone() {
85 Collection<NetworkEntity> networks =
86 networkManager.listNetworks(VSP_ID, null);
87 Assert.assertEquals(networks.size(), 0);
91 public void testList() {
92 doReturn(Arrays.asList(
93 createNetwork(VSP_ID, VERSION, NETWORK1_ID),
94 createNetwork(VSP_ID, VERSION, NETWORK2_ID)))
95 .when(networkDaoMock).list(anyObject());
97 Collection<NetworkEntity> actual = networkManager.listNetworks(VSP_ID, VERSION);
98 Assert.assertEquals(actual.size(), 2);
101 /* @Test(dependsOnMethods = "testListWhenNone")
102 public void testCreate() {
103 NETWORK1_ID = testCreate(VSP_ID);
106 private String testCreate(String vspId) {
107 NetworkEntity expected = new NetworkEntity(vspId, null, null);
108 Network networkData = new Network();
109 networkData.setName("network1 name");
110 networkData.setDhcp(true);
111 expected.setNetworkCompositionData(networkData);
114 NetworkEntity created = networkManager.createNetwork(expected);
115 Assert.assertNotNull(created);
116 expected.setId(created.getId());
117 expected.setVersion(VERSION01);
119 NetworkEntity actual = networkDaoMock.getNetwork(vspId, VERSION01, created.getId());
121 Assert.assertEquals(actual, expected);
122 return created.getId();
125 @Test(dependsOnMethods = {"testCreate"})
126 public void testCreateWithExistingName_negative() {
127 NetworkEntity network = new NetworkEntity(VSP_ID, null, null);
128 Network networkData = new Network();
129 networkData.setName("network1 name");
130 networkData.setDhcp(true);
131 network.setNetworkCompositionData(networkData);
132 testCreate_negative(network, UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
136 public void testCreateOnUploadVsp_negative() {
137 testCreate_negative(new NetworkEntity(VSP_ID, VERSION, null),
138 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
141 /* @Test(dependsOnMethods = {"testCreate"})
142 public void testCreateWithExistingNameUnderOtherVsp() {
148 public void testUpdateNonExistingNetworkId_negative() {
149 testUpdate_negative(VSP_ID, VERSION, NETWORK1_ID,
150 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
154 public void testIllegalUpdateOnUploadVsp() {
155 doReturn(createNetwork(VSP_ID, VERSION, NETWORK1_ID))
156 .when(networkDaoMock).get(anyObject());
158 CompositionEntityValidationData toBeReturned =
159 new CompositionEntityValidationData(CompositionEntityType.network, NETWORK1_ID);
160 toBeReturned.setErrors(Arrays.asList("error1", "error2"));
161 doReturn(toBeReturned)
162 .when(compositionEntityDataManagerMock)
163 .validateEntity(anyObject(), anyObject(), anyObject());
165 NetworkEntity networkEntity = new NetworkEntity(VSP_ID, VERSION, NETWORK1_ID);
166 Network networkData = new Network();
167 networkData.setName(NETWORK1_ID + " name updated");
168 networkData.setDhcp(false);
169 networkEntity.setNetworkCompositionData(networkData);
171 CompositionEntityValidationData validationData =
172 networkManager.updateNetwork(networkEntity);
173 Assert.assertNotNull(validationData);
174 Assert.assertEquals(validationData.getErrors().size(), 2);
176 verify(networkDaoMock, never()).update(networkEntity);
180 public void testGetNonExistingNetworkId_negative() {
181 testGet_negative(VSP_ID, VERSION, NETWORK1_ID,
182 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
186 public void testGet() {
187 NetworkEntity network = createNetwork(VSP_ID, VERSION, NETWORK1_ID);
189 .when(networkDaoMock).get(anyObject());
190 doReturn("schema string").when(networkManager).getCompositionSchema(anyObject());
192 CompositionEntityResponse<Network> response =
193 networkManager.getNetwork(VSP_ID, VERSION, NETWORK1_ID);
194 Assert.assertEquals(response.getId(), network.getId());
195 Assert.assertEquals(response.getData(), network.getNetworkCompositionData());
196 Assert.assertNotNull(response.getSchema());
200 @Test(dependsOnMethods = {"testUpdateOnUploadVsp", "testList"})
201 public void testCreateWithERemovedName() {
205 @Test(dependsOnMethods = "testList")
206 public void testDeleteNonExistingNetworkId_negative() {
207 testDelete_negative(VSP_ID, "non existing network id", VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
211 @Test(dependsOnMethods = "testList")
212 public void testDelete() {
213 networkManager.deleteNetwork(VSP_ID, NETWORK1_ID);
214 NetworkEntity actual = networkDaoMock.getNetwork(VSP_ID, VERSION01, NETWORK1_ID);
215 Assert.assertNull(actual);
220 @Test(dependsOnMethods = "testDelete")
221 public void testDeleteList() {
222 NetworkEntity network3 = new NetworkEntity(VSP_ID, null, null);
223 network3.setName("network3 name");
224 network3.setDescription("network3 desc");
225 networkManager.createNetwork(network3);
227 networkManager.deleteNetworks(VSP_ID);
229 Collection<NetworkEntity> actual = networkManager.listNetworks(VSP_ID, null);
230 Assert.assertEquals(actual.size(), 0);
233 @Test(dependsOnMethods = "testList")
234 public void testDeleteOnUploadVsp_negative() {
235 testDelete_negative(VSP_ID, VERSION, NETWORK1_ID,
236 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
239 private void testCreate_negative(NetworkEntity network, String expectedErrorCode) {
241 networkManager.createNetwork(network);
243 } catch (CoreException exception) {
244 log.debug("", exception);
245 Assert.assertEquals(exception.code().id(), expectedErrorCode);
249 private void testGet_negative(String vspId, Version version, String networkId,
250 String expectedErrorCode) {
252 networkManager.getNetwork(vspId, version, networkId);
254 } catch (CoreException exception) {
255 log.debug("", exception);
256 Assert.assertEquals(exception.code().id(), expectedErrorCode);
260 private void testUpdate_negative(String vspId, Version version, String networkId,
261 String expectedErrorCode) {
263 networkManager.updateNetwork(new NetworkEntity(vspId, version, networkId));
265 } catch (CoreException exception) {
266 log.debug("", exception);
267 Assert.assertEquals(exception.code().id(), expectedErrorCode);
271 private void testList_negative(String vspId, Version version, String expectedErrorCode) {
273 networkManager.listNetworks(vspId, version);
275 } catch (CoreException exception) {
276 log.debug("", exception);
277 Assert.assertEquals(exception.code().id(), expectedErrorCode);
281 private void testDelete_negative(String vspId, Version version, String networkId,
282 String expectedErrorCode) {
284 networkManager.deleteNetwork(vspId, version, networkId);
286 } catch (CoreException exception) {
287 log.debug("", exception);
288 Assert.assertEquals(exception.code().id(), expectedErrorCode);