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