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.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";
65 private static final String tenant = "dox";
68 private NetworkDao networkDaoMock;
70 private CompositionEntityDataManager compositionEntityDataManagerMock;
72 private VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao;
76 private NetworkManagerImpl networkManager;
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);
88 public void setUp() throws Exception {
89 MockitoAnnotations.initMocks(this);
90 SessionContextProviderFactory.getInstance().createInterface().create(USER_ID, tenant);
94 public void tearDown() {
95 networkManager = null;
96 SessionContextProviderFactory.getInstance().createInterface().close();
100 public void testListWhenNone() {
101 Collection<NetworkEntity> networks =
102 networkManager.listNetworks(VSP_ID, null);
103 Assert.assertEquals(networks.size(), 0);
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());
113 Collection<NetworkEntity> actual = networkManager.listNetworks(VSP_ID, VERSION);
114 Assert.assertEquals(actual.size(), 2);
118 public void testCreateOnUploadVsp_negative() {
119 testCreate_negative(new NetworkEntity(VSP_ID, VERSION, null),
120 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
124 public void testUpdateNonExistingNetworkId_negative() {
125 testUpdate_negative(VSP_ID, VERSION, NETWORK1_ID,
126 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
130 public void testIllegalUpdateOnUploadVsp() {
131 doReturn(createNetwork(VSP_ID, VERSION, NETWORK1_ID))
132 .when(networkDaoMock).get(anyObject());
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());
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);
148 CompositionEntityValidationData validationData =
149 networkManager.updateNetwork(networkEntity);
150 Assert.assertNotNull(validationData);
151 Assert.assertEquals(validationData.getErrors().size(), 2);
153 verify(networkDaoMock, never()).update(networkEntity);
157 public void testGetNonExistingNetworkId_negative() {
158 testGet_negative(VSP_ID, VERSION, NETWORK1_ID,
159 VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
163 public void testGet() {
164 NetworkEntity network = createNetwork(VSP_ID, VERSION, NETWORK1_ID);
166 .when(networkDaoMock).get(anyObject());
167 doReturn("schema string").when(networkManager).getCompositionSchema(anyObject());
168 doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
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());
177 @Test(dependsOnMethods = "testList")
178 public void testDeleteOnUploadVsp_negative() {
179 testDelete_negative(VSP_ID, VERSION, NETWORK1_ID,
180 VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
183 private void testCreate_negative(NetworkEntity network, String expectedErrorCode) {
185 doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
186 networkManager.createNetwork(network);
188 } catch (CoreException exception) {
189 log.debug("", exception);
190 Assert.assertEquals(exception.code().id(), expectedErrorCode);
194 private void testGet_negative(String vspId, Version version, String networkId,
195 String expectedErrorCode) {
197 networkManager.getNetwork(vspId, version, networkId);
199 } catch (CoreException exception) {
200 log.debug("", exception);
201 Assert.assertEquals(exception.code().id(), expectedErrorCode);
205 private void testUpdate_negative(String vspId, Version version, String networkId,
206 String expectedErrorCode) {
208 networkManager.updateNetwork(new NetworkEntity(vspId, version, networkId));
210 } catch (CoreException exception) {
211 log.debug("", exception);
212 Assert.assertEquals(exception.code().id(), expectedErrorCode);
216 private void testList_negative(String vspId, Version version, String expectedErrorCode) {
218 networkManager.listNetworks(vspId, version);
220 } catch (CoreException exception) {
221 log.debug("", exception);
222 Assert.assertEquals(exception.code().id(), expectedErrorCode);
226 private void testDelete_negative(String vspId, Version version, String networkId,
227 String expectedErrorCode) {
229 doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
230 networkManager.deleteNetwork(vspId, version, networkId);
232 } catch (CoreException exception) {
233 log.debug("", exception);
234 Assert.assertEquals(exception.code().id(), expectedErrorCode);