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.ValidationResult;
34 import org.onap.policy.common.parameters.ValidationStatus;
35 import org.onap.policy.common.utils.coder.CoderException;
36 import org.onap.policy.common.utils.coder.StandardCoder;
38 public class ValidatedTest {
39 private static final @NonNull String MY_FIELD = "myField";
40 private static final @NonNull String Q_KEY = "\"" + Validated.KEY_TOKEN + "\"";
41 private static final @NonNull String Q_VALUE = "\"" + Validated.VALUE_TOKEN + "\"";
42 private static final String NOT_SAME = "not same";
43 private static final String TEXT = "some text";
44 private static final String OTHER = "other text";
45 private static final String NAME = "myKey";
46 private static final String VERSION = "1.0.0";
49 public void testAddResult() {
50 BeanValidationResult result = new BeanValidationResult("", this);
51 Validated.addResult(result, MY_FIELD, TEXT, "some message");
52 assertThat(result.getResult()).contains(MY_FIELD).contains(TEXT).contains("some message");
54 assertThatThrownBy(() -> Validated.addResult(null, MY_FIELD, TEXT, OTHER))
55 .isInstanceOf(NullPointerException.class);
57 assertThatThrownBy(() -> Validated.addResult(result, null, TEXT, OTHER))
58 .isInstanceOf(NullPointerException.class);
60 assertThatCode(() -> Validated.addResult(result, MY_FIELD, null, OTHER)).doesNotThrowAnyException();
62 assertThatThrownBy(() -> Validated.addResult(result, MY_FIELD, TEXT, null))
63 .isInstanceOf(NullPointerException.class);
67 public void testMakeNullResult() {
68 ValidationResult rnull = Validated.makeNullResult(MY_FIELD, TEXT);
69 assertEquals(MY_FIELD, rnull.getName());
70 assertThat(rnull.getResult()).contains(MY_FIELD).contains(TEXT).contains(Validated.IS_NULL);
71 assertFalse(rnull.isValid());
73 assertThatThrownBy(() -> Validated.makeNullResult(null, TEXT)).isInstanceOf(NullPointerException.class);
75 assertThatCode(() -> Validated.makeNullResult(MY_FIELD, null)).doesNotThrowAnyException();
79 public void testValidateKeyNotNull() throws CoderException {
80 BeanValidationResult result = new BeanValidationResult("", this);
81 Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(NAME, VERSION));
82 assertThat(result.getResult()).isNull();
84 result = new BeanValidationResult("", this);
85 Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(NAME, PfConceptKey.NULL_KEY_VERSION));
86 assertThat(result.getResult()).isNull();
88 result = new BeanValidationResult("", this);
89 Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(PfConceptKey.NULL_KEY_NAME, VERSION));
90 assertThat(result.getResult()).isNull();
93 result = new BeanValidationResult("", this);
94 Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey());
95 assertThat(result.getResult()).contains(MY_FIELD, Validated.IS_A_NULL_KEY)
96 .doesNotContain("\"name\"", "\"version\"");
99 * Key is not null, but key.validate() should fail due to an invalid version.
100 * Note: have to create the key by decoding from json, as the class will prevent
101 * an invalid version from being assigned.
103 PfConceptKey key = new StandardCoder().decode("{'name':'myKey','version':'bogus'}".replace('\'', '"'),
105 result = new BeanValidationResult("", this);
106 Validated.validateKeyNotNull(result, MY_FIELD, key);
107 assertThat(result.getResult()).contains(MY_FIELD, "version", "does not match regular expression");
109 BeanValidationResult result2 = new BeanValidationResult("", this);
111 // null parameter tests
112 PfConceptKey conceptKey = new PfConceptKey();
113 assertThatThrownBy(() -> Validated.validateKeyNotNull(result2, null, conceptKey))
114 .isInstanceOf(NullPointerException.class);
116 assertThatCode(() -> Validated.validateKeyNotNull(result2, MY_FIELD, null)).doesNotThrowAnyException();
120 public void testValidateKeyVersionNotNull() {
121 BeanValidationResult result = new BeanValidationResult("", this);
122 Validated.validateKeyVersionNotNull(result, MY_FIELD, null);
123 assertThat(result.getResult()).isNull();
125 result = new BeanValidationResult("", this);
126 Validated.validateKeyVersionNotNull(result, MY_FIELD, new PfConceptKey(NAME, VERSION));
127 assertThat(result.getResult()).isNull();
129 result = new BeanValidationResult("", this);
130 Validated.validateKeyVersionNotNull(result, MY_FIELD, new PfConceptKey(NAME, PfConceptKey.NULL_KEY_VERSION));
131 assertThat(result.getResult()).contains(MY_FIELD).contains("version").contains(Validated.IS_NULL);
133 BeanValidationResult result2 = new BeanValidationResult("", this);
134 PfConceptKey conceptKey = new PfConceptKey();
135 assertThatThrownBy(() -> Validated.validateKeyVersionNotNull(result2, null, conceptKey))
136 .isInstanceOf(NullPointerException.class);
138 assertThatCode(() -> Validated.validateKeyVersionNotNull(result2, MY_FIELD, null)).doesNotThrowAnyException();
142 public void testGetKeyId() {
143 // not a key field - should just use the given value
144 BeanValidationResult result = new BeanValidationResult("", this);
145 Validated.addResult(result, MY_FIELD, TEXT, "some message");
146 assertThat(result.getResult()).contains(MY_FIELD).contains(TEXT).contains("some message");
148 // repeat with a key field - should use the key's ID
149 result = new BeanValidationResult("", this);
150 Validated.addResult(result, MY_FIELD, new PfConceptKey(NAME, VERSION), "some message");
151 assertThat(result.getResult()).contains(MY_FIELD).contains("myKey:1.0.0").contains("some message");
155 private static class MyString extends Validated {
156 private final String text;
159 public BeanValidationResult validate(String fieldName) {
160 if (TEXT.equals(text)) {
164 BeanValidationResult result = new BeanValidationResult(fieldName, this);
165 result.addResult(fieldName, text, ValidationStatus.INVALID, NOT_SAME);