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.dao.type.ComponentEntity;
15 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
16 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
17 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
18 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
19 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
20 import org.openecomp.sdc.versioning.dao.types.Version;
21 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentCreationDto;
22 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDto;
23 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentRequestDto;
24 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
25 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
26 import org.powermock.core.classloader.annotations.PrepareForTest;
27 import org.powermock.modules.junit4.PowerMockRunner;
29 import javax.ws.rs.core.Response;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.UUID;
34 import static org.mockito.MockitoAnnotations.initMocks;
35 import static org.powermock.api.mockito.PowerMockito.mockStatic;
36 import static org.powermock.api.mockito.PowerMockito.when;
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest({ComponentsImpl.class, ComponentManagerFactory.class})
40 public class ComponentImplTest {
42 private Logger logger = LoggerFactory.getLogger(ComponentImplTest.class);
46 private ComponentManagerFactory componentManagerFactory;
49 private ComponentManager componentManager;
51 private final String vspId = UUID.randomUUID().toString();
52 private final String versionId = UUID.randomUUID().toString();
53 private final String componentId = "" + System.currentTimeMillis();
54 private final String user = "cs0008";
61 mockStatic(ComponentManagerFactory.class);
62 when(ComponentManagerFactory.getInstance()).thenReturn(componentManagerFactory);
63 when(componentManagerFactory.createInterface()).thenReturn(componentManager);
65 ComponentEntity ce = new ComponentEntity();
68 ce.setVersion(new Version(versionId));
70 Collection<ComponentEntity> ceList = Collections.singletonList(ce);
71 when(componentManager.listComponents(
72 ArgumentMatchers.eq(vspId),
73 ArgumentMatchers.any())).thenReturn(ceList);
75 when(componentManager.createComponent(
76 ArgumentMatchers.any())).thenReturn(ce);
78 CompositionEntityResponse<ComponentData> r = new CompositionEntityResponse<>();
80 when(componentManager.getComponent(
81 ArgumentMatchers.eq(vspId),
82 ArgumentMatchers.any(),
83 ArgumentMatchers.eq(componentId))).thenReturn(r);
85 CompositionEntityType tpe = CompositionEntityType.component;
86 CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
87 when(componentManager.updateComponent(
88 ArgumentMatchers.any())).thenReturn(data);
91 QuestionnaireResponse qr = new QuestionnaireResponse();
92 qr.setData("helloworld");
93 when(componentManager.getQuestionnaire(
94 ArgumentMatchers.eq(vspId),
95 ArgumentMatchers.any(),
96 ArgumentMatchers.eq(componentId))).thenReturn(qr);
99 } catch (Exception e) {
100 logger.error(e.getMessage(), e);
105 public void testList() {
106 ComponentsImpl ci = new ComponentsImpl();
108 Response rsp = ci.list(vspId, versionId, user);
109 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
110 Object e = rsp.getEntity();
111 Assert.assertNotNull(e);
112 @SuppressWarnings("unchecked")
113 GenericCollectionWrapper<ComponentDto> results = (GenericCollectionWrapper<ComponentDto>)e;
114 Assert.assertEquals("result length", 1, results.getListCount());
118 public void testDeleteList() {
119 ComponentsImpl ci = new ComponentsImpl();
120 Response rsp = ci.deleteList(vspId, versionId, user);
121 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
122 Assert.assertNull(rsp.getEntity());
128 public void testCreate() {
130 ComponentRequestDto dto = new ComponentRequestDto();
131 dto.setDescription("hello");
133 dto.setDisplayName("world");
135 ComponentsImpl ci = new ComponentsImpl();
136 Response rsp = ci.create(dto, vspId, versionId, user);
137 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
138 Object e = rsp.getEntity();
139 Assert.assertNotNull(e);
141 ComponentCreationDto ccdto = (ComponentCreationDto)e;
142 Assert.assertEquals(vspId, ccdto.getVfcId());
143 } catch (ClassCastException ex) {
144 Assert.fail("unexpected class for DTO " + e.getClass().getName());
150 public void testDelete() {
151 ComponentsImpl ci = new ComponentsImpl();
152 Response rsp = ci.delete(vspId, versionId, componentId, user);
153 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
154 Assert.assertNull(rsp.getEntity());
159 public void testGet() {
160 ComponentsImpl ci = new ComponentsImpl();
161 Response rsp = ci.get(vspId, versionId, componentId, user);
162 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
163 Assert.assertNotNull(rsp.getEntity());
167 public void testUpdate() {
168 ComponentsImpl ci = new ComponentsImpl();
169 ComponentRequestDto dto = new ComponentRequestDto();
170 Response rsp = ci.update(dto, vspId, versionId, componentId, user);
171 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
172 Assert.assertNull(rsp.getEntity());
176 public void testGetQuestionaire() {
177 ComponentsImpl ci = new ComponentsImpl();
178 Response rsp = ci.getQuestionnaire(vspId, versionId, componentId, user);
179 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
181 QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
182 Assert.assertEquals("helloworld", dto.getData());
184 catch (Exception ex) {
185 logger.error("caught exception", ex);
186 Assert.fail(ex.getMessage());
192 public void testUpdateQuestionaire() {
193 ComponentsImpl ci = new ComponentsImpl();
194 Response rsp = ci.updateQuestionnaire("helloworld", vspId, versionId, componentId, user);
195 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
196 Assert.assertNull(rsp.getEntity());