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.mockito.ArgumentMatchers;
28 import org.mockito.Mock;
29 import org.openecomp.sdc.logging.api.Logger;
30 import org.openecomp.sdc.logging.api.LoggerFactory;
31 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
32 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ComponentEntity;
33 import org.openecomp.sdc.vendorsoftwareproduct.types.CompositionEntityResponse;
34 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireResponse;
35 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.ComponentData;
36 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityType;
37 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
38 import org.openecomp.sdc.versioning.dao.types.Version;
39 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentCreationDto;
40 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentDto;
41 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentRequestDto;
42 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireResponseDto;
43 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
45 import javax.ws.rs.core.Response;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.UUID;
50 import static org.mockito.MockitoAnnotations.openMocks;
51 import static org.mockito.Mockito.when;
53 public class ComponentImplTest {
55 private Logger logger = LoggerFactory.getLogger(ComponentImplTest.class);
58 private ComponentManager componentManager;
60 private final String vspId = UUID.randomUUID().toString();
61 private final String versionId = UUID.randomUUID().toString();
62 private final String componentId = "" + System.currentTimeMillis();
63 private final String user = "cs0008";
65 private ComponentsImpl ci;
72 ComponentEntity ce = new ComponentEntity();
75 ce.setVersion(new Version(versionId));
77 Collection<ComponentEntity> ceList = Collections.singletonList(ce);
78 when(componentManager.listComponents(
79 ArgumentMatchers.eq(vspId),
80 ArgumentMatchers.any())).thenReturn(ceList);
82 when(componentManager.createComponent(
83 ArgumentMatchers.any())).thenReturn(ce);
85 CompositionEntityResponse<ComponentData> r = new CompositionEntityResponse<>();
87 when(componentManager.getComponent(
88 ArgumentMatchers.eq(vspId),
89 ArgumentMatchers.any(),
90 ArgumentMatchers.eq(componentId))).thenReturn(r);
92 CompositionEntityType tpe = CompositionEntityType.component;
93 CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
94 when(componentManager.updateComponent(
95 ArgumentMatchers.any())).thenReturn(data);
98 QuestionnaireResponse qr = new QuestionnaireResponse();
99 qr.setData("helloworld");
100 when(componentManager.getQuestionnaire(
101 ArgumentMatchers.eq(vspId),
102 ArgumentMatchers.any(),
103 ArgumentMatchers.eq(componentId))).thenReturn(qr);
105 ci = new ComponentsImpl(componentManager);
108 } catch (Exception e) {
109 logger.error(e.getMessage(), e);
114 public void testList() {
115 Response rsp = ci.list(vspId, versionId, user);
116 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
117 Object e = rsp.getEntity();
118 Assert.assertNotNull(e);
119 @SuppressWarnings("unchecked")
120 GenericCollectionWrapper<ComponentDto> results = (GenericCollectionWrapper<ComponentDto>)e;
121 Assert.assertEquals("result length", 1, results.getListCount());
125 public void testDeleteList() {
126 Response rsp = ci.deleteList(vspId, versionId, user);
127 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
128 Assert.assertNull(rsp.getEntity());
134 public void testCreate() {
136 ComponentRequestDto dto = new ComponentRequestDto();
137 dto.setDescription("hello");
139 dto.setDisplayName("world");
141 Response rsp = ci.create(dto, vspId, versionId, user);
142 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
143 Object e = rsp.getEntity();
144 Assert.assertNotNull(e);
146 ComponentCreationDto ccdto = (ComponentCreationDto)e;
147 Assert.assertEquals(vspId, ccdto.getVfcId());
148 } catch (ClassCastException ex) {
149 Assert.fail("unexpected class for DTO " + e.getClass().getName());
155 public void testDelete() {
156 Response rsp = ci.delete(vspId, versionId, componentId, user);
157 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
158 Assert.assertNull(rsp.getEntity());
163 public void testGet() {
164 Response rsp = ci.get(vspId, versionId, componentId, user);
165 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
166 Assert.assertNotNull(rsp.getEntity());
170 public void testUpdate() {
171 ComponentRequestDto dto = new ComponentRequestDto();
172 Response rsp = ci.update(dto, vspId, versionId, componentId, user);
173 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
174 Assert.assertNull(rsp.getEntity());
178 public void testGetQuestionaire() {
179 Response rsp = ci.getQuestionnaire(vspId, versionId, componentId, user);
180 Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
182 QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
183 Assert.assertEquals("helloworld", dto.getData());
185 catch (Exception ex) {
186 logger.error("caught exception", ex);
187 Assert.fail(ex.getMessage());
193 public void testUpdateQuestionaire() {
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());