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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
29 import java.util.List;
32 import java.util.TreeMap;
34 import org.junit.Test;
35 import org.onap.policy.models.base.testconcepts.DummyPfConcept;
36 import org.onap.policy.models.base.testconcepts.DummyPfConceptContainer;
37 import org.onap.policy.models.base.testconcepts.DummyPfConceptSub;
40 * Test the PfCOnceptCOntainer class.
42 * @author Liam Fallon (liam.fallon@est.tech)
44 public class PfConceptContainerTest {
48 DummyPfConceptContainer container = new DummyPfConceptContainer();
49 assertNotNull(container);
51 container = new DummyPfConceptContainer();
52 assertNotNull(container);
54 container = new DummyPfConceptContainer(new PfConceptKey());
55 assertNotNull(container);
57 container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
58 assertNotNull(container);
61 container = new DummyPfConceptContainer((PfConceptKey) null, null);
62 fail("test should throw an exception here");
63 } catch (Exception exc) {
64 assertEquals("key is marked @NonNull but is null", exc.getMessage());
68 container = new DummyPfConceptContainer(new PfConceptKey(), null);
69 fail("test should throw an exception here");
70 } catch (Exception exc) {
71 assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage());
75 container = new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>());
76 fail("test should throw an exception here");
77 } catch (Exception exc) {
78 assertEquals("key is marked @NonNull but is null", exc.getMessage());
81 container.getKey().setName("Dummy");
82 DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
83 assertNotNull(clonedContainer);
84 assertEquals("Dummy", clonedContainer.getKey().getName());
87 DummyPfConceptContainer conceptContainter = null;
88 container = new DummyPfConceptContainer(conceptContainter);
89 fail("test should throw an exception here");
90 } catch (Exception exc) {
91 assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
94 List<PfKey> keyList = container.getKeys();
95 assertEquals(1, keyList.size());
97 PfConceptKey conceptKey = new PfConceptKey("Key", "0.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", 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));
121 container.copyTo(null);
122 fail("test should throw an exception here");
123 } catch (Exception exc) {
124 assertEquals("target is marked @NonNull but is null", exc.getMessage());
127 assertFalse(container.compareTo(null) == 0);
128 assertEquals(0, container.compareTo(container));
129 assertFalse(container.compareTo(conceptKey) == 0);
131 DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
132 testContainer.getKey().setVersion("0.0.2");
133 assertFalse(container.compareTo(testContainer) == 0);
134 testContainer.getKey().setVersion(container.getKey().getVersion());
135 assertEquals(0, container.compareTo(testContainer));
137 PfConceptKey testConceptKey = new PfConceptKey("TestKey", "0.0.1");
138 testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
139 assertFalse(container.compareTo(testContainer) == 0);
142 container.validate(null);
143 fail("test should throw an exception here");
144 } catch (Exception exc) {
145 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
148 DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
149 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
150 validateContainer.setKey(new PfConceptKey("VCKey", "0.0.1"));
151 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
153 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
154 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
156 validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
157 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
158 validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
159 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
161 validateContainer.getConceptMap().put(testConceptKey, null);
162 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
163 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
164 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
166 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
167 assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
168 validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
169 assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
171 assertEquals(conceptKey, container.get(conceptKey).getKey());
172 assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
173 assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
175 Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
176 assertEquals(conceptKey, returnSet.iterator().next().getKey());
178 returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
179 assertEquals(conceptKey, returnSet.iterator().next().getKey());
181 container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
183 DummyPfConceptContainer exceptionOnCopyContainer = new DummyPfConceptContainer();
185 container.copyTo(exceptionOnCopyContainer);
186 fail("test should throw an exception here");
187 } catch (Exception exc) {
189 "Failed to create a clone of class \"org.onap.policy.models.base.testconcepts.DummyPfConceptSub\"",
194 @Test(expected = NullPointerException.class)
195 public void testnullKey() {
196 PfConceptKey nullKey = null;
197 new DummyPfConceptContainer(nullKey);