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;
37 import org.junit.Test;
38 import org.onap.policy.models.base.testconcepts.DummyAuthorativeConcept;
39 import org.onap.policy.models.base.testconcepts.DummyBadPfConceptContainer;
40 import org.onap.policy.models.base.testconcepts.DummyPfConcept;
41 import org.onap.policy.models.base.testconcepts.DummyPfConceptContainer;
42 import org.onap.policy.models.base.testconcepts.DummyPfConceptSub;
45 * Test the PfCOnceptCOntainer class.
47 * @author Liam Fallon (liam.fallon@est.tech)
49 public class PfConceptContainerTest {
51 private static final String NAME0 = "name0";
52 private static final String NAME1 = "name1";
53 private static final String NAME2 = "name2";
54 private static final String NAME3 = "name3";
55 private static final String ID3 = "name3:0.0.1";
56 private static final String VERSION0_0_1 = "0.0.1";
57 private static final String KEY_IS_NULL = "^key is marked .*on.*ull but is null$";
58 private static final String DUMMY_VALUE = "Dummy";
60 @SuppressWarnings({"unchecked", "rawtypes"})
62 public void testConceptContainer() {
63 DummyPfConceptContainer container = new DummyPfConceptContainer();
64 assertNotNull(container);
66 container = new DummyPfConceptContainer();
67 assertNotNull(container);
69 container = new DummyPfConceptContainer(new PfConceptKey());
70 assertNotNull(container);
72 container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
73 assertNotNull(container);
75 assertThatThrownBy(() -> new PfConceptContainer((PfConceptKey) null, null)).hasMessageMatching(KEY_IS_NULL);
77 assertThatThrownBy(() -> new DummyPfConceptContainer((PfConceptKey) null, null))
78 .hasMessageMatching(KEY_IS_NULL);
80 assertThatThrownBy(() -> new DummyPfConceptContainer(new PfConceptKey(), null))
81 .hasMessageMatching("^conceptMap is marked .*on.*ull but is null$");
83 assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>()))
84 .hasMessageMatching(KEY_IS_NULL);
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 PfValidationResult result = new PfValidationResult();
115 result = container.validate(result);
116 assertTrue(result.isOk());
118 assertEquals(0, container.compareTo(clonedContainer));
120 assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
121 .isInstanceOf(NullPointerException.class);
123 assertFalse(container.compareTo(null) == 0);
124 assertEquals(0, container.compareTo(container));
125 assertFalse(container.compareTo(conceptKey) == 0);
127 DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
128 testContainer.getKey().setVersion("0.0.2");
129 assertFalse(container.compareTo(testContainer) == 0);
130 testContainer.getKey().setVersion(container.getKey().getVersion());
131 assertEquals(0, container.compareTo(testContainer));
133 PfConceptKey testConceptKey = new PfConceptKey("TestKey", VERSION0_0_1);
134 testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
135 assertFalse(container.compareTo(testContainer) == 0);
137 final DummyPfConceptContainer container3 = container;
138 assertThatThrownBy(() -> container3.validate(null))
139 .hasMessageMatching("^resultIn is marked .*on.*ull but is null$");
141 DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
142 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
143 validateContainer.setKey(new PfConceptKey("VCKey", VERSION0_0_1));
144 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
146 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
147 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
149 validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
150 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
151 validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
152 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
154 validateContainer.getConceptMap().put(testConceptKey, null);
155 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
156 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
157 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
159 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
160 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
161 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
162 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
164 assertEquals(conceptKey, container.get(conceptKey).getKey());
165 assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
166 assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
168 Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
169 assertEquals(conceptKey, returnSet.iterator().next().getKey());
171 returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
172 assertEquals(conceptKey, returnSet.iterator().next().getKey());
174 container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
178 public void testAuthorative() {
179 Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
180 dacMap.put(NAME0, new DummyAuthorativeConcept(NAME0, "1.2.3", "Hello"));
181 dacMap.put(NAME1, new DummyAuthorativeConcept("IncorrectName", PfKey.NULL_KEY_VERSION, "Hi"));
182 dacMap.put(NAME2, new DummyAuthorativeConcept(NAME2, "1.2.3", "Howdy"));
183 dacMap.put(ID3, new DummyAuthorativeConcept(NAME3, "9.9.9", "Ciao"));
184 dacMap.put("name4:1.2.3", new DummyAuthorativeConcept(null, null, "Slan"));
185 dacMap.put("name5", new DummyAuthorativeConcept(null, null, "Bye"));
187 List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
188 authorativeList.add(dacMap);
190 DummyPfConceptContainer container = new DummyPfConceptContainer();
192 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
193 .hasMessage("Key name1:0.0.0 field name1 does not match the value IncorrectName in the concept field");
195 dacMap.put(NAME1, new DummyAuthorativeConcept(NAME1, PfKey.NULL_KEY_VERSION, "Hi"));
197 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
198 .hasMessage("Key name3:0.0.1 field 0.0.1 does not match the value 9.9.9 in the concept field");
200 dacMap.put(ID3, new DummyAuthorativeConcept(NAME3, "0.0.1", "Ciao"));
202 container.fromAuthorative(authorativeList);
204 assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
205 assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("name1:0.0.0")).getDescription());
206 assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
207 assertEquals("Ciao", container.getConceptMap().get(new PfConceptKey("name3:0.0.1")).getDescription());
208 assertEquals("name4", container.getConceptMap().get(new PfConceptKey("name4:1.2.3")).getName());
209 assertEquals("1.2.3", container.getConceptMap().get(new PfConceptKey("name4:1.2.3")).getVersion());
210 assertEquals("0.0.0", container.getConceptMap().get(new PfConceptKey("name5:0.0.0")).getVersion());
212 List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
214 assertEquals(dacMap.get(NAME0), outMapList.get(0).get(NAME0));
215 assertEquals(dacMap.get(NAME1).getDescription(), outMapList.get(1).get(NAME1).getDescription());
216 assertEquals(dacMap.get(NAME2), outMapList.get(2).get(NAME2));
217 assertEquals(dacMap.get(NAME3), outMapList.get(2).get(NAME3));
219 DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
220 assertThatThrownBy(() -> badContainer.fromAuthorative(authorativeList))
221 .hasMessage("failed to instantiate instance of container concept class");
223 authorativeList.clear();
224 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
225 .hasMessage("An incoming list of concepts must have at least one entry");
228 @Test(expected = NullPointerException.class)
229 public void testnullKey() {
230 PfConceptKey nullKey = null;
231 new DummyPfConceptContainer(nullKey);