Improve testing stability
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / test / java / org / openecomp / sdcrests / vsp / rest / services / ComponentImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdcrests.vsp.rest.services;
22
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;
44
45 import javax.ws.rs.core.Response;
46 import java.util.Collection;
47 import java.util.Collections;
48 import java.util.UUID;
49
50 import static org.mockito.MockitoAnnotations.openMocks;
51 import static org.mockito.Mockito.when;
52
53 public class ComponentImplTest {
54
55   private Logger logger = LoggerFactory.getLogger(ComponentImplTest.class);
56
57   @Mock
58   private ComponentManager componentManager;
59
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";
64
65   private ComponentsImpl ci;
66
67   @Before
68   public void setUp() {
69     try {
70       openMocks(this);
71
72       ComponentEntity ce = new ComponentEntity();
73       ce.setId(vspId);
74       ce.setVspId(vspId);
75       ce.setVersion(new Version(versionId));
76
77       Collection<ComponentEntity> ceList = Collections.singletonList(ce);
78       when(componentManager.listComponents(
79               ArgumentMatchers.eq(vspId),
80               ArgumentMatchers.any())).thenReturn(ceList);
81
82       when(componentManager.createComponent(
83               ArgumentMatchers.any())).thenReturn(ce);
84
85       CompositionEntityResponse<ComponentData> r = new CompositionEntityResponse<>();
86       r.setId(vspId);
87       when(componentManager.getComponent(
88               ArgumentMatchers.eq(vspId),
89               ArgumentMatchers.any(),
90               ArgumentMatchers.eq(componentId))).thenReturn(r);
91
92       CompositionEntityType tpe = CompositionEntityType.component;
93       CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
94       when(componentManager.updateComponent(
95               ArgumentMatchers.any())).thenReturn(data);
96
97
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);
104
105       ci = new ComponentsImpl(componentManager);
106
107
108     } catch (Exception e) {
109       logger.error(e.getMessage(), e);
110     }
111   }
112
113   @Test
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());
122   }
123
124   @Test
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());
129   }
130
131
132
133   @Test
134   public void testCreate() {
135
136     ComponentRequestDto dto = new ComponentRequestDto();
137     dto.setDescription("hello");
138     dto.setName("name");
139     dto.setDisplayName("world");
140
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);
145     try {
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());
150     }
151   }
152
153
154   @Test
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());
159   }
160
161
162   @Test
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());
167   }
168
169   @Test
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());
175   }
176
177   @Test
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());
181     try {
182       QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
183       Assert.assertEquals("helloworld", dto.getData());
184     }
185     catch (Exception ex) {
186       logger.error("caught exception", ex);
187       Assert.fail(ex.getMessage());
188     }
189   }
190
191
192   @Test
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());
197   }
198 }