Changes for Checkstyle 8.32
[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 import org.junit.Test;
37 import org.onap.policy.models.base.testconcepts.DummyAuthorativeConcept;
38 import org.onap.policy.models.base.testconcepts.DummyBadPfConceptContainer;
39 import org.onap.policy.models.base.testconcepts.DummyPfConcept;
40 import org.onap.policy.models.base.testconcepts.DummyPfConceptContainer;
41 import org.onap.policy.models.base.testconcepts.DummyPfConceptSub;
42
43 /**
44  * Test the PfCOnceptCOntainer class.
45  *
46  * @author Liam Fallon (liam.fallon@est.tech)
47  */
48 public class PfConceptContainerTest {
49
50     private static final String NAME0 = "name0";
51     private static final String NAME1 = "name1";
52     private static final String NAME2 = "name2";
53     private static final String NAME3 = "name3";
54     private static final String ID3 = "name3:0.0.1";
55     private static final String VERSION0_0_1 = "0.0.1";
56     private static final String KEY_IS_NULL = "^key is marked .*on.*ull but is null$";
57     private static final String DUMMY_VALUE = "Dummy";
58
59     @SuppressWarnings({"unchecked", "rawtypes"})
60     @Test
61     public void testConceptContainer() {
62         DummyPfConceptContainer container = new DummyPfConceptContainer();
63         assertNotNull(container);
64
65         container = new DummyPfConceptContainer();
66         assertNotNull(container);
67
68         container = new DummyPfConceptContainer(new PfConceptKey());
69         assertNotNull(container);
70
71         container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
72         assertNotNull(container);
73
74         assertThatThrownBy(() -> new PfConceptContainer((PfConceptKey) null, null)).hasMessageMatching(KEY_IS_NULL);
75
76         assertThatThrownBy(() -> new DummyPfConceptContainer((PfConceptKey) null, null))
77             .hasMessageMatching(KEY_IS_NULL);
78
79         assertThatThrownBy(() -> new DummyPfConceptContainer(new PfConceptKey(), null))
80             .hasMessageMatching("^conceptMap is marked .*on.*ull but is null$");
81
82         assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>()))
83             .hasMessageMatching(KEY_IS_NULL);
84
85         container.getKey().setName(DUMMY_VALUE);
86         DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
87         assertNotNull(clonedContainer);
88         assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
89
90         assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
91             .hasMessageMatching("^copyConcept is marked .*on.*ull but is null$");
92
93         List<PfKey> keyList = container.getKeys();
94         assertEquals(1, keyList.size());
95
96         PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0_0_1);
97         Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
98         conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
99
100         container.setConceptMap(conceptMap);
101         keyList = container.getKeys();
102         assertEquals(2, keyList.size());
103
104         clonedContainer = new DummyPfConceptContainer(container);
105         assertNotNull(clonedContainer);
106         assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
107         assertEquals(2, clonedContainer.getKeys().size());
108
109         assertEquals(clonedContainer, container);
110         container.clean();
111         assertEquals(clonedContainer, container);
112
113         PfValidationResult result = new PfValidationResult();
114         result = container.validate(result);
115         assertTrue(result.isOk());
116
117         assertEquals(0, container.compareTo(clonedContainer));
118
119         assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
120             .isInstanceOf(NullPointerException.class);
121
122         assertFalse(container.compareTo(null) == 0);
123         assertEquals(0, container.compareTo(container));
124         assertFalse(container.compareTo(conceptKey) == 0);
125
126         DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
127         testContainer.getKey().setVersion("0.0.2");
128         assertFalse(container.compareTo(testContainer) == 0);
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         assertFalse(container.compareTo(testContainer) == 0);
135
136         final DummyPfConceptContainer container3 = container;
137         assertThatThrownBy(() -> container3.validate(null))
138             .hasMessageMatching("^resultIn is marked .*on.*ull but is null$");
139
140         DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
141         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
142         validateContainer.setKey(new PfConceptKey("VCKey", VERSION0_0_1));
143         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
144
145         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
146         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
147
148         validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
149         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
150         validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
151         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
152
153         validateContainer.getConceptMap().put(testConceptKey, null);
154         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
155         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
156         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
157
158         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
159         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
160         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
161         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
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 }