2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021, 2023 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.base;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
32 import java.util.ArrayList;
33 import java.util.LinkedHashMap;
34 import java.util.List;
37 import java.util.TreeMap;
38 import org.junit.Test;
39 import org.onap.policy.models.base.testconcepts.DummyAuthorativeConcept;
40 import org.onap.policy.models.base.testconcepts.DummyBadPfConceptContainer;
41 import org.onap.policy.models.base.testconcepts.DummyPfConcept;
42 import org.onap.policy.models.base.testconcepts.DummyPfConceptContainer;
43 import org.onap.policy.models.base.testconcepts.DummyPfConceptSub;
45 public class PfConceptContainerTest {
47 private static final String NAME0 = "name0";
48 private static final String NAME1 = "name1";
49 private static final String NAME2 = "name2";
50 private static final String NAME3 = "name3";
51 private static final String ID3 = "name3:0.0.1";
52 private static final String VERSION0_0_1 = "0.0.1";
53 private static final String KEY_IS_NULL = "^key is marked .*on.*ull but is null$";
54 private static final String DUMMY_VALUE = "Dummy";
56 @SuppressWarnings({"unchecked", "rawtypes"})
58 public void testConceptContainer() {
59 DummyPfConceptContainer container = new DummyPfConceptContainer();
60 assertNotNull(container);
62 container = new DummyPfConceptContainer();
63 assertNotNull(container);
65 container = new DummyPfConceptContainer(new PfConceptKey());
66 assertNotNull(container);
68 container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<>());
69 assertNotNull(container);
71 assertThatThrownBy(() -> new PfConceptContainer(null, null)).hasMessageMatching(KEY_IS_NULL);
73 assertThatThrownBy(() -> new DummyPfConceptContainer(null, null))
74 .hasMessageMatching(KEY_IS_NULL);
76 assertThatThrownBy(() -> new DummyPfConceptContainer(new PfConceptKey(), null))
77 .hasMessageMatching("^conceptMap is marked .*on.*ull but is null$");
79 assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<>()))
80 .hasMessageMatching(KEY_IS_NULL);
84 public void testNamedConceptContainer() {
85 DummyPfConceptContainer container = new DummyPfConceptContainer();
86 container.getKey().setName(DUMMY_VALUE);
87 DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
88 assertNotNull(clonedContainer);
89 assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
91 assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
92 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
94 List<PfKey> keyList = container.getKeys();
95 assertEquals(1, keyList.size());
97 PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0_0_1);
98 Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
99 conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
101 container.setConceptMap(conceptMap);
102 keyList = container.getKeys();
103 assertEquals(2, keyList.size());
105 clonedContainer = new DummyPfConceptContainer(container);
106 assertNotNull(clonedContainer);
107 assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
108 assertEquals(2, clonedContainer.getKeys().size());
110 assertEquals(clonedContainer, container);
112 assertEquals(clonedContainer, container);
114 assertThat(container.validate("").getResult()).isNull();
116 assertEquals(0, container.compareTo(clonedContainer));
118 assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
119 .isInstanceOf(NullPointerException.class);
120 assertThatThrownBy(() -> container.compareTo(null)).isInstanceOf(NullPointerException.class);
122 assertEquals(0, container.compareTo(container));
123 assertNotEquals(0, container.compareTo(conceptKey));
125 DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
126 testContainer.getKey().setVersion("0.0.2");
127 assertNotEquals(0, container.compareTo(testContainer));
128 testContainer.getKey().setVersion(container.getKey().getVersion());
129 assertEquals(0, container.compareTo(testContainer));
131 PfConceptKey testConceptKey = new PfConceptKey("TestKey", VERSION0_0_1);
132 testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
133 assertNotEquals(0, container.compareTo(testContainer));
137 public void testValidationContainer() {
138 DummyPfConceptContainer container = new DummyPfConceptContainer();
139 PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0_0_1);
140 Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
141 conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
142 container.setConceptMap(conceptMap);
144 final DummyPfConceptContainer container3 = container;
145 assertThatThrownBy(() -> container3.validate(null))
146 .hasMessageMatching("^fieldName is marked .*on.*ull but is null$");
148 DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
149 assertFalse(validateContainer.validate("").isValid());
150 validateContainer.setKey(new PfConceptKey("VCKey", VERSION0_0_1));
151 assertTrue(validateContainer.validate("").isValid());
153 PfConceptKey testConceptKey = new PfConceptKey("TestKey", VERSION0_0_1);
154 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
155 assertTrue(validateContainer.validate("").isValid());
157 validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
158 assertFalse(validateContainer.validate("").isValid());
159 validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
160 assertTrue(validateContainer.validate("").isValid());
162 validateContainer.getConceptMap().put(testConceptKey, null);
163 assertFalse(validateContainer.validate("").isValid());
164 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
165 assertTrue(validateContainer.validate("").isValid());
167 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
168 assertFalse(validateContainer.validate("").isValid());
169 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
170 assertTrue(validateContainer.validate("").isValid());
174 public void testSetContainer() {
175 DummyPfConceptContainer container = new DummyPfConceptContainer();
176 PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0_0_1);
177 Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
178 conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
179 container.setConceptMap(conceptMap);
181 assertEquals(conceptKey, container.get(conceptKey).getKey());
182 assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
183 assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
185 Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
186 assertEquals(conceptKey, returnSet.iterator().next().getKey());
188 returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
189 assertEquals(conceptKey, returnSet.iterator().next().getKey());
191 returnSet = container.getAllNamesAndVersions(conceptKey.getName(), conceptKey.getVersion());
192 assertEquals(conceptKey, returnSet.iterator().next().getKey());
193 returnSet = container.getAllNamesAndVersions(null, conceptKey.getVersion());
194 assertEquals(conceptKey, returnSet.iterator().next().getKey());
195 returnSet = container.getAllNamesAndVersions(null, null);
196 assertEquals(conceptKey, returnSet.iterator().next().getKey());
197 returnSet = container.getAllNamesAndVersions(conceptKey.getName(), null);
198 assertEquals(conceptKey, returnSet.iterator().next().getKey());
199 returnSet = container.getAllNamesAndVersions(conceptKey.getName(), "0.0.0");
200 assertEquals(conceptKey, returnSet.iterator().next().getKey());
201 returnSet = container.getAllNamesAndVersions("IDontExist", "1.0.0");
202 assertTrue(returnSet.isEmpty());
204 container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
206 PfConceptKey anotherKey = new PfConceptKey(conceptKey);
207 assertEquals(conceptKey, container.get(anotherKey).getKey());
208 anotherKey.setVersion(PfKey.NULL_KEY_VERSION);
209 assertEquals(conceptKey, container.get(anotherKey).getKey());
214 public void testAuthorative() {
215 Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
216 dacMap.put(NAME0, new DummyAuthorativeConcept(NAME0, "1.2.3", "Hello"));
217 dacMap.put(NAME1, new DummyAuthorativeConcept("IncorrectName", PfKey.NULL_KEY_VERSION, "Hi"));
218 dacMap.put(NAME2, new DummyAuthorativeConcept(NAME2, "1.2.3", "Howdy"));
219 dacMap.put(ID3, new DummyAuthorativeConcept(NAME3, "9.9.9", "Ciao"));
220 dacMap.put("name4:1.2.3", new DummyAuthorativeConcept(null, null, "Slan"));
221 dacMap.put("name5", new DummyAuthorativeConcept(null, null, "Bye"));
223 List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
224 authorativeList.add(dacMap);
226 DummyPfConceptContainer container = new DummyPfConceptContainer();
228 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
229 .hasMessage("Key name1:0.0.0 field name1 does not match the value IncorrectName in the concept field");
231 dacMap.put(NAME1, new DummyAuthorativeConcept(NAME1, PfKey.NULL_KEY_VERSION, "Hi"));
233 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
234 .hasMessage("Key name3:0.0.1 field 0.0.1 does not match the value 9.9.9 in the concept field");
236 dacMap.put(ID3, new DummyAuthorativeConcept(NAME3, "0.0.1", "Ciao"));
238 container.fromAuthorative(authorativeList);
240 assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
241 assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("name1:0.0.0")).getDescription());
242 assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
243 assertEquals("Ciao", container.getConceptMap().get(new PfConceptKey("name3:0.0.1")).getDescription());
244 assertEquals("name4", container.getConceptMap().get(new PfConceptKey("name4:1.2.3")).getName());
245 assertEquals("1.2.3", container.getConceptMap().get(new PfConceptKey("name4:1.2.3")).getVersion());
246 assertEquals("0.0.0", container.getConceptMap().get(new PfConceptKey("name5:0.0.0")).getVersion());
248 List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
250 assertEquals(dacMap.get(NAME0), outMapList.get(0).get(NAME0));
251 assertEquals(dacMap.get(NAME1).getDescription(), outMapList.get(1).get(NAME1).getDescription());
252 assertEquals(dacMap.get(NAME2), outMapList.get(2).get(NAME2));
253 assertEquals(dacMap.get(NAME3), outMapList.get(2).get(NAME3));
255 List<DummyAuthorativeConcept> outConceptList = container.toAuthorativeList();
256 assertEquals("Hello", outConceptList.get(0).getDescription());
257 assertEquals("Hi", outConceptList.get(1).getDescription());
258 assertEquals("Howdy", outConceptList.get(2).getDescription());
259 assertEquals("Ciao", outConceptList.get(3).getDescription());
260 assertEquals("name4", outConceptList.get(4).getName());
261 assertEquals("1.2.3", outConceptList.get(4).getVersion());
262 assertEquals("0.0.0", outConceptList.get(5).getVersion());
264 DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
265 assertThatThrownBy(() -> badContainer.fromAuthorative(authorativeList))
266 .hasMessage("failed to instantiate instance of container concept class");
268 authorativeList.clear();
269 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
270 .hasMessage("An incoming list of concepts must have at least one entry");
273 @Test(expected = NullPointerException.class)
274 public void testNullKey() {
275 PfConceptKey nullKey = null;
276 new DummyPfConceptContainer(nullKey);