re base code
[sdc.git] / openecomp-be / api / openecomp-sdc-rest-webapp / vendor-software-products-rest / vendor-software-products-rest-services / src / main / java / org / openecomp / sdcrests / vsp / rest / mapping / MapValidationResponseToDto.java
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.*;
36 import java.util.stream.Collectors;
37
38 public class MapValidationResponseToDto
39     extends MappingBase<ValidationResponse, ValidationResponseDto> {
40   private static Map<String, List<ErrorMessageDto>> mapUploadDataErrors(
41       Map<String, List<ErrorMessage>> uploadDataErrors) {
42     if (MapUtils.isEmpty(uploadDataErrors)) {
43       return null;
44     }
45     return uploadDataErrors.entrySet().stream().collect(
46         Collectors.toMap(Map.Entry::getKey, entry -> mapErrorMessages(entry.getValue())));
47   }
48
49   private static QuestionnaireValidationResultDto mapQuestionnaireValidationResult(
50       QuestionnaireValidationResult questionnaireValidationResult) {
51     if (Objects.isNull(questionnaireValidationResult)
52         || Objects.isNull(questionnaireValidationResult.getValidationData())) {
53       return null;
54     }
55     QuestionnaireValidationResultDto questionnaireValidationResultDto =
56         new QuestionnaireValidationResultDto();
57     questionnaireValidationResultDto.setValid(questionnaireValidationResult.isValid());
58
59     Set<CompositionEntityValidationDataDto> validationDataDto = new HashSet<>();
60     for (CompositionEntityValidationData validationData : questionnaireValidationResult
61         .getValidationData()) {
62       validationDataDto.add(new MapCompositionEntityValidationDataToDto().applyMapping(
63               validationData, CompositionEntityValidationDataDto.class));
64     }
65
66     questionnaireValidationResultDto.setValidationData(validationDataDto);
67     return questionnaireValidationResultDto;
68   }
69
70
71   private static List<ErrorMessageDto> mapErrorMessages(List<ErrorMessage> errorMessages) {
72     return errorMessages == null ? null : errorMessages.stream().map(
73         errorMessage -> new MapErrorMessageToDto()
74             .applyMapping(errorMessage, ErrorMessageDto.class)).collect(Collectors.toList());
75   }
76
77   private static Collection<ErrorCodeDto> mapErrorCodes(Collection<ErrorCode> errorCodes) {
78     return CollectionUtils.isEmpty(errorCodes) ? null : errorCodes.stream()
79         .map(errorCode -> new MapErrorCodeToDto().applyMapping(errorCode, ErrorCodeDto.class))
80         .collect(Collectors.toList());
81   }
82
83   @Override
84   public void doMapping(ValidationResponse source, ValidationResponseDto target) {
85     target.setValid(source.isValid());
86     target.setVspErrors(mapErrorCodes(source.getVspErrors()));
87     target.setLicensingDataErrors(mapErrorCodes(source.getLicensingDataErrors()));
88     target.setUploadDataErrors(mapUploadDataErrors(source.getUploadDataErrors()));
89     target.setQuestionnaireValidationResult(
90         mapQuestionnaireValidationResult(source.getQuestionnaireValidationResult()));
91   }
92 }