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 NAME2 = "name2";
52 private static final String NAME1 = "name1";
53 private static final String NAME0 = "name0";
54 private static final String KEY_IS_NULL = "key is marked @NonNull but is null";
55 private static final String DUMMY_VALUE = "Dummy";
56 private static final String VERSION0 = "0.0.1";
58 @SuppressWarnings({"unchecked", "rawtypes"})
60 public void testConceptContainer() {
61 DummyPfConceptContainer container = new DummyPfConceptContainer();
62 assertNotNull(container);
64 container = new DummyPfConceptContainer();
65 assertNotNull(container);
67 container = new DummyPfConceptContainer(new PfConceptKey());
68 assertNotNull(container);
70 container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
71 assertNotNull(container);
73 assertThatThrownBy(() -> new PfConceptContainer((PfConceptKey) null, null)).hasMessage(KEY_IS_NULL);
75 assertThatThrownBy(() -> new DummyPfConceptContainer((PfConceptKey) null, null)).hasMessage(KEY_IS_NULL);
77 assertThatThrownBy(() -> new DummyPfConceptContainer(new PfConceptKey(), null))
78 .hasMessage("conceptMap is marked @NonNull but is null");
80 assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>()))
81 .hasMessage(KEY_IS_NULL);
83 container.getKey().setName(DUMMY_VALUE);
84 DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
85 assertNotNull(clonedContainer);
86 assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
88 assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
89 .hasMessage("copyConcept is marked @NonNull but is null");
91 List<PfKey> keyList = container.getKeys();
92 assertEquals(1, keyList.size());
94 PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0);
95 Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
96 conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
98 container.setConceptMap(conceptMap);
99 keyList = container.getKeys();
100 assertEquals(2, keyList.size());
102 clonedContainer = new DummyPfConceptContainer(container);
103 assertNotNull(clonedContainer);
104 assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
105 assertEquals(2, clonedContainer.getKeys().size());
107 assertEquals(clonedContainer, container);
109 assertEquals(clonedContainer, container);
111 PfValidationResult result = new PfValidationResult();
112 result = container.validate(result);
113 assertTrue(result.isOk());
115 assertEquals(0, container.compareTo(clonedContainer));
117 assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
118 .isInstanceOf(NullPointerException.class);
120 assertFalse(container.compareTo(null) == 0);
121 assertEquals(0, container.compareTo(container));
122 assertFalse(container.compareTo(conceptKey) == 0);
124 DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
125 testContainer.getKey().setVersion("0.0.2");
126 assertFalse(container.compareTo(testContainer) == 0);
127 testContainer.getKey().setVersion(container.getKey().getVersion());
128 assertEquals(0, container.compareTo(testContainer));
130 PfConceptKey testConceptKey = new PfConceptKey("TestKey", VERSION0);
131 testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
132 assertFalse(container.compareTo(testContainer) == 0);
134 final DummyPfConceptContainer container3 = container;
135 assertThatThrownBy(() -> container3.validate(null)).hasMessage("resultIn is marked @NonNull but is null");
137 DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
138 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
139 validateContainer.setKey(new PfConceptKey("VCKey", VERSION0));
140 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
142 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
143 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
145 validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
146 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
147 validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
148 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
150 validateContainer.getConceptMap().put(testConceptKey, null);
151 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
152 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
153 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
155 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
156 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
157 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
158 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
160 assertEquals(conceptKey, container.get(conceptKey).getKey());
161 assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
162 assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
164 Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
165 assertEquals(conceptKey, returnSet.iterator().next().getKey());
167 returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
168 assertEquals(conceptKey, returnSet.iterator().next().getKey());
170 container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
174 public void testAuthorative() {
175 Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
176 dacMap.put(NAME0, new DummyAuthorativeConcept(NAME0, "1.2.3", "Hello"));
177 dacMap.put(NAME1, new DummyAuthorativeConcept(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "Hi"));
178 dacMap.put(NAME2, new DummyAuthorativeConcept(NAME2, "1.2.3", "Howdy"));
180 List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
181 authorativeList.add(dacMap);
183 DummyPfConceptContainer container = new DummyPfConceptContainer();
184 container.fromAuthorative(authorativeList);
186 assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
187 assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("NULL:0.0.0")).getDescription());
188 assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
190 List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
192 assertEquals(dacMap.get(NAME1), outMapList.get(0).get("NULL"));
193 assertEquals(dacMap.get(NAME0).getDescription(), outMapList.get(1).get(NAME0).getDescription());
194 assertEquals(dacMap.get(NAME2), outMapList.get(2).get(NAME2));
196 DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
197 assertThatThrownBy(() -> badContainer.fromAuthorative(authorativeList))
198 .hasMessage("failed to instantiate instance of container concept class");
200 authorativeList.clear();
201 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
202 .hasMessage("An incoming list of concepts must have at least one entry");
205 @Test(expected = NullPointerException.class)
206 public void testnullKey() {
207 PfConceptKey nullKey = null;
208 new DummyPfConceptContainer(nullKey);