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;
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.services.composition.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;
48 import java.util.Arrays;
49 import java.util.Collection;
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;
56 public class NetworkManagerImplTest {
58 private final Logger log = (Logger) LoggerFactory.getLogger(this.getClass().getName());
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";
67 private NetworkDao networkDaoMock;
69 private CompositionEntityDataManager compositionEntityDataManagerMock;
71 private VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao;
75 private NetworkManagerImpl networkManager;
77 static NetworkEntity createNetwork(String vspId, Version version, String networkId) {
78 NetworkEntity networkEntity = new NetworkEntity(vspId, version, networkId);
79 Network networkData = new Network();
80 networkData.setName(networkId + " name");
81 networkData.setDhcp(true);
82 networkEntity.setNetworkCompositionData(networkData);
87 public void setUp() throws Exception {
88 MockitoAnnotations.initMocks(this);
89 SessionContextProviderFactory.getInstance().createInterface().create(USER_ID);
93 public void tearDown() {
94 networkManager = null;
95 SessionContextProviderFactory.getInstance().createInterface().close();
99 public void testListWhenNone() {
100 Collection<NetworkEntity> networks =
101 networkManager.listNetworks(VSP_ID, null);
102 Assert.assertEquals(networks.size(), 0);
106 public void testList() {
107 doReturn(Arrays.asList(
108 createNetwork(VSP_ID, VERSION, NETWORK1_ID),
109 createNetwork(VSP_ID, VERSION, NETWORK2_ID)))
110 .when(networkDaoMock).list(anyObject());
112 Collection<NetworkEntity> actual = networkManager.listNetworks(VSP_ID, VERSION);
113 Assert.assertEquals(actual.size(), 2);
117 public void testCreateOnUploadVsp_negative() {
118 testCreate_negative(new NetworkEntity(VSP_ID, VERSION, null),
119 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
123 public void testUpdateNonExistingNetworkId_negative() {
124 testUpdate_negative(VSP_ID, VERSION, NETWORK1_ID,
125 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
129 public void testIllegalUpdateOnUploadVsp() {
130 doReturn(createNetwork(VSP_ID, VERSION, NETWORK1_ID))
131 .when(networkDaoMock).get(anyObject());
133 CompositionEntityValidationData toBeReturned =
134 new CompositionEntityValidationData(CompositionEntityType.network, NETWORK1_ID);
135 toBeReturned.setErrors(Arrays.asList("error1", "error2"));
136 doReturn(toBeReturned)
137 .when(compositionEntityDataManagerMock)
138 .validateEntity(anyObject(), anyObject(), anyObject());
139 doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
141 NetworkEntity networkEntity = new NetworkEntity(VSP_ID, VERSION, NETWORK1_ID);
142 Network networkData = new Network();
143 networkData.setName(NETWORK1_ID + " name updated");
144 networkData.setDhcp(false);
145 networkEntity.setNetworkCompositionData(networkData);
147 CompositionEntityValidationData validationData =
148 networkManager.updateNetwork(networkEntity);
149 Assert.assertNotNull(validationData);
150 Assert.assertEquals(validationData.getErrors().size(), 2);
152 verify(networkDaoMock, never()).update(networkEntity);
156 public void testGetNonExistingNetworkId_negative() {
157 testGet_negative(VSP_ID, VERSION, NETWORK1_ID,
158 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
162 public void testGet() {
163 NetworkEntity network = createNetwork(VSP_ID, VERSION, NETWORK1_ID);
165 .when(networkDaoMock).get(anyObject());
166 doReturn("schema string").when(networkManager).getCompositionSchema(anyObject());
167 doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
169 CompositionEntityResponse<Network> response =
170 networkManager.getNetwork(VSP_ID, VERSION, NETWORK1_ID);
171 Assert.assertEquals(response.getId(), network.getId());
172 Assert.assertEquals(response.getData(), network.getNetworkCompositionData());
173 Assert.assertNotNull(response.getSchema());
176 @Test(dependsOnMethods = "testList")
177 public void testDeleteOnUploadVsp_negative() {
178 testDelete_negative(VSP_ID, VERSION, NETWORK1_ID,
179 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
182 private void testCreate_negative(NetworkEntity network, String expectedErrorCode) {
184 doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
185 networkManager.createNetwork(network);
187 } catch (CoreException exception) {
188 log.debug("", exception);
189 Assert.assertEquals(exception.code().id(), expectedErrorCode);
193 private void testGet_negative(String vspId, Version version, String networkId,
194 String expectedErrorCode) {
196 networkManager.getNetwork(vspId, version, networkId);
198 } catch (CoreException exception) {
199 log.debug("", exception);
200 Assert.assertEquals(exception.code().id(), expectedErrorCode);
204 private void testUpdate_negative(String vspId, Version version, String networkId,
205 String expectedErrorCode) {
207 networkManager.updateNetwork(new NetworkEntity(vspId, version, networkId));
209 } catch (CoreException exception) {
210 log.debug("", exception);
211 Assert.assertEquals(exception.code().id(), expectedErrorCode);
215 private void testList_negative(String vspId, Version version, String expectedErrorCode) {
217 networkManager.listNetworks(vspId, version);
219 } catch (CoreException exception) {
220 log.debug("", exception);
221 Assert.assertEquals(exception.code().id(), expectedErrorCode);
225 private void testDelete_negative(String vspId, Version version, String networkId,
226 String expectedErrorCode) {
228 doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
229 networkManager.deleteNetwork(vspId, version, networkId);
231 } catch (CoreException exception) {
232 log.debug("", exception);
233 Assert.assertEquals(exception.code().id(), expectedErrorCode);