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.powermock.api.mockito.PowerMockito.mock;
28 import static org.powermock.api.mockito.PowerMockito.mockStatic;
29 import static org.powermock.api.mockito.PowerMockito.when;
30 import static org.powermock.api.mockito.PowerMockito.whenNew;
32 import java.io.IOException;
33 import java.util.Arrays;
34 import java.util.Optional;
35 import java.util.UUID;
36 import javax.ws.rs.core.Response;
37 import org.apache.commons.lang3.tuple.Pair;
38 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
39 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
40 import org.junit.Assert;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.junit.runner.RunWith;
44 import org.mockito.ArgumentMatchers;
45 import org.mockito.Mock;
46 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
47 import org.openecomp.sdc.activitylog.ActivityLogManager;
48 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
49 import org.openecomp.sdc.logging.api.Logger;
50 import org.openecomp.sdc.logging.api.LoggerFactory;
51 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
52 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
53 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
54 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
55 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
56 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
57 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
58 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
59 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
60 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
61 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
62 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
63 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
64 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
65 import org.openecomp.sdcrests.vsp.rest.data.PackageArchive;
66 import org.powermock.core.classloader.annotations.PrepareForTest;
67 import org.powermock.modules.junit4.PowerMockRunner;
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest({VspManagerFactory.class, ActivityLogManagerFactory.class,
71 OrchestrationTemplateCandidateManagerFactory.class, OrchestrationTemplateCandidateImpl.class})
72 public class OrchestrationTemplateCandidateImplTest {
74 private Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
77 private OrchestrationTemplateCandidateManager candidateManager;
79 private VendorSoftwareProductManager vendorSoftwareProductManager;
81 private PackageArchive packageArchive;
83 private VspManagerFactory vspManagerFactory;
85 private ActivityLogManager activityLogManager;
87 private ActivityLogManagerFactory activityLogManagerFactory;
89 OrchestrationTemplateCandidateManagerFactory orchestrationTemplateCandidateManagerFactory;
91 private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
93 private final String candidateId = UUID.randomUUID().toString();
94 private final String softwareProductId = UUID.randomUUID().toString();
95 private final String versionId = UUID.randomUUID().toString();
97 private final String user = "cs0008";
103 packageArchive = mock(PackageArchive.class);
104 mockStatic(VspManagerFactory.class);
105 when(VspManagerFactory.getInstance()).thenReturn(vspManagerFactory);
106 when(vspManagerFactory.createInterface()).thenReturn(vendorSoftwareProductManager);
107 mockStatic(ActivityLogManagerFactory.class);
108 when(ActivityLogManagerFactory.getInstance()).thenReturn(activityLogManagerFactory);
109 when(activityLogManagerFactory.createInterface()).thenReturn(activityLogManager);
110 whenNew(PackageArchive.class).withAnyArguments().thenReturn(packageArchive);
111 mockStatic(OrchestrationTemplateCandidateManagerFactory.class);
112 when(OrchestrationTemplateCandidateManagerFactory.getInstance()).thenReturn(orchestrationTemplateCandidateManagerFactory);
113 when(orchestrationTemplateCandidateManagerFactory.createInterface()).thenReturn(candidateManager);
114 when(packageArchive.getArchiveFileName()).thenReturn(Optional.of("test"));
115 when(packageArchive.getPackageFileContents()).thenReturn(new byte[0]);
116 UploadFileResponse uploadFileResponse = new UploadFileResponse();
117 uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
118 uploadFileResponse.setNetworkPackageName("test");
119 when(candidateManager.upload(any(), any())).thenReturn(uploadFileResponse);
122 // get using the candidate manager.
123 Optional<Pair<String,byte[]>> zipFile =
124 Optional.of(Pair.of("Hello", "World".getBytes()));
126 when(candidateManager.get(
127 ArgumentMatchers.eq(candidateId),
128 ArgumentMatchers.any())).thenReturn(zipFile);
130 when(vendorSoftwareProductManager.get(
131 ArgumentMatchers.eq(softwareProductId),
132 ArgumentMatchers.any())).thenReturn(zipFile);
135 OrchestrationTemplateActionResponse processResponse =
136 new OrchestrationTemplateActionResponse();
137 processResponse.setStatus(UploadFileStatus.Success);
138 when(candidateManager.process(
139 ArgumentMatchers.eq(candidateId),
140 ArgumentMatchers.any())).thenReturn(processResponse);
143 ValidationResponse vr = new ValidationResponse();
144 when(candidateManager.updateFilesDataStructure(
145 ArgumentMatchers.eq(candidateId),
146 ArgumentMatchers.any(),
147 ArgumentMatchers.any())).thenReturn(vr);
149 FilesDataStructure fds = new FilesDataStructure();
150 fds.setArtifacts(Arrays.asList("a","b"));
151 fds.setNested(Arrays.asList("foo", "bar"));
152 fds.setUnassigned(Arrays.asList("c", "d"));
153 fds.setModules(Arrays.asList(new Module(), new Module()));
155 when(candidateManager.getFilesDataStructure(
156 ArgumentMatchers.eq(candidateId),
157 ArgumentMatchers.any())).thenReturn(Optional.of(fds));
160 }catch (Exception e){
161 logger.error(e.getMessage(), e);
166 public void uploadSignedTest() throws SecurityManagerException {
167 when(packageArchive.isSigned()).thenReturn(true);
168 when(packageArchive.isSignatureValid()).thenReturn(true);
169 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
170 Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment(), "1");
171 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
175 public void uploadNotSignedTest(){
176 when(packageArchive.isSigned()).thenReturn(false);
177 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
178 Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment(), "1");
179 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
182 private Attachment mockAttachment() {
183 final Attachment attachment = mock(Attachment.class);
184 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
185 final byte[] bytes = "upload package Test".getBytes();
186 when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);
191 public void uploadSignNotValidTest() throws SecurityManagerException {
192 when(packageArchive.isSigned()).thenReturn(true);
193 when(packageArchive.isSignatureValid()).thenReturn(false);
194 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
195 Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment(), "1");
196 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
197 assertFalse(((UploadFileResponseDto)response.getEntity()).getErrors().isEmpty());
201 public void testCandidateGet() throws IOException {
202 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
203 Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
204 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
205 Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"),-1);
206 byte[] content = (byte[])rsp.getEntity();
207 Assert.assertEquals("World", new String(content));
211 public void testVendorSoftwareProductGet() throws IOException {
212 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
213 Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
214 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
215 Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"),-1);
216 byte[] content = (byte[])rsp.getEntity();
217 Assert.assertEquals("World", new String(content));
221 public void testMissingGet() throws IOException {
222 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
223 Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
224 Assert.assertEquals("Response status equals", Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus());
228 public void testAbort() {
230 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
231 Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
232 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
233 Assert.assertNull(rsp.getEntity());
235 catch (Exception ex) {
236 logger.error("unexpected exception", ex);
237 Assert.fail("abort should not throw an exception");
242 public void testProcess() {
244 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
245 Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
246 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
247 Assert.assertNotNull(rsp.getEntity());
248 OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto)rsp.getEntity();
249 Assert.assertEquals("status check", UploadFileStatus.Success, dto.getStatus());
251 catch (Exception ex) {
252 logger.error("unexpected exception", ex);
253 Assert.fail("abort should not throw an exception");
258 public void testFilesDataStructureUpload() {
260 FileDataStructureDto dto = new FileDataStructureDto();
261 dto.setArtifacts(Arrays.asList("a", "b", "c"));
262 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
263 Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
264 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
266 catch (Exception ex) {
267 logger.error("unexpected exception", ex);
268 Assert.fail("abort should not throw an exception");
273 public void testFilesDataStructureGet() {
275 FileDataStructureDto dto = new FileDataStructureDto();
276 dto.setArtifacts(Arrays.asList("a", "b", "c"));
277 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
278 Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
279 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
281 catch (Exception ex) {
282 logger.error("unexpected exception", ex);
283 Assert.fail("abort should not throw an exception");