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