Added some JUnit tests to improve coverage. Additional work could be added to improv...
[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 package org.openecomp.sdcrests.vsp.rest.services;
2
3 import org.apache.commons.lang3.tuple.Pair;
4 import org.apache.cxf.jaxrs.ext.multipart.Attachment;
5 import org.apache.cxf.jaxrs.ext.multipart.ContentDisposition;
6 import org.junit.Assert;
7 import org.junit.Before;
8 import org.junit.Test;
9 import org.junit.runner.RunWith;
10 import org.mockito.ArgumentMatchers;
11 import org.mockito.Mock;
12 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
13 import org.openecomp.sdc.activitylog.ActivityLogManager;
14 import org.openecomp.sdc.activitylog.ActivityLogManagerFactory;
15 import org.openecomp.sdc.logging.api.Logger;
16 import org.openecomp.sdc.logging.api.LoggerFactory;
17 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManager;
18 import org.openecomp.sdc.vendorsoftwareproduct.OrchestrationTemplateCandidateManagerFactory;
19 import org.openecomp.sdc.vendorsoftwareproduct.VendorSoftwareProductManager;
20 import org.openecomp.sdc.vendorsoftwareproduct.VspManagerFactory;
21 import org.openecomp.sdc.vendorsoftwareproduct.security.SecurityManagerException;
22 import org.openecomp.sdc.vendorsoftwareproduct.types.OrchestrationTemplateActionResponse;
23 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
24 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileStatus;
25 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
26 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.FilesDataStructure;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.candidateheat.Module;
28 import org.openecomp.sdcrests.vendorsoftwareproducts.types.FileDataStructureDto;
29 import org.openecomp.sdcrests.vendorsoftwareproducts.types.OrchestrationTemplateActionResponseDto;
30 import org.openecomp.sdcrests.vendorsoftwareproducts.types.UploadFileResponseDto;
31 import org.openecomp.sdcrests.vsp.rest.data.PackageArchive;
32 import org.powermock.core.classloader.annotations.PrepareForTest;
33 import org.powermock.modules.junit4.PowerMockRunner;
34
35 import javax.ws.rs.core.Response;
36 import java.io.IOException;
37 import java.util.Arrays;
38 import java.util.Optional;
39 import java.util.UUID;
40
41 import static junit.framework.TestCase.assertEquals;
42 import static junit.framework.TestCase.assertFalse;
43 import static org.mockito.ArgumentMatchers.any;
44 import static org.mockito.MockitoAnnotations.initMocks;
45 import static org.powermock.api.mockito.PowerMockito.*;
46
47 @RunWith(PowerMockRunner.class)
48 @PrepareForTest({VspManagerFactory.class, ActivityLogManagerFactory.class,
49         OrchestrationTemplateCandidateManagerFactory.class, OrchestrationTemplateCandidateImpl.class})
50 public class OrchestrationTemplateCandidateImplTest {
51
52     private Logger logger = LoggerFactory.getLogger(OrchestrationTemplateCandidateImplTest.class);
53
54     @Mock
55     private OrchestrationTemplateCandidateManager candidateManager;
56     @Mock
57     private VendorSoftwareProductManager vendorSoftwareProductManager;
58     @Mock
59     private PackageArchive packageArchive;
60     @Mock
61     private VspManagerFactory vspManagerFactory;
62     @Mock
63     private ActivityLogManager activityLogManager;
64     @Mock
65     private ActivityLogManagerFactory activityLogManagerFactory;
66     @Mock
67     OrchestrationTemplateCandidateManagerFactory orchestrationTemplateCandidateManagerFactory;
68
69     private OrchestrationTemplateCandidateImpl orchestrationTemplateCandidate;
70
71     private final String candidateId = UUID.randomUUID().toString();
72     private final String softwareProductId = UUID.randomUUID().toString();
73     private final String versionId = UUID.randomUUID().toString();
74
75     private final String user = "cs0008";
76
77     @Before
78     public void setUp(){
79         try {
80             initMocks(this);
81             packageArchive = mock(PackageArchive.class);
82             mockStatic(VspManagerFactory.class);
83             when(VspManagerFactory.getInstance()).thenReturn(vspManagerFactory);
84             when(vspManagerFactory.createInterface()).thenReturn(vendorSoftwareProductManager);
85             mockStatic(ActivityLogManagerFactory.class);
86             when(ActivityLogManagerFactory.getInstance()).thenReturn(activityLogManagerFactory);
87             when(activityLogManagerFactory.createInterface()).thenReturn(activityLogManager);
88             whenNew(PackageArchive.class).withAnyArguments().thenReturn(packageArchive);
89             mockStatic(OrchestrationTemplateCandidateManagerFactory.class);
90             when(OrchestrationTemplateCandidateManagerFactory.getInstance()).thenReturn(orchestrationTemplateCandidateManagerFactory);
91             when(orchestrationTemplateCandidateManagerFactory.createInterface()).thenReturn(candidateManager);
92             when(packageArchive.getArchiveFileName()).thenReturn(Optional.of("test"));
93             when(packageArchive.getPackageFileContents()).thenReturn(new byte[0]);
94             UploadFileResponse uploadFileResponse = new UploadFileResponse();
95             uploadFileResponse.setOnboardingType(OnboardingTypesEnum.ZIP);
96             uploadFileResponse.setNetworkPackageName("test");
97             when(candidateManager.upload(any(), any(), any(), any(), any())).thenReturn(uploadFileResponse);
98
99
100             // get using the candidate manager.
101             Optional<Pair<String,byte[]>> zipFile =
102                     Optional.of(Pair.of("Hello", "World".getBytes()));
103
104             when(candidateManager.get(
105                     ArgumentMatchers.eq(candidateId),
106                     ArgumentMatchers.any())).thenReturn(zipFile);
107
108             when(vendorSoftwareProductManager.get(
109                     ArgumentMatchers.eq(softwareProductId),
110                     ArgumentMatchers.any())).thenReturn(zipFile);
111
112
113             OrchestrationTemplateActionResponse processResponse =
114                     new OrchestrationTemplateActionResponse();
115             processResponse.setStatus(UploadFileStatus.Success);
116             when(candidateManager.process(
117                     ArgumentMatchers.eq(candidateId),
118                     ArgumentMatchers.any())).thenReturn(processResponse);
119
120
121             ValidationResponse vr = new ValidationResponse();
122             when(candidateManager.updateFilesDataStructure(
123                     ArgumentMatchers.eq(candidateId),
124                     ArgumentMatchers.any(),
125                     ArgumentMatchers.any())).thenReturn(vr);
126
127             FilesDataStructure fds = new FilesDataStructure();
128             fds.setArtifacts(Arrays.asList("a","b"));
129             fds.setNested(Arrays.asList("foo", "bar"));
130             fds.setUnassigned(Arrays.asList("c", "d"));
131             fds.setModules(Arrays.asList(new Module(), new Module()));
132
133             when(candidateManager.getFilesDataStructure(
134                     ArgumentMatchers.eq(candidateId),
135                     ArgumentMatchers.any())).thenReturn(Optional.of(fds));
136
137
138         }catch (Exception e){
139            logger.error(e.getMessage(), e);
140         }
141     }
142
143     @Test
144     public void uploadSignedTest() throws SecurityManagerException {
145         when(packageArchive.isSigned()).thenReturn(true);
146         when(packageArchive.isSignatureValid()).thenReturn(true);
147         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
148         Attachment attachment = mock(Attachment.class);
149         when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
150         Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
151         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
152
153     }
154
155     @Test
156     public void uploadNotSignedTest(){
157         when(packageArchive.isSigned()).thenReturn(false);
158         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
159         Attachment attachment = mock(Attachment.class);
160         when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
161         Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
162         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
163
164     }
165
166     @Test
167     public void uploadSignNotValidTest() throws SecurityManagerException {
168         when(packageArchive.isSigned()).thenReturn(true);
169         when(packageArchive.isSignatureValid()).thenReturn(false);
170         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
171         Attachment attachment = mock(Attachment.class);
172         when(attachment.getContentDisposition()).thenReturn(new ContentDisposition("test"));
173         Response response = orchestrationTemplateCandidate.upload("1", "1", attachment, "1");
174         assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
175         assertFalse(((UploadFileResponseDto)response.getEntity()).getErrors().isEmpty());
176
177     }
178
179     @Test
180     public void testCandidateGet() throws IOException {
181         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
182         Response rsp = orchestrationTemplateCandidate.get(candidateId, versionId, user);
183         Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
184         Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Candidate"),-1);
185         byte[] content = (byte[])rsp.getEntity();
186         Assert.assertEquals("World", new String(content));
187
188     }
189
190     @Test
191     public void testVendorSoftwareProductGet() throws IOException {
192         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
193         Response rsp = orchestrationTemplateCandidate.get(softwareProductId, versionId, user);
194         Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
195         Assert.assertNotEquals(rsp.getHeaderString("Content-Disposition").indexOf("Processed"),-1);
196         byte[] content = (byte[])rsp.getEntity();
197         Assert.assertEquals("World", new String(content));
198     }
199
200     @Test
201     public void testMissingGet() throws IOException {
202         orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
203         Response rsp = orchestrationTemplateCandidate.get(UUID.randomUUID().toString(), versionId, user);
204         Assert.assertEquals("Response status equals", Response.Status.NOT_FOUND.getStatusCode(), rsp.getStatus());
205     }
206
207     @Test
208     public void testAbort() {
209         try {
210             orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
211             Response rsp = orchestrationTemplateCandidate.abort(candidateId, versionId);
212             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
213             Assert.assertNull(rsp.getEntity());
214         }
215         catch (Exception ex) {
216             logger.error("unexpected exception", ex);
217             Assert.fail("abort should not throw an exception");
218         }
219     }
220
221     @Test
222     public void testProcess() {
223         try {
224             orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
225             Response rsp = orchestrationTemplateCandidate.process(candidateId, versionId, user);
226             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
227             Assert.assertNotNull(rsp.getEntity());
228             OrchestrationTemplateActionResponseDto dto = (OrchestrationTemplateActionResponseDto)rsp.getEntity();
229             Assert.assertEquals("status check", UploadFileStatus.Success, dto.getStatus());
230         }
231         catch (Exception ex) {
232             logger.error("unexpected exception", ex);
233             Assert.fail("abort should not throw an exception");
234         }
235     }
236
237     @Test
238     public void testFilesDataStructureUpload() {
239         try {
240             FileDataStructureDto dto = new FileDataStructureDto();
241             dto.setArtifacts(Arrays.asList("a", "b", "c"));
242             orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
243             Response rsp = orchestrationTemplateCandidate.updateFilesDataStructure(candidateId, versionId, dto, user);
244             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
245         }
246         catch (Exception ex) {
247             logger.error("unexpected exception", ex);
248             Assert.fail("abort should not throw an exception");
249         }
250     }
251
252     @Test
253     public void testFilesDataStructureGet() {
254         try {
255             FileDataStructureDto dto = new FileDataStructureDto();
256             dto.setArtifacts(Arrays.asList("a", "b", "c"));
257             orchestrationTemplateCandidate = new OrchestrationTemplateCandidateImpl();
258             Response rsp = orchestrationTemplateCandidate.getFilesDataStructure(candidateId, versionId, user);
259             Assert.assertEquals("Response status equals", Response.Status.OK.getStatusCode(), rsp.getStatus());
260         }
261         catch (Exception ex) {
262             logger.error("unexpected exception", ex);
263             Assert.fail("abort should not throw an exception");
264         }
265     }
266
267 }