b760e17c083eee6b46d277336a34126d27acd687
[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 NAME2 = "name2";
52     private static final String NAME1 = "name1";
53     private static final String NAME0 = "name0";
54     private static final String KEY_IS_NULL = "key is marked @NonNull but is null";
55     private static final String DUMMY_VALUE = "Dummy";
56     private static final String VERSION0 = "0.0.1";
57
58     @SuppressWarnings({"unchecked", "rawtypes"})
59     @Test
60     public void testConceptContainer() {
61         DummyPfConceptContainer container = new DummyPfConceptContainer();
62         assertNotNull(container);
63
64         container = new DummyPfConceptContainer();
65         assertNotNull(container);
66
67         container = new DummyPfConceptContainer(new PfConceptKey());
68         assertNotNull(container);
69
70         container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
71         assertNotNull(container);
72
73         assertThatThrownBy(() -> new PfConceptContainer((PfConceptKey) null, null)).hasMessage(KEY_IS_NULL);
74
75         assertThatThrownBy(() -> new DummyPfConceptContainer((PfConceptKey) null, null)).hasMessage(KEY_IS_NULL);
76
77         assertThatThrownBy(() -> new DummyPfConceptContainer(new PfConceptKey(), null))
78                 .hasMessage("conceptMap is marked @NonNull but is null");
79
80         assertThatThrownBy(() -> new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>()))
81                 .hasMessage(KEY_IS_NULL);
82
83         container.getKey().setName(DUMMY_VALUE);
84         DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
85         assertNotNull(clonedContainer);
86         assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
87
88         assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
89                 .hasMessage("copyConcept is marked @NonNull but is null");
90
91         List<PfKey> keyList = container.getKeys();
92         assertEquals(1, keyList.size());
93
94         PfConceptKey conceptKey = new PfConceptKey("Key", VERSION0);
95         Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
96         conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
97
98         container.setConceptMap(conceptMap);
99         keyList = container.getKeys();
100         assertEquals(2, keyList.size());
101
102         clonedContainer = new DummyPfConceptContainer(container);
103         assertNotNull(clonedContainer);
104         assertEquals(DUMMY_VALUE, clonedContainer.getKey().getName());
105         assertEquals(2, clonedContainer.getKeys().size());
106
107         assertEquals(clonedContainer, container);
108         container.clean();
109         assertEquals(clonedContainer, container);
110
111         PfValidationResult result = new PfValidationResult();
112         result = container.validate(result);
113         assertTrue(result.isOk());
114
115         assertEquals(0, container.compareTo(clonedContainer));
116
117         assertThatThrownBy(() -> new DummyPfConceptContainer((DummyPfConceptContainer) null))
118                 .isInstanceOf(NullPointerException.class);
119
120         assertFalse(container.compareTo(null) == 0);
121         assertEquals(0, container.compareTo(container));
122         assertFalse(container.compareTo(conceptKey) == 0);
123
124         DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
125         testContainer.getKey().setVersion("0.0.2");
126         assertFalse(container.compareTo(testContainer) == 0);
127         testContainer.getKey().setVersion(container.getKey().getVersion());
128         assertEquals(0, container.compareTo(testContainer));
129
130         PfConceptKey testConceptKey = new PfConceptKey("TestKey", VERSION0);
131         testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
132         assertFalse(container.compareTo(testContainer) == 0);
133
134         final DummyPfConceptContainer container3 = container;
135         assertThatThrownBy(() -> container3.validate(null)).hasMessage("resultIn is marked @NonNull but is null");
136
137         DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
138         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
139         validateContainer.setKey(new PfConceptKey("VCKey", VERSION0));
140         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
141
142         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
143         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
144
145         validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
146         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
147         validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
148         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
149
150         validateContainer.getConceptMap().put(testConceptKey, null);
151         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
152         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
153         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
154
155         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
156         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
157         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
158         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
159
160         assertEquals(conceptKey, container.get(conceptKey).getKey());
161         assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
162         assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
163
164         Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
165         assertEquals(conceptKey, returnSet.iterator().next().getKey());
166
167         returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
168         assertEquals(conceptKey, returnSet.iterator().next().getKey());
169
170         container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
171     }
172
173     @Test
174     public void testAuthorative() {
175         Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
176         dacMap.put(NAME0, new DummyAuthorativeConcept(NAME0, "1.2.3", "Hello"));
177         dacMap.put(NAME1, new DummyAuthorativeConcept(PfKey.NULL_KEY_NAME, PfKey.NULL_KEY_VERSION, "Hi"));
178         dacMap.put(NAME2, new DummyAuthorativeConcept(NAME2, "1.2.3", "Howdy"));
179
180         List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
181         authorativeList.add(dacMap);
182
183         DummyPfConceptContainer container = new DummyPfConceptContainer();
184         container.fromAuthorative(authorativeList);
185
186         assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
187         assertEquals("Hi", container.getConceptMap().get(new PfConceptKey("NULL:0.0.0")).getDescription());
188         assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
189
190         List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
191
192         assertEquals(dacMap.get(NAME1), outMapList.get(0).get("NULL"));
193         assertEquals(dacMap.get(NAME0).getDescription(), outMapList.get(1).get(NAME0).getDescription());
194         assertEquals(dacMap.get(NAME2), outMapList.get(2).get(NAME2));
195
196         DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
197         assertThatThrownBy(() -> badContainer.fromAuthorative(authorativeList))
198                 .hasMessage("failed to instantiate instance of container concept class");
199
200         authorativeList.clear();
201         assertThatThrownBy(() -> container.fromAuthorative(authorativeList))
202                 .hasMessage("An incoming list of concepts must have at least one entry");
203     }
204
205     @Test(expected = NullPointerException.class)
206     public void testnullKey() {
207         PfConceptKey nullKey = null;
208         new DummyPfConceptContainer(nullKey);
209     }
210 }