e28fa4cdfeaa114c21f43c099e5f47fd79ff00c2
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfConceptContainerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2020 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.models.base;
23
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;
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     private static final String NAME0 = "name0";
52     private static final String NAME1 = "name1";
53     private static final String NAME2 = "name2";
54     private static final String NAME3 = "name3";
55     private static final String ID3 = "name3:0.0.1";
56     private static final String VERSION0_0_1 = "0.0.1";
57     private static final String KEY_IS_NULL = "^key is marked .*on.*ull but is null$";
58     private static final String DUMMY_VALUE = "Dummy";
59
60     @SuppressWarnings({"unchecked", "rawtypes"})
61     @Test
62     public void testConceptContainer() {
63         DummyPfConceptContainer container = new DummyPfConceptContainer();
64         assertNotNull(container);
65
66         container = new DummyPfConceptContainer();
67         assertNotNull(container);
68
69         container = new DummyPfConceptContainer(new PfConceptKey());
70         assertNotNull(container);
71
72         container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
73         assertNotNull(container);
74
75         assertThatThrownBy(() -> new PfConceptContainer((PfConceptKey) null, null)).hasMessageMatching(KEY_IS_NULL);
76
77         assertThatThrownBy(() -> new DummyPfConceptContainer((PfConceptKey) null, null))
78             .hasMessageMatching(KEY_IS_NULL);
79
80         assertThatThrownBy(() -> new DummyPfConceptContainer(new PfConceptKey(), null))
81             .hasMessageMatching("^conceptMap is marked .*on.*ull but is null$");
82
83         assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>()))
84             .hasMessageMatching(KEY_IS_NULL);
85
86         container.getKey().setName(DUMMY_VALUE);
87         DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
88         assertNotNull(clonedContainer);
89         assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
90
91         assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
92             .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
93
94         List<PfKey> keyList = container.getKeys();
95         assertEquals(1, keyList.size());
96
97         PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0_0_1);
98         Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
99         conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
100
101         container.setConceptMap(conceptMap);
102         keyList = container.getKeys();
103         assertEquals(2, keyList.size());
104
105         clonedContainer = new DummyPfConceptContainer(container);
106         assertNotNull(clonedContainer);
107         assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
108         assertEquals(2, clonedContainer.getKeys().size());
109
110         assertEquals(clonedContainer, container);
111         container.clean();
112         assertEquals(clonedContainer, container);
113
114         PfValidationResult result = new PfValidationResult();
115         result = container.validate(result);
116         assertTrue(result.isOk());
117
118         assertEquals(0, container.compareTo(clonedContainer));
119
120         assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
121             .isInstanceOf(NullPointerException.class);
122
123         assertFalse(container.compareTo(null) == 0);
124         assertEquals(0, container.compareTo(container));
125         assertFalse(container.compareTo(conceptKey) == 0);
126
127         DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
128         testContainer.getKey().setVersion("0.0.2");
129         assertFalse(container.compareTo(testContainer) == 0);
130         testContainer.getKey().setVersion(container.getKey().getVersion());
131         assertEquals(0, container.compareTo(testContainer));
132
133         PfConceptKey testConceptKey = new PfConceptKey("TestKey", VERSION0_0_1);
134         testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
135         assertFalse(container.compareTo(testContainer) == 0);
136
137         final DummyPfConceptContainer container3 = container;
138         assertThatThrownBy(() -> container3.validate(null))
139             .hasMessageMatching("^resultIn is marked .*on.*ull but is null$");
140
141         DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
142         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
143         validateContainer.setKey(new PfConceptKey("VCKey", VERSION0_0_1));
144         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
145
146         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
147         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
148
149         validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
150         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
151         validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
152         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
153
154         validateContainer.getConceptMap().put(testConceptKey, null);
155         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
156         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
157         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
158
159         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
160         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
161         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
162         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
163
164         assertEquals(conceptKey, container.get(conceptKey).getKey());
165         assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
166         assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
167
168         Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
169         assertEquals(conceptKey, returnSet.iterator().next().getKey());
170
171         returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
172         assertEquals(conceptKey, returnSet.iterator().next().getKey());
173
174         returnSet = container.getAllNamesAndVersions(conceptKey.getName(), conceptKey.getVersion());
175         assertEquals(conceptKey, returnSet.iterator().next().getKey());
176         returnSet = container.getAllNamesAndVersions(null, conceptKey.getVersion());
177         assertEquals(conceptKey, returnSet.iterator().next().getKey());
178         returnSet = container.getAllNamesAndVersions(null, null);
179         assertEquals(conceptKey, returnSet.iterator().next().getKey());
180         returnSet = container.getAllNamesAndVersions(conceptKey.getName(), null);
181         assertEquals(conceptKey, returnSet.iterator().next().getKey());
182         returnSet = container.getAllNamesAndVersions(conceptKey.getName(), "0.0.0");
183         assertEquals(conceptKey, returnSet.iterator().next().getKey());
184         returnSet = container.getAllNamesAndVersions("IDontExist", "1.0.0");
185         assertTrue(returnSet.isEmpty());
186
187         container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
188
189         PfConceptKey anotherKey = new PfConceptKey(conceptKey);
190         assertEquals(conceptKey, container.get(anotherKey).getKey());
191         anotherKey.setVersion(PfKey.NULL_KEY_VERSION);
192         assertEquals(conceptKey, container.get(anotherKey).getKey());
193     }
194
195     @Test
196     public void testAuthorative() {
197         Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
198         dacMap.put(NAME0, new DummyAuthorativeConcept(NAME0, "1.2.3", "Hello"));
199         dacMap.put(NAME1, new DummyAuthorativeConcept("IncorrectName", PfKey.NULL_KEY_VERSION, "Hi"));
200         dacMap.put(NAME2, new DummyAuthorativeConcept(NAME2, "1.2.3", "Howdy"));
201         dacMap.put(ID3, new DummyAuthorativeConcept(NAME3, "9.9.9", "Ciao"));
202         dacMap.put("name4:1.2.3", new DummyAuthorativeConcept(null, null, "Slan"));
203         dacMap.put("name5", new DummyAuthorativeConcept(null, null, "Bye"));
204
205         List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
206         authorativeList.add(dacMap);
207
208         DummyPfConceptContainer container = new DummyPfConceptContainer();
209
210         assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
211             .hasMessage("Key name1:0.0.0 field name1 does not match the value IncorrectName in the concept field");
212
213         dacMap.put(NAME1, new DummyAuthorativeConcept(NAME1, PfKey.NULL_KEY_VERSION, "Hi"));
214
215         assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
216             .hasMessage("Key name3:0.0.1 field 0.0.1 does not match the value 9.9.9 in the concept field");
217
218         dacMap.put(ID3, new DummyAuthorativeConcept(NAME3, "0.0.1", "Ciao"));
219
220         container.fromAuthorative(authorativeList);
221
222         assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
223         assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("name1:0.0.0")).getDescription());
224         assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
225         assertEquals("Ciao", container.getConceptMap().get(new PfConceptKey("name3:0.0.1")).getDescription());
226         assertEquals("name4", container.getConceptMap().get(new PfConceptKey("name4:1.2.3")).getName());
227         assertEquals("1.2.3", container.getConceptMap().get(new PfConceptKey("name4:1.2.3")).getVersion());
228         assertEquals("0.0.0", container.getConceptMap().get(new PfConceptKey("name5:0.0.0")).getVersion());
229
230         List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
231
232         assertEquals(dacMap.get(NAME0), outMapList.get(0).get(NAME0));
233         assertEquals(dacMap.get(NAME1).getDescription(), outMapList.get(1).get(NAME1).getDescription());
234         assertEquals(dacMap.get(NAME2), outMapList.get(2).get(NAME2));
235         assertEquals(dacMap.get(NAME3), outMapList.get(2).get(NAME3));
236
237         List<DummyAuthorativeConcept> outConceptList = container.toAuthorativeList();
238         assertEquals("Hello", outConceptList.get(0).getDescription());
239         assertEquals("Hi", outConceptList.get(1).getDescription());
240         assertEquals("Howdy", outConceptList.get(2).getDescription());
241         assertEquals("Ciao", outConceptList.get(3).getDescription());
242         assertEquals("name4", outConceptList.get(4).getName());
243         assertEquals("1.2.3", outConceptList.get(4).getVersion());
244         assertEquals("0.0.0", outConceptList.get(5).getVersion());
245
246         DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
247         assertThatThrownBy(() -> badContainer.fromAuthorative(authorativeList))
248             .hasMessage("failed to instantiate instance of container concept class");
249
250         authorativeList.clear();
251         assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
252             .hasMessage("An incoming list of concepts must have at least one entry");
253     }
254
255     @Test(expected = NullPointerException.class)
256     public void testnullKey() {
257         PfConceptKey nullKey = null;
258         new DummyPfConceptContainer(nullKey);
259     }
260 }