5add920dd633096ae71247e73e13dc4cb7a829dc
[sdc.git] /
1 package org.openecomp.sdcrests.vsp.rest.services;
2
3 import org.apache.http.HttpStatus;
4 import org.junit.Assert;
5 import org.junit.Before;
6 import org.junit.Test;
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;
28
29 import javax.ws.rs.core.Response;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.UUID;
33
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;
37
38 @RunWith(PowerMockRunner.class)
39 @PrepareForTest({ComponentsImpl.class, ComponentManagerFactory.class})
40 public class ComponentImplTest {
41
42   private Logger logger = LoggerFactory.getLogger(ComponentImplTest.class);
43
44
45   @Mock
46   private ComponentManagerFactory componentManagerFactory;
47
48   @Mock
49   private ComponentManager componentManager;
50
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";
55
56   @Before
57   public void setUp() {
58     try {
59       initMocks(this);
60
61       mockStatic(ComponentManagerFactory.class);
62       when(ComponentManagerFactory.getInstance()).thenReturn(componentManagerFactory);
63       when(componentManagerFactory.createInterface()).thenReturn(componentManager);
64
65       ComponentEntity ce = new ComponentEntity();
66       ce.setId(vspId);
67       ce.setVspId(vspId);
68       ce.setVersion(new Version(versionId));
69
70       Collection<ComponentEntity> ceList = Collections.singletonList(ce);
71       when(componentManager.listComponents(
72               ArgumentMatchers.eq(vspId),
73               ArgumentMatchers.any())).thenReturn(ceList);
74
75       when(componentManager.createComponent(
76               ArgumentMatchers.any())).thenReturn(ce);
77
78       CompositionEntityResponse<ComponentData> r = new CompositionEntityResponse<>();
79       r.setId(vspId);
80       when(componentManager.getComponent(
81               ArgumentMatchers.eq(vspId),
82               ArgumentMatchers.any(),
83               ArgumentMatchers.eq(componentId))).thenReturn(r);
84
85       CompositionEntityType tpe = CompositionEntityType.component;
86       CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
87       when(componentManager.updateComponent(
88               ArgumentMatchers.any())).thenReturn(data);
89
90
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);
97
98
99     } catch (Exception e) {
100       logger.error(e.getMessage(), e);
101     }
102   }
103
104   @Test
105   public void testList() {
106     ComponentsImpl ci = new ComponentsImpl();
107
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());
115   }
116
117   @Test
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());
123   }
124
125
126
127   @Test
128   public void testCreate() {
129
130     ComponentRequestDto dto = new ComponentRequestDto();
131     dto.setDescription("hello");
132     dto.setName("name");
133     dto.setDisplayName("world");
134
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);
140     try {
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());
145     }
146   }
147
148
149   @Test
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());
155   }
156
157
158   @Test
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());
164   }
165
166   @Test
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());
173   }
174
175   @Test
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());
180     try {
181       QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
182       Assert.assertEquals("helloworld", dto.getData());
183     }
184     catch (Exception ex) {
185       logger.error("caught exception", ex);
186       Assert.fail(ex.getMessage());
187     }
188   }
189
190
191   @Test
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());
197   }
198 }