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.BeforeClass;
31 import org.junit.Test;
32 import org.onap.policy.apex.context.ContextAlbum;
33 import org.onap.policy.apex.context.ContextException;
34 import org.onap.policy.apex.context.ContextRuntimeException;
35 import org.onap.policy.apex.context.Distributor;
36 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JVMLocalDistributor;
37 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
38 import org.onap.policy.apex.context.parameters.ContextParameters;
39 import org.onap.policy.apex.context.parameters.SchemaParameters;
40 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
41 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
42 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
44 import org.onap.policy.apex.model.basicmodel.service.ModelService;
45 import org.onap.policy.apex.model.basicmodel.service.ParameterService;
46 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
47 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
48 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
50 public class ContextAlbumImplTest {
52 * Set ups everything for the test.
55 public static void prepareForTest() {
56 final ContextParameters contextParameters = new ContextParameters();
57 contextParameters.getLockManagerParameters()
58 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager");
59 ParameterService.registerParameters(ContextParameters.class, contextParameters);
61 final SchemaParameters schemaParameters = new SchemaParameters();
62 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
63 ParameterService.registerParameters(SchemaParameters.class, schemaParameters);
67 public void testNullsOnConstructor() {
69 new ContextAlbumImpl(null, null, null);
70 fail("this test should throw an exception");
71 } catch (IllegalArgumentException e) {
72 assertEquals("Context album definition may not be null", e.getMessage());
73 } catch (ContextException e) {
74 fail("this test should throw an IllegalArgumentException");
78 new ContextAlbumImpl(new AxContextAlbum(), null, null);
79 fail("this test should throw an exception");
80 } catch (IllegalArgumentException e) {
81 assertEquals("Distributor may not be null", e.getMessage());
82 } catch (ContextException e) {
83 fail("this test should throw an IllegalArgumentException");
87 new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), null);
88 fail("this test should throw an exception");
89 } catch (IllegalArgumentException e) {
90 assertEquals("Album map may not be null", e.getMessage());
91 } catch (ContextException e) {
92 fail("this test should throw an IllegalArgumentException");
96 new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), new LinkedHashMap<String, Object>());
97 fail("this test should throw an exception");
98 } catch (ApexRuntimeException e) {
99 assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
100 + "not found in model service", e.getMessage());
101 } catch (ContextException e) {
102 fail("this test should throw an ApexRuntimeException");
107 public void testAlbumInterface() throws ContextException {
108 AxContextSchemas schemas = new AxContextSchemas();
109 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
110 "JAVA", "java.lang.String");
111 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
112 ModelService.registerModel(AxContextSchemas.class, schemas);
114 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
115 true, AxArtifactKey.getNullKey());
118 new ContextAlbumImpl(axContextAlbum, new JVMLocalDistributor(), new LinkedHashMap<String, Object>());
119 fail("this test should throw an exception");
120 } catch (ContextException e) {
121 assertEquals("could not initiate schema management for context album AxContextAlbum",
122 e.getMessage().substring(0, 69));
125 axContextAlbum.setItemSchema(simpleStringSchema.getKey());
126 Distributor distributor = new JVMLocalDistributor();
127 distributor.init(axContextAlbum.getKey());
128 ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
130 AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
131 false, simpleStringSchema.getKey());
132 ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());
134 assertEquals("TestContextAlbum", album.getName());
135 assertEquals("TestContextAlbum:0.0.1", album.getKey().getID());
136 assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getID());
137 assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getID());
140 album.containsKey(null);
141 fail("test should throw an exception");
142 } catch (ContextRuntimeException e) {
143 assertEquals("null values are illegal on method parameter \"key\"", e.getMessage());
145 assertEquals(false, album.containsKey("Key0"));
148 album.containsValue(null);
149 fail("test should throw an exception");
150 } catch (ContextRuntimeException e) {
151 assertEquals("null values are illegal on method parameter \"value\"", e.getMessage());
153 assertEquals(false, album.containsValue("some value"));
157 fail("test should throw an exception");
158 } catch (ContextRuntimeException e) {
159 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage());
163 album.put(null, null);
164 fail("test should throw an exception");
165 } catch (ContextRuntimeException e) {
166 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
170 album.put("KeyNull", null);
171 fail("test should throw an exception");
172 } catch (ContextRuntimeException e) {
173 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()",
178 albumRo.put("KeyReadOnly", "A value for a Read Only Album");
179 fail("test should throw an exception");
180 } catch (ContextRuntimeException e) {
181 assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
182 + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage());
185 Map<String, Object> putAllData = new LinkedHashMap<>();
186 putAllData.put("AllKey0", "vaue of AllKey0");
187 putAllData.put("AllKey1", "vaue of AllKey1");
188 putAllData.put("AllKey2", "vaue of AllKey2");
191 albumRo.putAll(putAllData);
192 fail("test should throw an exception");
193 } catch (ContextRuntimeException e) {
194 assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage());
198 albumRo.remove("AllKey0");
199 fail("test should throw an exception");
200 } catch (ContextRuntimeException e) {
202 "album \"TestContextAlbum:0.0.1\" remove() not allowed on read only albums for key=\"AllKey0\"",
208 fail("test should throw an exception");
209 } catch (ContextRuntimeException e) {
210 assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage());
215 fail("test should throw an exception");
216 } catch (ContextRuntimeException e) {
217 assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage());
220 // The following locking tests pass because the locking protects access to Key0 across all
221 // copies of the distributed album whether the key exists or not
222 album.lockForReading("Key0");
223 assertEquals(null, album.get("Key0"));
224 album.unlockForReading("Key0");
225 assertEquals(null, album.get("Key0"));
227 album.lockForWriting("Key0");
228 assertEquals(null, album.get("Key0"));
229 album.unlockForWriting("Key0");
230 assertEquals(null, album.get("Key0"));
232 // Test write access, trivial test because Integration Test does
233 // a full test of access locking over albums in different JVMs
234 album.lockForWriting("Key0");
235 assertEquals(null, album.get("Key0"));
236 album.put("Key0", "value of Key0");
237 assertEquals("value of Key0", album.get("Key0"));
238 album.unlockForWriting("Key0");
239 assertEquals("value of Key0", album.get("Key0"));
241 // Test read access, trivial test because Integration Test does
242 // a full test of access locking over albums in different JVMs
243 album.lockForReading("Key0");
244 assertEquals("value of Key0", album.get("Key0"));
245 album.unlockForReading("Key0");
247 AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
248 AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
250 AxConcept[] userArtifactStack = { somePolicyKey, somePolicyState };
251 album.setUserArtifactStack(userArtifactStack);
252 assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getID());
253 assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getID());
255 assertEquals(true, album.keySet().contains("Key0"));
256 assertEquals(true, album.values().contains("value of Key0"));
257 assertEquals(1, album.entrySet().size());
259 // The flush() operation fails because the distributor is not initialized with the album which
260 // is fine for unit test
263 fail("test should throw an exception");
264 } catch (ContextException e) {
265 assertEquals("map flush failed, supplied map is null", e.getMessage());
268 assertEquals(1, album.size());
269 assertEquals(false, album.isEmpty());
271 album.put("Key0", "New value of Key0");
272 assertEquals("New value of Key0", album.get("Key0"));
274 album.putAll(putAllData);
276 putAllData.put("AllKey3", null);
278 album.putAll(putAllData);
279 fail("test should throw an exception");
280 } catch (ContextRuntimeException e) {
281 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()",
285 assertEquals("New value of Key0", album.remove("Key0"));
288 assertTrue(album.isEmpty());
290 ModelService.clear();