Fix Null key issue in PfReferenceTimestampKey
[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(
65                         new ObjectValidationResult(fieldName, getKeyId(value), ValidationStatus.INVALID, errorMessage));
66     }
67
68     /**
69      * Makes a result that indicates a value is invalid, because it is null.
70      *
71      * @param fieldName name of the field containing the value
72      * @param value the field's value
73      * @return a result indicating the value is invalid
74      */
75     public static ValidationResult makeNullResult(@NonNull String fieldName, Object value) {
76         return new ObjectValidationResult(fieldName, getKeyId(value), ValidationStatus.INVALID, IS_NULL);
77     }
78
79     /**
80      * Validates a key, ensuring that it isn't null and that it's structurally sound.
81      *
82      * @param result where to add the validation result
83      * @param fieldName name of the field containing the key
84      * @param key the field's value
85      */
86     public static void validateKeyNotNull(BeanValidationResult result, @NonNull String fieldName, PfKey key) {
87         if (key == null) {
88             result.addResult(new ObjectValidationResult(fieldName, key, ValidationStatus.INVALID, IS_A_NULL_KEY));
89             return;
90         }
91
92         if (key.isNullKey()) {
93             result.addResult(new ObjectValidationResult(fieldName, key.getId(), ValidationStatus.INVALID,
94                             IS_A_NULL_KEY));
95             return;
96         }
97
98         result.addResult(key.validate(fieldName));
99     }
100
101     /**
102      * Validates a key's version, ensuring that it isn't null.
103      *
104      * @param result where to add the validation result
105      * @param fieldName name of the field containing the key
106      * @param key the field's value
107      */
108     public static void validateKeyVersionNotNull(BeanValidationResult result, @NonNull String fieldName,
109                     PfConceptKey key) {
110         if (key != null && key.isNullVersion()) {
111             BeanValidationResult result2 = new BeanValidationResult(fieldName, key);
112             result2.addResult(makeNullResult(PfKeyImpl.VERSION_TOKEN, key.getVersion()));
113             result.addResult(result2);
114         }
115     }
116
117     /**
118      * Gets a key's ID, if the value is a {@link PfKey}.
119      *
120      * @param value value from which to get the ID
121      * @return the value's ID, if it's a key, the original value otherwise
122      */
123     private static Object getKeyId(Object value) {
124         return (value instanceof PfKey ? ((PfKey) value).getId() : value);
125     }
126 }