2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.vendorsoftwareproduct;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.nio.ByteBuffer;
27 import java.util.List;
28 import java.util.Objects;
29 import org.apache.commons.collections4.MapUtils;
30 import org.apache.commons.io.IOUtils;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.mockito.InjectMocks;
34 import org.mockito.MockitoAnnotations;
35 import org.openecomp.core.utilities.orchestration.OnboardingTypesEnum;
36 import org.openecomp.core.validation.util.MessageContainerUtil;
37 import org.openecomp.sdc.datatypes.error.ErrorLevel;
38 import org.openecomp.sdc.logging.api.Logger;
39 import org.openecomp.sdc.logging.api.LoggerFactory;
40 import org.openecomp.sdc.vendorsoftwareproduct.dao.type.VspDetails;
41 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
42 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactData;
43 import org.openecomp.sdc.vendorsoftwareproduct.questionnaire.QuestionnaireDataService;
44 import org.openecomp.sdc.vendorsoftwareproduct.tree.UploadFileTest;
45 import org.openecomp.sdc.vendorsoftwareproduct.types.OnboardPackageInfo;
46 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
47 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.ComponentQuestionnaire;
48 import org.openecomp.sdc.versioning.dao.types.Version;
50 public class QuestionnaireDataServiceTest {
51 private static final Logger LOGGER = LoggerFactory.getLogger(QuestionnaireDataServiceTest.class);
53 public static final Version VERSION = new Version(0, 1);
54 private QuestionnaireDataService questionnaireDataService;// = new QuestionnaireDataServiceImpl();
57 private OrchestrationTemplateCandidateManagerImpl candidateManager;
59 private UploadFileTest uploadFileTest = new UploadFileTest();
60 private OnboardPackageInfo onboardPackageInfo;
62 private static String vspId;
63 private static Version vspActiveVersion;
64 private static final String USER1 = "vspTestUser1";
65 private static final VspDetails vspDetails = new VspDetails(vspId, VERSION);
66 private static final String CSAR = "csar";
67 private static final String ZIP = "zip";
70 public void setUp() throws Exception {
71 MockitoAnnotations.initMocks(this);
74 // TODO: 3/15/2017 fix and enable //@Test
75 public void testQuestionnaireDataAfterLegalUploadWithComposition() throws IOException {
76 InformationArtifactData informationArtifactData =
77 uploadFileAndValidateInformationArtifactData("/fullComposition", 5);
79 assertQuestionnaireValuesAreAsExpected(informationArtifactData, false);
83 // TODO: 3/15/2017 fix and enable //@Test
84 public void testQuestionnaireDataAfterLegalUploadEmptyComposition() throws IOException {
85 uploadFileAndValidateInformationArtifactData("/emptyComposition", 0);
89 // TODO: 3/15/2017 fix and enable //@Test
90 public void testQuestionnaireDataAfterIllegalUpload() throws IOException {
91 try (InputStream zipInputStream = uploadFileTest.getZipInputStream("/missingYml")) {
92 onboardPackageInfo = new OnboardPackageInfo("missingYml", CSAR, convertFileInputStream(zipInputStream));
93 UploadFileResponse uploadFileResponse =
94 candidateManager.upload(vspDetails, onboardPackageInfo);
96 InformationArtifactData informationArtifactData = questionnaireDataService
97 .generateQuestionnaireDataForInformationArtifact(vspId, vspActiveVersion);
101 private InformationArtifactData uploadFileAndValidateInformationArtifactData(final String filePath,
102 final int listSizeToCheck)
105 try (final InputStream zipInputStream = uploadFileTest.getZipInputStream(filePath)) {
106 onboardPackageInfo = new OnboardPackageInfo("file", OnboardingTypesEnum.CSAR.toString(),
107 convertFileInputStream(zipInputStream));
108 final UploadFileResponse uploadFileResponse = candidateManager.upload(vspDetails, onboardPackageInfo);
109 candidateManager.process(vspId, VERSION);
111 Assert.assertTrue(MapUtils.isEmpty(
112 MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, uploadFileResponse.getErrors())));
114 final InformationArtifactData informationArtifactData = questionnaireDataService
115 .generateQuestionnaireDataForInformationArtifact(vspId, vspActiveVersion);
116 Assert.assertNotNull(informationArtifactData);
118 final List<ComponentQuestionnaire> componentQuestionnaireList =
119 informationArtifactData.getComponentQuestionnaires();
120 Assert.assertEquals(componentQuestionnaireList.size(), listSizeToCheck);
122 return informationArtifactData;
125 private void assertQuestionnaireValuesAreAsExpected(
126 InformationArtifactData informationArtifactData, boolean condition) {
128 Objects.isNull(informationArtifactData.getComponentQuestionnaires().get(0).getCompute()),
131 Objects.isNull(informationArtifactData.getComponentQuestionnaires().get(0).getStorage()),
135 private ByteBuffer convertFileInputStream(final InputStream fileInputStream) {
136 byte[] fileContent = new byte[0];
138 fileContent = IOUtils.toByteArray(fileInputStream);
139 } catch (final IOException e) {
140 LOGGER.error(String.format("Could not convert %s into byte[]", fileInputStream), e);
142 return ByteBuffer.wrap(fileContent);