Add version on legacy get/delete
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfConceptContainerTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
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;
29
30 import java.util.ArrayList;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.Set;
35 import java.util.TreeMap;
36
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;
43
44 /**
45  * Test the PfCOnceptCOntainer class.
46  *
47  * @author Liam Fallon (liam.fallon@est.tech)
48  */
49 public class PfConceptContainerTest {
50
51     @SuppressWarnings({ "unchecked", "rawtypes" })
52     @Test
53     public void testConceptContainer() {
54         DummyPfConceptContainer container = new DummyPfConceptContainer();
55         assertNotNull(container);
56
57         container = new DummyPfConceptContainer();
58         assertNotNull(container);
59
60         container = new DummyPfConceptContainer(new PfConceptKey());
61         assertNotNull(container);
62
63         container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
64         assertNotNull(container);
65
66         try {
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());
71         }
72
73         try {
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());
78         }
79
80         try {
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());
85         }
86
87         try {
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());
92         }
93
94         container.getKey().setName("Dummy");
95         DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
96         assertNotNull(clonedContainer);
97         assertEquals("Dummy", clonedContainer.getKey().getName());
98
99         try {
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());
105         }
106
107         List<PfKey> keyList = container.getKeys();
108         assertEquals(1, keyList.size());
109
110         PfConceptKey conceptKey = new PfConceptKey("Key", "0.0.1");
111         Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
112         conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
113
114         container.setConceptMap(conceptMap);
115         keyList = container.getKeys();
116         assertEquals(2, keyList.size());
117
118         clonedContainer = new DummyPfConceptContainer(container);
119         assertNotNull(clonedContainer);
120         assertEquals("Dummy", clonedContainer.getKey().getName());
121         assertEquals(2, clonedContainer.getKeys().size());
122
123         assertEquals(clonedContainer, container);
124         container.clean();
125         assertEquals(clonedContainer, container);
126
127         PfValidationResult result = new PfValidationResult();
128         result = container.validate(result);
129         assertTrue(result.isOk());
130
131         assertEquals(0, container.compareTo(clonedContainer));
132
133         try {
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());
138         }
139
140         assertFalse(container.compareTo(null) == 0);
141         assertEquals(0, container.compareTo(container));
142         assertFalse(container.compareTo(conceptKey) == 0);
143
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));
149
150         PfConceptKey testConceptKey = new PfConceptKey("TestKey", "0.0.1");
151         testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
152         assertFalse(container.compareTo(testContainer) == 0);
153
154         try {
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());
159         }
160
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());
165
166         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
167         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
168
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());
173
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());
178
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());
183
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());
187
188         Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
189         assertEquals(conceptKey, returnSet.iterator().next().getKey());
190
191         returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
192         assertEquals(conceptKey, returnSet.iterator().next().getKey());
193
194         container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
195     }
196
197     @Test
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"));
203
204         List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
205         authorativeList.add(dacMap);
206
207         DummyPfConceptContainer container = new DummyPfConceptContainer();
208         container.fromAuthorative(authorativeList);
209
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());
213
214         List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
215
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"));
219
220         DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
221         assertThatThrownBy(() -> {
222             badContainer.fromAuthorative(authorativeList);
223         }).hasMessage("failed to instantiate instance of container concept class");
224
225         authorativeList.clear();
226         assertThatThrownBy(() -> {
227             container.fromAuthorative(authorativeList);
228         }).hasMessage("An incoming list of concepts must have at least one entry");
229     }
230
231     @Test(expected = NullPointerException.class)
232     public void testnullKey() {
233         PfConceptKey nullKey = null;
234         new DummyPfConceptContainer(nullKey);
235     }
236 }