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