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