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 public static void cleanUpAfterTest() {
81 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
82 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
83 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
84 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
85 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
86 ParameterService.clear();
90 public void testNullsOnConstructor() {
92 new ContextAlbumImpl(null, null, null);
93 fail("this test should throw an exception");
94 } catch (IllegalArgumentException e) {
95 assertEquals("Context album definition may not be null", e.getMessage());
96 } catch (ContextException e) {
97 fail("this test should throw an IllegalArgumentException");
101 new ContextAlbumImpl(new AxContextAlbum(), null, null);
102 fail("this test should throw an exception");
103 } catch (IllegalArgumentException e) {
104 assertEquals("Distributor may not be null", e.getMessage());
105 } catch (ContextException e) {
106 fail("this test should throw an IllegalArgumentException");
110 new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), null);
111 fail("this test should throw an exception");
112 } catch (IllegalArgumentException e) {
113 assertEquals("Album map may not be null", e.getMessage());
114 } catch (ContextException e) {
115 fail("this test should throw an IllegalArgumentException");
119 new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), new LinkedHashMap<String, Object>());
120 fail("this test should throw an exception");
121 } catch (ApexRuntimeException e) {
122 assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
123 + "not found in model service", e.getMessage());
124 } catch (ContextException e) {
125 fail("this test should throw an ApexRuntimeException");
130 public void testAlbumInterface() throws ContextException {
131 AxContextSchemas schemas = new AxContextSchemas();
132 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
133 "JAVA", "java.lang.String");
134 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
135 ModelService.registerModel(AxContextSchemas.class, schemas);
137 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
138 true, AxArtifactKey.getNullKey());
141 new ContextAlbumImpl(axContextAlbum, new JVMLocalDistributor(), new LinkedHashMap<String, Object>());
142 fail("this test should throw an exception");
143 } catch (ContextException e) {
144 assertEquals("could not initiate schema management for context album AxContextAlbum",
145 e.getMessage().substring(0, 69));
148 axContextAlbum.setItemSchema(simpleStringSchema.getKey());
149 Distributor distributor = new JVMLocalDistributor();
150 distributor.init(axContextAlbum.getKey());
151 ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
153 AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
154 false, simpleStringSchema.getKey());
155 ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());
157 assertEquals("TestContextAlbum", album.getName());
158 assertEquals("TestContextAlbum:0.0.1", album.getKey().getID());
159 assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getID());
160 assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getID());
163 album.containsKey(null);
164 fail("test should throw an exception");
165 } catch (ContextRuntimeException e) {
166 assertEquals("null values are illegal on method parameter \"key\"", e.getMessage());
168 assertEquals(false, album.containsKey("Key0"));
171 album.containsValue(null);
172 fail("test should throw an exception");
173 } catch (ContextRuntimeException e) {
174 assertEquals("null values are illegal on method parameter \"value\"", e.getMessage());
176 assertEquals(false, album.containsValue("some value"));
180 fail("test should throw an exception");
181 } catch (ContextRuntimeException e) {
182 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage());
186 album.put(null, null);
187 fail("test should throw an exception");
188 } catch (ContextRuntimeException e) {
189 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
193 album.put("KeyNull", null);
194 fail("test should throw an exception");
195 } catch (ContextRuntimeException e) {
196 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()",
201 albumRo.put("KeyReadOnly", "A value for a Read Only Album");
202 fail("test should throw an exception");
203 } catch (ContextRuntimeException e) {
204 assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
205 + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage());
208 Map<String, Object> putAllData = new LinkedHashMap<>();
209 putAllData.put("AllKey0", "vaue of AllKey0");
210 putAllData.put("AllKey1", "vaue of AllKey1");
211 putAllData.put("AllKey2", "vaue of AllKey2");
214 albumRo.putAll(putAllData);
215 fail("test should throw an exception");
216 } catch (ContextRuntimeException e) {
217 assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage());
221 albumRo.remove("AllKey0");
222 fail("test should throw an exception");
223 } catch (ContextRuntimeException e) {
225 "album \"TestContextAlbum:0.0.1\" remove() not allowed on read only albums for key=\"AllKey0\"",
231 fail("test should throw an exception");
232 } catch (ContextRuntimeException e) {
233 assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage());
238 fail("test should throw an exception");
239 } catch (ContextRuntimeException e) {
240 assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage());
243 // The following locking tests pass because the locking protects access to Key0 across all
244 // copies of the distributed album whether the key exists or not
245 album.lockForReading("Key0");
246 assertEquals(null, album.get("Key0"));
247 album.unlockForReading("Key0");
248 assertEquals(null, album.get("Key0"));
250 album.lockForWriting("Key0");
251 assertEquals(null, album.get("Key0"));
252 album.unlockForWriting("Key0");
253 assertEquals(null, album.get("Key0"));
255 // Test write access, trivial test because Integration Test does
256 // a full test of access locking over albums in different JVMs
257 album.lockForWriting("Key0");
258 assertEquals(null, album.get("Key0"));
259 album.put("Key0", "value of Key0");
260 assertEquals("value of Key0", album.get("Key0"));
261 album.unlockForWriting("Key0");
262 assertEquals("value of Key0", album.get("Key0"));
264 // Test read access, trivial test because Integration Test does
265 // a full test of access locking over albums in different JVMs
266 album.lockForReading("Key0");
267 assertEquals("value of Key0", album.get("Key0"));
268 album.unlockForReading("Key0");
270 AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
271 AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
273 AxConcept[] userArtifactStack = { somePolicyKey, somePolicyState };
274 album.setUserArtifactStack(userArtifactStack);
275 assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getID());
276 assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getID());
278 assertEquals(true, album.keySet().contains("Key0"));
279 assertEquals(true, album.values().contains("value of Key0"));
280 assertEquals(1, album.entrySet().size());
282 // The flush() operation fails because the distributor is not initialized with the album which
283 // is fine for unit test
286 fail("test should throw an exception");
287 } catch (ContextException e) {
288 assertEquals("map flush failed, supplied map is null", e.getMessage());
291 assertEquals(1, album.size());
292 assertEquals(false, album.isEmpty());
294 album.put("Key0", "New value of Key0");
295 assertEquals("New value of Key0", album.get("Key0"));
297 album.putAll(putAllData);
299 putAllData.put("AllKey3", null);
301 album.putAll(putAllData);
302 fail("test should throw an exception");
303 } catch (ContextRuntimeException e) {
304 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()",
308 assertEquals("New value of Key0", album.remove("Key0"));
311 assertTrue(album.isEmpty());
313 ModelService.clear();