2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 Nordix Foundation.
4 * Modifications Copyright (C) 2019 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.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
35 import java.util.TreeMap;
36 import org.junit.Test;
37 import org.onap.policy.models.base.testconcepts.DummyAuthorativeConcept;
38 import org.onap.policy.models.base.testconcepts.DummyBadPfConceptContainer;
39 import org.onap.policy.models.base.testconcepts.DummyPfConcept;
40 import org.onap.policy.models.base.testconcepts.DummyPfConceptContainer;
41 import org.onap.policy.models.base.testconcepts.DummyPfConceptSub;
44 * Test the PfCOnceptCOntainer class.
46 * @author Liam Fallon (liam.fallon@est.tech)
48 public class PfConceptContainerTest {
50 private static final String NAME0 = "name0";
51 private static final String NAME1 = "name1";
52 private static final String NAME2 = "name2";
53 private static final String NAME3 = "name3";
54 private static final String ID3 = "name3:0.0.1";
55 private static final String VERSION0_0_1 = "0.0.1";
56 private static final String KEY_IS_NULL = "^key is marked .*on.*ull but is null$";
57 private static final String DUMMY_VALUE = "Dummy";
59 @SuppressWarnings({"unchecked", "rawtypes"})
61 public void testConceptContainer() {
62 DummyPfConceptContainer container = new DummyPfConceptContainer();
63 assertNotNull(container);
65 container = new DummyPfConceptContainer();
66 assertNotNull(container);
68 container = new DummyPfConceptContainer(new PfConceptKey());
69 assertNotNull(container);
71 container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
72 assertNotNull(container);
74 assertThatThrownBy(() -> new PfConceptContainer((PfConceptKey) null, null)).hasMessageMatching(KEY_IS_NULL);
76 assertThatThrownBy(() -> new DummyPfConceptContainer((PfConceptKey) null, null))
77 .hasMessageMatching(KEY_IS_NULL);
79 assertThatThrownBy(() -> new DummyPfConceptContainer(new PfConceptKey(), null))
80 .hasMessageMatching("^conceptMap is marked .*on.*ull but is null$");
82 assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>()))
83 .hasMessageMatching(KEY_IS_NULL);
85 container.getKey().setName(DUMMY_VALUE);
86 DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
87 assertNotNull(clonedContainer);
88 assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
90 assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
91 .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
93 List<PfKey> keyList = container.getKeys();
94 assertEquals(1, keyList.size());
96 PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0_0_1);
97 Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
98 conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
100 container.setConceptMap(conceptMap);
101 keyList = container.getKeys();
102 assertEquals(2, keyList.size());
104 clonedContainer = new DummyPfConceptContainer(container);
105 assertNotNull(clonedContainer);
106 assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
107 assertEquals(2, clonedContainer.getKeys().size());
109 assertEquals(clonedContainer, container);
111 assertEquals(clonedContainer, container);
113 PfValidationResult result = new PfValidationResult();
114 result = container.validate(result);
115 assertTrue(result.isOk());
117 assertEquals(0, container.compareTo(clonedContainer));
119 assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
120 .isInstanceOf(NullPointerException.class);
122 assertFalse(container.compareTo(null) == 0);
123 assertEquals(0, container.compareTo(container));
124 assertFalse(container.compareTo(conceptKey) == 0);
126 DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
127 testContainer.getKey().setVersion("0.0.2");
128 assertFalse(container.compareTo(testContainer) == 0);
129 testContainer.getKey().setVersion(container.getKey().getVersion());
130 assertEquals(0, container.compareTo(testContainer));
132 PfConceptKey testConceptKey = new PfConceptKey("TestKey", VERSION0_0_1);
133 testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
134 assertFalse(container.compareTo(testContainer) == 0);
136 final DummyPfConceptContainer container3 = container;
137 assertThatThrownBy(() -> container3.validate(null))
138 .hasMessageMatching("^resultIn is marked .*on.*ull but is null$");
140 DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
141 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
142 validateContainer.setKey(new PfConceptKey("VCKey", VERSION0_0_1));
143 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
145 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
146 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
148 validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
149 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
150 validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
151 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
153 validateContainer.getConceptMap().put(testConceptKey, null);
154 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
155 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
156 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
158 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
159 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
160 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
161 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
163 assertEquals(conceptKey, container.get(conceptKey).getKey());
164 assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
165 assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
167 Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
168 assertEquals(conceptKey, returnSet.iterator().next().getKey());
170 returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
171 assertEquals(conceptKey, returnSet.iterator().next().getKey());
173 returnSet = container.getAllNamesAndVersions(conceptKey.getName(), conceptKey.getVersion());
174 assertEquals(conceptKey, returnSet.iterator().next().getKey());
175 returnSet = container.getAllNamesAndVersions(null, conceptKey.getVersion());
176 assertEquals(conceptKey, returnSet.iterator().next().getKey());
177 returnSet = container.getAllNamesAndVersions(null, null);
178 assertEquals(conceptKey, returnSet.iterator().next().getKey());
179 returnSet = container.getAllNamesAndVersions(conceptKey.getName(), null);
180 assertEquals(conceptKey, returnSet.iterator().next().getKey());
181 returnSet = container.getAllNamesAndVersions(conceptKey.getName(), "0.0.0");
182 assertEquals(conceptKey, returnSet.iterator().next().getKey());
183 returnSet = container.getAllNamesAndVersions("IDontExist", "1.0.0");
184 assertTrue(returnSet.isEmpty());
186 container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
188 PfConceptKey anotherKey = new PfConceptKey(conceptKey);
189 assertEquals(conceptKey, container.get(anotherKey).getKey());
190 anotherKey.setVersion(PfKey.NULL_KEY_VERSION);
191 assertEquals(conceptKey, container.get(anotherKey).getKey());
195 public void testAuthorative() {
196 Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
197 dacMap.put(NAME0, new DummyAuthorativeConcept(NAME0, "1.2.3", "Hello"));
198 dacMap.put(NAME1, new DummyAuthorativeConcept("IncorrectName", PfKey.NULL_KEY_VERSION, "Hi"));
199 dacMap.put(NAME2, new DummyAuthorativeConcept(NAME2, "1.2.3", "Howdy"));
200 dacMap.put(ID3, new DummyAuthorativeConcept(NAME3, "9.9.9", "Ciao"));
201 dacMap.put("name4:1.2.3", new DummyAuthorativeConcept(null, null, "Slan"));
202 dacMap.put("name5", new DummyAuthorativeConcept(null, null, "Bye"));
204 List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
205 authorativeList.add(dacMap);
207 DummyPfConceptContainer container = new DummyPfConceptContainer();
209 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
210 .hasMessage("Key name1:0.0.0 field name1 does not match the value IncorrectName in the concept field");
212 dacMap.put(NAME1, new DummyAuthorativeConcept(NAME1, PfKey.NULL_KEY_VERSION, "Hi"));
214 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
215 .hasMessage("Key name3:0.0.1 field 0.0.1 does not match the value 9.9.9 in the concept field");
217 dacMap.put(ID3, new DummyAuthorativeConcept(NAME3, "0.0.1", "Ciao"));
219 container.fromAuthorative(authorativeList);
221 assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
222 assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("name1:0.0.0")).getDescription());
223 assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
224 assertEquals("Ciao", container.getConceptMap().get(new PfConceptKey("name3:0.0.1")).getDescription());
225 assertEquals("name4", container.getConceptMap().get(new PfConceptKey("name4:1.2.3")).getName());
226 assertEquals("1.2.3", container.getConceptMap().get(new PfConceptKey("name4:1.2.3")).getVersion());
227 assertEquals("0.0.0", container.getConceptMap().get(new PfConceptKey("name5:0.0.0")).getVersion());
229 List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
231 assertEquals(dacMap.get(NAME0), outMapList.get(0).get(NAME0));
232 assertEquals(dacMap.get(NAME1).getDescription(), outMapList.get(1).get(NAME1).getDescription());
233 assertEquals(dacMap.get(NAME2), outMapList.get(2).get(NAME2));
234 assertEquals(dacMap.get(NAME3), outMapList.get(2).get(NAME3));
236 List<DummyAuthorativeConcept> outConceptList = container.toAuthorativeList();
237 assertEquals("Hello", outConceptList.get(0).getDescription());
238 assertEquals("Hi", outConceptList.get(1).getDescription());
239 assertEquals("Howdy", outConceptList.get(2).getDescription());
240 assertEquals("Ciao", outConceptList.get(3).getDescription());
241 assertEquals("name4", outConceptList.get(4).getName());
242 assertEquals("1.2.3", outConceptList.get(4).getVersion());
243 assertEquals("0.0.0", outConceptList.get(5).getVersion());
245 DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
246 assertThatThrownBy(() -> badContainer.fromAuthorative(authorativeList))
247 .hasMessage("failed to instantiate instance of container concept class");
249 authorativeList.clear();
250 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
251 .hasMessage("An incoming list of concepts must have at least one entry");
254 @Test(expected = NullPointerException.class)
255 public void testnullKey() {
256 PfConceptKey nullKey = null;
257 new DummyPfConceptContainer(nullKey);