Added oparent to sdc main
[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.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;
48
49 import javax.ws.rs.core.Response;
50 import java.util.Collection;
51 import java.util.Collections;
52 import java.util.UUID;
53
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;
57
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest({ComponentsImpl.class, ComponentManagerFactory.class})
60 public class ComponentImplTest {
61
62   private Logger logger = LoggerFactory.getLogger(ComponentImplTest.class);
63
64
65   @Mock
66   private ComponentManagerFactory componentManagerFactory;
67
68   @Mock
69   private ComponentManager componentManager;
70
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";
75
76   @Before
77   public void setUp() {
78     try {
79       initMocks(this);
80
81       mockStatic(ComponentManagerFactory.class);
82       when(ComponentManagerFactory.getInstance()).thenReturn(componentManagerFactory);
83       when(componentManagerFactory.createInterface()).thenReturn(componentManager);
84
85       ComponentEntity ce = new ComponentEntity();
86       ce.setId(vspId);
87       ce.setVspId(vspId);
88       ce.setVersion(new Version(versionId));
89
90       Collection<ComponentEntity> ceList = Collections.singletonList(ce);
91       when(componentManager.listComponents(
92               ArgumentMatchers.eq(vspId),
93               ArgumentMatchers.any())).thenReturn(ceList);
94
95       when(componentManager.createComponent(
96               ArgumentMatchers.any())).thenReturn(ce);
97
98       CompositionEntityResponse<ComponentData> r = new CompositionEntityResponse<>();
99       r.setId(vspId);
100       when(componentManager.getComponent(
101               ArgumentMatchers.eq(vspId),
102               ArgumentMatchers.any(),
103               ArgumentMatchers.eq(componentId))).thenReturn(r);
104
105       CompositionEntityType tpe = CompositionEntityType.component;
106       CompositionEntityValidationData data = new CompositionEntityValidationData(tpe, vspId);
107       when(componentManager.updateComponent(
108               ArgumentMatchers.any())).thenReturn(data);
109
110
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);
117
118
119     } catch (Exception e) {
120       logger.error(e.getMessage(), e);
121     }
122   }
123
124   @Test
125   public void testList() {
126     ComponentsImpl ci = new ComponentsImpl();
127
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());
135   }
136
137   @Test
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());
143   }
144
145
146
147   @Test
148   public void testCreate() {
149
150     ComponentRequestDto dto = new ComponentRequestDto();
151     dto.setDescription("hello");
152     dto.setName("name");
153     dto.setDisplayName("world");
154
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);
160     try {
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());
165     }
166   }
167
168
169   @Test
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());
175   }
176
177
178   @Test
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());
184   }
185
186   @Test
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());
193   }
194
195   @Test
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());
200     try {
201       QuestionnaireResponseDto dto = (QuestionnaireResponseDto)rsp.getEntity();
202       Assert.assertEquals("helloworld", dto.getData());
203     }
204     catch (Exception ex) {
205       logger.error("caught exception", ex);
206       Assert.fail(ex.getMessage());
207     }
208   }
209
210
211   @Test
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());
217   }
218 }