dec6342cb85f1f3296728f0047476b86a1da3794
[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.mockito.Mockito.when;
28
29 import java.io.IOException;
30 import java.util.Arrays;
31 import java.util.Optional;
32 import java.util.UUID;
33 import javax.activation.DataHandler;
34 import javax.ws.rs.core.Response;
35 import org.apache.commons.lang3.tuple.Pair;
36 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
37 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
38 import org.junit.Assert;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.ArgumentMatchers;
42 import org.mockito.Mock;
43 import org.mockito.Mockito;
44 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
45 import org.openecomp.sdc.activitylog.ActivityLogManager;
46 import org.openecomp.sdc.logging.api.Logger;
47 import org.openecomp.sdc.logging.api.LoggerFactory;
48 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
49 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
50 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
51 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
52 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
53 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
54 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
55 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
56 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
57 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
58 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
59
60 public class OrchestrationTemplateCandidateImplTest {
61
62     private Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
63
64     @Mock
65     private OrchestrationTemplateCandidateManager candidateManager;
66     @Mock
67     private VendorSoftwareProductManager vendorSoftwareProductManager;
68
69     @Mock
70     private ActivityLogManager activityLogManager;
71
72     private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
73
74     private final String candidateId = UUID.randomUUID().toString();
75     private final String softwareProductId = UUID.randomUUID().toString();
76     private final String versionId = UUID.randomUUID().toString();
77
78     private final String user = "cs0008";
79
80     @Before
81     public void setUp(){
82         try {
83             initMocks(this);
84             UploadFileResponse uploadFileResponse = new UploadFileResponse();
85             uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
86             uploadFileResponse.setNetworkPackageName("test");
87             when(candidateManager.upload(any(), any())).thenReturn(uploadFileResponse);
88
89
90             // get using the candidate manager.
91             Optional<Pair<String,byte[]>> zipFile =
92                     Optional.of(Pair.of("Hello", "World".getBytes()));
93
94             when(candidateManager.get(
95                     ArgumentMatchers.eq(candidateId),
96                     ArgumentMatchers.any())).thenReturn(zipFile);
97
98             when(vendorSoftwareProductManager.get(
99                     ArgumentMatchers.eq(softwareProductId),
100                     ArgumentMatchers.any())).thenReturn(zipFile);
101
102
103             OrchestrationTemplateActionResponse processResponse =
104                     new OrchestrationTemplateActionResponse();
105             processResponse.setStatus(UploadFileStatus.Success);
106             when(candidateManager.process(
107                     ArgumentMatchers.eq(candidateId),
108                     ArgumentMatchers.any())).thenReturn(processResponse);
109
110
111             ValidationResponse vr = new ValidationResponse();
112             when(candidateManager.updateFilesDataStructure(
113                     ArgumentMatchers.eq(candidateId),
114                     ArgumentMatchers.any(),
115                     ArgumentMatchers.any())).thenReturn(vr);
116
117             FilesDataStructure fds = new FilesDataStructure();
118             fds.setArtifacts(Arrays.asList("a","b"));
119             fds.setNested(Arrays.asList("foo", "bar"));
120             fds.setUnassigned(Arrays.asList("c", "d"));
121             fds.setModules(Arrays.asList(new Module(), new Module()));
122
123             when(candidateManager.getFilesDataStructure(
124                     ArgumentMatchers.eq(candidateId),
125                     ArgumentMatchers.any())).thenReturn(Optional.of(fds));
126
127             orchestrationTemplateCandidate =
128                 new OrchestrationTemplateCandidateImpl(candidateManager, vendorSoftwareProductManager, activityLogManager);
129
130
131         }catch (Exception e){
132            logger.error(e.getMessage(), e);
133         }
134     }
135
136     @Test
137     public void uploadSignedTest() {
138         Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment("filename.zip"), "1");
139         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
140     }
141
142     @Test
143     public void uploadNotSignedTest(){
144         Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment("filename.csar"), "1");
145         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
146     }
147
148     private Attachment mockAttachment(final String fileName) {
149         final Attachment attachment = Mockito.mock(Attachment.class);
150         when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
151         final DataHandler dataHandler = Mockito.mock(DataHandler.class);
152         when(dataHandler.getName()).thenReturn(fileName);
153         when(attachment.getDataHandler()).thenReturn(dataHandler);
154         final byte[] bytes = "upload package Test".getBytes();
155         when(attachment.getObject(ArgumentMatchers.any())).thenReturn(bytes);
156         return attachment;
157     }
158
159     @Test
160     public void uploadSignNotValidTest() {
161         Response response = orchestrationTemplateCandidate.upload("1", "1", mockAttachment("filename.zip"), "1");
162         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
163         assertFalse(((UploadFileResponseDto)response.getEntity()).getErrors().isEmpty());
164     }
165
166     @Test
167     public void testCandidateGet() throws IOException {
168         Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
169         Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
170         Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"),-1);
171         byte[] content = (byte[])rsp.getEntity();
172         Assert.assertEquals("World", new String(content));
173     }
174
175     @Test
176     public void testVendorSoftwareProductGet() throws IOException {
177         Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
178         Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
179         Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"),-1);
180         byte[] content = (byte[])rsp.getEntity();
181         Assert.assertEquals("World", new String(content));
182     }
183
184     @Test
185     public void testMissingGet() throws IOException {
186         Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
187         Assert.assertEquals("Response status equals", Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus());
188     }
189
190     @Test
191     public void testAbort() {
192         try {
193             Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
194             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
195             Assert.assertNull(rsp.getEntity());
196         }
197         catch (Exception ex) {
198             logger.error("unexpected exception", ex);
199             Assert.fail("abort should not throw an exception");
200         }
201     }
202
203     @Test
204     public void testProcess() {
205         try {
206             Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
207             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
208             Assert.assertNotNull(rsp.getEntity());
209             OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto)rsp.getEntity();
210             Assert.assertEquals("status check", UploadFileStatus.Success, dto.getStatus());
211         }
212         catch (Exception ex) {
213             logger.error("unexpected exception", ex);
214             Assert.fail("abort should not throw an exception");
215         }
216     }
217
218     @Test
219     public void testFilesDataStructureUpload() {
220         try {
221             FileDataStructureDto dto = new FileDataStructureDto();
222             dto.setArtifacts(Arrays.asList("a", "b", "c"));
223             Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
224             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
225         }
226         catch (Exception ex) {
227             logger.error("unexpected exception", ex);
228             Assert.fail("abort should not throw an exception");
229         }
230     }
231
232     @Test
233     public void testFilesDataStructureGet() {
234         try {
235             FileDataStructureDto dto = new FileDataStructureDto();
236             dto.setArtifacts(Arrays.asList("a", "b", "c"));
237             Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
238             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
239         }
240         catch (Exception ex) {
241             logger.error("unexpected exception", ex);
242             Assert.fail("abort should not throw an exception");
243         }
244     }
245
246 }