589be207c1edb2f9feb0da5dc1c73bf69d695b5a
[sdc.git] /
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
31 import java.io.IOException;
32 import java.util.Arrays;
33 import java.util.Optional;
34 import java.util.UUID;
35 import javax.activation.DataHandler;
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.types.OrchestrationTemplateActionResponse;
56 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
57 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
58 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
59 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
60 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
61 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
62 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
63 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
64 import org.powermock.core.classloader.annotations.PrepareForTest;
65 import org.powermock.modules.junit4.PowerMockRunner;
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 VspManagerFactory vspManagerFactory;
80     @Mock
81     private ActivityLogManager activityLogManager;
82     @Mock
83     private ActivityLogManagerFactory activityLogManagerFactory;
84     @Mock
85     OrchestrationTemplateCandidateManagerFactory orchestrationTemplateCandidateManagerFactory;
86
87     private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
88
89     private final String candidateId = UUID.randomUUID().toString();
90     private final String softwareProductId = UUID.randomUUID().toString();
91     private final String versionId = UUID.randomUUID().toString();
92
93     private final String user = "cs0008";
94
95     @Before
96     public void setUp(){
97         try {
98             initMocks(this);
99             mockStatic(VspManagerFactory.class);
100             when(VspManagerFactory.getInstance()).thenReturn(vspManagerFactory);
101             when(vspManagerFactory.createInterface()).thenReturn(vendorSoftwareProductManager);
102             mockStatic(ActivityLogManagerFactory.class);
103             when(ActivityLogManagerFactory.getInstance()).thenReturn(activityLogManagerFactory);
104             when(activityLogManagerFactory.createInterface()).thenReturn(activityLogManager);
105             mockStatic(OrchestrationTemplateCandidateManagerFactory.class);
106             when(OrchestrationTemplateCandidateManagerFactory.getInstance()).thenReturn(orchestrationTemplateCandidateManagerFactory);
107             when(orchestrationTemplateCandidateManagerFactory.createInterface()).thenReturn(candidateManager);
108             UploadFileResponse uploadFileResponse = new UploadFileResponse();
109             uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
110             uploadFileResponse.setNetworkPackageName("test");
111             when(candidateManager.upload(any(), any())).thenReturn(uploadFileResponse);
112
113
114             // get using the candidate manager.
115             Optional<Pair<String,byte[]>> zipFile =
116                     Optional.of(Pair.of("Hello", "World".getBytes()));
117
118             when(candidateManager.get(
119                     ArgumentMatchers.eq(candidateId),
120                     ArgumentMatchers.any())).thenReturn(zipFile);
121
122             when(vendorSoftwareProductManager.get(
123                     ArgumentMatchers.eq(softwareProductId),
124                     ArgumentMatchers.any())).thenReturn(zipFile);
125
126
127             OrchestrationTemplateActionResponse processResponse =
128                     new OrchestrationTemplateActionResponse();
129             processResponse.setStatus(UploadFileStatus.Success);
130             when(candidateManager.process(
131                     ArgumentMatchers.eq(candidateId),
132                     ArgumentMatchers.any())).thenReturn(processResponse);
133
134
135             ValidationResponse vr = new ValidationResponse();
136             when(candidateManager.updateFilesDataStructure(
137                     ArgumentMatchers.eq(candidateId),
138                     ArgumentMatchers.any(),
139                     ArgumentMatchers.any())).thenReturn(vr);
140
141             FilesDataStructure fds = new FilesDataStructure();
142             fds.setArtifacts(Arrays.asList("a","b"));
143             fds.setNested(Arrays.asList("foo", "bar"));
144             fds.setUnassigned(Arrays.asList("c", "d"));
145             fds.setModules(Arrays.asList(new Module(), new Module()));
146
147             when(candidateManager.getFilesDataStructure(
148                     ArgumentMatchers.eq(candidateId),
149                     ArgumentMatchers.any())).thenReturn(Optional.of(fds));
150
151
152         }catch (Exception e){
153            logger.error(e.getMessage(), e);
154         }
155     }
156
157     @Test
158     public void uploadSignedTest() {
159         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
160         Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment("filename.zip"), "1");
161         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
162     }
163
164     @Test
165     public void uploadNotSignedTest(){
166         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
167         Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment("filename.csar"), "1");
168         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
169     }
170
171     private Attachment mockAttachment(final String fileName) {
172         final Attachment attachment = mock(Attachment.class);
173         when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
174         final DataHandler dataHandler = mock(DataHandler.class);
175         when(dataHandler.getName()).thenReturn(fileName);
176         when(attachment.getDataHandler()).thenReturn(dataHandler);
177         final byte[] bytes = "upload package Test".getBytes();
178         when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);
179         return attachment;
180     }
181
182     @Test
183     public void uploadSignNotValidTest() {
184         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
185         Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment("filename.zip"), "1");
186         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
187         assertFalse(((UploadFileResponseDto)response.getEntity()).getErrors().isEmpty());
188     }
189
190     @Test
191     public void testCandidateGet() throws IOException {
192         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
193         Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
194         Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
195         Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"),-1);
196         byte[] content = (byte[])rsp.getEntity();
197         Assert.assertEquals("World", new String(content));
198     }
199
200     @Test
201     public void testVendorSoftwareProductGet() throws IOException {
202         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
203         Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
204         Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
205         Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"),-1);
206         byte[] content = (byte[])rsp.getEntity();
207         Assert.assertEquals("World", new String(content));
208     }
209
210     @Test
211     public void testMissingGet() throws IOException {
212         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
213         Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
214         Assert.assertEquals("Response status equals", Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus());
215     }
216
217     @Test
218     public void testAbort() {
219         try {
220             orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
221             Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
222             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
223             Assert.assertNull(rsp.getEntity());
224         }
225         catch (Exception ex) {
226             logger.error("unexpected exception", ex);
227             Assert.fail("abort should not throw an exception");
228         }
229     }
230
231     @Test
232     public void testProcess() {
233         try {
234             orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
235             Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
236             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
237             Assert.assertNotNull(rsp.getEntity());
238             OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto)rsp.getEntity();
239             Assert.assertEquals("status check", UploadFileStatus.Success, dto.getStatus());
240         }
241         catch (Exception ex) {
242             logger.error("unexpected exception", ex);
243             Assert.fail("abort should not throw an exception");
244         }
245     }
246
247     @Test
248     public void testFilesDataStructureUpload() {
249         try {
250             FileDataStructureDto dto = new FileDataStructureDto();
251             dto.setArtifacts(Arrays.asList("a", "b", "c"));
252             orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
253             Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
254             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
255         }
256         catch (Exception ex) {
257             logger.error("unexpected exception", ex);
258             Assert.fail("abort should not throw an exception");
259         }
260     }
261
262     @Test
263     public void testFilesDataStructureGet() {
264         try {
265             FileDataStructureDto dto = new FileDataStructureDto();
266             dto.setArtifacts(Arrays.asList("a", "b", "c"));
267             orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
268             Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
269             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
270         }
271         catch (Exception ex) {
272             logger.error("unexpected exception", ex);
273             Assert.fail("abort should not throw an exception");
274         }
275     }
276
277 }