push addional 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  * ============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.sdcrests.common.mapping.MapErrorCodeToDto;
30 import org.openecomp.sdcrests.common.mapping.MapErrorMessageToDto;
31 import org.openecomp.sdcrests.common.types.ErrorCodeDto;
32 import org.openecomp.sdcrests.common.types.ErrorMessageDto;
33 import org.openecomp.sdcrests.mapping.MappingBase;
34 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
35 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireValidationResultDto;
36 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
37
38
39 import java.util.Collection;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.stream.Collectors;
43
44 public class MapValidationResponseToDto
45     extends MappingBase<ValidationResponse, ValidationResponseDto> {
46   private static Map<String, List<ErrorMessageDto>> mapUploadDataErrors(
47       Map<String, List<ErrorMessage>> uploadDataErrors) {
48     if (MapUtils.isEmpty(uploadDataErrors)) {
49       return null;
50     }
51     return uploadDataErrors.entrySet().stream().collect(
52         Collectors.toMap(entry -> entry.getKey(), entry -> mapErrorMessages(entry.getValue())));
53   }
54
55   private static QuestionnaireValidationResultDto mapQuestionnaireValidationResult(
56       QuestionnaireValidationResult questionnaireValidationResult) {
57     if (questionnaireValidationResult == null) {
58       return null;
59     }
60     QuestionnaireValidationResultDto questionnaireValidationResultDto =
61         new QuestionnaireValidationResultDto();
62     questionnaireValidationResultDto.setValid(questionnaireValidationResult.isValid());
63     questionnaireValidationResultDto.setValidationData(new MapCompositionEntityValidationDataToDto()
64         .applyMapping(questionnaireValidationResult.getValidationData(),
65             CompositionEntityValidationDataDto.class));
66     return questionnaireValidationResultDto;
67   }
68
69   private static List<ErrorMessageDto> mapErrorMessages(List<ErrorMessage> errorMessages) {
70     return errorMessages == null ? null : errorMessages.stream().map(
71         errorMessage -> new MapErrorMessageToDto()
72             .applyMapping(errorMessage, ErrorMessageDto.class)).collect(Collectors.toList());
73   }
74
75   private static Collection<ErrorCodeDto> mapErrorCodes(Collection<ErrorCode> errorCodes) {
76     return CollectionUtils.isEmpty(errorCodes) ? null : errorCodes.stream()
77         .map(errorCode -> new MapErrorCodeToDto().applyMapping(errorCode, ErrorCodeDto.class))
78         .collect(Collectors.toList());
79   }
80
81   @Override
82   public void doMapping(ValidationResponse source, ValidationResponseDto target) {
83     target.setValid(source.isValid());
84     target.setVspErrors(mapErrorCodes(source.getVspErrors()));
85     target.setLicensingDataErrors(mapErrorCodes(source.getLicensingDataErrors()));
86     target.setUploadDataErrors(mapUploadDataErrors(source.getUploadDataErrors()));
87     target.setQuestionnaireValidationResult(
88         mapQuestionnaireValidationResult(source.getQuestionnaireValidationResult()));
89   }
90 }