3cabebbb69cb716939e518e2fb784c97022d3e1b
[sdc.git] /
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.sdcrests.vsp.rest.mapping;
22
23 import org.apache.commons.collections.CollectionUtils;
24 import org.apache.commons.collections4.MapUtils;
25 import org.openecomp.sdc.common.errors.ErrorCode;
26 import org.openecomp.sdc.datatypes.error.ErrorMessage;
27 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireValidationResult;
28 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
30 import org.openecomp.sdcrests.common.mapping.MapErrorCodeToDto;
31 import org.openecomp.sdcrests.common.mapping.MapErrorMessageToDto;
32 import org.openecomp.sdcrests.common.types.ErrorCodeDto;
33 import org.openecomp.sdcrests.common.types.ErrorMessageDto;
34 import org.openecomp.sdcrests.mapping.MappingBase;
35 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ComponentValidationResultDto;
36 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
37 import org.openecomp.sdcrests.vendorsoftwareproducts.types.DeploymentFlavorValidationResultDto;
38 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireValidationResultDto;
39 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
40
41 import java.util.Collection;
42 import java.util.HashSet;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Objects;
46 import java.util.Set;
47 import java.util.stream.Collectors;
48
49 public class MapValidationResponseToDto
50         extends MappingBase<ValidationResponse, ValidationResponseDto> {
51   private static Map<String, List<ErrorMessageDto>> mapUploadDataErrors(
52           Map<String, List<ErrorMessage>> uploadDataErrors) {
53     if (MapUtils.isEmpty(uploadDataErrors)) {
54       return null;
55     }
56     return uploadDataErrors.entrySet().stream().collect(
57             Collectors.toMap(entry -> entry.getKey(), entry -> mapErrorMessages(entry.getValue())));
58   }
59
60   private static QuestionnaireValidationResultDto mapQuestionnaireValidationResult(
61           QuestionnaireValidationResult questionnaireValidationResult) {
62     if (Objects.isNull(questionnaireValidationResult)
63             || Objects.isNull(questionnaireValidationResult.getValidationData())) {
64       return null;
65     }
66     QuestionnaireValidationResultDto questionnaireValidationResultDto =
67             new QuestionnaireValidationResultDto();
68     questionnaireValidationResultDto.setValid(questionnaireValidationResult.isValid());
69
70     Set<CompositionEntityValidationDataDto> validationDataDto = new HashSet<>();
71     for(CompositionEntityValidationData validationData : questionnaireValidationResult.getValidationData()){
72       validationDataDto.add(new MapCompositionEntityValidationDataToDto().applyMapping
73               (validationData, CompositionEntityValidationDataDto.class));
74     }
75
76     questionnaireValidationResultDto.setValidationData(validationDataDto);
77     return questionnaireValidationResultDto;
78   }
79
80   /*private static ComponentValidationResultDto mapcomponentValidationResult(
81       ComponentValidationResult componentValidationResult) {
82     if (componentValidationResult == null) {
83       return null;
84     }
85     ComponentValidationResultDto componentValidationResultDto =
86         new ComponentValidationResultDto();
87     componentValidationResultDto.setValid(componentValidationResult.isValid());
88
89     Set<CompositionEntityValidationDataDto> validationDataDto = new HashSet<>();
90     for(CompositionEntityValidationData validationData : componentValidationResult.getValidationData()){
91       validationDataDto.add(new MapCompositionEntityValidationDataToDto().applyMapping
92           (validationData, CompositionEntityValidationDataDto.class));
93     }
94
95     componentValidationResultDto.setValidationData(validationDataDto);
96     return componentValidationResultDto;
97   }
98
99   private static DeploymentFlavorValidationResultDto mapdeploymentFlavorValidationResult(
100       DeploymentFlavorValidationResult deploymentFlavorValidationResult) {
101     if (deploymentFlavorValidationResult == null) {
102       return null;
103     }
104     DeploymentFlavorValidationResultDto deploymentFlavorValidationResultDto =
105         new DeploymentFlavorValidationResultDto();
106     deploymentFlavorValidationResultDto.setValid(deploymentFlavorValidationResult.isValid());
107
108     Set<CompositionEntityValidationDataDto> validationDataDto = new HashSet<>();
109     for(CompositionEntityValidationData validationData : deploymentFlavorValidationResult.getValidationData()){
110       validationDataDto.add(new MapCompositionEntityValidationDataToDto().applyMapping
111           (validationData, CompositionEntityValidationDataDto.class));
112     }
113
114     deploymentFlavorValidationResultDto.setValidationData(validationDataDto);
115     return deploymentFlavorValidationResultDto;
116   }*/
117
118   private static List<ErrorMessageDto> mapErrorMessages(List<ErrorMessage> errorMessages) {
119     return errorMessages == null ? null : errorMessages.stream().map(
120             errorMessage -> new MapErrorMessageToDto()
121                     .applyMapping(errorMessage, ErrorMessageDto.class)).collect(Collectors.toList());
122   }
123
124   private static Collection<ErrorCodeDto> mapErrorCodes(Collection<ErrorCode> errorCodes) {
125     return CollectionUtils.isEmpty(errorCodes) ? null : errorCodes.stream()
126             .map(errorCode -> new MapErrorCodeToDto().applyMapping(errorCode, ErrorCodeDto.class))
127             .collect(Collectors.toList());
128   }
129
130   @Override
131   public void doMapping(ValidationResponse source, ValidationResponseDto target) {
132     target.setValid(source.isValid());
133     target.setVspErrors(mapErrorCodes(source.getVspErrors()));
134     target.setLicensingDataErrors(mapErrorCodes(source.getLicensingDataErrors()));
135     target.setUploadDataErrors(mapUploadDataErrors(source.getUploadDataErrors()));
136     target.setQuestionnaireValidationResult(
137             mapQuestionnaireValidationResult(source.getQuestionnaireValidationResult()));
138   }
139 }