2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.impl;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.util.LinkedHashMap;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.ContextAlbum;
35 import org.onap.policy.apex.context.ContextException;
36 import org.onap.policy.apex.context.ContextRuntimeException;
37 import org.onap.policy.apex.context.Distributor;
38 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
39 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
40 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
41 import org.onap.policy.apex.context.parameters.ContextParameters;
42 import org.onap.policy.apex.context.parameters.SchemaParameters;
43 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
47 import org.onap.policy.apex.model.basicmodel.service.ModelService;
48 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
49 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
50 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
51 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
52 import org.onap.policy.common.parameters.ParameterService;
54 public class ContextAlbumImplTest {
56 * Set ups everything for the test.
59 public static void prepareForTest() {
60 final ContextParameters contextParameters = new ContextParameters();
61 contextParameters.getLockManagerParameters()
62 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager");
64 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
65 contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
66 contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
67 contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
69 ParameterService.register(contextParameters);
70 ParameterService.register(contextParameters.getDistributorParameters());
71 ParameterService.register(contextParameters.getLockManagerParameters());
72 ParameterService.register(contextParameters.getPersistorParameters());
74 final SchemaParameters schemaParameters = new SchemaParameters();
75 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
76 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
78 ParameterService.register(schemaParameters);
82 * Clear down the test data.
85 public static void cleanUpAfterTest() {
86 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
87 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
88 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
89 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
90 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
91 ParameterService.clear();
95 public void testNullsOnConstructor() {
97 new ContextAlbumImpl(null, null, null);
98 fail("this test should throw an exception");
99 } catch (IllegalArgumentException e) {
100 assertEquals("Context album definition may not be null", e.getMessage());
101 } catch (ContextException e) {
102 fail("this test should throw an IllegalArgumentException");
106 new ContextAlbumImpl(new AxContextAlbum(), null, null);
107 fail("this test should throw an exception");
108 } catch (IllegalArgumentException e) {
109 assertEquals("Distributor may not be null", e.getMessage());
110 } catch (ContextException e) {
111 fail("this test should throw an IllegalArgumentException");
115 new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null);
116 fail("this test should throw an exception");
117 } catch (IllegalArgumentException e) {
118 assertEquals("Album map may not be null", e.getMessage());
119 } catch (ContextException e) {
120 fail("this test should throw an IllegalArgumentException");
124 new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), new LinkedHashMap<String, Object>());
125 fail("this test should throw an exception");
126 } catch (ApexRuntimeException e) {
127 assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
128 + "not found in model service", e.getMessage());
129 } catch (ContextException e) {
130 fail("this test should throw an ApexRuntimeException");
135 public void testAlbumInterface() throws ContextException {
136 AxContextSchemas schemas = new AxContextSchemas();
137 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
138 "JAVA", "java.lang.String");
139 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
140 ModelService.registerModel(AxContextSchemas.class, schemas);
142 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
143 true, AxArtifactKey.getNullKey());
146 new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap<String, Object>());
147 fail("this test should throw an exception");
148 } catch (ContextException e) {
149 assertEquals("could not initiate schema management for context album AxContextAlbum",
150 e.getMessage().substring(0, 69));
153 axContextAlbum.setItemSchema(simpleStringSchema.getKey());
154 Distributor distributor = new JvmLocalDistributor();
155 distributor.init(axContextAlbum.getKey());
156 ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
158 AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
159 false, simpleStringSchema.getKey());
160 ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());
162 assertEquals("TestContextAlbum", album.getName());
163 assertEquals("TestContextAlbum:0.0.1", album.getKey().getId());
164 assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId());
165 assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId());
168 album.containsKey(null);
169 fail("test should throw an exception");
170 } catch (ContextRuntimeException e) {
171 assertEquals("null values are illegal on method parameter \"key\"", e.getMessage());
173 assertEquals(false, album.containsKey("Key0"));
176 album.containsValue(null);
177 fail("test should throw an exception");
178 } catch (ContextRuntimeException e) {
179 assertEquals("null values are illegal on method parameter \"value\"", e.getMessage());
181 assertEquals(false, album.containsValue("some value"));
185 fail("test should throw an exception");
186 } catch (ContextRuntimeException e) {
187 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage());
191 album.put(null, null);
192 fail("test should throw an exception");
193 } catch (ContextRuntimeException e) {
194 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
198 album.put("KeyNull", null);
199 fail("test should throw an exception");
200 } catch (ContextRuntimeException e) {
201 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()",
206 albumRo.put("KeyReadOnly", "A value for a Read Only Album");
207 fail("test should throw an exception");
208 } catch (ContextRuntimeException e) {
209 assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
210 + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage());
213 Map<String, Object> putAllData = new LinkedHashMap<>();
214 putAllData.put("AllKey0", "vaue of AllKey0");
215 putAllData.put("AllKey1", "vaue of AllKey1");
216 putAllData.put("AllKey2", "vaue of AllKey2");
219 albumRo.putAll(putAllData);
220 fail("test should throw an exception");
221 } catch (ContextRuntimeException e) {
222 assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage());
226 albumRo.remove("AllKey0");
227 fail("test should throw an exception");
228 } catch (ContextRuntimeException e) {
229 assertEquals("album \"TestContextAlbum:0.0.1\" remove() not allowed "
230 + "on read only albums for key=\"AllKey0\"", e.getMessage());
235 fail("test should throw an exception");
236 } catch (ContextRuntimeException e) {
237 assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage());
242 fail("test should throw an exception");
243 } catch (ContextRuntimeException e) {
244 assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage());
247 // The following locking tests pass because the locking protects access to Key0 across all
248 // copies of the distributed album whether the key exists or not
249 album.lockForReading("Key0");
250 assertEquals(null, album.get("Key0"));
251 album.unlockForReading("Key0");
252 assertEquals(null, album.get("Key0"));
254 album.lockForWriting("Key0");
255 assertEquals(null, album.get("Key0"));
256 album.unlockForWriting("Key0");
257 assertEquals(null, album.get("Key0"));
259 // Test write access, trivial test because Integration Test does
260 // a full test of access locking over albums in different JVMs
261 album.lockForWriting("Key0");
262 assertEquals(null, album.get("Key0"));
263 album.put("Key0", "value of Key0");
264 assertEquals("value of Key0", album.get("Key0"));
265 album.unlockForWriting("Key0");
266 assertEquals("value of Key0", album.get("Key0"));
268 // Test read access, trivial test because Integration Test does
269 // a full test of access locking over albums in different JVMs
270 album.lockForReading("Key0");
271 assertEquals("value of Key0", album.get("Key0"));
272 album.unlockForReading("Key0");
274 AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
275 AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
277 AxConcept[] userArtifactStack =
278 { somePolicyKey, somePolicyState };
279 album.setUserArtifactStack(userArtifactStack);
280 assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId());
281 assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId());
283 assertEquals(true, album.keySet().contains("Key0"));
284 assertEquals(true, album.values().contains("value of Key0"));
285 assertEquals(1, album.entrySet().size());
287 // The flush() operation fails because the distributor is not initialized with the album which
288 // is fine for unit test
291 fail("test should throw an exception");
292 } catch (ContextException e) {
293 assertEquals("map flush failed, supplied map is null", e.getMessage());
296 assertEquals(1, album.size());
297 assertEquals(false, album.isEmpty());
299 album.put("Key0", "New value of Key0");
300 assertEquals("New value of Key0", album.get("Key0"));
302 album.putAll(putAllData);
304 putAllData.put("AllKey3", null);
306 album.putAll(putAllData);
307 fail("test should throw an exception");
308 } catch (ContextRuntimeException e) {
309 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()",
313 assertEquals("New value of Key0", album.remove("Key0"));
316 assertTrue(album.isEmpty());
318 ModelService.clear();
321 @SuppressWarnings("unlikely-arg-type")
323 public void testCompareToEqualsHash() throws ContextException {
324 AxContextSchemas schemas = new AxContextSchemas();
325 AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
326 "java.lang.Integer");
327 schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
328 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
329 "JAVA", "java.lang.String");
330 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
331 ModelService.registerModel(AxContextSchemas.class, schemas);
333 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
334 true, AxArtifactKey.getNullKey());
336 axContextAlbum.setItemSchema(simpleIntSchema.getKey());
337 Distributor distributor = new JvmLocalDistributor();
338 distributor.init(axContextAlbum.getKey());
339 ContextAlbumImpl album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
341 assertTrue(album.hashCode() != 0);
343 assertEquals(0, album.compareTo(album));
344 assertEquals(1, album.compareTo(null));
346 assertTrue(album.equals(album));
347 assertFalse(album.equals(new DummyContextAlbumImpl()));
349 ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor,
350 new LinkedHashMap<String, Object>());
351 assertTrue(album.equals(otherAlbum));
353 otherAlbum.put("Key", 123);
354 assertFalse(album.equals(otherAlbum));
357 otherAlbum.put("Key", "BadValue");
358 fail("test should throw an exception here");
359 } catch (ContextRuntimeException cre) {
360 assertEquals("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": "
361 + "TestContextAlbum:0.0.1: object \"BadValue\" of class \"java.lang.String\" "
362 + "not compatible with class \"java.lang.Integer\"", cre.getMessage());
365 AxContextAlbum otherAxContextAlbum = new AxContextAlbum(new AxArtifactKey("OtherTestContextAlbum", "0.0.1"),
366 "Policy", true, AxArtifactKey.getNullKey());
368 otherAxContextAlbum.setItemSchema(simpleStringSchema.getKey());
369 otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<String, Object>());
370 assertFalse(album.equals(otherAlbum));
374 fail("test should throw an exception here");
375 } catch (ContextException ce) {
376 assertEquals("map flush failed, supplied map is null", ce.getMessage());
379 AxContextAlbums albums = new AxContextAlbums();
380 ModelService.registerModel(AxContextAlbums.class, albums);
381 albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum);
382 distributor.createContextAlbum(album.getKey());
386 ModelService.clear();