Update onboarding upload status during processing
[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 package org.openecomp.sdcrests.vsp.rest.mapping;
17
18 import java.util.Collection;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.Objects;
23 import java.util.Set;
24 import java.util.stream.Collectors;
25 import org.apache.commons.collections.CollectionUtils;
26 import org.apache.commons.collections4.MapUtils;
27 import org.openecomp.sdc.common.errors.ErrorCode;
28 import org.openecomp.sdc.datatypes.error.ErrorMessage;
29 import org.openecomp.sdc.vendorsoftwareproduct.types.QuestionnaireValidationResult;
30 import org.openecomp.sdc.vendorsoftwareproduct.types.ValidationResponse;
31 import org.openecomp.sdc.vendorsoftwareproduct.types.composition.CompositionEntityValidationData;
32 import org.openecomp.sdcrests.common.mapping.MapErrorCodeToDto;
33 import org.openecomp.sdcrests.common.mapping.MapErrorMessageToDto;
34 import org.openecomp.sdcrests.common.types.ErrorCodeDto;
35 import org.openecomp.sdcrests.common.types.ErrorMessageDto;
36 import org.openecomp.sdcrests.mapping.MappingBase;
37 import org.openecomp.sdcrests.vendorsoftwareproducts.types.CompositionEntityValidationDataDto;
38 import org.openecomp.sdcrests.vendorsoftwareproducts.types.QuestionnaireValidationResultDto;
39 import org.openecomp.sdcrests.vendorsoftwareproducts.types.ValidationResponseDto;
40
41 public class MapValidationResponseToDto extends MappingBase<ValidationResponse, ValidationResponseDto> {
42
43     private static Map<String, List<ErrorMessageDto>> mapUploadDataErrors(Map<String, List<ErrorMessage>> uploadDataErrors) {
44         if (MapUtils.isEmpty(uploadDataErrors)) {
45             return null;
46         }
47         return uploadDataErrors.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> mapErrorMessages(entry.getValue())));
48     }
49
50     private static QuestionnaireValidationResultDto mapQuestionnaireValidationResult(QuestionnaireValidationResult questionnaireValidationResult) {
51         if (Objects.isNull(questionnaireValidationResult) || Objects.isNull(questionnaireValidationResult.getValidationData())) {
52             return null;
53         }
54         QuestionnaireValidationResultDto questionnaireValidationResultDto = new QuestionnaireValidationResultDto();
55         questionnaireValidationResultDto.setValid(questionnaireValidationResult.isValid());
56         Set<CompositionEntityValidationDataDto> validationDataDto = new HashSet<>();
57         for (CompositionEntityValidationData validationData : questionnaireValidationResult.getValidationData()) {
58             validationDataDto
59                 .add(new MapCompositionEntityValidationDataToDto().applyMapping(validationData, CompositionEntityValidationDataDto.class));
60         }
61         questionnaireValidationResultDto.setValidationData(validationDataDto);
62         return questionnaireValidationResultDto;
63     }
64
65     private static List<ErrorMessageDto> mapErrorMessages(List<ErrorMessage> errorMessages) {
66         return errorMessages == null ? null
67             : errorMessages.stream().map(errorMessage -> new MapErrorMessageToDto().applyMapping(errorMessage, ErrorMessageDto.class))
68                 .collect(Collectors.toList());
69     }
70
71     private static Collection<ErrorCodeDto> mapErrorCodes(Collection<ErrorCode> errorCodes) {
72         return CollectionUtils.isEmpty(errorCodes) ? null
73             : errorCodes.stream().map(errorCode -> new MapErrorCodeToDto().applyMapping(errorCode, ErrorCodeDto.class)).collect(Collectors.toList());
74     }
75
76     @Override
77     public void doMapping(ValidationResponse source, ValidationResponseDto target) {
78         target.setValid(source.isValid());
79         target.setVspErrors(mapErrorCodes(source.getVspErrors()));
80         target.setLicensingDataErrors(mapErrorCodes(source.getLicensingDataErrors()));
81         target.setUploadDataErrors(mapUploadDataErrors(source.getUploadDataErrors()));
82         target.setQuestionnaireValidationResult(mapQuestionnaireValidationResult(source.getQuestionnaireValidationResult()));
83     }
84 }