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