Allow semantic versioning in all templates in models
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / ValidatedTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
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;
28
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;
37
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";
47
48     @Test
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");
53
54         assertThatThrownBy(() -> Validated.addResult(null, MY_FIELD, TEXT, OTHER))
55                         .isInstanceOf(NullPointerException.class);
56
57         assertThatThrownBy(() -> Validated.addResult(result, null, TEXT, OTHER))
58                         .isInstanceOf(NullPointerException.class);
59
60         assertThatCode(() -> Validated.addResult(result, MY_FIELD, null, OTHER)).doesNotThrowAnyException();
61
62         assertThatThrownBy(() -> Validated.addResult(result, MY_FIELD, TEXT, null))
63                         .isInstanceOf(NullPointerException.class);
64     }
65
66     @Test
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());
72
73         assertThatThrownBy(() -> Validated.makeNullResult(null, TEXT)).isInstanceOf(NullPointerException.class);
74
75         assertThatCode(() -> Validated.makeNullResult(MY_FIELD, null)).doesNotThrowAnyException();
76     }
77
78     @Test
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();
83
84         result = new BeanValidationResult("", this);
85         Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(NAME, PfConceptKey.NULL_KEY_VERSION));
86         assertThat(result.getResult()).isNull();
87
88         result = new BeanValidationResult("", this);
89         Validated.validateKeyNotNull(result, MY_FIELD, new PfConceptKey(PfConceptKey.NULL_KEY_NAME, VERSION));
90         assertThat(result.getResult()).isNull();
91
92         // key is null
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\"");
97
98         /*
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.
102          */
103         PfConceptKey key = new StandardCoder().decode("{'name':'myKey','version':'bogus'}".replace('\'', '"'),
104                         PfConceptKey.class);
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");
108
109         BeanValidationResult result2 = new BeanValidationResult("", this);
110
111         // null parameter tests
112         PfConceptKey conceptKey = new PfConceptKey();
113         assertThatThrownBy(() -> Validated.validateKeyNotNull(result2, null, conceptKey))
114                         .isInstanceOf(NullPointerException.class);
115
116         assertThatCode(() -> Validated.validateKeyNotNull(result2, MY_FIELD, null)).doesNotThrowAnyException();
117     }
118
119     @Test
120     public void testValidateKeyVersionNotNull() {
121         BeanValidationResult result = new BeanValidationResult("", this);
122         Validated.validateKeyVersionNotNull(result, MY_FIELD, null);
123         assertThat(result.getResult()).isNull();
124
125         result = new BeanValidationResult("", this);
126         Validated.validateKeyVersionNotNull(result, MY_FIELD, new PfConceptKey(NAME, VERSION));
127         assertThat(result.getResult()).isNull();
128
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);
132
133         BeanValidationResult result2 = new BeanValidationResult("", this);
134         PfConceptKey conceptKey = new PfConceptKey();
135         assertThatThrownBy(() -> Validated.validateKeyVersionNotNull(result2, null, conceptKey))
136                         .isInstanceOf(NullPointerException.class);
137
138         assertThatCode(() -> Validated.validateKeyVersionNotNull(result2, MY_FIELD, null)).doesNotThrowAnyException();
139     }
140
141     @Test
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");
147
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");
152     }
153
154     @AllArgsConstructor
155     private static class MyString extends Validated {
156         private final String text;
157
158         @Override
159         public BeanValidationResult validate(String fieldName) {
160             if (TEXT.equals(text)) {
161                 return null;
162             }
163
164             BeanValidationResult result = new BeanValidationResult(fieldName, this);
165             result.addResult(fieldName, text, ValidationStatus.INVALID, NOT_SAME);
166             return result;
167         }
168     }
169 }