Remove GroupValidationResult
[policy/models.git] / models-base / src / main / java / org / onap / policy / models / base / Validated.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import lombok.NonNull;
24 import org.onap.policy.common.parameters.BeanValidationResult;
25 import org.onap.policy.common.parameters.ObjectValidationResult;
26 import org.onap.policy.common.parameters.ValidationResult;
27 import org.onap.policy.common.parameters.ValidationStatus;
28
29 /**
30  * Classes that can be validated. This can be used as a super class or as a stand-alone
31  * utility class.
32  */
33 public class Validated {
34     public static final String IS_BLANK = "is blank";
35     public static final String IS_A_NULL_KEY = "is a null key";
36     public static final String IS_NULL = "is null";
37     public static final String NOT_DEFINED = "not defined";
38     public static final String NOT_FOUND = "not found";
39
40     public static final String KEY_TOKEN = "key";
41     public static final String VALUE_TOKEN = "value";
42
43     /**
44      * Validates the fields of the object. The default method uses a {@link PfValidator}
45      * to validate the object.
46      *
47      * @param fieldName name of the field containing this
48      * @return the result, or {@code null}
49      */
50     public BeanValidationResult validate(@NonNull String fieldName) {
51         return new PfValidator().validateTop(fieldName, this);
52     }
53
54     /**
55      * Adds a result indicating that a value is invalid.
56      *
57      * @param result where to put the result
58      * @param fieldName name of the field containing the value
59      * @param value the field's value
60      * @param errorMessage the error message
61      */
62     public static void addResult(@NonNull BeanValidationResult result, @NonNull String fieldName, Object value,
63                     @NonNull String errorMessage) {
64         result.addResult(fieldName, getKeyId(value), ValidationStatus.INVALID, errorMessage);
65     }
66
67     /**
68      * Makes a result that indicates a value is invalid, because it is null.
69      *
70      * @param fieldName name of the field containing the value
71      * @param value the field's value
72      * @return a result indicating the value is invalid
73      */
74     public static ValidationResult makeNullResult(@NonNull String fieldName, Object value) {
75         return new ObjectValidationResult(fieldName, getKeyId(value), ValidationStatus.INVALID, IS_NULL);
76     }
77
78     /**
79      * Validates a key, ensuring that it isn't null and that it's structurally sound.
80      *
81      * @param result where to add the validation result
82      * @param fieldName name of the field containing the key
83      * @param key the field's value
84      */
85     public static void validateKeyNotNull(BeanValidationResult result, @NonNull String fieldName, PfKey key) {
86         if (key == null) {
87             result.addResult(fieldName, key, ValidationStatus.INVALID, IS_A_NULL_KEY);
88             return;
89         }
90
91         if (key.isNullKey()) {
92             result.addResult(fieldName, key.getId(), ValidationStatus.INVALID, IS_A_NULL_KEY);
93             return;
94         }
95
96         result.addResult(key.validate(fieldName));
97     }
98
99     /**
100      * Validates a key's version, ensuring that it isn't null.
101      *
102      * @param result where to add the validation result
103      * @param fieldName name of the field containing the key
104      * @param key the field's value
105      */
106     public static void validateKeyVersionNotNull(BeanValidationResult result, @NonNull String fieldName,
107                     PfConceptKey key) {
108         if (key != null && key.isNullVersion()) {
109             BeanValidationResult result2 = new BeanValidationResult(fieldName, key);
110             result2.addResult(makeNullResult(PfKeyImpl.VERSION_TOKEN, key.getVersion()));
111             result.addResult(result2);
112         }
113     }
114
115     /**
116      * Gets a key's ID, if the value is a {@link PfKey}.
117      *
118      * @param value value from which to get the ID
119      * @return the value's ID, if it's a key, the original value otherwise
120      */
121     private static Object getKeyId(Object value) {
122         return (value instanceof PfKey ? ((PfKey) value).getId() : value);
123     }
124 }