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