Merge "move actors code in drools-applications to policy/models"
[policy/models.git] / models-base / src / test / java / org / onap / policy / models / base / PfConceptContainerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.base;
22
23 import static org.assertj.core.api.Assertions.assertThatThrownBy;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
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     @Test
52     public void testConceptContainer() {
53         DummyPfConceptContainer container = new DummyPfConceptContainer();
54         assertNotNull(container);
55
56         container = new DummyPfConceptContainer();
57         assertNotNull(container);
58
59         container = new DummyPfConceptContainer(new PfConceptKey());
60         assertNotNull(container);
61
62         container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
63         assertNotNull(container);
64
65         try {
66             container = new DummyPfConceptContainer((PfConceptKey) null, null);
67             fail("test should throw an exception here");
68         } catch (Exception exc) {
69             assertEquals("key is marked @NonNull but is null", exc.getMessage());
70         }
71
72         try {
73             container = new DummyPfConceptContainer(new PfConceptKey(), null);
74             fail("test should throw an exception here");
75         } catch (Exception exc) {
76             assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage());
77         }
78
79         try {
80             container = new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>());
81             fail("test should throw an exception here");
82         } catch (Exception exc) {
83             assertEquals("key is marked @NonNull but is null", exc.getMessage());
84         }
85
86         container.getKey().setName("Dummy");
87         DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
88         assertNotNull(clonedContainer);
89         assertEquals("Dummy", clonedContainer.getKey().getName());
90
91         try {
92             DummyPfConceptContainer conceptContainter = null;
93             container = new DummyPfConceptContainer(conceptContainter);
94             fail("test should throw an exception here");
95         } catch (Exception exc) {
96             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
97         }
98
99         List<PfKey> keyList = container.getKeys();
100         assertEquals(1, keyList.size());
101
102         PfConceptKey conceptKey = new PfConceptKey("Key", "0.0.1");
103         Map<PfConceptKey, DummyPfConcept> conceptMap = new TreeMap<>();
104         conceptMap.put(conceptKey, new DummyPfConcept(conceptKey));
105
106         container.setConceptMap(conceptMap);
107         keyList = container.getKeys();
108         assertEquals(2, keyList.size());
109
110         clonedContainer = new DummyPfConceptContainer(container);
111         assertNotNull(clonedContainer);
112         assertEquals("Dummy", clonedContainer.getKey().getName());
113         assertEquals(2, clonedContainer.getKeys().size());
114
115         assertEquals(clonedContainer, container);
116         container.clean();
117         assertEquals(clonedContainer, container);
118
119         PfValidationResult result = new PfValidationResult();
120         result = container.validate(result);
121         assertTrue(result.isOk());
122
123         assertEquals(0, container.compareTo(clonedContainer));
124
125         try {
126             container.copyTo(null);
127             fail("test should throw an exception here");
128         } catch (Exception exc) {
129             assertEquals("target is marked @NonNull but is null", exc.getMessage());
130         }
131
132         assertFalse(container.compareTo(null) == 0);
133         assertEquals(0, container.compareTo(container));
134         assertFalse(container.compareTo(conceptKey) == 0);
135
136         DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
137         testContainer.getKey().setVersion("0.0.2");
138         assertFalse(container.compareTo(testContainer) == 0);
139         testContainer.getKey().setVersion(container.getKey().getVersion());
140         assertEquals(0, container.compareTo(testContainer));
141
142         PfConceptKey testConceptKey = new PfConceptKey("TestKey", "0.0.1");
143         testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
144         assertFalse(container.compareTo(testContainer) == 0);
145
146         try {
147             container.validate(null);
148             fail("test should throw an exception here");
149         } catch (Exception exc) {
150             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
151         }
152
153         DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
154         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
155         validateContainer.setKey(new PfConceptKey("VCKey", "0.0.1"));
156         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
157
158         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
159         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
160
161         validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
162         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
163         validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
164         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
165
166         validateContainer.getConceptMap().put(testConceptKey, null);
167         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
168         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
169         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
170
171         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
172         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
173         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
174         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
175
176         assertEquals(conceptKey, container.get(conceptKey).getKey());
177         assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
178         assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
179
180         Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
181         assertEquals(conceptKey, returnSet.iterator().next().getKey());
182
183         returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
184         assertEquals(conceptKey, returnSet.iterator().next().getKey());
185
186         container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
187     }
188
189     @Test
190     public void testAuthorative() {
191         Map<String, DummyAuthorativeConcept> dacMap = new LinkedHashMap<>();
192         dacMap.put("name0", new DummyAuthorativeConcept("name0", "1.2.3", "Hello"));
193         dacMap.put("name1", new DummyAuthorativeConcept("name1", "1.2.3", "Hi"));
194         dacMap.put("name2", new DummyAuthorativeConcept("name2", "1.2.3", "Howdy"));
195
196         List<Map<String, DummyAuthorativeConcept>> authorativeList = new ArrayList<>();
197         authorativeList.add(dacMap);
198
199         DummyPfConceptContainer container = new DummyPfConceptContainer();
200         container.fromAuthorative(authorativeList);
201
202         assertEquals("Hello", container.getConceptMap().get(new PfConceptKey("name0:1.2.3")).getDescription());
203         assertEquals("Hi",    container.getConceptMap().get(new PfConceptKey("name1:1.2.3")).getDescription());
204         assertEquals("Howdy", container.getConceptMap().get(new PfConceptKey("name2:1.2.3")).getDescription());
205
206         List<Map<String, DummyAuthorativeConcept>> outMapList = container.toAuthorative();
207
208         assertEquals(dacMap, outMapList.get(0));
209
210         DummyBadPfConceptContainer badContainer = new DummyBadPfConceptContainer();
211         assertThatThrownBy(() -> {
212             badContainer.fromAuthorative(authorativeList);
213         }).hasMessage("failed to instantiate instance of container concept class");
214
215         authorativeList.clear();
216         assertThatThrownBy(() -> {
217             container.fromAuthorative(authorativeList);
218         }).hasMessage("An incoming list of concepts must have at least one entry");
219     }
220
221     @Test(expected = NullPointerException.class)
222     public void testnullKey() {
223         PfConceptKey nullKey = null;
224         new DummyPfConceptContainer(nullKey);
225     }
226 }