re base code
[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 org.mockito.InjectMocks;
21 import org.mockito.Mock;
22 import org.mockito.MockitoAnnotations;
23 import org.mockito.Spy;
24 import org.openecomp.sdc.common.errors.Messages;
25 import org.openecomp.sdc.datatypes.error.ErrorMessage;
26 import org.openecomp.sdc.vendorsoftwareproduct.dao.OrchestrationTemplateCandidateDao;
27 import org.openecomp.sdc.vendorsoftwareproduct.dao.VendorSoftwareProductInfoDao;
28 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
29 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
30 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.CandidateServiceImpl;
31 import org.openecomp.sdc.vendorsoftwareproduct.services.impl.filedatastructuremodule.ManifestCreatorNamingConventionImpl;
32 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
33 import org.openecomp.sdc.versioning.dao.types.Version;
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
36
37 import java.io.ByteArrayInputStream;
38 import java.io.File;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import java.util.List;
42 import java.util.function.Predicate;
43
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertTrue;
46 import static org.mockito.Matchers.any;
47 import static org.mockito.Mockito.doReturn;
48
49 public class UploadCSARFileTest {
50
51   public static final Version VERSION01 = new Version("0.1");
52
53   @Spy
54   private CandidateServiceImpl candidateService;
55   @Mock
56   private VendorSoftwareProductInfoDao vspInfoDaoMock;
57   @Mock
58   private OrchestrationTemplateCandidateDao orchestrationTemplateCandidateDao;
59   @Mock
60   private ManifestCreatorNamingConventionImpl manifestCreator;
61
62   @InjectMocks
63   private OrchestrationTemplateCandidateManagerImpl candidateManager;
64
65
66   private static String id001 = "dummyId";
67   private static Version activeVersion002 = new Version("dummyVersion");
68   private static final String BASE_DIR = "/vspmanager.csar";
69   private static final String CSAR = "csar";
70
71
72   @BeforeMethod
73   public void setUp() throws Exception {
74     MockitoAnnotations.initMocks(this);
75     candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
76     candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
77         candidateService);
78   }
79
80   @Test
81   public void testSuccessfulUploadFile() throws Exception {
82     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
83     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
84
85     testCsarUpload("successfulUpload.csar", 0);
86   }
87
88   @Test
89   public void testIllegalUploadInvalidFileInRoot() throws Exception {
90     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
91     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
92
93     UploadFileResponse response = testCsarUpload("invalidFileInRoot.csar", 1);
94     assertTrue(response.getErrors().values().stream().anyMatch(
95         getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage().substring(0, 7))));
96   }
97
98   @Test
99   public void testIllegalUploadMissingMainServiceTemplate() throws Exception {
100     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
101     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
102
103     UploadFileResponse response = testCsarUpload("missingMainServiceTemplate.csar", 1);
104     assertTrue(response.getErrors().values().stream().anyMatch(
105         getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
106   }
107
108   @Test
109   public void testUploadFileIsNotZip() throws Exception {
110     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
111     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
112
113     UploadFileResponse response = testCsarUpload("notCsar.txt", 1);
114     assertEquals("no csar file was uploaded or file doesn't exist",
115         response.getErrors().values().iterator().next().get(0).getMessage());
116   }
117
118   @Test
119   public void testUploadFileIsEmpty() throws Exception {
120     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
121     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
122
123     try (InputStream is = new ByteArrayInputStream(new byte[]{})) {
124       UploadFileResponse uploadFileResponse = candidateManager.upload(id001,
125           activeVersion002, is, "csar", "file");
126       assertEquals(1, uploadFileResponse.getErrors().size());
127     }
128   }
129
130   @Test
131   public void testInvalidManifestContent() throws Exception {
132     VspDetails vspDetails = new VspDetails(id001, activeVersion002);
133     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
134
135
136     try (InputStream is = getClass()
137         .getResourceAsStream(BASE_DIR + "/invalidManifestContent.csar")) {
138       UploadFileResponse response =
139           candidateManager.upload(id001, activeVersion002, is, "csar", "invalidManifestContent");
140       assertEquals(1, response.getErrors().size());
141       assertEquals(response.getErrors().values().iterator().next().get(0).getMessage(),
142           "Manifest " +
143               "contains invalid line : aaa: vCSCF");
144
145     }
146   }
147
148   private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
149     return error -> isEquals(substring, error);
150   }
151
152   private boolean isEquals(String substring, List<ErrorMessage> error) {
153     return error.iterator().next().getMessage().contains(substring);
154   }
155
156   private UploadFileResponse testCsarUpload(String csarFileName, int expectedErrorsNumber)
157       throws IOException {
158     UploadFileResponse uploadFileResponse;
159     try (InputStream is = getClass()
160         .getResourceAsStream(BASE_DIR + File.separator + csarFileName)) {
161       uploadFileResponse =
162           candidateManager.upload(id001, activeVersion002, is, CSAR, csarFileName);
163       assertEquals(expectedErrorsNumber, uploadFileResponse.getErrors().size());
164     }
165     return uploadFileResponse;
166   }
167
168
169 }