722bb556be9636bb8630366e142b7db80ac2bff2
[sdc.git] /
1 /*
2  * Copyright © 2016-2017 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdcrests.vsp.rest.mapping;
18
19 import org.apache.commons.collections.CollectionUtils;
20 import org.apache.commons.collections4.MapUtils;
21 import org.openecomp.sdc.common.errors.ErrorCode;
22 import org.openecomp.sdc.datatypes.error.ErrorMessage;
23 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireValidationResult;
24 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
25 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
26 import org.openecomp.sdcrests.common.mapping.MapErrorCodeToDto;
27 import org.openecomp.sdcrests.common.mapping.MapErrorMessageToDto;
28 import org.openecomp.sdcrests.common.types.ErrorCodeDto;
29 import org.openecomp.sdcrests.common.types.ErrorMessageDto;
30 import org.openecomp.sdcrests.mapping.MappingBase;
31 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
32 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireValidationResultDto;
33 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
34
35 import java.util.Collection;
36 import java.util.HashSet;
37 import java.util.List;
38 import java.util.Map;
39 import java.util.Objects;
40 import java.util.Set;
41 import java.util.stream.Collectors;
42
43 public class MapValidationResponseToDto
44     extends MappingBase<ValidationResponse, ValidationResponseDto> {
45   private static Map<String, List<ErrorMessageDto>> mapUploadDataErrors(
46       Map<String, List<ErrorMessage>> uploadDataErrors) {
47     if (MapUtils.isEmpty(uploadDataErrors)) {
48       return null;
49     }
50     return uploadDataErrors.entrySet().stream().collect(
51         Collectors.toMap(Map.Entry::getKey, entry -> mapErrorMessages(entry.getValue())));
52   }
53
54   private static QuestionnaireValidationResultDto mapQuestionnaireValidationResult(
55       QuestionnaireValidationResult questionnaireValidationResult) {
56     if (Objects.isNull(questionnaireValidationResult)
57         || Objects.isNull(questionnaireValidationResult.getValidationData())) {
58       return null;
59     }
60     QuestionnaireValidationResultDto questionnaireValidationResultDto =
61         new QuestionnaireValidationResultDto();
62     questionnaireValidationResultDto.setValid(questionnaireValidationResult.isValid());
63
64     Set<CompositionEntityValidationDataDto> validationDataDto = new HashSet<>();
65     for (CompositionEntityValidationData validationData : questionnaireValidationResult
66         .getValidationData()) {
67       validationDataDto.add(new MapCompositionEntityValidationDataToDto().applyMapping(
68               validationData, CompositionEntityValidationDataDto.class));
69     }
70
71     questionnaireValidationResultDto.setValidationData(validationDataDto);
72     return questionnaireValidationResultDto;
73   }
74
75
76   private static List<ErrorMessageDto> mapErrorMessages(List<ErrorMessage> errorMessages) {
77     return errorMessages == null ? null : errorMessages.stream().map(
78         errorMessage -> new MapErrorMessageToDto()
79             .applyMapping(errorMessage, ErrorMessageDto.class)).collect(Collectors.toList());
80   }
81
82   private static Collection<ErrorCodeDto> mapErrorCodes(Collection<ErrorCode> errorCodes) {
83     return CollectionUtils.isEmpty(errorCodes) ? null : errorCodes.stream()
84         .map(errorCode -> new MapErrorCodeToDto().applyMapping(errorCode, ErrorCodeDto.class))
85         .collect(Collectors.toList());
86   }
87
88   @Override
89   public void doMapping(ValidationResponse source, ValidationResponseDto target) {
90     target.setValid(source.isValid());
91     target.setVspErrors(mapErrorCodes(source.getVspErrors()));
92     target.setLicensingDataErrors(mapErrorCodes(source.getLicensingDataErrors()));
93     target.setUploadDataErrors(mapUploadDataErrors(source.getUploadDataErrors()));
94     target.setQuestionnaireValidationResult(
95         mapQuestionnaireValidationResult(source.getQuestionnaireValidationResult()));
96   }
97 }