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 / ComponentProcessesImplTest.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.cxf.jaxrs.ext.multipart.Attachment;
24 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
25 import org.apache.http.HttpStatus;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.mockito.ArgumentMatchers;
30 import org.mockito.Mock;
31 import org.openecomp.sdc.activitylog.ActivityLogManager;
32 import org.openecomp.sdc.logging.api.Logger;
33 import org.openecomp.sdc.logging.api.LoggerFactory;
34 import org.openecomp.sdc.vendorsoftwareproduct.ComponentManager;
35 import org.openecomp.sdc.vendorsoftwareproduct.ProcessManager;
36 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessEntity;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.ProcessType;
38 import org.openecomp.sdc.versioning.dao.types.Version;
39 import org.openecomp.sdcrests.vendorsoftwareproducts.types.*;
40 import org.openecomp.sdcrests.wrappers.GenericCollectionWrapper;
41 import org.openecomp.sdcrests.wrappers.StringWrapperResponse;
42
43 import javax.ws.rs.core.Response;
44 import java.io.ByteArrayInputStream;
45 import java.io.File;
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 import static org.mockito.Mockito.mock;
53
54 public class ComponentProcessesImplTest {
55
56   private Logger logger = LoggerFactory.getLogger(ComponentProcessesImplTest.class);
57
58   @Mock
59   private ActivityLogManager activityLogManager;
60
61   @Mock
62   private ProcessManager processManager;
63
64   @Mock
65   private ComponentManager componentManager;
66
67   private final String vspId = UUID.randomUUID().toString();
68   private final String versionId = UUID.randomUUID().toString();
69   private final String componentId = "" + System.currentTimeMillis();
70   private final String processId = "" + System.currentTimeMillis();
71   private final String user = "cs0008";
72
73   private ComponentProcessesImpl cpi;
74
75   @Before
76   public void setUp() {
77     try {
78       openMocks(this);
79
80       ProcessEntity pe = new ProcessEntity();
81       pe.setId(vspId);
82       pe.setComponentId(componentId);
83       pe.setVspId(vspId);
84       pe.setVersion(new Version(versionId));
85
86       Collection<ProcessEntity> peList = Collections.singletonList(pe);
87       when(processManager.listProcesses(
88               ArgumentMatchers.eq(vspId),
89               ArgumentMatchers.any(),
90               ArgumentMatchers.eq(componentId))).thenReturn(peList);
91
92       when(processManager.createProcess(
93               ArgumentMatchers.any())).thenReturn(pe);
94
95       when(processManager.getProcess(
96               ArgumentMatchers.eq(vspId),
97               ArgumentMatchers.any(),
98               ArgumentMatchers.eq(componentId),
99               ArgumentMatchers.eq(processId))).thenReturn(pe);
100
101       File processArtifact = new File(vspId);
102       when(processManager.getProcessArtifact(
103               ArgumentMatchers.eq(vspId),
104               ArgumentMatchers.any(),
105               ArgumentMatchers.eq(componentId),
106               ArgumentMatchers.eq(processId))).thenReturn(processArtifact);
107
108       cpi = new ComponentProcessesImpl(processManager, componentManager, activityLogManager);
109
110     } catch (Exception e) {
111       logger.error(e.getMessage(), e);
112     }
113   }
114
115   @Test
116   public void testList() {
117     Response rsp = cpi.list(vspId, versionId, componentId, user);
118     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
119     Object e = rsp.getEntity();
120     Assert.assertNotNull(e);
121     @SuppressWarnings("unchecked")
122     GenericCollectionWrapper<ProcessEntityDto> results = (GenericCollectionWrapper<ProcessEntityDto>)e;
123     Assert.assertEquals("result length", 1, results.getListCount());
124   }
125
126   @Test
127   public void testDeleteList() {
128     Response rsp = cpi.deleteList(vspId, versionId, componentId, user);
129     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
130     Assert.assertNull(rsp.getEntity());
131   }
132
133
134
135   @Test
136   public void testCreate() {
137
138     ProcessRequestDto dto = new ProcessRequestDto();
139     dto.setDescription("hello");
140     dto.setName("name");
141     dto.setType(ProcessType.Other);
142
143     Response rsp = cpi.create(dto, vspId, versionId, componentId, user);
144     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
145     Object e = rsp.getEntity();
146     Assert.assertNotNull(e);
147     try {
148       StringWrapperResponse swr = (StringWrapperResponse)e;
149       Assert.assertEquals(vspId, swr.getValue());
150     } catch (ClassCastException ex) {
151       Assert.fail("unexpected class for DTO " + e.getClass().getName());
152     }
153   }
154
155
156   @Test
157   public void testDelete() {
158     Response rsp = cpi.delete(vspId, versionId, componentId, processId, user);
159     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
160     Assert.assertNull(rsp.getEntity());
161   }
162
163
164   @Test
165   public void testGet() {
166     Response rsp = cpi.get(vspId, versionId, componentId, processId, user);
167     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
168     Assert.assertNotNull(rsp.getEntity());
169   }
170
171   @Test
172   public void testUpdate() {
173     ProcessRequestDto dto = new ProcessRequestDto();
174     Response rsp = cpi.update(dto, vspId, versionId, componentId, processId, user);
175     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
176     Assert.assertNull(rsp.getEntity());
177   }
178
179   @Test
180   public void testGetUploadedFile() {
181     Response rsp = cpi.getUploadedFile(vspId, versionId, componentId, processId, user);
182     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
183     Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf(vspId),-1);
184   }
185
186
187   @Test
188   public void testDeleteUploadedFile() {
189     Response rsp = cpi.deleteUploadedFile(vspId, versionId, componentId, processId, user);
190     Assert.assertEquals("Response should be 200", HttpStatus.SC_OK, rsp.getStatus());
191     Assert.assertNull(rsp.getEntity());
192   }
193
194
195   @Test
196   public void testUploadFile() {
197
198     Attachment attachment = mock(Attachment.class);
199     when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
200     byte[] bytes = "Hello World".getBytes();
201     when(attachment.getObject(ArgumentMatchers.any())).thenReturn(new ByteArrayInputStream(bytes));
202     Response rsp = cpi.uploadFile(attachment, vspId, versionId, componentId, processId, user);
203     Assert.assertNull(rsp.getEntity());
204   }
205
206 }