2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.base;
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
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 {
52 public void testConceptContainer() {
53 DummyPfConceptContainer container = new DummyPfConceptContainer();
54 assertNotNull(container);
56 container = new DummyPfConceptContainer();
57 assertNotNull(container);
59 container = new DummyPfConceptContainer(new PfConceptKey());
60 assertNotNull(container);
62 container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
63 assertNotNull(container);
66 container = new DummyPfConceptContainer((PfConceptKey) null, null);
67 fail("test should throw an exception here");
68 } catch (Exception exc) {
69 assertEquals("key is marked @NonNull but is null", exc.getMessage());
73 container = new DummyPfConceptContainer(new PfConceptKey(), null);
74 fail("test should throw an exception here");
75 } catch (Exception exc) {
76 assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage());
80 container = new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>());
81 fail("test should throw an exception here");
82 } catch (Exception exc) {
83 assertEquals("key is marked @NonNull but is null", exc.getMessage());
86 container.getKey().setName("Dummy");
87 DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
88 assertNotNull(clonedContainer);
89 assertEquals("Dummy", clonedContainer.getKey().getName());
92 DummyPfConceptContainer conceptContainter = null;
93 container = new DummyPfConceptContainer(conceptContainter);
94 fail("test should throw an exception here");
95 } catch (Exception exc) {
96 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
99 List<PfKey> keyList = container.getKeys();
100 assertEquals(1, keyList.size());
102 PfConceptKey conceptKey = new PfConceptKey("Key", "0.0.1");
103 Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
104 conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
106 container.setConceptMap(conceptMap);
107 keyList = container.getKeys();
108 assertEquals(2, keyList.size());
110 clonedContainer = new DummyPfConceptContainer(container);
111 assertNotNull(clonedContainer);
112 assertEquals("Dummy", clonedContainer.getKey().getName());
113 assertEquals(2, clonedContainer.getKeys().size());
115 assertEquals(clonedContainer, container);
117 assertEquals(clonedContainer, container);
119 PfValidationResult result = new PfValidationResult();
120 result = container.validate(result);
121 assertTrue(result.isOk());
123 assertEquals(0, container.compareTo(clonedContainer));
126 container.copyTo(null);
127 fail("test should throw an exception here");
128 } catch (Exception exc) {
129 assertEquals("target is marked @NonNull but is null", exc.getMessage());
132 assertFalse(container.compareTo(null) == 0);
133 assertEquals(0, container.compareTo(container));
134 assertFalse(container.compareTo(conceptKey) == 0);
136 DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
137 testContainer.getKey().setVersion("0.0.2");
138 assertFalse(container.compareTo(testContainer) == 0);
139 testContainer.getKey().setVersion(container.getKey().getVersion());
140 assertEquals(0, container.compareTo(testContainer));
142 PfConceptKey testConceptKey = new PfConceptKey("TestKey", "0.0.1");
143 testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
144 assertFalse(container.compareTo(testContainer) == 0);
147 container.validate(null);
148 fail("test should throw an exception here");
149 } catch (Exception exc) {
150 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
153 DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
154 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
155 validateContainer.setKey(new PfConceptKey("VCKey", "0.0.1"));
156 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
158 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
159 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
161 validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
162 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
163 validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
164 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
166 validateContainer.getConceptMap().put(testConceptKey, null);
167 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
168 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
169 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
171 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
172 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
173 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
174 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
176 assertEquals(conceptKey, container.get(conceptKey).getKey());
177 assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
178 assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
180 Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
181 assertEquals(conceptKey, returnSet.iterator().next().getKey());
183 returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
184 assertEquals(conceptKey, returnSet.iterator().next().getKey());
186 container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
190 public void testAuthorative() {
191 Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
192 dacMap.put("name0", new DummyAuthorativeConcept("name0", "1.2.3", "Hello"));
193 dacMap.put("name1", new DummyAuthorativeConcept("name1", "1.2.3", "Hi"));
194 dacMap.put("name2", new DummyAuthorativeConcept("name2", "1.2.3", "Howdy"));
196 List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
197 authorativeList.add(dacMap);
199 DummyPfConceptContainer container = new DummyPfConceptContainer();
200 container.fromAuthorative(authorativeList);
202 assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
203 assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("name1:1.2.3")).getDescription());
204 assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
206 List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
208 assertEquals(dacMap, outMapList.get(0));
210 DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
211 assertThatThrownBy(() -> {
212 badContainer.fromAuthorative(authorativeList);
213 }).hasMessage("failed to instantiate instance of container concept class");
215 authorativeList.clear();
216 assertThatThrownBy(() -> {
217 container.fromAuthorative(authorativeList);
218 }).hasMessage("An incoming list of concepts must have at least one entry");
221 @Test(expected = NullPointerException.class)
222 public void testnullKey() {
223 PfConceptKey nullKey = null;
224 new DummyPfConceptContainer(nullKey);