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 {
51 @SuppressWarnings({ "unchecked", "rawtypes" })
53 public void testConceptContainer() {
54 DummyPfConceptContainer container = new DummyPfConceptContainer();
55 assertNotNull(container);
57 container = new DummyPfConceptContainer();
58 assertNotNull(container);
60 container = new DummyPfConceptContainer(new PfConceptKey());
61 assertNotNull(container);
63 container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
64 assertNotNull(container);
67 new PfConceptContainer((PfConceptKey) null, null);
68 fail("test should throw an exception here");
69 } catch (Exception exc) {
70 assertEquals("key is marked @NonNull but is null", exc.getMessage());
74 container = new DummyPfConceptContainer((PfConceptKey) null, null);
75 fail("test should throw an exception here");
76 } catch (Exception exc) {
77 assertEquals("key is marked @NonNull but is null", exc.getMessage());
81 container = new DummyPfConceptContainer(new PfConceptKey(), null);
82 fail("test should throw an exception here");
83 } catch (Exception exc) {
84 assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage());
88 container = new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>());
89 fail("test should throw an exception here");
90 } catch (Exception exc) {
91 assertEquals("key is marked @NonNull but is null", exc.getMessage());
94 container.getKey().setName("Dummy");
95 DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
96 assertNotNull(clonedContainer);
97 assertEquals("Dummy", clonedContainer.getKey().getName());
100 DummyPfConceptContainer conceptContainter = null;
101 container = new DummyPfConceptContainer(conceptContainter);
102 fail("test should throw an exception here");
103 } catch (Exception exc) {
104 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
107 List<PfKey> keyList = container.getKeys();
108 assertEquals(1, keyList.size());
110 PfConceptKey conceptKey = new PfConceptKey("Key", "0.0.1");
111 Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
112 conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
114 container.setConceptMap(conceptMap);
115 keyList = container.getKeys();
116 assertEquals(2, keyList.size());
118 clonedContainer = new DummyPfConceptContainer(container);
119 assertNotNull(clonedContainer);
120 assertEquals("Dummy", clonedContainer.getKey().getName());
121 assertEquals(2, clonedContainer.getKeys().size());
123 assertEquals(clonedContainer, container);
125 assertEquals(clonedContainer, container);
127 PfValidationResult result = new PfValidationResult();
128 result = container.validate(result);
129 assertTrue(result.isOk());
131 assertEquals(0, container.compareTo(clonedContainer));
134 container.copyTo(null);
135 fail("test should throw an exception here");
136 } catch (Exception exc) {
137 assertEquals("target is marked @NonNull but is null", exc.getMessage());
140 assertFalse(container.compareTo(null) == 0);
141 assertEquals(0, container.compareTo(container));
142 assertFalse(container.compareTo(conceptKey) == 0);
144 DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
145 testContainer.getKey().setVersion("0.0.2");
146 assertFalse(container.compareTo(testContainer) == 0);
147 testContainer.getKey().setVersion(container.getKey().getVersion());
148 assertEquals(0, container.compareTo(testContainer));
150 PfConceptKey testConceptKey = new PfConceptKey("TestKey", "0.0.1");
151 testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
152 assertFalse(container.compareTo(testContainer) == 0);
155 container.validate(null);
156 fail("test should throw an exception here");
157 } catch (Exception exc) {
158 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
161 DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
162 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
163 validateContainer.setKey(new PfConceptKey("VCKey", "0.0.1"));
164 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
166 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
167 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
169 validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
170 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
171 validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
172 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
174 validateContainer.getConceptMap().put(testConceptKey, null);
175 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
176 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
177 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
179 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
180 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
181 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
182 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
184 assertEquals(conceptKey, container.get(conceptKey).getKey());
185 assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
186 assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
188 Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
189 assertEquals(conceptKey, returnSet.iterator().next().getKey());
191 returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
192 assertEquals(conceptKey, returnSet.iterator().next().getKey());
194 container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
198 public void testAuthorative() {
199 Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
200 dacMap.put("name0", new DummyAuthorativeConcept("name0", "1.2.3", "Hello"));
201 dacMap.put("name1", new DummyAuthorativeConcept(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "Hi"));
202 dacMap.put("name2", new DummyAuthorativeConcept("name2", "1.2.3", "Howdy"));
204 List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
205 authorativeList.add(dacMap);
207 DummyPfConceptContainer container = new DummyPfConceptContainer();
208 container.fromAuthorative(authorativeList);
210 assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
211 assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("NULL:0.0.0")).getDescription());
212 assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
214 List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
216 assertEquals(dacMap.get("name0"), outMapList.get(0).get("name0"));
217 assertEquals(dacMap.get("name1").getDescription(), outMapList.get(0).get("NULL").getDescription());
218 assertEquals(dacMap.get("name2"), outMapList.get(0).get("name2"));
220 DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
221 assertThatThrownBy(() -> {
222 badContainer.fromAuthorative(authorativeList);
223 }).hasMessage("failed to instantiate instance of container concept class");
225 authorativeList.clear();
226 assertThatThrownBy(() -> {
227 container.fromAuthorative(authorativeList);
228 }).hasMessage("An incoming list of concepts must have at least one entry");
231 @Test(expected = NullPointerException.class)
232 public void testnullKey() {
233 PfConceptKey nullKey = null;
234 new DummyPfConceptContainer(nullKey);