2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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 NAME2 = "name2";
51 private static final String NAME1 = "name1";
52 private static final String NAME0 = "name0";
53 private static final String KEY_IS_NULL = "key is marked @NonNull but is null";
54 private static final String DUMMY_VALUE = "Dummy";
55 private static final String VERSION0 = "0.0.1";
57 @SuppressWarnings({ "unchecked", "rawtypes" })
59 public void testConceptContainer() {
60 DummyPfConceptContainer container = new DummyPfConceptContainer();
61 assertNotNull(container);
63 container = new DummyPfConceptContainer();
64 assertNotNull(container);
66 container = new DummyPfConceptContainer(new PfConceptKey());
67 assertNotNull(container);
69 container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
70 assertNotNull(container);
72 assertThatThrownBy(() -> new PfConceptContainer((PfConceptKey) null, null)).hasMessage(KEY_IS_NULL);
74 assertThatThrownBy(() -> new DummyPfConceptContainer((PfConceptKey) null, null)).hasMessage(KEY_IS_NULL);
76 assertThatThrownBy(() -> new DummyPfConceptContainer(new PfConceptKey(), null))
77 .hasMessage("conceptMap is marked @NonNull but is null");
79 assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>()))
80 .hasMessage(KEY_IS_NULL);
82 container.getKey().setName(DUMMY_VALUE);
83 DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
84 assertNotNull(clonedContainer);
85 assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
87 assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
88 .hasMessage("copyConcept is marked @NonNull but is null");
90 List<PfKey> keyList = container.getKeys();
91 assertEquals(1, keyList.size());
93 PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0);
94 Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
95 conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
97 container.setConceptMap(conceptMap);
98 keyList = container.getKeys();
99 assertEquals(2, keyList.size());
101 clonedContainer = new DummyPfConceptContainer(container);
102 assertNotNull(clonedContainer);
103 assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
104 assertEquals(2, clonedContainer.getKeys().size());
106 assertEquals(clonedContainer, container);
108 assertEquals(clonedContainer, container);
110 PfValidationResult result = new PfValidationResult();
111 result = container.validate(result);
112 assertTrue(result.isOk());
114 assertEquals(0, container.compareTo(clonedContainer));
116 assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
117 .isInstanceOf(NullPointerException.class);
119 assertFalse(container.compareTo(null) == 0);
120 assertEquals(0, container.compareTo(container));
121 assertFalse(container.compareTo(conceptKey) == 0);
123 DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
124 testContainer.getKey().setVersion("0.0.2");
125 assertFalse(container.compareTo(testContainer) == 0);
126 testContainer.getKey().setVersion(container.getKey().getVersion());
127 assertEquals(0, container.compareTo(testContainer));
129 PfConceptKey testConceptKey = new PfConceptKey("TestKey", VERSION0);
130 testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
131 assertFalse(container.compareTo(testContainer) == 0);
133 final DummyPfConceptContainer container3 = container;
134 assertThatThrownBy(() -> container3.validate(null)).hasMessage("resultIn is marked @NonNull but is null");
136 DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
137 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
138 validateContainer.setKey(new PfConceptKey("VCKey", VERSION0));
139 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
141 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
142 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
144 validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
145 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
146 validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
147 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
149 validateContainer.getConceptMap().put(testConceptKey, null);
150 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
151 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
152 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
154 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
155 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
156 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
157 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
159 assertEquals(conceptKey, container.get(conceptKey).getKey());
160 assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
161 assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
163 Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
164 assertEquals(conceptKey, returnSet.iterator().next().getKey());
166 returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
167 assertEquals(conceptKey, returnSet.iterator().next().getKey());
169 container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
173 public void testAuthorative() {
174 Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
175 dacMap.put(NAME0, new DummyAuthorativeConcept(NAME0, "1.2.3", "Hello"));
176 dacMap.put(NAME1, new DummyAuthorativeConcept(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "Hi"));
177 dacMap.put(NAME2, new DummyAuthorativeConcept(NAME2, "1.2.3", "Howdy"));
179 List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
180 authorativeList.add(dacMap);
182 DummyPfConceptContainer container = new DummyPfConceptContainer();
183 container.fromAuthorative(authorativeList);
185 assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
186 assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("NULL:0.0.0")).getDescription());
187 assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
189 List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
191 assertEquals(dacMap.get(NAME1), outMapList.get(0).get("NULL"));
192 assertEquals(dacMap.get(NAME0).getDescription(), outMapList.get(1).get(NAME0).getDescription());
193 assertEquals(dacMap.get(NAME2), outMapList.get(2).get(NAME2));
195 DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
196 assertThatThrownBy(() -> badContainer.fromAuthorative(authorativeList))
197 .hasMessage("failed to instantiate instance of container concept class");
199 authorativeList.clear();
200 assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
201 .hasMessage("An incoming list of concepts must have at least one entry");
204 @Test(expected = NullPointerException.class)
205 public void testnullKey() {
206 PfConceptKey nullKey = null;
207 new DummyPfConceptContainer(nullKey);