8234741f639cdc0a786f85221344d5b6d3b4eab7
[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         container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
175     }
176
177     @Test
178     public void testAuthorative() {
179         Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
180         dacMap.put(NAME0, new DummyAuthorativeConcept(NAME0, "1.2.3", "Hello"));
181         dacMap.put(NAME1, new DummyAuthorativeConcept("IncorrectName", PfKey.NULL_KEY_VERSION, "Hi"));
182         dacMap.put(NAME2, new DummyAuthorativeConcept(NAME2, "1.2.3", "Howdy"));
183         dacMap.put(ID3, new DummyAuthorativeConcept(NAME3, "9.9.9", "Ciao"));
184         dacMap.put("name4:1.2.3", new DummyAuthorativeConcept(null, null, "Slan"));
185         dacMap.put("name5", new DummyAuthorativeConcept(null, null, "Bye"));
186
187         List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
188         authorativeList.add(dacMap);
189
190         DummyPfConceptContainer container = new DummyPfConceptContainer();
191
192         assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
193             .hasMessage("Key name1:0.0.0 field name1 does not match the value IncorrectName in the concept field");
194
195         dacMap.put(NAME1, new DummyAuthorativeConcept(NAME1, PfKey.NULL_KEY_VERSION, "Hi"));
196
197         assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
198             .hasMessage("Key name3:0.0.1 field 0.0.1 does not match the value 9.9.9 in the concept field");
199
200         dacMap.put(ID3, new DummyAuthorativeConcept(NAME3, "0.0.1", "Ciao"));
201
202         container.fromAuthorative(authorativeList);
203
204         assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
205         assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("name1:0.0.0")).getDescription());
206         assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
207         assertEquals("Ciao", container.getConceptMap().get(new PfConceptKey("name3:0.0.1")).getDescription());
208         assertEquals("name4", container.getConceptMap().get(new PfConceptKey("name4:1.2.3")).getName());
209         assertEquals("1.2.3", container.getConceptMap().get(new PfConceptKey("name4:1.2.3")).getVersion());
210         assertEquals("0.0.0", container.getConceptMap().get(new PfConceptKey("name5:0.0.0")).getVersion());
211
212         List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
213
214         assertEquals(dacMap.get(NAME0), outMapList.get(0).get(NAME0));
215         assertEquals(dacMap.get(NAME1).getDescription(), outMapList.get(1).get(NAME1).getDescription());
216         assertEquals(dacMap.get(NAME2), outMapList.get(2).get(NAME2));
217         assertEquals(dacMap.get(NAME3), outMapList.get(2).get(NAME3));
218
219         DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
220         assertThatThrownBy(() -> badContainer.fromAuthorative(authorativeList))
221             .hasMessage("failed to instantiate instance of container concept class");
222
223         authorativeList.clear();
224         assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
225             .hasMessage("An incoming list of concepts must have at least one entry");
226     }
227
228     @Test(expected = NullPointerException.class)
229     public void testnullKey() {
230         PfConceptKey nullKey = null;
231         new DummyPfConceptContainer(nullKey);
232     }
233 }