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