1 package org.openecomp.sdcrests.vsp.rest.services;
3 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
4 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
5 import org.junit.Before;
7 import org.junit.runner.RunWith;
8 import org.mockito.Mock;
9 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
10 import org.openecomp.sdc.activitylog.ActivityLogManager;
11 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
12 import org.openecomp.sdc.logging.api.Logger;
13 import org.openecomp.sdc.logging.api.LoggerFactory;
14 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
15 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
16 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
17 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
18 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
19 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
20 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
21 import org.openecomp.sdcrests.vsp.rest.data.PackageArchive;
22 import org.powermock.core.classloader.annotations.PrepareForTest;
23 import org.powermock.modules.junit4.PowerMockRunner;
24 import javax.ws.rs.core.Response;
26 import java.util.Optional;
28 import static junit.framework.TestCase.assertEquals;
29 import static junit.framework.TestCase.assertFalse;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.MockitoAnnotations.initMocks;
32 import static org.powermock.api.mockito.PowerMockito.mock;
33 import static org.powermock.api.mockito.PowerMockito.mockStatic;
34 import static org.powermock.api.mockito.PowerMockito.when;
35 import static org.powermock.api.mockito.PowerMockito.whenNew;
37 @RunWith(PowerMockRunner.class)
38 @PrepareForTest({VspManagerFactory.class, ActivityLogManagerFactory.class,
39 OrchestrationTemplateCandidateManagerFactory.class, OrchestrationTemplateCandidateImpl.class})
40 public class OrchestrationTemplateCandidateImplTest {
42 Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
44 private OrchestrationTemplateCandidateManager candidateManager;
46 private VendorSoftwareProductManager vendorSoftwareProductManager;
48 private PackageArchive packageArchive;
50 private VspManagerFactory vspManagerFactory;
52 private ActivityLogManager activityLogManager;
54 private ActivityLogManagerFactory activityLogManagerFactory;
56 OrchestrationTemplateCandidateManagerFactory orchestrationTemplateCandidateManagerFactory;
58 private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
64 packageArchive = mock(PackageArchive.class);
65 mockStatic(VspManagerFactory.class);
66 when(VspManagerFactory.getInstance()).thenReturn(vspManagerFactory);
67 when(vspManagerFactory.createInterface()).thenReturn(vendorSoftwareProductManager);
68 mockStatic(ActivityLogManagerFactory.class);
69 when(ActivityLogManagerFactory.getInstance()).thenReturn(activityLogManagerFactory);
70 when(activityLogManagerFactory.createInterface()).thenReturn(activityLogManager);
71 whenNew(PackageArchive.class).withAnyArguments().thenReturn(packageArchive);
72 mockStatic(OrchestrationTemplateCandidateManagerFactory.class);
73 when(OrchestrationTemplateCandidateManagerFactory.getInstance()).thenReturn(orchestrationTemplateCandidateManagerFactory);
74 when(orchestrationTemplateCandidateManagerFactory.createInterface()).thenReturn(candidateManager);
75 when(packageArchive.getArchiveFileName()).thenReturn(Optional.of("test"));
76 when(packageArchive.getPackageFileContents()).thenReturn(new byte[0]);
77 UploadFileResponse uploadFileResponse = new UploadFileResponse();
78 uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
79 uploadFileResponse.setNetworkPackageName("test");
80 when(candidateManager.upload(any(), any(), any(), any(), any())).thenReturn(uploadFileResponse);
82 logger.error(e.getMessage(), e);
87 public void uploadSignedTest() throws SecurityManagerException {
88 when(packageArchive.isSigned()).thenReturn(true);
89 when(packageArchive.isSignatureValid()).thenReturn(true);
90 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
91 Attachment attachment = mock(Attachment.class);
92 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
93 Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
94 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
99 public void uploadNotSignedTest(){
100 when(packageArchive.isSigned()).thenReturn(false);
101 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
102 Attachment attachment = mock(Attachment.class);
103 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
104 Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
105 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
110 public void uploadSignNotValidTest() throws SecurityManagerException {
111 when(packageArchive.isSigned()).thenReturn(true);
112 when(packageArchive.isSignatureValid()).thenReturn(false);
113 orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
114 Attachment attachment = mock(Attachment.class);
115 when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
116 Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
117 assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
118 assertFalse(((UploadFileResponseDto)response.getEntity()).getErrors().isEmpty());