b922da9b46af11c581feda8cbab421dd2be8e821
[sdc.git] / openecomp-be / backend / openecomp-sdc-vendor-software-product-manager / src / test / java / org / openecomp / sdc / vendorsoftwareproduct / QuestionnaireDataServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.vendorsoftwareproduct;
22
23
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;
49
50 public class QuestionnaireDataServiceTest {
51   private static final Logger LOGGER = LoggerFactory.getLogger(QuestionnaireDataServiceTest.class);
52
53   public static final Version VERSION = new Version(0, 1);
54   private QuestionnaireDataService questionnaireDataService;// = new QuestionnaireDataServiceImpl();
55
56   @InjectMocks
57   private OrchestrationTemplateCandidateManagerImpl candidateManager;
58
59   private UploadFileTest uploadFileTest = new UploadFileTest();
60   private OnboardPackageInfo onboardPackageInfo;
61
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";
68
69   @Before
70   public void setUp() throws Exception {
71     MockitoAnnotations.initMocks(this);
72   }
73
74   // TODO: 3/15/2017 fix and enable   //@Test
75   public void testQuestionnaireDataAfterLegalUploadWithComposition() throws IOException {
76     InformationArtifactData informationArtifactData =
77         uploadFileAndValidateInformationArtifactData("/fullComposition", 5);
78
79     assertQuestionnaireValuesAreAsExpected(informationArtifactData, false);
80   }
81
82
83   // TODO: 3/15/2017 fix and enable   //@Test
84   public void testQuestionnaireDataAfterLegalUploadEmptyComposition() throws IOException {
85     uploadFileAndValidateInformationArtifactData("/emptyComposition", 0);
86   }
87
88
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);
95     }
96     InformationArtifactData informationArtifactData = questionnaireDataService
97         .generateQuestionnaireDataForInformationArtifact(vspId, vspActiveVersion);
98
99   }
100
101   private InformationArtifactData uploadFileAndValidateInformationArtifactData(final String filePath,
102                                                                                final int listSizeToCheck)
103       throws IOException {
104
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);
110
111       Assert.assertTrue(MapUtils.isEmpty(
112               MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, uploadFileResponse.getErrors())));
113     }
114     final InformationArtifactData informationArtifactData = questionnaireDataService
115         .generateQuestionnaireDataForInformationArtifact(vspId, vspActiveVersion);
116     Assert.assertNotNull(informationArtifactData);
117
118     final List<ComponentQuestionnaire> componentQuestionnaireList =
119         informationArtifactData.getComponentQuestionnaires();
120     Assert.assertEquals(componentQuestionnaireList.size(), listSizeToCheck);
121
122     return informationArtifactData;
123   }
124
125   private void assertQuestionnaireValuesAreAsExpected(
126       InformationArtifactData informationArtifactData, boolean condition) {
127     Assert.assertEquals(
128         Objects.isNull(informationArtifactData.getComponentQuestionnaires().get(0).getCompute()),
129         condition);
130     Assert.assertEquals(
131         Objects.isNull(informationArtifactData.getComponentQuestionnaires().get(0).getStorage()),
132         condition);
133   }
134
135   private ByteBuffer convertFileInputStream(final InputStream fileInputStream) {
136     byte[] fileContent = new byte[0];
137     try {
138       fileContent = IOUtils.toByteArray(fileInputStream);
139     } catch (final IOException e) {
140       LOGGER.error(String.format("Could not convert %s into byte[]", fileInputStream), e);
141     }
142     return ByteBuffer.wrap(fileContent);
143   }
144
145 }