Add DAO Enabled Tosca Model
[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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.TreeMap;
33
34 import org.junit.Test;
35 import org.onap.policy.models.base.testconcepts.DummyPfConcept;
36 import org.onap.policy.models.base.testconcepts.DummyPfConceptContainer;
37 import org.onap.policy.models.base.testconcepts.DummyPfConceptSub;
38
39 /**
40  * Test the PfCOnceptCOntainer class.
41  *
42  * @author Liam Fallon (liam.fallon@est.tech)
43  */
44 public class PfConceptContainerTest {
45
46     @Test
47     public void test() {
48         DummyPfConceptContainer container = new DummyPfConceptContainer();
49         assertNotNull(container);
50
51         container = new DummyPfConceptContainer();
52         assertNotNull(container);
53
54         container = new DummyPfConceptContainer(new PfConceptKey());
55         assertNotNull(container);
56
57         container = new DummyPfConceptContainer(new PfConceptKey(), new TreeMap<PfConceptKey, DummyPfConcept>());
58         assertNotNull(container);
59
60         try {
61             container = new DummyPfConceptContainer((PfConceptKey) null, null);
62             fail("test should throw an exception here");
63         } catch (Exception exc) {
64             assertEquals("key is marked @NonNull but is null", exc.getMessage());
65         }
66
67         try {
68             container = new DummyPfConceptContainer(new PfConceptKey(), null);
69             fail("test should throw an exception here");
70         } catch (Exception exc) {
71             assertEquals("conceptMap is marked @NonNull but is null", exc.getMessage());
72         }
73
74         try {
75             container = new DummyPfConceptContainer(null, new TreeMap<PfConceptKey, DummyPfConcept>());
76             fail("test should throw an exception here");
77         } catch (Exception exc) {
78             assertEquals("key is marked @NonNull but is null", exc.getMessage());
79         }
80
81         container.getKey().setName("Dummy");
82         DummyPfConceptContainer clonedContainer = new DummyPfConceptContainer(container);
83         assertNotNull(clonedContainer);
84         assertEquals("Dummy", clonedContainer.getKey().getName());
85
86         try {
87             DummyPfConceptContainer conceptContainter = null;
88             container = new DummyPfConceptContainer(conceptContainter);
89             fail("test should throw an exception here");
90         } catch (Exception exc) {
91             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
92         }
93
94         List<PfKey> keyList = container.getKeys();
95         assertEquals(1, keyList.size());
96
97         PfConceptKey conceptKey = new PfConceptKey("Key", "0.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", 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         try {
121             container.copyTo(null);
122             fail("test should throw an exception here");
123         } catch (Exception exc) {
124             assertEquals("target is marked @NonNull but is null", exc.getMessage());
125         }
126
127         assertFalse(container.compareTo(null) == 0);
128         assertEquals(0, container.compareTo(container));
129         assertFalse(container.compareTo(conceptKey) == 0);
130
131         DummyPfConceptContainer testContainer = new DummyPfConceptContainer(container);
132         testContainer.getKey().setVersion("0.0.2");
133         assertFalse(container.compareTo(testContainer) == 0);
134         testContainer.getKey().setVersion(container.getKey().getVersion());
135         assertEquals(0, container.compareTo(testContainer));
136
137         PfConceptKey testConceptKey = new PfConceptKey("TestKey", "0.0.1");
138         testContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
139         assertFalse(container.compareTo(testContainer) == 0);
140
141         try {
142             container.validate(null);
143             fail("test should throw an exception here");
144         } catch (Exception exc) {
145             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
146         }
147
148         DummyPfConceptContainer validateContainer = new DummyPfConceptContainer();
149         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
150         validateContainer.setKey(new PfConceptKey("VCKey", "0.0.1"));
151         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
152
153         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
154         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
155
156         validateContainer.getConceptMap().put(PfConceptKey.getNullKey(), new DummyPfConcept(PfConceptKey.getNullKey()));
157         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
158         validateContainer.getConceptMap().remove(PfConceptKey.getNullKey());
159         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
160
161         validateContainer.getConceptMap().put(testConceptKey, null);
162         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
163         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
164         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
165
166         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(conceptKey));
167         assertFalse(validateContainer.validate(new PfValidationResult()).isOk());
168         validateContainer.getConceptMap().put(testConceptKey, new DummyPfConcept(testConceptKey));
169         assertTrue(validateContainer.validate(new PfValidationResult()).isOk());
170
171         assertEquals(conceptKey, container.get(conceptKey).getKey());
172         assertEquals(conceptKey, container.get(conceptKey.getName()).getKey());
173         assertEquals(conceptKey, container.get(conceptKey.getName(), conceptKey.getVersion()).getKey());
174
175         Set<DummyPfConcept> returnSet = container.getAll(conceptKey.getName());
176         assertEquals(conceptKey, returnSet.iterator().next().getKey());
177
178         returnSet = container.getAll(conceptKey.getName(), conceptKey.getVersion());
179         assertEquals(conceptKey, returnSet.iterator().next().getKey());
180
181         container.getConceptMap().put(conceptKey, new DummyPfConceptSub(conceptKey));
182
183         DummyPfConceptContainer exceptionOnCopyContainer = new DummyPfConceptContainer();
184         try {
185             container.copyTo(exceptionOnCopyContainer);
186             fail("test should throw an exception here");
187         } catch (Exception exc) {
188             assertEquals(
189                     "Failed to create a clone of class \"org.onap.policy.models.base.testconcepts.DummyPfConceptSub\"",
190                     exc.getMessage());
191         }
192     }
193
194     @Test(expected = NullPointerException.class)
195     public void testnullKey() {
196         PfConceptKey nullKey = null;
197         new DummyPfConceptContainer(nullKey);
198     }
199 }