Onboard Package Handling
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / test / java / org / openecomp / sdcrests / vsp / rest / services / OrchestrationTemplateCandidateImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdcrests.vsp.rest.services;
22
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;
31
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;
68
69 @RunWith(PowerMockRunner.class)
70 @PrepareForTest({VspManagerFactory.class, ActivityLogManagerFactory.class,
71         OrchestrationTemplateCandidateManagerFactory.class, OrchestrationTemplateCandidateImpl.class})
72 public class OrchestrationTemplateCandidateImplTest {
73
74     private Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
75
76     @Mock
77     private OrchestrationTemplateCandidateManager candidateManager;
78     @Mock
79     private VendorSoftwareProductManager vendorSoftwareProductManager;
80     @Mock
81     private PackageArchive packageArchive;
82     @Mock
83     private VspManagerFactory vspManagerFactory;
84     @Mock
85     private ActivityLogManager activityLogManager;
86     @Mock
87     private ActivityLogManagerFactory activityLogManagerFactory;
88     @Mock
89     OrchestrationTemplateCandidateManagerFactory orchestrationTemplateCandidateManagerFactory;
90
91     private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
92
93     private final String candidateId = UUID.randomUUID().toString();
94     private final String softwareProductId = UUID.randomUUID().toString();
95     private final String versionId = UUID.randomUUID().toString();
96
97     private final String user = "cs0008";
98
99     @Before
100     public void setUp(){
101         try {
102             initMocks(this);
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);
120
121
122             // get using the candidate manager.
123             Optional<Pair<String,byte[]>> zipFile =
124                     Optional.of(Pair.of("Hello", "World".getBytes()));
125
126             when(candidateManager.get(
127                     ArgumentMatchers.eq(candidateId),
128                     ArgumentMatchers.any())).thenReturn(zipFile);
129
130             when(vendorSoftwareProductManager.get(
131                     ArgumentMatchers.eq(softwareProductId),
132                     ArgumentMatchers.any())).thenReturn(zipFile);
133
134
135             OrchestrationTemplateActionResponse processResponse =
136                     new OrchestrationTemplateActionResponse();
137             processResponse.setStatus(UploadFileStatus.Success);
138             when(candidateManager.process(
139                     ArgumentMatchers.eq(candidateId),
140                     ArgumentMatchers.any())).thenReturn(processResponse);
141
142
143             ValidationResponse vr = new ValidationResponse();
144             when(candidateManager.updateFilesDataStructure(
145                     ArgumentMatchers.eq(candidateId),
146                     ArgumentMatchers.any(),
147                     ArgumentMatchers.any())).thenReturn(vr);
148
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()));
154
155             when(candidateManager.getFilesDataStructure(
156                     ArgumentMatchers.eq(candidateId),
157                     ArgumentMatchers.any())).thenReturn(Optional.of(fds));
158
159
160         }catch (Exception e){
161            logger.error(e.getMessage(), e);
162         }
163     }
164
165     @Test
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());
172     }
173
174     @Test
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());
180     }
181
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);
187         return attachment;
188     }
189
190     @Test
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());
198     }
199
200     @Test
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));
208     }
209
210     @Test
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));
218     }
219
220     @Test
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());
225     }
226
227     @Test
228     public void testAbort() {
229         try {
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());
234         }
235         catch (Exception ex) {
236             logger.error("unexpected exception", ex);
237             Assert.fail("abort should not throw an exception");
238         }
239     }
240
241     @Test
242     public void testProcess() {
243         try {
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());
250         }
251         catch (Exception ex) {
252             logger.error("unexpected exception", ex);
253             Assert.fail("abort should not throw an exception");
254         }
255     }
256
257     @Test
258     public void testFilesDataStructureUpload() {
259         try {
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());
265         }
266         catch (Exception ex) {
267             logger.error("unexpected exception", ex);
268             Assert.fail("abort should not throw an exception");
269         }
270     }
271
272     @Test
273     public void testFilesDataStructureGet() {
274         try {
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());
280         }
281         catch (Exception ex) {
282             logger.error("unexpected exception", ex);
283             Assert.fail("abort should not throw an exception");
284         }
285     }
286
287 }