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.assertTrue;
25 import static org.junit.Assert.fail;
27 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.AxContextSchema;
49 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
50 import org.onap.policy.common.parameters.ParameterService;
52 public class ContextAlbumImplTest {
54 * Set ups everything for the test.
57 public static void prepareForTest() {
58 final ContextParameters contextParameters = new ContextParameters();
59 contextParameters.getLockManagerParameters()
60 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager");
62 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
63 contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
64 contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
65 contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
67 ParameterService.register(contextParameters);
68 ParameterService.register(contextParameters.getDistributorParameters());
69 ParameterService.register(contextParameters.getLockManagerParameters());
70 ParameterService.register(contextParameters.getPersistorParameters());
72 final SchemaParameters schemaParameters = new SchemaParameters();
73 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
74 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
76 ParameterService.register(schemaParameters);
80 * Clear down the test data.
83 public static void cleanUpAfterTest() {
84 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
85 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
86 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
87 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
88 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
89 ParameterService.clear();
93 public void testNullsOnConstructor() {
95 new ContextAlbumImpl(null, null, null);
96 fail("this test should throw an exception");
97 } catch (IllegalArgumentException e) {
98 assertEquals("Context album definition may not be null", e.getMessage());
99 } catch (ContextException e) {
100 fail("this test should throw an IllegalArgumentException");
104 new ContextAlbumImpl(new AxContextAlbum(), null, null);
105 fail("this test should throw an exception");
106 } catch (IllegalArgumentException e) {
107 assertEquals("Distributor may not be null", e.getMessage());
108 } catch (ContextException e) {
109 fail("this test should throw an IllegalArgumentException");
113 new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null);
114 fail("this test should throw an exception");
115 } catch (IllegalArgumentException e) {
116 assertEquals("Album map may not be null", e.getMessage());
117 } catch (ContextException e) {
118 fail("this test should throw an IllegalArgumentException");
122 new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), new LinkedHashMap<String, Object>());
123 fail("this test should throw an exception");
124 } catch (ApexRuntimeException e) {
125 assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
126 + "not found in model service", e.getMessage());
127 } catch (ContextException e) {
128 fail("this test should throw an ApexRuntimeException");
133 public void testAlbumInterface() throws ContextException {
134 AxContextSchemas schemas = new AxContextSchemas();
135 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
136 "JAVA", "java.lang.String");
137 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
138 ModelService.registerModel(AxContextSchemas.class, schemas);
140 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
141 true, AxArtifactKey.getNullKey());
144 new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap<String, Object>());
145 fail("this test should throw an exception");
146 } catch (ContextException e) {
147 assertEquals("could not initiate schema management for context album AxContextAlbum",
148 e.getMessage().substring(0, 69));
151 axContextAlbum.setItemSchema(simpleStringSchema.getKey());
152 Distributor distributor = new JvmLocalDistributor();
153 distributor.init(axContextAlbum.getKey());
154 ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
156 AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
157 false, simpleStringSchema.getKey());
158 ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());
160 assertEquals("TestContextAlbum", album.getName());
161 assertEquals("TestContextAlbum:0.0.1", album.getKey().getId());
162 assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId());
163 assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId());
166 album.containsKey(null);
167 fail("test should throw an exception");
168 } catch (ContextRuntimeException e) {
169 assertEquals("null values are illegal on method parameter \"key\"", e.getMessage());
171 assertEquals(false, album.containsKey("Key0"));
174 album.containsValue(null);
175 fail("test should throw an exception");
176 } catch (ContextRuntimeException e) {
177 assertEquals("null values are illegal on method parameter \"value\"", e.getMessage());
179 assertEquals(false, album.containsValue("some value"));
183 fail("test should throw an exception");
184 } catch (ContextRuntimeException e) {
185 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage());
189 album.put(null, null);
190 fail("test should throw an exception");
191 } catch (ContextRuntimeException e) {
192 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
196 album.put("KeyNull", null);
197 fail("test should throw an exception");
198 } catch (ContextRuntimeException e) {
199 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()",
204 albumRo.put("KeyReadOnly", "A value for a Read Only Album");
205 fail("test should throw an exception");
206 } catch (ContextRuntimeException e) {
207 assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
208 + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage());
211 Map<String, Object> putAllData = new LinkedHashMap<>();
212 putAllData.put("AllKey0", "vaue of AllKey0");
213 putAllData.put("AllKey1", "vaue of AllKey1");
214 putAllData.put("AllKey2", "vaue of AllKey2");
217 albumRo.putAll(putAllData);
218 fail("test should throw an exception");
219 } catch (ContextRuntimeException e) {
220 assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage());
224 albumRo.remove("AllKey0");
225 fail("test should throw an exception");
226 } catch (ContextRuntimeException e) {
228 "album \"TestContextAlbum:0.0.1\" remove() not allowed on read only albums for key=\"AllKey0\"",
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 = { somePolicyKey, somePolicyState };
277 album.setUserArtifactStack(userArtifactStack);
278 assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId());
279 assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId());
281 assertEquals(true, album.keySet().contains("Key0"));
282 assertEquals(true, album.values().contains("value of Key0"));
283 assertEquals(1, album.entrySet().size());
285 // The flush() operation fails because the distributor is not initialized with the album which
286 // is fine for unit test
289 fail("test should throw an exception");
290 } catch (ContextException e) {
291 assertEquals("map flush failed, supplied map is null", e.getMessage());
294 assertEquals(1, album.size());
295 assertEquals(false, album.isEmpty());
297 album.put("Key0", "New value of Key0");
298 assertEquals("New value of Key0", album.get("Key0"));
300 album.putAll(putAllData);
302 putAllData.put("AllKey3", null);
304 album.putAll(putAllData);
305 fail("test should throw an exception");
306 } catch (ContextRuntimeException e) {
307 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()",
311 assertEquals("New value of Key0", album.remove("Key0"));
314 assertTrue(album.isEmpty());
316 ModelService.clear();