Sorted out unit-test libraries in onboarding
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / upload / csar / UploadCSARFileTest.java
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.vendorsoftwareproduct.upload.csar;
18
19
20 import static org.mockito.Matchers.any;
21 import static org.mockito.Mockito.doReturn;
22 import static org.testng.Assert.assertEquals;
23 import static org.testng.Assert.assertTrue;
24
25 import java.io.ByteArrayInputStream;
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.List;
30 import java.util.function.Predicate;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.MockitoAnnotations;
34 import org.mockito.Spy;
35 import org.openecomp.sdc.common.errors.Messages;
36 import org.openecomp.sdc.datatypes.error.ErrorMessage;
37 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
38 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
39 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
40 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
41 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
42 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.ManifestCreatorNamingConventionImpl;
43 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
44 import org.openecomp.sdc.versioning.dao.types.Version;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
47
48 public class UploadCSARFileTest {
49
50   public static final Version VERSION01 = new Version("0.1");
51
52   @Spy
53   private CandidateServiceImpl candidateService;
54   @Mock
55   private VendorSoftwareProductInfoDao vspInfoDaoMock;
56   @Mock
57   private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
58   @Mock
59   private ManifestCreatorNamingConventionImpl manifestCreator;
60
61   @InjectMocks
62   private OrchestrationTemplateCandidateManagerImpl candidateManager;
63
64
65   private static String id001 = "dummyId";
66   private static Version activeVersion002 = new Version("dummyVersion");
67   private static final String BASE_DIR = "/vspmanager.csar";
68   private static final String CSAR = "csar";
69
70
71   @BeforeMethod
72   public void setUp() throws Exception {
73     MockitoAnnotations.initMocks(this);
74     candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
75     candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
76         candidateService);
77   }
78
79   @Test
80   public void testSuccessfulUploadFile() throws Exception {
81     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
82     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
83
84     testCsarUpload("successfulUpload.csar", 0);
85   }
86
87   @Test
88   public void testIllegalUploadInvalidFileInRoot() throws Exception {
89     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
90     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
91
92     UploadFileResponse response = testCsarUpload("invalidFileInRoot.csar", 1);
93     assertTrue(response.getErrors().values().stream().anyMatch(
94         getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage().substring(0, 7))));
95   }
96
97   @Test
98   public void testIllegalUploadMissingMainServiceTemplate() throws Exception {
99     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
100     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
101
102     UploadFileResponse response = testCsarUpload("missingMainServiceTemplate.csar", 1);
103     assertTrue(response.getErrors().values().stream().anyMatch(
104         getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
105   }
106
107   @Test
108   public void testUploadFileIsNotZip() throws Exception {
109     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
110     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
111
112     UploadFileResponse response = testCsarUpload("notCsar.txt", 1);
113     assertEquals("no csar file was uploaded or file doesn't exist",
114         response.getErrors().values().iterator().next().get(0).getMessage());
115   }
116
117   @Test
118   public void testUploadFileIsEmpty() throws Exception {
119     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
120     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
121
122     try (InputStream is = new ByteArrayInputStream(new byte[]{})) {
123       UploadFileResponse uploadFileResponse = candidateManager.upload(id001,
124           activeVersion002, is, "csar", "file");
125       assertEquals(1, uploadFileResponse.getErrors().size());
126     }
127   }
128
129   @Test
130   public void testInvalidManifestContent() throws Exception {
131     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
132     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
133
134
135     try (InputStream is = getClass()
136         .getResourceAsStream(BASE_DIR + "/invalidManifestContent.csar")) {
137       UploadFileResponse response =
138           candidateManager.upload(id001, activeVersion002, is, "csar", "invalidManifestContent");
139       assertEquals(1, response.getErrors().size());
140       assertEquals(response.getErrors().values().iterator().next().get(0).getMessage(),
141           "Manifest " +
142               "contains invalid line : aaa: vCSCF");
143
144     }
145   }
146
147   private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
148     return error -> isEquals(substring, error);
149   }
150
151   private boolean isEquals(String substring, List<ErrorMessage> error) {
152     return error.iterator().next().getMessage().contains(substring);
153   }
154
155   private UploadFileResponse testCsarUpload(String csarFileName, int expectedErrorsNumber)
156       throws IOException {
157     UploadFileResponse uploadFileResponse;
158     try (InputStream is = getClass()
159         .getResourceAsStream(BASE_DIR + File.separator + csarFileName)) {
160       uploadFileResponse =
161           candidateManager.upload(id001, activeVersion002, is, CSAR, csarFileName);
162       assertEquals(expectedErrorsNumber, uploadFileResponse.getErrors().size());
163     }
164     return uploadFileResponse;
165   }
166
167
168 }