Add failing event to invalid event exception
[cps/cps-temporal.git] / src / main / java / org / onap / cps / temporal / controller / event / listener / exception / InvalidEventEnvelopException.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (c) 2021 Bell Canada.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.cps.temporal.controller.event.listener.exception;
20
21 import java.io.Serializable;
22 import java.util.ArrayList;
23 import java.util.List;
24 import lombok.AllArgsConstructor;
25 import lombok.EqualsAndHashCode;
26 import lombok.Getter;
27 import lombok.ToString;
28 import org.onap.cps.event.model.CpsDataUpdatedEvent;
29
30 /**
31  * Class representing an invalid event envelop exception.
32  * It refers to the invalid event and details the invalid fields it has.
33  */
34 @Getter
35 public class InvalidEventEnvelopException extends EventListenerException {
36
37     private final CpsDataUpdatedEvent cpsDataUpdatedEvent;
38     private final List<InvalidField> invalidFields = new ArrayList<>();
39
40     public InvalidEventEnvelopException(final String message, final CpsDataUpdatedEvent cpsDataUpdatedEvent) {
41         super(message);
42         this.cpsDataUpdatedEvent = cpsDataUpdatedEvent;
43     }
44
45     public void addInvalidField(final InvalidField invalidField) {
46         this.invalidFields.add(invalidField);
47     }
48
49     public boolean hasInvalidFields() {
50         return ! this.invalidFields.isEmpty();
51     }
52
53     @Override
54     public String getMessage() {
55         return
56                 String.format("%s. Event: %s. Invalid fields: %s",
57                         super.getMessage(), this.cpsDataUpdatedEvent, this.invalidFields.toString());
58     }
59
60     @AllArgsConstructor
61     @Getter
62     @EqualsAndHashCode
63     @ToString
64     public static class InvalidField implements Serializable {
65
66         private static final long serialVersionUID = -7118283787669377391L;
67
68         private final ErrorType errorType;
69         private final String fieldName;
70         private final String actualValue;
71         private final String expectedValue;
72
73         public enum ErrorType {
74             UNEXPECTED, MISSING
75         }
76
77     }
78
79 }