Added oparent to sdc main
[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 org.apache.commons.lang3.tuple.Pair;
24 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
25 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.ArgumentMatchers;
31 import org.mockito.Mock;
32 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
33 import org.openecomp.sdc.activitylog.ActivityLogManager;
34 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
35 import org.openecomp.sdc.logging.api.Logger;
36 import org.openecomp.sdc.logging.api.LoggerFactory;
37 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
38 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
39 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
40 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
41 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
42 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
44 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
48 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
49 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
50 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
51 import org.openecomp.sdcrests.vsp.rest.data.PackageArchive;
52 import org.powermock.core.classloader.annotations.PrepareForTest;
53 import org.powermock.modules.junit4.PowerMockRunner;
54
55 import javax.ws.rs.core.Response;
56 import java.io.IOException;
57 import java.util.Arrays;
58 import java.util.Optional;
59 import java.util.UUID;
60
61 import static junit.framework.TestCase.assertEquals;
62 import static junit.framework.TestCase.assertFalse;
63 import static org.mockito.ArgumentMatchers.any;
64 import static org.mockito.MockitoAnnotations.initMocks;
65 import static org.powermock.api.mockito.PowerMockito.*;
66
67 @RunWith(PowerMockRunner.class)
68 @PrepareForTest({VspManagerFactory.class, ActivityLogManagerFactory.class,
69         OrchestrationTemplateCandidateManagerFactory.class, OrchestrationTemplateCandidateImpl.class})
70 public class OrchestrationTemplateCandidateImplTest {
71
72     private Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
73
74     @Mock
75     private OrchestrationTemplateCandidateManager candidateManager;
76     @Mock
77     private VendorSoftwareProductManager vendorSoftwareProductManager;
78     @Mock
79     private PackageArchive packageArchive;
80     @Mock
81     private VspManagerFactory vspManagerFactory;
82     @Mock
83     private ActivityLogManager activityLogManager;
84     @Mock
85     private ActivityLogManagerFactory activityLogManagerFactory;
86     @Mock
87     OrchestrationTemplateCandidateManagerFactory orchestrationTemplateCandidateManagerFactory;
88
89     private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
90
91     private final String candidateId = UUID.randomUUID().toString();
92     private final String softwareProductId = UUID.randomUUID().toString();
93     private final String versionId = UUID.randomUUID().toString();
94
95     private final String user = "cs0008";
96
97     @Before
98     public void setUp(){
99         try {
100             initMocks(this);
101             packageArchive = mock(PackageArchive.class);
102             mockStatic(VspManagerFactory.class);
103             when(VspManagerFactory.getInstance()).thenReturn(vspManagerFactory);
104             when(vspManagerFactory.createInterface()).thenReturn(vendorSoftwareProductManager);
105             mockStatic(ActivityLogManagerFactory.class);
106             when(ActivityLogManagerFactory.getInstance()).thenReturn(activityLogManagerFactory);
107             when(activityLogManagerFactory.createInterface()).thenReturn(activityLogManager);
108             whenNew(PackageArchive.class).withAnyArguments().thenReturn(packageArchive);
109             mockStatic(OrchestrationTemplateCandidateManagerFactory.class);
110             when(OrchestrationTemplateCandidateManagerFactory.getInstance()).thenReturn(orchestrationTemplateCandidateManagerFactory);
111             when(orchestrationTemplateCandidateManagerFactory.createInterface()).thenReturn(candidateManager);
112             when(packageArchive.getArchiveFileName()).thenReturn(Optional.of("test"));
113             when(packageArchive.getPackageFileContents()).thenReturn(new byte[0]);
114             UploadFileResponse uploadFileResponse = new UploadFileResponse();
115             uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
116             uploadFileResponse.setNetworkPackageName("test");
117             when(candidateManager.upload(any(), any(), any(), any(), any())).thenReturn(uploadFileResponse);
118
119
120             // get using the candidate manager.
121             Optional<Pair<String,byte[]>> zipFile =
122                     Optional.of(Pair.of("Hello", "World".getBytes()));
123
124             when(candidateManager.get(
125                     ArgumentMatchers.eq(candidateId),
126                     ArgumentMatchers.any())).thenReturn(zipFile);
127
128             when(vendorSoftwareProductManager.get(
129                     ArgumentMatchers.eq(softwareProductId),
130                     ArgumentMatchers.any())).thenReturn(zipFile);
131
132
133             OrchestrationTemplateActionResponse processResponse =
134                     new OrchestrationTemplateActionResponse();
135             processResponse.setStatus(UploadFileStatus.Success);
136             when(candidateManager.process(
137                     ArgumentMatchers.eq(candidateId),
138                     ArgumentMatchers.any())).thenReturn(processResponse);
139
140
141             ValidationResponse vr = new ValidationResponse();
142             when(candidateManager.updateFilesDataStructure(
143                     ArgumentMatchers.eq(candidateId),
144                     ArgumentMatchers.any(),
145                     ArgumentMatchers.any())).thenReturn(vr);
146
147             FilesDataStructure fds = new FilesDataStructure();
148             fds.setArtifacts(Arrays.asList("a","b"));
149             fds.setNested(Arrays.asList("foo", "bar"));
150             fds.setUnassigned(Arrays.asList("c", "d"));
151             fds.setModules(Arrays.asList(new Module(), new Module()));
152
153             when(candidateManager.getFilesDataStructure(
154                     ArgumentMatchers.eq(candidateId),
155                     ArgumentMatchers.any())).thenReturn(Optional.of(fds));
156
157
158         }catch (Exception e){
159            logger.error(e.getMessage(), e);
160         }
161     }
162
163     @Test
164     public void uploadSignedTest() throws SecurityManagerException {
165         when(packageArchive.isSigned()).thenReturn(true);
166         when(packageArchive.isSignatureValid()).thenReturn(true);
167         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
168         Attachment attachment = mock(Attachment.class);
169         when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
170         Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
171         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
172
173     }
174
175     @Test
176     public void uploadNotSignedTest(){
177         when(packageArchive.isSigned()).thenReturn(false);
178         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
179         Attachment attachment = mock(Attachment.class);
180         when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
181         Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
182         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
183
184     }
185
186     @Test
187     public void uploadSignNotValidTest() throws SecurityManagerException {
188         when(packageArchive.isSigned()).thenReturn(true);
189         when(packageArchive.isSignatureValid()).thenReturn(false);
190         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
191         Attachment attachment = mock(Attachment.class);
192         when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
193         Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
194         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
195         assertFalse(((UploadFileResponseDto)response.getEntity()).getErrors().isEmpty());
196
197     }
198
199     @Test
200     public void testCandidateGet() throws IOException {
201         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
202         Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
203         Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
204         Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"),-1);
205         byte[] content = (byte[])rsp.getEntity();
206         Assert.assertEquals("World", new String(content));
207
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 }