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.ComponentManager;
33 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManagerFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.NicManagerFactory;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
41 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
42 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicCreationResponseDto;
43 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicDto;
44 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicRequestDto;
45 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
46 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
47 import org.powermock.core.classloader.annotations.PrepareForTest;
48 import org.powermock.modules.junit4.PowerMockRunner;
50 import javax.ws.rs.core.Response;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.UUID;
55 import static org.mockito.MockitoAnnotations.initMocks;
56 import static org.powermock.api.mockito.PowerMockito.mockStatic;
57 import static org.powermock.api.mockito.PowerMockito.when;
59 @RunWith(PowerMockRunner.class)
60 @PrepareForTest({NicsImpl.class, ComponentManagerFactory.class, NicManagerFactory.class})
61 public class NicsImplTest {
63 private Logger logger = LoggerFactory.getLogger(NicsImplTest.class);
66 private NicManagerFactory nicManagerFactory;
69 private NicManager nicManager;
72 private ComponentManagerFactory componentManagerFactory;
75 private ComponentManager componentManager;
77 private final String vspId = UUID.randomUUID().toString();
78 private final String versionId = UUID.randomUUID().toString();
79 private final String componentId = "" + System.currentTimeMillis();
80 private final String nicId = "" + System.currentTimeMillis();
81 private final String user = "cs0008";
88 mockStatic(ComponentManagerFactory.class);
89 when(ComponentManagerFactory.getInstance()).thenReturn(componentManagerFactory);
90 when(componentManagerFactory.createInterface()).thenReturn(componentManager);
92 mockStatic(NicManagerFactory.class);
93 when(NicManagerFactory.getInstance()).thenReturn(nicManagerFactory);
94 when(nicManagerFactory.createInterface()).thenReturn(nicManager);
98 NicEntity e = new NicEntity();
99 e.setComponentId(componentId);
101 e.setCompositionData("{\"name\":\"nm\",\"description\":\"d\"}");
104 Collection<NicEntity> lst = Collections.singletonList(e);
105 when(nicManager.listNics(
106 ArgumentMatchers.eq(vspId),
107 ArgumentMatchers.any(),
108 ArgumentMatchers.eq(componentId))).thenReturn(lst);
110 when(nicManager.createNic(
111 ArgumentMatchers.any())).thenReturn(e);
113 CompositionEntityResponse<Nic> r = new CompositionEntityResponse<>();
115 when(nicManager.getNic(
116 ArgumentMatchers.eq(vspId),
117 ArgumentMatchers.any(),
118 ArgumentMatchers.eq(componentId),
119 ArgumentMatchers.eq(nicId))).thenReturn(r);
121 CompositionEntityType tpe = CompositionEntityType.component;
122 CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
123 when(nicManager.updateNic(
124 ArgumentMatchers.any())).thenReturn(data);
127 QuestionnaireResponse qr = new QuestionnaireResponse();
128 qr.setData("helloworld");
129 when(nicManager.getNicQuestionnaire(
130 ArgumentMatchers.eq(vspId),
131 ArgumentMatchers.any(),
132 ArgumentMatchers.eq(componentId),
133 ArgumentMatchers.eq(nicId))).thenReturn(qr);
136 } catch (Exception e) {
137 logger.error(e.getMessage(), e);
142 public void testList() {
143 NicsImpl bean = new NicsImpl();
145 Response rsp = bean.list(vspId, versionId, componentId, user);
146 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
147 Object e = rsp.getEntity();
148 Assert.assertNotNull(e);
149 @SuppressWarnings("unchecked")
150 GenericCollectionWrapper<NicDto> results = (GenericCollectionWrapper<NicDto>)e;
151 Assert.assertEquals("result length", 1, results.getListCount());
156 public void testCreate() {
158 NicRequestDto dto = new NicRequestDto();
159 dto.setDescription("hello");
161 dto.setNetworkDescription("nd");
162 dto.setNetworkId(nicId);
163 dto.setNetworkType("External");
165 NicsImpl bean = new NicsImpl();
166 Response rsp = bean.create(dto, vspId, versionId, componentId, user);
167 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
168 Object e = rsp.getEntity();
169 Assert.assertNotNull(e);
171 NicCreationResponseDto creationDto = (NicCreationResponseDto)e;
172 Assert.assertEquals(nicId, creationDto.getNicId());
173 } catch (ClassCastException ex) {
174 Assert.fail("unexpected class for DTO " + e.getClass().getName());
180 public void testDelete() {
181 NicsImpl bean = new NicsImpl();
182 Response rsp = bean.delete(vspId, versionId, componentId, nicId, user);
183 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
184 Assert.assertNull(rsp.getEntity());
189 public void testGet() {
190 NicsImpl bean = new NicsImpl();
191 Response rsp = bean.get(vspId, versionId, componentId, nicId, user);
192 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
193 Assert.assertNotNull(rsp.getEntity());
197 public void testUpdate() {
198 NicsImpl bean = new NicsImpl();
199 NicRequestDto dto = new NicRequestDto();
200 dto.setDescription("hello");
202 dto.setNetworkDescription("nd");
203 dto.setNetworkId(nicId);
204 dto.setNetworkType("External");
206 Response rsp = bean.update(dto, vspId, versionId, componentId, nicId, user);
207 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
208 Assert.assertNull(rsp.getEntity());
212 public void testGetQuestionaire() {
213 NicsImpl bean = new NicsImpl();
214 Response rsp = bean.getQuestionnaire(vspId, versionId, componentId, nicId, user);
215 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
217 QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
218 Assert.assertEquals("helloworld", dto.getData());
220 catch (Exception ex) {
221 logger.error("caught exception", ex);
222 Assert.fail(ex.getMessage());
228 public void testUpdateQuestionaire() {
229 NicsImpl bean = new NicsImpl();
230 Response rsp = bean.updateQuestionnaire("helloworld", vspId, versionId, componentId, nicId, user);
231 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
232 Assert.assertNull(rsp.getEntity());