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 static junit.framework.TestCase.assertEquals;
24 import static junit.framework.TestCase.assertFalse;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.MockitoAnnotations.initMocks;
27 import static org.mockito.Mockito.when;
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.Optional;
32 import java.util.UUID;
33 import javax.activation.DataHandler;
34 import javax.ws.rs.core.Response;
35 import org.apache.commons.lang3.tuple.Pair;
36 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
37 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.ArgumentMatchers;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
45 import org.openecomp.sdc.activitylog.ActivityLogManager;
46 import org.openecomp.sdc.logging.api.Logger;
47 import org.openecomp.sdc.logging.api.LoggerFactory;
48 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
49 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
50 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
51 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
52 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
53 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
54 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
55 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
56 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
57 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
58 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
60 public class OrchestrationTemplateCandidateImplTest {
62 private Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
65 private OrchestrationTemplateCandidateManager candidateManager;
67 private VendorSoftwareProductManager vendorSoftwareProductManager;
70 private ActivityLogManager activityLogManager;
72 private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
74 private final String candidateId = UUID.randomUUID().toString();
75 private final String softwareProductId = UUID.randomUUID().toString();
76 private final String versionId = UUID.randomUUID().toString();
78 private final String user = "cs0008";
84 UploadFileResponse uploadFileResponse = new UploadFileResponse();
85 uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
86 uploadFileResponse.setNetworkPackageName("test");
87 when(candidateManager.upload(any(), any())).thenReturn(uploadFileResponse);
90 // get using the candidate manager.
91 Optional<Pair<String,byte[]>> zipFile =
92 Optional.of(Pair.of("Hello", "World".getBytes()));
94 when(candidateManager.get(
95 ArgumentMatchers.eq(candidateId),
96 ArgumentMatchers.any())).thenReturn(zipFile);
98 when(vendorSoftwareProductManager.get(
99 ArgumentMatchers.eq(softwareProductId),
100 ArgumentMatchers.any())).thenReturn(zipFile);
103 OrchestrationTemplateActionResponse processResponse =
104 new OrchestrationTemplateActionResponse();
105 processResponse.setStatus(UploadFileStatus.Success);
106 when(candidateManager.process(
107 ArgumentMatchers.eq(candidateId),
108 ArgumentMatchers.any())).thenReturn(processResponse);
111 ValidationResponse vr = new ValidationResponse();
112 when(candidateManager.updateFilesDataStructure(
113 ArgumentMatchers.eq(candidateId),
114 ArgumentMatchers.any(),
115 ArgumentMatchers.any())).thenReturn(vr);
117 FilesDataStructure fds = new FilesDataStructure();
118 fds.setArtifacts(Arrays.asList("a","b"));
119 fds.setNested(Arrays.asList("foo", "bar"));
120 fds.setUnassigned(Arrays.asList("c", "d"));
121 fds.setModules(Arrays.asList(new Module(), new Module()));
123 when(candidateManager.getFilesDataStructure(
124 ArgumentMatchers.eq(candidateId),
125 ArgumentMatchers.any())).thenReturn(Optional.of(fds));
127 orchestrationTemplateCandidate =
128 new OrchestrationTemplateCandidateImpl(candidateManager, vendorSoftwareProductManager, activityLogManager);
131 }catch (Exception e){
132 logger.error(e.getMessage(), e);
137 public void uploadSignedTest() {
138 Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment("filename.zip"), "1");
139 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
143 public void uploadNotSignedTest(){
144 Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment("filename.csar"), "1");
145 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
148 private Attachment mockAttachment(final String fileName) {
149 final Attachment attachment = Mockito.mock(Attachment.class);
150 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
151 final DataHandler dataHandler = Mockito.mock(DataHandler.class);
152 when(dataHandler.getName()).thenReturn(fileName);
153 when(attachment.getDataHandler()).thenReturn(dataHandler);
154 final byte[] bytes = "upload package Test".getBytes();
155 when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);
160 public void uploadSignNotValidTest() {
161 Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment("filename.zip"), "1");
162 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
163 assertFalse(((UploadFileResponseDto)response.getEntity()).getErrors().isEmpty());
167 public void testCandidateGet() throws IOException {
168 Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
169 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
170 Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"),-1);
171 byte[] content = (byte[])rsp.getEntity();
172 Assert.assertEquals("World", new String(content));
176 public void testVendorSoftwareProductGet() throws IOException {
177 Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
178 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
179 Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"),-1);
180 byte[] content = (byte[])rsp.getEntity();
181 Assert.assertEquals("World", new String(content));
185 public void testMissingGet() throws IOException {
186 Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
187 Assert.assertEquals("Response status equals", Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus());
191 public void testAbort() {
193 Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
194 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
195 Assert.assertNull(rsp.getEntity());
197 catch (Exception ex) {
198 logger.error("unexpected exception", ex);
199 Assert.fail("abort should not throw an exception");
204 public void testProcess() {
206 Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
207 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
208 Assert.assertNotNull(rsp.getEntity());
209 OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto)rsp.getEntity();
210 Assert.assertEquals("status check", UploadFileStatus.Success, dto.getStatus());
212 catch (Exception ex) {
213 logger.error("unexpected exception", ex);
214 Assert.fail("abort should not throw an exception");
219 public void testFilesDataStructureUpload() {
221 FileDataStructureDto dto = new FileDataStructureDto();
222 dto.setArtifacts(Arrays.asList("a", "b", "c"));
223 Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
224 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
226 catch (Exception ex) {
227 logger.error("unexpected exception", ex);
228 Assert.fail("abort should not throw an exception");
233 public void testFilesDataStructureGet() {
235 FileDataStructureDto dto = new FileDataStructureDto();
236 dto.setArtifacts(Arrays.asList("a", "b", "c"));
237 Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
238 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
240 catch (Exception ex) {
241 logger.error("unexpected exception", ex);
242 Assert.fail("abort should not throw an exception");