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