push addional code
[sdc.git] / openecomp-be / backend / openecomp-sdc-validation-manager / src / main / java / org / openecomp / sdc / validation / errors / ValidationInvalidErrorBuilder.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.sdc.validation.errors;
22
23 import static org.openecomp.sdc.validation.errors.ValidationErrorCodes.VALIDATION_INVALID;
24
25 import org.openecomp.sdc.common.errors.BaseErrorBuilder;
26 import org.openecomp.sdc.common.errors.ErrorCategory;
27 import org.openecomp.sdc.datatypes.error.ErrorMessage;
28
29 import java.util.List;
30 import java.util.Map;
31
32 /**
33  * The type Validation invalid error builder.
34  */
35 public class ValidationInvalidErrorBuilder extends BaseErrorBuilder {
36   private static final String VALIDATION_INVALID_DETAILED_MSG = "File is invalid: %s";
37   private static final String VALIDATION_INVALID_MSG = "Validated file is invalid";
38
39   /**
40    * Instantiates a new Validation invalid error builder.
41    *
42    * @param errors the errors
43    */
44   public ValidationInvalidErrorBuilder(Map<String, List<ErrorMessage>> errors) {
45     getErrorCodeBuilder().withId(VALIDATION_INVALID);
46     getErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION);
47     getErrorCodeBuilder()
48         .withMessage(String.format(VALIDATION_INVALID_DETAILED_MSG, toString(errors)));
49   }
50
51   /**
52    * Instantiates a new Validation invalid error builder.
53    */
54   public ValidationInvalidErrorBuilder() {
55     getErrorCodeBuilder().withId(VALIDATION_INVALID);
56     getErrorCodeBuilder().withCategory(ErrorCategory.APPLICATION);
57     getErrorCodeBuilder().withMessage(VALIDATION_INVALID_MSG);
58   }
59
60   private String toString(Map<String, List<ErrorMessage>> errors) {
61     StringBuffer sb = new StringBuffer();
62     errors.entrySet().stream()
63         .forEach(entry -> singleErrorToString(sb, entry.getKey(), entry.getValue()));
64     return sb.toString();
65   }
66
67   private void singleErrorToString(StringBuffer sb, String fileName, List<ErrorMessage> errors) {
68     sb.append(System.lineSeparator());
69     sb.append(fileName);
70     sb.append(sb.append(": "));
71     errors.stream().forEach(
72         error -> sb.append(error.getMessage()).append("[").append(error.getLevel()).append("], "));
73   }
74
75 }