b86a1db5d427a31e4c0d41a7d923b56fda7c0c78
[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 org.apache.commons.collections4.MapUtils;
25 import org.mockito.*;
26 import org.openecomp.core.model.dao.ServiceModelDao;
27 import org.openecomp.core.model.types.ServiceElement;
28 import org.openecomp.core.validation.util.MessageContainerUtil;
29 import org.openecomp.sdc.activitylog.dao.type.ActivityLogEntity;
30 import org.openecomp.sdc.datatypes.error.ErrorLevel;
31 import org.openecomp.sdc.healing.api.HealingManager;
32 import org.openecomp.sdc.tosca.datatypes.ToscaServiceModel;
33 import org.openecomp.sdc.vendorsoftwareproduct.impl.OrchestrationTemplateCandidateManagerImpl;
34 import org.openecomp.sdc.vendorsoftwareproduct.informationArtifact.InformationArtifactData;
35 import org.openecomp.sdc.vendorsoftwareproduct.questionnaire.QuestionnaireDataService;
36 import org.openecomp.sdc.vendorsoftwareproduct.services.composition.CompositionDataExtractor;
37 import org.openecomp.sdc.vendorsoftwareproduct.services.filedatastructuremodule.CandidateService;
38 import org.openecomp.sdc.vendorsoftwareproduct.tree.UploadFileTest;
39 import org.openecomp.sdc.vendorsoftwareproduct.types.UploadFileResponse;
40 import org.openecomp.sdc.vendorsoftwareproduct.types.questionnaire.component.ComponentQuestionnaire;
41 import org.openecomp.sdc.versioning.dao.types.Version;
42 import org.testng.Assert;
43 import org.testng.annotations.BeforeMethod;
44
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.util.List;
48 import java.util.Objects;
49
50 public class QuestionnaireDataServiceTest {
51   public static final Version VERSION = new Version(0, 1);
52   private QuestionnaireDataService questionnaireDataService;// = new QuestionnaireDataServiceImpl();
53
54   @Mock
55   private CandidateService candidateServiceMock;
56   @Mock
57   private HealingManager healingManagerMock;
58   @Mock
59   private CompositionDataExtractor compositionDataExtractorMock;
60   @Mock
61   private ServiceModelDao<ToscaServiceModel, ServiceElement> serviceModelDaoMock;
62   @Mock
63   private CompositionEntityDataManager compositionEntityDataManagerMock;
64
65   @Captor
66   private ArgumentCaptor<ActivityLogEntity> activityLogEntityArg;
67
68   @InjectMocks
69   private OrchestrationTemplateCandidateManagerImpl candidateManager;
70
71   private UploadFileTest uploadFileTest = new UploadFileTest();
72
73   private static String vspId;
74   private static Version vspActiveVersion;
75   private static final String USER1 = "vspTestUser1";
76
77   @BeforeMethod
78   public void setUp() throws Exception {
79     MockitoAnnotations.initMocks(this);
80   }
81
82   // TODO: 3/15/2017 fix and enable   //@Test
83   public void testQuestionnaireDataAfterLegalUploadWithComposition() throws IOException {
84     InformationArtifactData informationArtifactData =
85         uploadFileAndValidateInformationArtifactData("/fullComposition", 5);
86
87     assertQuestionnaireValuesAreAsExpected(informationArtifactData, false);
88   }
89
90
91   // TODO: 3/15/2017 fix and enable   //@Test
92   public void testQuestionnaireDataAfterLegalUploadEmptyComposition() throws IOException {
93     uploadFileAndValidateInformationArtifactData("/emptyComposition", 0);
94   }
95
96
97   // TODO: 3/15/2017 fix and enable   //@Test
98   public void testQuestionnaireDataAfterIllegalUpload() throws IOException {
99     try (InputStream zipInputStream = uploadFileTest.getZipInputStream("/missingYml")) {
100       UploadFileResponse uploadFileResponse =
101               candidateManager.upload(vspId, VERSION, zipInputStream, "zip", "missingYml");
102     }
103     InformationArtifactData informationArtifactData = questionnaireDataService
104         .generateQuestionnaireDataForInformationArtifact(vspId, vspActiveVersion);
105
106   }
107
108   private InformationArtifactData uploadFileAndValidateInformationArtifactData(String filePath,
109                                                                                int listSizeToCheck)
110             throws IOException {
111
112     try (InputStream zipInputStream = uploadFileTest.getZipInputStream(filePath)) {
113       UploadFileResponse uploadFileResponse =
114               candidateManager.upload(vspId, VERSION, zipInputStream, "zip", "file");
115       candidateManager.process(vspId, VERSION);
116
117       Assert.assertTrue(MapUtils.isEmpty(
118               MessageContainerUtil.getMessageByLevel(ErrorLevel.ERROR, uploadFileResponse.getErrors())));
119     }
120     InformationArtifactData informationArtifactData = questionnaireDataService
121         .generateQuestionnaireDataForInformationArtifact(vspId, vspActiveVersion);
122     Assert.assertNotNull(informationArtifactData);
123
124     List<ComponentQuestionnaire> componentQuestionnaireList =
125         informationArtifactData.getComponentQuestionnaires();
126     Assert.assertEquals(componentQuestionnaireList.size(), listSizeToCheck);
127
128     return informationArtifactData;
129   }
130
131
132   private void assertQuestionnaireValuesAreAsExpected(
133       InformationArtifactData informationArtifactData, boolean condition) {
134     Assert.assertEquals(
135         Objects.isNull(informationArtifactData.getComponentQuestionnaires().get(0).getCompute()),
136         condition);
137     Assert.assertEquals(
138         Objects.isNull(informationArtifactData.getComponentQuestionnaires().get(0).getStorage()),
139         condition);
140   }
141
142 }