Fixed sonar issues - OrchestrationTemplateCandidat
[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.Mock;
21 import org.mockito.MockitoAnnotations;
22 import org.mockito.Spy;
23 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
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.InputStream;
39 import java.util.List;
40 import java.util.function.Predicate;
41
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertFalse;
44 import static org.junit.Assert.assertTrue;
45 import static org.mockito.Matchers.any;
46 import static org.mockito.Mockito.doReturn;
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   private OrchestrationTemplateCandidateManagerImpl candidateManager;
62
63
64   public static String id001 = null;
65
66   public static Version activeVersion002 = null;
67
68
69   @BeforeMethod
70   public void setUp() throws Exception {
71     MockitoAnnotations.initMocks(this);
72     candidateService = new CandidateServiceImpl(manifestCreator, orchestrationTemplateCandidateDao);
73     candidateManager = new OrchestrationTemplateCandidateManagerImpl(vspInfoDaoMock,
74         candidateService);
75   }
76
77   @Test
78   public void testSuccessfulUploadFile() throws Exception {
79     VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
80     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
81
82     try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmock.csar")) {
83       UploadFileResponse uploadFileResponse =
84           candidateManager.upload(id001, activeVersion002, is, "csar", "SDCmock");
85       assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
86       assertEquals(0, uploadFileResponse.getErrors().size());
87       assertTrue(uploadFileResponse.getErrors().isEmpty());
88     }
89   }
90
91   @Test
92   public void testFail1UploadFile() throws Exception {
93     VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
94     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
95
96     try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail1.csar")) {
97       UploadFileResponse uploadFileResponse =
98           candidateManager.upload(id001, activeVersion002, is,
99               "csar", "SDCmockFail1");
100       assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
101       assertEquals(1, uploadFileResponse.getErrors().size());
102       assertTrue(uploadFileResponse.getErrors().values().stream().anyMatch(
103           getListPredicate(Messages.CSAR_FILES_NOT_ALLOWED.getErrorMessage().substring(0, 7))));
104     }
105   }
106
107   private Predicate<List<ErrorMessage>> getListPredicate(String substring) {
108     return error -> isEquals(substring, error);
109   }
110
111   private boolean isEquals(String substring, List<ErrorMessage> error) {
112     return error.iterator().next().getMessage().contains(substring);
113   }
114
115   @Test
116   public void testFail2UploadFile() throws Exception {
117     VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
118     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
119
120     try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail2.csar")) {
121       UploadFileResponse uploadFileResponse =
122           candidateManager.upload(id001, activeVersion002, is,
123               "csar", "SDCmockFail2");
124       assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
125       assertEquals(1, uploadFileResponse.getErrors().size());
126       assertTrue(uploadFileResponse.getErrors().values().stream().anyMatch(
127           getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
128     }
129   }
130
131   @Test
132   public void testFail3UploadFile() throws Exception {
133     VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
134     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
135
136     try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockFail3.csar")) {
137       UploadFileResponse uploadFileResponse =
138           candidateManager.upload(id001, activeVersion002, is,
139               "csar", "SDCmockFail3");
140       assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
141       assertEquals(1, uploadFileResponse.getErrors().size());
142     }
143   }
144
145   @Test
146   public void testUploadFileIsNotZip() throws Exception {
147     VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
148     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
149
150     try (InputStream is = new ByteArrayInputStream("Thia is not a zip file".getBytes());) {
151       UploadFileResponse uploadFileResponse =
152           candidateManager.upload(id001, activeVersion002, is,
153               "csar", "file");
154       assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
155       assertFalse(uploadFileResponse.getErrors().isEmpty());
156       assertTrue(uploadFileResponse.getErrors().values().stream().anyMatch(
157           getListPredicate(Messages.CSAR_FILE_NOT_FOUND.getErrorMessage().substring(0, 7))));
158     }
159   }
160
161   @Test
162   public void testUploadFileIsEmpty() throws Exception {
163     VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
164     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
165
166     try (InputStream is = new ByteArrayInputStream(new byte[]{})) {
167       UploadFileResponse uploadFileResponse = candidateManager.upload(id001,
168           activeVersion002, is, "csar", "file");
169       assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
170       assertEquals(1, uploadFileResponse.getErrors().size());
171     }
172   }
173
174   @Test
175   public void testMFError() throws Exception {
176     VspDetails vspDetails = new VspDetails("dummyId", new Version(1, 0));
177     doReturn(vspDetails).when(vspInfoDaoMock).get(any(VspDetails.class));
178
179     try (InputStream is = getClass().getResourceAsStream("/vspmanager.csar/SDCmockBrokenMF.csar")) {
180       UploadFileResponse uploadFileResponse =
181           candidateManager.upload(id001, activeVersion002, is, "csar", "SDCmockBrokenMF");
182       assertEquals(uploadFileResponse.getOnboardingType(), OnboardingTypesEnum.CSAR);
183       assertEquals(1, uploadFileResponse.getErrors().size());
184       assertTrue(uploadFileResponse.getErrors().values().stream()
185           .anyMatch(getListPredicate(Messages.MANIFEST_NO_METADATA.getErrorMessage())));
186
187     }
188   }
189
190
191 }