1 package org.openecomp.sdcrests.vsp.rest.services;
3 import org.apache.commons.lang3.tuple.Pair;
4 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
5 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
6 import org.junit.Assert;
7 import org.junit.Before;
9 import org.junit.runner.RunWith;
10 import org.mockito.ArgumentMatchers;
11 import org.mockito.Mock;
12 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
13 import org.openecomp.sdc.activitylog.ActivityLogManager;
14 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
15 import org.openecomp.sdc.logging.api.Logger;
16 import org.openecomp.sdc.logging.api.LoggerFactory;
17 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
18 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
19 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
20 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
21 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
22 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
23 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
24 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
25 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
26 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
28 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
29 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
30 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
31 import org.openecomp.sdcrests.vsp.rest.data.PackageArchive;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.junit4.PowerMockRunner;
35 import javax.ws.rs.core.Response;
36 import java.io.IOException;
37 import java.util.Arrays;
38 import java.util.Optional;
39 import java.util.UUID;
41 import static junit.framework.TestCase.assertEquals;
42 import static junit.framework.TestCase.assertFalse;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.MockitoAnnotations.initMocks;
45 import static org.powermock.api.mockito.PowerMockito.*;
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({VspManagerFactory.class, ActivityLogManagerFactory.class,
49 OrchestrationTemplateCandidateManagerFactory.class, OrchestrationTemplateCandidateImpl.class})
50 public class OrchestrationTemplateCandidateImplTest {
52 private Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
55 private OrchestrationTemplateCandidateManager candidateManager;
57 private VendorSoftwareProductManager vendorSoftwareProductManager;
59 private PackageArchive packageArchive;
61 private VspManagerFactory vspManagerFactory;
63 private ActivityLogManager activityLogManager;
65 private ActivityLogManagerFactory activityLogManagerFactory;
67 OrchestrationTemplateCandidateManagerFactory orchestrationTemplateCandidateManagerFactory;
69 private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
71 private final String candidateId = UUID.randomUUID().toString();
72 private final String softwareProductId = UUID.randomUUID().toString();
73 private final String versionId = UUID.randomUUID().toString();
75 private final String user = "cs0008";
81 packageArchive = mock(PackageArchive.class);
82 mockStatic(VspManagerFactory.class);
83 when(VspManagerFactory.getInstance()).thenReturn(vspManagerFactory);
84 when(vspManagerFactory.createInterface()).thenReturn(vendorSoftwareProductManager);
85 mockStatic(ActivityLogManagerFactory.class);
86 when(ActivityLogManagerFactory.getInstance()).thenReturn(activityLogManagerFactory);
87 when(activityLogManagerFactory.createInterface()).thenReturn(activityLogManager);
88 whenNew(PackageArchive.class).withAnyArguments().thenReturn(packageArchive);
89 mockStatic(OrchestrationTemplateCandidateManagerFactory.class);
90 when(OrchestrationTemplateCandidateManagerFactory.getInstance()).thenReturn(orchestrationTemplateCandidateManagerFactory);
91 when(orchestrationTemplateCandidateManagerFactory.createInterface()).thenReturn(candidateManager);
92 when(packageArchive.getArchiveFileName()).thenReturn(Optional.of("test"));
93 when(packageArchive.getPackageFileContents()).thenReturn(new byte[0]);
94 UploadFileResponse uploadFileResponse = new UploadFileResponse();
95 uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
96 uploadFileResponse.setNetworkPackageName("test");
97 when(candidateManager.upload(any(), any(), any(), any(), any())).thenReturn(uploadFileResponse);
100 // get using the candidate manager.
101 Optional<Pair<String,byte[]>> zipFile =
102 Optional.of(Pair.of("Hello", "World".getBytes()));
104 when(candidateManager.get(
105 ArgumentMatchers.eq(candidateId),
106 ArgumentMatchers.any())).thenReturn(zipFile);
108 when(vendorSoftwareProductManager.get(
109 ArgumentMatchers.eq(softwareProductId),
110 ArgumentMatchers.any())).thenReturn(zipFile);
113 OrchestrationTemplateActionResponse processResponse =
114 new OrchestrationTemplateActionResponse();
115 processResponse.setStatus(UploadFileStatus.Success);
116 when(candidateManager.process(
117 ArgumentMatchers.eq(candidateId),
118 ArgumentMatchers.any())).thenReturn(processResponse);
121 ValidationResponse vr = new ValidationResponse();
122 when(candidateManager.updateFilesDataStructure(
123 ArgumentMatchers.eq(candidateId),
124 ArgumentMatchers.any(),
125 ArgumentMatchers.any())).thenReturn(vr);
127 FilesDataStructure fds = new FilesDataStructure();
128 fds.setArtifacts(Arrays.asList("a","b"));
129 fds.setNested(Arrays.asList("foo", "bar"));
130 fds.setUnassigned(Arrays.asList("c", "d"));
131 fds.setModules(Arrays.asList(new Module(), new Module()));
133 when(candidateManager.getFilesDataStructure(
134 ArgumentMatchers.eq(candidateId),
135 ArgumentMatchers.any())).thenReturn(Optional.of(fds));
138 }catch (Exception e){
139 logger.error(e.getMessage(), e);
144 public void uploadSignedTest() throws SecurityManagerException {
145 when(packageArchive.isSigned()).thenReturn(true);
146 when(packageArchive.isSignatureValid()).thenReturn(true);
147 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
148 Attachment attachment = mock(Attachment.class);
149 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
150 Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
151 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
156 public void uploadNotSignedTest(){
157 when(packageArchive.isSigned()).thenReturn(false);
158 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
159 Attachment attachment = mock(Attachment.class);
160 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
161 Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
162 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
167 public void uploadSignNotValidTest() throws SecurityManagerException {
168 when(packageArchive.isSigned()).thenReturn(true);
169 when(packageArchive.isSignatureValid()).thenReturn(false);
170 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
171 Attachment attachment = mock(Attachment.class);
172 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
173 Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
174 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
175 assertFalse(((UploadFileResponseDto)response.getEntity()).getErrors().isEmpty());
180 public void testCandidateGet() throws IOException {
181 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
182 Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
183 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
184 Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"),-1);
185 byte[] content = (byte[])rsp.getEntity();
186 Assert.assertEquals("World", new String(content));
191 public void testVendorSoftwareProductGet() throws IOException {
192 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
193 Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
194 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
195 Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"),-1);
196 byte[] content = (byte[])rsp.getEntity();
197 Assert.assertEquals("World", new String(content));
201 public void testMissingGet() throws IOException {
202 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
203 Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
204 Assert.assertEquals("Response status equals", Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus());
208 public void testAbort() {
210 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
211 Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
212 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
213 Assert.assertNull(rsp.getEntity());
215 catch (Exception ex) {
216 logger.error("unexpected exception", ex);
217 Assert.fail("abort should not throw an exception");
222 public void testProcess() {
224 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
225 Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
226 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
227 Assert.assertNotNull(rsp.getEntity());
228 OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto)rsp.getEntity();
229 Assert.assertEquals("status check", UploadFileStatus.Success, dto.getStatus());
231 catch (Exception ex) {
232 logger.error("unexpected exception", ex);
233 Assert.fail("abort should not throw an exception");
238 public void testFilesDataStructureUpload() {
240 FileDataStructureDto dto = new FileDataStructureDto();
241 dto.setArtifacts(Arrays.asList("a", "b", "c"));
242 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
243 Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
244 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
246 catch (Exception ex) {
247 logger.error("unexpected exception", ex);
248 Assert.fail("abort should not throw an exception");
253 public void testFilesDataStructureGet() {
255 FileDataStructureDto dto = new FileDataStructureDto();
256 dto.setArtifacts(Arrays.asList("a", "b", "c"));
257 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
258 Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
259 Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
261 catch (Exception ex) {
262 logger.error("unexpected exception", ex);
263 Assert.fail("abort should not throw an exception");