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.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;
43 import javax.ws.rs.core.Response;
44 import java.io.ByteArrayInputStream;
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;
52 import static org.mockito.Mockito.mock;
54 public class ComponentProcessesImplTest {
56 private Logger logger = LoggerFactory.getLogger(ComponentProcessesImplTest.class);
59 private ActivityLogManager activityLogManager;
62 private ProcessManager processManager;
65 private ComponentManager componentManager;
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";
73 private ComponentProcessesImpl cpi;
80 ProcessEntity pe = new ProcessEntity();
82 pe.setComponentId(componentId);
84 pe.setVersion(new Version(versionId));
86 Collection<ProcessEntity> peList = Collections.singletonList(pe);
87 when(processManager.listProcesses(
88 ArgumentMatchers.eq(vspId),
89 ArgumentMatchers.any(),
90 ArgumentMatchers.eq(componentId))).thenReturn(peList);
92 when(processManager.createProcess(
93 ArgumentMatchers.any())).thenReturn(pe);
95 when(processManager.getProcess(
96 ArgumentMatchers.eq(vspId),
97 ArgumentMatchers.any(),
98 ArgumentMatchers.eq(componentId),
99 ArgumentMatchers.eq(processId))).thenReturn(pe);
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);
108 cpi = new ComponentProcessesImpl(processManager, componentManager, activityLogManager);
110 } catch (Exception e) {
111 logger.error(e.getMessage(), e);
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());
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());
136 public void testCreate() {
138 ProcessRequestDto dto = new ProcessRequestDto();
139 dto.setDescription("hello");
141 dto.setType(ProcessType.Other);
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);
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());
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());
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());
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());
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);
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());
196 public void testUploadFile() {
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());