2dc6cd737c659ce3d6b1b205066ed7c1004ac008
[sdc.git] /
1 package org.openecomp.sdcrests.vsp.rest.services;
2
3 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
4 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
5 import org.junit.Before;
6 import org.junit.Test;
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;
25
26 import java.util.Optional;
27
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;
36
37 @RunWith(PowerMockRunner.class)
38 @PrepareForTest({VspManagerFactory.class, ActivityLogManagerFactory.class,
39         OrchestrationTemplateCandidateManagerFactory.class, OrchestrationTemplateCandidateImpl.class})
40 public class OrchestrationTemplateCandidateImplTest {
41
42     Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
43     @Mock
44     private OrchestrationTemplateCandidateManager candidateManager;
45     @Mock
46     private VendorSoftwareProductManager vendorSoftwareProductManager;
47     @Mock
48     private PackageArchive packageArchive;
49     @Mock
50     private VspManagerFactory vspManagerFactory;
51     @Mock
52     private ActivityLogManager activityLogManager;
53     @Mock
54     private ActivityLogManagerFactory activityLogManagerFactory;
55     @Mock
56     OrchestrationTemplateCandidateManagerFactory orchestrationTemplateCandidateManagerFactory;
57
58     private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
59
60     @Before
61     public void setUp(){
62         try {
63             initMocks(this);
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);
81         }catch (Exception e){
82            logger.error(e.getMessage(), e);
83         }
84     }
85
86     @Test
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());
95
96     }
97
98     @Test
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());
106
107     }
108
109     @Test
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());
119
120     }
121 }