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;
30 import org.junit.AfterClass;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.policy.apex.context.ContextAlbum;
34 import org.onap.policy.apex.context.ContextException;
35 import org.onap.policy.apex.context.ContextRuntimeException;
36 import org.onap.policy.apex.context.Distributor;
37 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
38 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
39 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
40 import org.onap.policy.apex.context.parameters.ContextParameters;
41 import org.onap.policy.apex.context.parameters.SchemaParameters;
42 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
46 import org.onap.policy.apex.model.basicmodel.service.ModelService;
47 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
48 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
49 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
50 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
51 import org.onap.policy.common.parameters.ParameterService;
53 public class ContextAlbumImplTest {
55 * Set ups everything for the test.
58 public static void prepareForTest() {
59 final ContextParameters contextParameters = new ContextParameters();
60 contextParameters.getLockManagerParameters()
61 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager");
63 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
64 contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
65 contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
66 contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
68 ParameterService.register(contextParameters);
69 ParameterService.register(contextParameters.getDistributorParameters());
70 ParameterService.register(contextParameters.getLockManagerParameters());
71 ParameterService.register(contextParameters.getPersistorParameters());
73 final SchemaParameters schemaParameters = new SchemaParameters();
74 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
75 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
77 ParameterService.register(schemaParameters);
81 * Clear down the test data.
84 public static void cleanUpAfterTest() {
85 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
86 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
87 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
88 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
89 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
90 ParameterService.clear();
94 public void testNullsOnConstructor() {
96 new ContextAlbumImpl(null, null, null);
97 fail("this test should throw an exception");
98 } catch (IllegalArgumentException e) {
99 assertEquals("Context album definition may not be null", e.getMessage());
100 } catch (ContextException e) {
101 fail("this test should throw an IllegalArgumentException");
105 new ContextAlbumImpl(new AxContextAlbum(), null, null);
106 fail("this test should throw an exception");
107 } catch (IllegalArgumentException e) {
108 assertEquals("Distributor may not be null", e.getMessage());
109 } catch (ContextException e) {
110 fail("this test should throw an IllegalArgumentException");
114 new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null);
115 fail("this test should throw an exception");
116 } catch (IllegalArgumentException e) {
117 assertEquals("Album map may not be null", e.getMessage());
118 } catch (ContextException e) {
119 fail("this test should throw an IllegalArgumentException");
123 new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), new LinkedHashMap<String, Object>());
124 fail("this test should throw an exception");
125 } catch (ApexRuntimeException e) {
126 assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
127 + "not found in model service", e.getMessage());
128 } catch (ContextException e) {
129 fail("this test should throw an ApexRuntimeException");
134 public void testAlbumInterface() throws ContextException {
135 AxContextSchemas schemas = new AxContextSchemas();
136 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
137 "JAVA", "java.lang.String");
138 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
139 ModelService.registerModel(AxContextSchemas.class, schemas);
141 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
142 true, AxArtifactKey.getNullKey());
145 new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap<String, Object>());
146 fail("this test should throw an exception");
147 } catch (ContextException e) {
148 assertEquals("could not initiate schema management for context album AxContextAlbum",
149 e.getMessage().substring(0, 69));
152 axContextAlbum.setItemSchema(simpleStringSchema.getKey());
153 Distributor distributor = new JvmLocalDistributor();
154 distributor.init(axContextAlbum.getKey());
155 ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
157 AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
158 false, simpleStringSchema.getKey());
159 ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());
161 assertEquals("TestContextAlbum", album.getName());
162 assertEquals("TestContextAlbum:0.0.1", album.getKey().getId());
163 assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId());
164 assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId());
167 album.containsKey(null);
168 fail("test should throw an exception");
169 } catch (ContextRuntimeException e) {
170 assertEquals("null values are illegal on method parameter \"key\"", e.getMessage());
172 assertEquals(false, album.containsKey("Key0"));
175 album.containsValue(null);
176 fail("test should throw an exception");
177 } catch (ContextRuntimeException e) {
178 assertEquals("null values are illegal on method parameter \"value\"", e.getMessage());
180 assertEquals(false, album.containsValue("some value"));
184 fail("test should throw an exception");
185 } catch (ContextRuntimeException e) {
186 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage());
190 album.put(null, null);
191 fail("test should throw an exception");
192 } catch (ContextRuntimeException e) {
193 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
197 album.put("KeyNull", null);
198 fail("test should throw an exception");
199 } catch (ContextRuntimeException e) {
200 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()",
205 albumRo.put("KeyReadOnly", "A value for a Read Only Album");
206 fail("test should throw an exception");
207 } catch (ContextRuntimeException e) {
208 assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
209 + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage());
212 Map<String, Object> putAllData = new LinkedHashMap<>();
213 putAllData.put("AllKey0", "vaue of AllKey0");
214 putAllData.put("AllKey1", "vaue of AllKey1");
215 putAllData.put("AllKey2", "vaue of AllKey2");
218 albumRo.putAll(putAllData);
219 fail("test should throw an exception");
220 } catch (ContextRuntimeException e) {
221 assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage());
225 albumRo.remove("AllKey0");
226 fail("test should throw an exception");
227 } catch (ContextRuntimeException e) {
228 assertEquals("album \"TestContextAlbum:0.0.1\" remove() not allowed "
229 + "on read only albums for key=\"AllKey0\"", e.getMessage());
234 fail("test should throw an exception");
235 } catch (ContextRuntimeException e) {
236 assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage());
241 fail("test should throw an exception");
242 } catch (ContextRuntimeException e) {
243 assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage());
246 // The following locking tests pass because the locking protects access to Key0 across all
247 // copies of the distributed album whether the key exists or not
248 album.lockForReading("Key0");
249 assertEquals(null, album.get("Key0"));
250 album.unlockForReading("Key0");
251 assertEquals(null, album.get("Key0"));
253 album.lockForWriting("Key0");
254 assertEquals(null, album.get("Key0"));
255 album.unlockForWriting("Key0");
256 assertEquals(null, album.get("Key0"));
258 // Test write access, trivial test because Integration Test does
259 // a full test of access locking over albums in different JVMs
260 album.lockForWriting("Key0");
261 assertEquals(null, album.get("Key0"));
262 album.put("Key0", "value of Key0");
263 assertEquals("value of Key0", album.get("Key0"));
264 album.unlockForWriting("Key0");
265 assertEquals("value of Key0", album.get("Key0"));
267 // Test read access, trivial test because Integration Test does
268 // a full test of access locking over albums in different JVMs
269 album.lockForReading("Key0");
270 assertEquals("value of Key0", album.get("Key0"));
271 album.unlockForReading("Key0");
273 AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
274 AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
276 AxConcept[] userArtifactStack =
277 { somePolicyKey, somePolicyState };
278 album.setUserArtifactStack(userArtifactStack);
279 assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId());
280 assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId());
282 assertEquals(true, album.keySet().contains("Key0"));
283 assertEquals(true, album.values().contains("value of Key0"));
284 assertEquals(1, album.entrySet().size());
286 // The flush() operation fails because the distributor is not initialized with the album which
287 // is fine for unit test
290 fail("test should throw an exception");
291 } catch (ContextException e) {
292 assertEquals("map flush failed, supplied map is null", e.getMessage());
295 assertEquals(1, album.size());
296 assertEquals(false, album.isEmpty());
298 album.put("Key0", "New value of Key0");
299 assertEquals("New value of Key0", album.get("Key0"));
301 album.putAll(putAllData);
303 putAllData.put("AllKey3", null);
305 album.putAll(putAllData);
306 fail("test should throw an exception");
307 } catch (ContextRuntimeException e) {
308 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()",
312 assertEquals("New value of Key0", album.remove("Key0"));
315 assertTrue(album.isEmpty());
317 ModelService.clear();
320 @SuppressWarnings("unlikely-arg-type")
322 public void testCompareToEqualsHash() throws ContextException {
323 AxContextSchemas schemas = new AxContextSchemas();
324 AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
325 "java.lang.Integer");
326 schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
327 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
328 "JAVA", "java.lang.String");
329 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
330 ModelService.registerModel(AxContextSchemas.class, schemas);
332 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
333 true, AxArtifactKey.getNullKey());
335 axContextAlbum.setItemSchema(simpleIntSchema.getKey());
336 Distributor distributor = new JvmLocalDistributor();
337 distributor.init(axContextAlbum.getKey());
338 ContextAlbumImpl album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
340 assertTrue(album.hashCode() != 0);
342 assertEquals(0, album.compareTo(album));
343 assertEquals(1, album.compareTo(null));
345 assertTrue(album.equals(album));
346 assertFalse(album.equals(new DummyContextAlbumImpl()));
348 ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor,
349 new LinkedHashMap<String, Object>());
350 assertTrue(album.equals(otherAlbum));
352 otherAlbum.put("Key", 123);
353 assertFalse(album.equals(otherAlbum));
356 otherAlbum.put("Key", "BadValue");
357 fail("test should throw an exception here");
358 } catch (ContextRuntimeException cre) {
359 assertEquals("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": "
360 + "TestContextAlbum:0.0.1: object \"BadValue\" of class \"java.lang.String\" "
361 + "not compatible with class \"java.lang.Integer\"", cre.getMessage());
364 AxContextAlbum otherAxContextAlbum = new AxContextAlbum(new AxArtifactKey("OtherTestContextAlbum", "0.0.1"),
365 "Policy", true, AxArtifactKey.getNullKey());
367 otherAxContextAlbum.setItemSchema(simpleStringSchema.getKey());
368 otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<String, Object>());
369 assertFalse(album.equals(otherAlbum));
373 fail("test should throw an exception here");
374 } catch (ContextException ce) {
375 assertEquals("map flush failed, supplied map is null", ce.getMessage());
378 AxContextAlbums albums = new AxContextAlbums();
379 ModelService.registerModel(AxContextAlbums.class, albums);
380 albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum);
381 distributor.createContextAlbum(album.getKey());
385 ModelService.clear();