1 package org.openecomp.sdcrests.vsp.rest.services;
3 import org.apache.http.HttpStatus;
4 import org.junit.Assert;
5 import org.junit.Before;
7 import org.junit.runner.RunWith;
8 import org.mockito.ArgumentMatchers;
9 import org.mockito.Mock;
10 import org.openecomp.sdc.logging.api.Logger;
11 import org.openecomp.sdc.logging.api.LoggerFactory;
12 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
13 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManagerFactory;
14 import org.openecomp.sdc.vendorsoftwareproduct.NicManager;
15 import org.openecomp.sdc.vendorsoftwareproduct.NicManagerFactory;
16 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.NicEntity;
17 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
18 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
19 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
20 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
21 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.Nic;
22 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicCreationResponseDto;
23 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicDto;
24 import org.openecomp.sdcrests.vendorsoftwareproducts.types.NicRequestDto;
25 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
26 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
27 import org.powermock.core.classloader.annotations.PrepareForTest;
28 import org.powermock.modules.junit4.PowerMockRunner;
30 import javax.ws.rs.core.Response;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.UUID;
35 import static org.mockito.MockitoAnnotations.initMocks;
36 import static org.powermock.api.mockito.PowerMockito.mockStatic;
37 import static org.powermock.api.mockito.PowerMockito.when;
39 @RunWith(PowerMockRunner.class)
40 @PrepareForTest({NicsImpl.class, ComponentManagerFactory.class, NicManagerFactory.class})
41 public class NicsImplTest {
43 private Logger logger = LoggerFactory.getLogger(NicsImplTest.class);
46 private NicManagerFactory nicManagerFactory;
49 private NicManager nicManager;
52 private ComponentManagerFactory componentManagerFactory;
55 private ComponentManager componentManager;
57 private final String vspId = UUID.randomUUID().toString();
58 private final String versionId = UUID.randomUUID().toString();
59 private final String componentId = "" + System.currentTimeMillis();
60 private final String nicId = "" + System.currentTimeMillis();
61 private final String user = "cs0008";
68 mockStatic(ComponentManagerFactory.class);
69 when(ComponentManagerFactory.getInstance()).thenReturn(componentManagerFactory);
70 when(componentManagerFactory.createInterface()).thenReturn(componentManager);
72 mockStatic(NicManagerFactory.class);
73 when(NicManagerFactory.getInstance()).thenReturn(nicManagerFactory);
74 when(nicManagerFactory.createInterface()).thenReturn(nicManager);
78 NicEntity e = new NicEntity();
79 e.setComponentId(componentId);
81 e.setCompositionData("{\"name\":\"nm\",\"description\":\"d\"}");
84 Collection<NicEntity> lst = Collections.singletonList(e);
85 when(nicManager.listNics(
86 ArgumentMatchers.eq(vspId),
87 ArgumentMatchers.any(),
88 ArgumentMatchers.eq(componentId))).thenReturn(lst);
90 when(nicManager.createNic(
91 ArgumentMatchers.any())).thenReturn(e);
93 CompositionEntityResponse<Nic> r = new CompositionEntityResponse<>();
95 when(nicManager.getNic(
96 ArgumentMatchers.eq(vspId),
97 ArgumentMatchers.any(),
98 ArgumentMatchers.eq(componentId),
99 ArgumentMatchers.eq(nicId))).thenReturn(r);
101 CompositionEntityType tpe = CompositionEntityType.component;
102 CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
103 when(nicManager.updateNic(
104 ArgumentMatchers.any())).thenReturn(data);
107 QuestionnaireResponse qr = new QuestionnaireResponse();
108 qr.setData("helloworld");
109 when(nicManager.getNicQuestionnaire(
110 ArgumentMatchers.eq(vspId),
111 ArgumentMatchers.any(),
112 ArgumentMatchers.eq(componentId),
113 ArgumentMatchers.eq(nicId))).thenReturn(qr);
116 } catch (Exception e) {
117 logger.error(e.getMessage(), e);
122 public void testList() {
123 NicsImpl bean = new NicsImpl();
125 Response rsp = bean.list(vspId, versionId, componentId, user);
126 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
127 Object e = rsp.getEntity();
128 Assert.assertNotNull(e);
129 @SuppressWarnings("unchecked")
130 GenericCollectionWrapper<NicDto> results = (GenericCollectionWrapper<NicDto>)e;
131 Assert.assertEquals("result length", 1, results.getListCount());
136 public void testCreate() {
138 NicRequestDto dto = new NicRequestDto();
139 dto.setDescription("hello");
141 dto.setNetworkDescription("nd");
142 dto.setNetworkId(nicId);
143 dto.setNetworkType("External");
145 NicsImpl bean = new NicsImpl();
146 Response rsp = bean.create(dto, vspId, versionId, componentId, user);
147 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
148 Object e = rsp.getEntity();
149 Assert.assertNotNull(e);
151 NicCreationResponseDto creationDto = (NicCreationResponseDto)e;
152 Assert.assertEquals(nicId, creationDto.getNicId());
153 } catch (ClassCastException ex) {
154 Assert.fail("unexpected class for DTO " + e.getClass().getName());
160 public void testDelete() {
161 NicsImpl bean = new NicsImpl();
162 Response rsp = bean.delete(vspId, versionId, componentId, nicId, user);
163 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
164 Assert.assertNull(rsp.getEntity());
169 public void testGet() {
170 NicsImpl bean = new NicsImpl();
171 Response rsp = bean.get(vspId, versionId, componentId, nicId, user);
172 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
173 Assert.assertNotNull(rsp.getEntity());
177 public void testUpdate() {
178 NicsImpl bean = new NicsImpl();
179 NicRequestDto dto = new NicRequestDto();
180 dto.setDescription("hello");
182 dto.setNetworkDescription("nd");
183 dto.setNetworkId(nicId);
184 dto.setNetworkType("External");
186 Response rsp = bean.update(dto, vspId, versionId, componentId, nicId, user);
187 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
188 Assert.assertNull(rsp.getEntity());
192 public void testGetQuestionaire() {
193 NicsImpl bean = new NicsImpl();
194 Response rsp = bean.getQuestionnaire(vspId, versionId, componentId, nicId, user);
195 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
197 QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
198 Assert.assertEquals("helloworld", dto.getData());
200 catch (Exception ex) {
201 logger.error("caught exception", ex);
202 Assert.fail(ex.getMessage());
208 public void testUpdateQuestionaire() {
209 NicsImpl bean = new NicsImpl();
210 Response rsp = bean.updateQuestionnaire("helloworld", vspId, versionId, componentId, nicId, user);
211 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
212 Assert.assertNull(rsp.getEntity());