2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020-2021 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.models.base;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.assertj.core.api.Assertions.assertThatCode;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
29 import lombok.AllArgsConstructor;
30 import lombok.NonNull;
31 import org.junit.Test;
32 import org.onap.policy.common.parameters.BeanValidationResult;
33 import org.onap.policy.common.parameters.ObjectValidationResult;
34 import org.onap.policy.common.parameters.ValidationResult;
35 import org.onap.policy.common.parameters.ValidationStatus;
36 import org.onap.policy.common.utils.coder.CoderException;
37 import org.onap.policy.common.utils.coder.StandardCoder;
39 public class ValidatedTest {
40 private static final @NonNull String MY_FIELD = "myField";
41 private static final @NonNull String Q_KEY = "\"" + Validated.KEY_TOKEN + "\"";
42 private static final @NonNull String Q_VALUE = "\"" + Validated.VALUE_TOKEN + "\"";
43 private static final String NOT_SAME = "not same";
44 private static final String TEXT = "some text";
45 private static final String OTHER = "other text";
46 private static final String NAME = "myKey";
47 private static final String VERSION = "1.0.0";
50 public void testAddResult() {
51 BeanValidationResult result = new BeanValidationResult("", this);
52 Validated.addResult(result, MY_FIELD, TEXT, "some message");
53 assertThat(result.getResult()).contains(MY_FIELD).contains(TEXT).contains("some message");
55 assertThatThrownBy(() -> Validated.addResult(null, MY_FIELD, TEXT, OTHER))
56 .isInstanceOf(NullPointerException.class);
58 assertThatThrownBy(() -> Validated.addResult(result, null, TEXT, OTHER))
59 .isInstanceOf(NullPointerException.class);
61 assertThatCode(() -> Validated.addResult(result, MY_FIELD, null, OTHER)).doesNotThrowAnyException();
63 assertThatThrownBy(() -> Validated.addResult(result, MY_FIELD, TEXT, null))
64 .isInstanceOf(NullPointerException.class);
68 public void testMakeNullResult() {
69 ValidationResult rnull = Validated.makeNullResult(MY_FIELD, TEXT);
70 assertEquals(MY_FIELD, rnull.getName());
71 assertThat(rnull.getResult()).contains(MY_FIELD).contains(TEXT).contains(Validated.IS_NULL);
72 assertFalse(rnull.isValid());
74 assertThatThrownBy(() -> Validated.makeNullResult(null, TEXT)).isInstanceOf(NullPointerException.class);
76 assertThatCode(() -> Validated.makeNullResult(MY_FIELD, null)).doesNotThrowAnyException();
80 public void testValidateKeyNotNull() throws CoderException {
81 BeanValidationResult result = new BeanValidationResult("", this);
82 Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(NAME, VERSION));
83 assertThat(result.getResult()).isNull();
85 result = new BeanValidationResult("", this);
86 Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(NAME, PfConceptKey.NULL_KEY_VERSION));
87 assertThat(result.getResult()).isNull();
89 result = new BeanValidationResult("", this);
90 Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(PfConceptKey.NULL_KEY_NAME, VERSION));
91 assertThat(result.getResult()).isNull();
94 result = new BeanValidationResult("", this);
95 Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey());
96 assertThat(result.getResult()).contains(MY_FIELD, Validated.IS_A_NULL_KEY)
97 .doesNotContain("\"name\"", "\"version\"");
100 * Key is not null, but key.validate() should fail due to an invalid version.
101 * Note: have to create the key by decoding from json, as the class will prevent
102 * an invalid version from being assigned.
104 PfConceptKey key = new StandardCoder().decode("{'name':'myKey','version':'bogus'}".replace('\'', '"'),
106 result = new BeanValidationResult("", this);
107 Validated.validateKeyNotNull(result, MY_FIELD, key);
108 assertThat(result.getResult()).contains(MY_FIELD, "version", "does not match regular expression");
110 BeanValidationResult result2 = new BeanValidationResult("", this);
112 // null parameter tests
113 PfConceptKey conceptKey = new PfConceptKey();
114 assertThatThrownBy(() -> Validated.validateKeyNotNull(result2, null, conceptKey))
115 .isInstanceOf(NullPointerException.class);
117 assertThatCode(() -> Validated.validateKeyNotNull(result2, MY_FIELD, null)).doesNotThrowAnyException();
121 public void testValidateKeyVersionNotNull() {
122 BeanValidationResult result = new BeanValidationResult("", this);
123 Validated.validateKeyVersionNotNull(result, MY_FIELD, null);
124 assertThat(result.getResult()).isNull();
126 result = new BeanValidationResult("", this);
127 Validated.validateKeyVersionNotNull(result, MY_FIELD, new PfConceptKey(NAME, VERSION));
128 assertThat(result.getResult()).isNull();
130 result = new BeanValidationResult("", this);
131 Validated.validateKeyVersionNotNull(result, MY_FIELD, new PfConceptKey(NAME, PfConceptKey.NULL_KEY_VERSION));
132 assertThat(result.getResult()).contains(MY_FIELD).contains("version").contains(Validated.IS_NULL);
134 BeanValidationResult result2 = new BeanValidationResult("", this);
135 PfConceptKey conceptKey = new PfConceptKey();
136 assertThatThrownBy(() -> Validated.validateKeyVersionNotNull(result2, null, conceptKey))
137 .isInstanceOf(NullPointerException.class);
139 assertThatCode(() -> Validated.validateKeyVersionNotNull(result2, MY_FIELD, null)).doesNotThrowAnyException();
143 public void testGetKeyId() {
144 // not a key field - should just use the given value
145 BeanValidationResult result = new BeanValidationResult("", this);
146 Validated.addResult(result, MY_FIELD, TEXT, "some message");
147 assertThat(result.getResult()).contains(MY_FIELD).contains(TEXT).contains("some message");
149 // repeat with a key field - should use the key's ID
150 result = new BeanValidationResult("", this);
151 Validated.addResult(result, MY_FIELD, new PfConceptKey(NAME, VERSION), "some message");
152 assertThat(result.getResult()).contains(MY_FIELD).contains("myKey:1.0.0").contains("some message");
156 private static class MyString extends Validated {
157 private final String text;
160 public BeanValidationResult validate(String fieldName) {
161 if (TEXT.equals(text)) {
165 BeanValidationResult result = new BeanValidationResult(fieldName, this);
166 result.addResult(new ObjectValidationResult(fieldName, text, ValidationStatus.INVALID, NOT_SAME));