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.dao.type.ComponentEntity;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
38 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
40 import org.openecomp.sdc.versioning.dao.types.Version;
41 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentCreationDto;
42 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDto;
43 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentRequestDto;
44 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
45 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
46 import org.powermock.core.classloader.annotations.PrepareForTest;
47 import org.powermock.modules.junit4.PowerMockRunner;
49 import javax.ws.rs.core.Response;
50 import java.util.Collection;
51 import java.util.Collections;
52 import java.util.UUID;
54 import static org.mockito.MockitoAnnotations.initMocks;
55 import static org.powermock.api.mockito.PowerMockito.mockStatic;
56 import static org.powermock.api.mockito.PowerMockito.when;
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({ComponentsImpl.class, ComponentManagerFactory.class})
60 public class ComponentImplTest {
62 private Logger logger = LoggerFactory.getLogger(ComponentImplTest.class);
66 private ComponentManagerFactory componentManagerFactory;
69 private ComponentManager componentManager;
71 private final String vspId = UUID.randomUUID().toString();
72 private final String versionId = UUID.randomUUID().toString();
73 private final String componentId = "" + System.currentTimeMillis();
74 private final String user = "cs0008";
81 mockStatic(ComponentManagerFactory.class);
82 when(ComponentManagerFactory.getInstance()).thenReturn(componentManagerFactory);
83 when(componentManagerFactory.createInterface()).thenReturn(componentManager);
85 ComponentEntity ce = new ComponentEntity();
88 ce.setVersion(new Version(versionId));
90 Collection<ComponentEntity> ceList = Collections.singletonList(ce);
91 when(componentManager.listComponents(
92 ArgumentMatchers.eq(vspId),
93 ArgumentMatchers.any())).thenReturn(ceList);
95 when(componentManager.createComponent(
96 ArgumentMatchers.any())).thenReturn(ce);
98 CompositionEntityResponse<ComponentData> r = new CompositionEntityResponse<>();
100 when(componentManager.getComponent(
101 ArgumentMatchers.eq(vspId),
102 ArgumentMatchers.any(),
103 ArgumentMatchers.eq(componentId))).thenReturn(r);
105 CompositionEntityType tpe = CompositionEntityType.component;
106 CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
107 when(componentManager.updateComponent(
108 ArgumentMatchers.any())).thenReturn(data);
111 QuestionnaireResponse qr = new QuestionnaireResponse();
112 qr.setData("helloworld");
113 when(componentManager.getQuestionnaire(
114 ArgumentMatchers.eq(vspId),
115 ArgumentMatchers.any(),
116 ArgumentMatchers.eq(componentId))).thenReturn(qr);
119 } catch (Exception e) {
120 logger.error(e.getMessage(), e);
125 public void testList() {
126 ComponentsImpl ci = new ComponentsImpl();
128 Response rsp = ci.list(vspId, versionId, user);
129 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
130 Object e = rsp.getEntity();
131 Assert.assertNotNull(e);
132 @SuppressWarnings("unchecked")
133 GenericCollectionWrapper<ComponentDto> results = (GenericCollectionWrapper<ComponentDto>)e;
134 Assert.assertEquals("result length", 1, results.getListCount());
138 public void testDeleteList() {
139 ComponentsImpl ci = new ComponentsImpl();
140 Response rsp = ci.deleteList(vspId, versionId, user);
141 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
142 Assert.assertNull(rsp.getEntity());
148 public void testCreate() {
150 ComponentRequestDto dto = new ComponentRequestDto();
151 dto.setDescription("hello");
153 dto.setDisplayName("world");
155 ComponentsImpl ci = new ComponentsImpl();
156 Response rsp = ci.create(dto, vspId, versionId, user);
157 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
158 Object e = rsp.getEntity();
159 Assert.assertNotNull(e);
161 ComponentCreationDto ccdto = (ComponentCreationDto)e;
162 Assert.assertEquals(vspId, ccdto.getVfcId());
163 } catch (ClassCastException ex) {
164 Assert.fail("unexpected class for DTO " + e.getClass().getName());
170 public void testDelete() {
171 ComponentsImpl ci = new ComponentsImpl();
172 Response rsp = ci.delete(vspId, versionId, componentId, user);
173 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
174 Assert.assertNull(rsp.getEntity());
179 public void testGet() {
180 ComponentsImpl ci = new ComponentsImpl();
181 Response rsp = ci.get(vspId, versionId, componentId, user);
182 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
183 Assert.assertNotNull(rsp.getEntity());
187 public void testUpdate() {
188 ComponentsImpl ci = new ComponentsImpl();
189 ComponentRequestDto dto = new ComponentRequestDto();
190 Response rsp = ci.update(dto, vspId, versionId, componentId, user);
191 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
192 Assert.assertNull(rsp.getEntity());
196 public void testGetQuestionaire() {
197 ComponentsImpl ci = new ComponentsImpl();
198 Response rsp = ci.getQuestionnaire(vspId, versionId, componentId, user);
199 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
201 QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
202 Assert.assertEquals("helloworld", dto.getData());
204 catch (Exception ex) {
205 logger.error("caught exception", ex);
206 Assert.fail(ex.getMessage());
212 public void testUpdateQuestionaire() {
213 ComponentsImpl ci = new ComponentsImpl();
214 Response rsp = ci.updateQuestionnaire("helloworld", vspId, versionId, componentId, user);
215 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
216 Assert.assertNull(rsp.getEntity());