2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 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.sdcrests.vsp.rest.services;
23 import org.apache.http.HttpStatus;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.ArgumentMatchers;
29 import org.mockito.Mock;
30 import org.openecomp.sdc.logging.api.Logger;
31 import org.openecomp.sdc.logging.api.LoggerFactory;
32 import org.openecomp.sdc.vendorsoftwareproduct.NetworkManager;
33 import org.openecomp.sdc.vendorsoftwareproduct.NetworkManagerFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NetworkEntity;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Network;
39 import org.openecomp.sdc.versioning.dao.types.Version;
40 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NetworkDto;
41 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NetworkRequestDto;
42 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
43 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
44 import org.powermock.core.classloader.annotations.PrepareForTest;
45 import org.powermock.modules.junit4.PowerMockRunner;
47 import javax.ws.rs.core.Response;
48 import java.util.Collection;
49 import java.util.Collections;
50 import java.util.UUID;
52 import static org.mockito.MockitoAnnotations.initMocks;
53 import static org.powermock.api.mockito.PowerMockito.mockStatic;
54 import static org.powermock.api.mockito.PowerMockito.when;
56 @RunWith(PowerMockRunner.class)
57 @PrepareForTest({NetworksImpl.class, NetworkManagerFactory.class})
58 public class NetworksImplTest {
60 private Logger logger = LoggerFactory.getLogger(NetworksImplTest.class);
63 private NetworkManagerFactory networkManagerFactory;
66 private NetworkManager networkManager;
69 private final String vspId = UUID.randomUUID().toString();
70 private final String versionId = UUID.randomUUID().toString();
71 private final String networkId = "" + System.currentTimeMillis();
72 private final String user = "cs0008";
79 mockStatic(NetworkManagerFactory.class);
80 when(NetworkManagerFactory.getInstance()).thenReturn(networkManagerFactory);
81 when(networkManagerFactory.createInterface()).thenReturn(networkManager);
84 NetworkEntity e = new NetworkEntity();
87 e.setVersion(new Version(versionId));
88 e.setCompositionData("{\"name\":\"nm\",\"description\":\"d\"}");
91 Collection<NetworkEntity> lst = Collections.singletonList(e);
92 when(networkManager.listNetworks(
93 ArgumentMatchers.eq(vspId),
94 ArgumentMatchers.any())).thenReturn(lst);
96 when(networkManager.createNetwork(
97 ArgumentMatchers.any())).thenReturn(e);
99 CompositionEntityResponse<Network> r = new CompositionEntityResponse<>();
101 when(networkManager.getNetwork(
102 ArgumentMatchers.eq(vspId),
103 ArgumentMatchers.any(),
104 ArgumentMatchers.eq(networkId))).thenReturn(r);
106 CompositionEntityType tpe = CompositionEntityType.component;
107 CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
108 when(networkManager.updateNetwork(
109 ArgumentMatchers.any())).thenReturn(data);
112 } catch (Exception e) {
113 logger.error(e.getMessage(), e);
118 public void testList() {
119 NetworksImpl bean = new NetworksImpl();
121 Response rsp = bean.list(vspId, versionId, user);
122 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
123 Object e = rsp.getEntity();
124 Assert.assertNotNull(e);
125 @SuppressWarnings("unchecked")
126 GenericCollectionWrapper<NetworkDto> results = (GenericCollectionWrapper<NetworkDto>)e;
127 Assert.assertEquals("result length", 1, results.getListCount());
132 public void testCreate() {
134 NetworkRequestDto dto = new NetworkRequestDto();
138 NetworksImpl bean = new NetworksImpl();
139 Response rsp = bean.create(dto, vspId, versionId, user);
140 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
141 Object e = rsp.getEntity();
142 Assert.assertNotNull(e);
144 StringWrapperResponse s = (StringWrapperResponse)e;
145 Assert.assertEquals(networkId, s.getValue());
146 } catch (ClassCastException ex) {
147 Assert.fail("unexpected class for DTO " + e.getClass().getName());
153 public void testDelete() {
154 NetworksImpl bean = new NetworksImpl();
155 Response rsp = bean.delete(vspId, versionId, networkId, user);
156 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
157 Assert.assertNull(rsp.getEntity());
162 public void testGet() {
163 NetworksImpl bean = new NetworksImpl();
164 Response rsp = bean.get(vspId, versionId, networkId, user);
165 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
166 Assert.assertNotNull(rsp.getEntity());
170 public void testUpdate() {
171 NetworksImpl bean = new NetworksImpl();
172 NetworkRequestDto dto = new NetworkRequestDto();
173 Response rsp = bean.update(dto, vspId, versionId, networkId, user);
174 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
175 Assert.assertNull(rsp.getEntity());