f3cb2035da6aeb45b9322b45521e2f3a2dd5dce4
[sdc.git] /
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.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;
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
66   @Mock
67   private NetworkDao networkDaoMock;
68   @Mock
69   private CompositionEntityDataManager compositionEntityDataManagerMock;
70   @Mock
71   private VendorSoftwareProductInfoDao vendorSoftwareProductInfoDao;
72
73   @InjectMocks
74   @Spy
75   private NetworkManagerImpl networkManager;
76
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);
83     return networkEntity;
84   }
85
86   @BeforeMethod
87   public void setUp() throws Exception {
88     MockitoAnnotations.initMocks(this);
89     SessionContextProviderFactory.getInstance().createInterface().create(USER_ID);
90   }
91
92   @AfterMethod
93   public void tearDown() {
94     networkManager = null;
95     SessionContextProviderFactory.getInstance().createInterface().close();
96   }
97
98   @Test
99   public void testListWhenNone() {
100     Collection<NetworkEntity> networks =
101         networkManager.listNetworks(VSP_ID, null);
102     Assert.assertEquals(networks.size(), 0);
103   }
104
105   @Test
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());
111
112     Collection<NetworkEntity> actual = networkManager.listNetworks(VSP_ID, VERSION);
113     Assert.assertEquals(actual.size(), 2);
114   }
115
116   @Test
117   public void testCreateOnUploadVsp_negative() {
118     testCreate_negative(new NetworkEntity(VSP_ID, VERSION, null),
119         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
120   }
121
122   @Test
123   public void testUpdateNonExistingNetworkId_negative() {
124     testUpdate_negative(VSP_ID, VERSION, NETWORK1_ID,
125         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
126   }
127
128   @Test
129   public void testIllegalUpdateOnUploadVsp() {
130     doReturn(createNetwork(VSP_ID, VERSION, NETWORK1_ID))
131         .when(networkDaoMock).get(anyObject());
132
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());
140
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);
146
147     CompositionEntityValidationData validationData =
148         networkManager.updateNetwork(networkEntity);
149     Assert.assertNotNull(validationData);
150     Assert.assertEquals(validationData.getErrors().size(), 2);
151
152     verify(networkDaoMock, never()).update(networkEntity);
153   }
154
155   @Test
156   public void testGetNonExistingNetworkId_negative() {
157     testGet_negative(VSP_ID, VERSION, NETWORK1_ID,
158         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
159   }
160
161   @Test
162   public void testGet() {
163     NetworkEntity network = createNetwork(VSP_ID, VERSION, NETWORK1_ID);
164     doReturn(network)
165         .when(networkDaoMock).get(anyObject());
166     doReturn("schema string").when(networkManager).getCompositionSchema(anyObject());
167     doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
168
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());
174   }
175
176   @Test(dependsOnMethods = "testList")
177   public void testDeleteOnUploadVsp_negative() {
178     testDelete_negative(VSP_ID, VERSION, NETWORK1_ID,
179         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
180   }
181
182   private void testCreate_negative(NetworkEntity network, String expectedErrorCode) {
183     try {
184       doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
185       networkManager.createNetwork(network);
186       Assert.fail();
187     } catch (CoreException exception) {
188       log.debug("", exception);
189       Assert.assertEquals(exception.code().id(), expectedErrorCode);
190     }
191   }
192
193   private void testGet_negative(String vspId, Version version, String networkId,
194                                 String expectedErrorCode) {
195     try {
196       networkManager.getNetwork(vspId, version, networkId);
197       Assert.fail();
198     } catch (CoreException exception) {
199       log.debug("", exception);
200       Assert.assertEquals(exception.code().id(), expectedErrorCode);
201     }
202   }
203
204   private void testUpdate_negative(String vspId, Version version, String networkId,
205                                    String expectedErrorCode) {
206     try {
207       networkManager.updateNetwork(new NetworkEntity(vspId, version, networkId));
208       Assert.fail();
209     } catch (CoreException exception) {
210       log.debug("", exception);
211       Assert.assertEquals(exception.code().id(), expectedErrorCode);
212     }
213   }
214
215   private void testList_negative(String vspId, Version version, String expectedErrorCode) {
216     try {
217       networkManager.listNetworks(vspId, version);
218       Assert.fail();
219     } catch (CoreException exception) {
220       log.debug("", exception);
221       Assert.assertEquals(exception.code().id(), expectedErrorCode);
222     }
223   }
224
225   private void testDelete_negative(String vspId, Version version, String networkId,
226                                    String expectedErrorCode) {
227     try {
228       doReturn(false).when(vendorSoftwareProductInfoDao).isManual(anyObject(),anyObject());
229       networkManager.deleteNetwork(vspId, version, networkId);
230       Assert.fail();
231     } catch (CoreException exception) {
232       log.debug("", exception);
233       Assert.assertEquals(exception.code().id(), expectedErrorCode);
234     }
235   }
236 }