[SDC-29] Amdocs OnBoard 1707 initial commit.
[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 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.vendorsoftwareproduct.dao.NetworkDao;
29 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
30 import org.openecomp.sdc.vendorsoftwareproduct.errors.VendorSoftwareProductErrorCodes;
31 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionEntityDataManager;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
36 import org.openecomp.sdc.versioning.dao.types.Version;
37 import org.openecomp.sdc.versioning.errors.VersioningErrorCodes;
38 import org.testng.Assert;
39 import org.testng.annotations.BeforeMethod;
40 import org.testng.annotations.Test;
41
42 import java.util.Arrays;
43 import java.util.Collection;
44
45 import static org.mockito.Matchers.anyObject;
46 import static org.mockito.Mockito.doReturn;
47 import static org.mockito.Mockito.never;
48 import static org.mockito.Mockito.verify;
49
50 public class NetworkManagerImplTest {
51
52   private static final String USER1 = "networksTestUser1";
53   private static final String USER2 = "networksTestUser2";
54   private static final String VSP_ID = "vsp";
55   private static final Version VERSION = new Version(0, 1);
56   private static final String NETWORK1_ID = "network1";
57   private static final String NETWORK2_ID = "network2";
58
59   @Mock
60   private NetworkDao networkDaoMock;
61   @Mock
62   private CompositionEntityDataManager compositionEntityDataManagerMock;
63   @InjectMocks
64   @Spy
65   private NetworkManagerImpl networkManager;
66
67   static NetworkEntity createNetwork(String vspId, Version version, String networkId) {
68     NetworkEntity networkEntity = new NetworkEntity(vspId, version, networkId);
69     Network networkData = new Network();
70     networkData.setName(networkId + " name");
71     networkData.setDhcp(true);
72     networkEntity.setNetworkCompositionData(networkData);
73     return networkEntity;
74   }
75
76   @BeforeMethod
77   public void setUp() throws Exception {
78     MockitoAnnotations.initMocks(this);
79   }
80
81   @Test
82   public void testListWhenNone() {
83     Collection<NetworkEntity> networks =
84         networkManager.listNetworks(VSP_ID, null, USER1);
85     Assert.assertEquals(networks.size(), 0);
86   }
87
88   @Test
89   public void testList() {
90     doReturn(Arrays.asList(
91         createNetwork(VSP_ID, VERSION, NETWORK1_ID),
92         createNetwork(VSP_ID, VERSION, NETWORK2_ID)))
93         .when(networkDaoMock).list(anyObject());
94
95     Collection<NetworkEntity> actual = networkManager.listNetworks(VSP_ID, VERSION, USER1);
96     Assert.assertEquals(actual.size(), 2);
97   }
98
99 /*    @Test(dependsOnMethods = "testListWhenNone")
100     public void testCreate() {
101         NETWORK1_ID = testCreate(VSP_ID);
102     }
103
104     private String testCreate(String vspId) {
105         NetworkEntity expected = new NetworkEntity(vspId, null, null);
106         Network networkData = new Network();
107         networkData.setName("network1 name");
108         networkData.setDhcp(true);
109         expected.setNetworkCompositionData(networkData);
110
111
112         NetworkEntity created = networkManager.createNetwork(expected, USER1);
113         Assert.assertNotNull(created);
114         expected.setId(created.getId());
115         expected.setVersion(VERSION01);
116
117         NetworkEntity actual = networkDaoMock.getNetwork(vspId, VERSION01, created.getId());
118
119         Assert.assertEquals(actual, expected);
120         return created.getId();
121     }
122
123     @Test(dependsOnMethods = {"testCreate"})
124     public void testCreateWithExistingName_negative() {
125         NetworkEntity network = new NetworkEntity(VSP_ID, null, null);
126         Network networkData = new Network();
127         networkData.setName("network1 name");
128         networkData.setDhcp(true);
129         network.setNetworkCompositionData(networkData);
130         testCreate_negative(network, USER1, UniqueValueUtil.UNIQUE_VALUE_VIOLATION);
131     }*/
132
133   @Test
134   public void testCreateOnUploadVsp_negative() {
135     testCreate_negative(new NetworkEntity(VSP_ID, VERSION, null), USER1,
136         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
137   }
138
139   /*    @Test(dependsOnMethods = {"testCreate"})
140       public void testCreateWithExistingNameUnderOtherVsp() {
141           testCreate(vsp2Id);
142       }
143   */
144
145   @Test
146   public void testUpdateNonExistingNetworkId_negative() {
147     testUpdate_negative(VSP_ID, VERSION, NETWORK1_ID, USER1,
148         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
149   }
150
151   @Test
152   public void testIllegalUpdateOnUploadVsp() {
153     doReturn(createNetwork(VSP_ID, VERSION, NETWORK1_ID))
154         .when(networkDaoMock).get(anyObject());
155
156     CompositionEntityValidationData toBeReturned =
157         new CompositionEntityValidationData(CompositionEntityType.network, NETWORK1_ID);
158     toBeReturned.setErrors(Arrays.asList("error1", "error2"));
159     doReturn(toBeReturned)
160         .when(compositionEntityDataManagerMock)
161         .validateEntity(anyObject(), anyObject(), anyObject());
162
163     NetworkEntity networkEntity = new NetworkEntity(VSP_ID, VERSION, NETWORK1_ID);
164     Network networkData = new Network();
165     networkData.setName(NETWORK1_ID + " name updated");
166     networkData.setDhcp(false);
167     networkEntity.setNetworkCompositionData(networkData);
168
169     CompositionEntityValidationData validationData =
170         networkManager.updateNetwork(networkEntity, USER1);
171     Assert.assertNotNull(validationData);
172     Assert.assertEquals(validationData.getErrors().size(), 2);
173
174     verify(networkDaoMock, never()).update(networkEntity);
175   }
176
177   @Test
178   public void testGetNonExistingNetworkId_negative() {
179     testGet_negative(VSP_ID, VERSION, NETWORK1_ID, USER1,
180         VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
181   }
182
183   @Test
184   public void testGet() {
185     NetworkEntity network = createNetwork(VSP_ID, VERSION, NETWORK1_ID);
186     doReturn(network)
187         .when(networkDaoMock).get(anyObject());
188     doReturn("schema string").when(networkManager).getCompositionSchema(anyObject());
189
190     CompositionEntityResponse<Network> response =
191         networkManager.getNetwork(VSP_ID, VERSION, NETWORK1_ID, USER1);
192     Assert.assertEquals(response.getId(), network.getId());
193     Assert.assertEquals(response.getData(), network.getNetworkCompositionData());
194     Assert.assertNotNull(response.getSchema());
195   }
196
197     /*
198            @Test(dependsOnMethods = {"testUpdateOnUploadVsp", "testList"})
199            public void testCreateWithERemovedName() {
200                testCreate(VSP_ID);
201            }
202
203     @Test(dependsOnMethods = "testList")
204     public void testDeleteNonExistingNetworkId_negative() {
205         testDelete_negative(VSP_ID, "non existing network id", USER1, VersioningErrorCodes.VERSIONABLE_SUB_ENTITY_NOT_FOUND);
206     }*/
207
208 /*
209            @Test(dependsOnMethods = "testList")
210            public void testDelete() {
211                networkManager.deleteNetwork(VSP_ID, NETWORK1_ID, USER1);
212                NetworkEntity actual = networkDaoMock.getNetwork(VSP_ID, VERSION01, NETWORK1_ID);
213                Assert.assertNull(actual);
214            }
215
216
217
218            @Test(dependsOnMethods = "testDelete")
219            public void testDeleteList() {
220                NetworkEntity network3 = new NetworkEntity(VSP_ID, null, null);
221                network3.setName("network3 name");
222                network3.setDescription("network3 desc");
223                networkManager.createNetwork(network3, USER1);
224
225                networkManager.deleteNetworks(VSP_ID, USER1);
226
227                Collection<NetworkEntity> actual = networkManager.listNetworks(VSP_ID, null, USER1);
228                Assert.assertEquals(actual.size(), 0);
229            }*/
230
231   @Test(dependsOnMethods = "testList")
232   public void testDeleteOnUploadVsp_negative() {
233     testDelete_negative(VSP_ID, VERSION, NETWORK1_ID, USER1,
234         VendorSoftwareProductErrorCodes.VSP_COMPOSITION_EDIT_NOT_ALLOWED);
235   }
236
237   private void testCreate_negative(NetworkEntity network, String user, String expectedErrorCode) {
238     try {
239       networkManager.createNetwork(network, user);
240       Assert.fail();
241     } catch (CoreException exception) {
242       Assert.assertEquals(exception.code().id(), expectedErrorCode);
243     }
244   }
245
246   private void testGet_negative(String vspId, Version version, String networkId, String user,
247                                 String expectedErrorCode) {
248     try {
249       networkManager.getNetwork(vspId, version, networkId, user);
250       Assert.fail();
251     } catch (CoreException exception) {
252       Assert.assertEquals(exception.code().id(), expectedErrorCode);
253     }
254   }
255
256   private void testUpdate_negative(String vspId, Version version, String networkId, String user,
257                                    String expectedErrorCode) {
258     try {
259       networkManager.updateNetwork(new NetworkEntity(vspId, version, networkId), user);
260       Assert.fail();
261     } catch (CoreException exception) {
262       Assert.assertEquals(exception.code().id(), expectedErrorCode);
263     }
264   }
265
266   private void testList_negative(String vspId, Version version, String user,
267                                  String expectedErrorCode) {
268     try {
269       networkManager.listNetworks(vspId, version, user);
270       Assert.fail();
271     } catch (CoreException exception) {
272       Assert.assertEquals(exception.code().id(), expectedErrorCode);
273     }
274   }
275
276   private void testDelete_negative(String vspId, Version version, String networkId, String user,
277                                    String expectedErrorCode) {
278     try {
279       networkManager.deleteNetwork(vspId, version, networkId, user);
280       Assert.fail();
281     } catch (CoreException exception) {
282       Assert.assertEquals(exception.code().id(), expectedErrorCode);
283     }
284   }
285 }