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 public static void prepareForTest() {
53 final ContextParameters contextParameters = new ContextParameters();
54 contextParameters.getLockManagerParameters()
55 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager");
56 ParameterService.registerParameters(ContextParameters.class, contextParameters);
58 final SchemaParameters schemaParameters = new SchemaParameters();
59 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
60 ParameterService.registerParameters(SchemaParameters.class, schemaParameters);
64 public void testNullsOnConstructor() {
66 new ContextAlbumImpl(null, null, null);
67 fail("this test should throw an exception");
68 } catch (IllegalArgumentException e) {
69 assertEquals("Context album definition may not be null", e.getMessage());
70 } catch (ContextException e) {
71 fail("this test should throw an IllegalArgumentException");
75 new ContextAlbumImpl(new AxContextAlbum(), null, null);
76 fail("this test should throw an exception");
77 } catch (IllegalArgumentException e) {
78 assertEquals("Distributor may not be null", e.getMessage());
79 } catch (ContextException e) {
80 fail("this test should throw an IllegalArgumentException");
84 new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), null);
85 fail("this test should throw an exception");
86 } catch (IllegalArgumentException e) {
87 assertEquals("Album map may not be null", e.getMessage());
88 } catch (ContextException e) {
89 fail("this test should throw an IllegalArgumentException");
93 new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), new LinkedHashMap<String, Object>());
94 fail("this test should throw an exception");
95 } catch (ApexRuntimeException e) {
96 assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
97 + "not found in model service", e.getMessage());
98 } catch (ContextException e) {
99 fail("this test should throw an ApexRuntimeException");
104 public void testAlbumInterface() throws ContextException {
105 AxContextSchemas schemas = new AxContextSchemas();
106 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
107 "JAVA", "java.lang.String");
108 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
109 ModelService.registerModel(AxContextSchemas.class, schemas);
111 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
112 true, AxArtifactKey.getNullKey());
114 AxContextAlbum axContextAlbumRO = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
115 false, simpleStringSchema.getKey());
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>());
129 ContextAlbum albumRO = new ContextAlbumImpl(axContextAlbumRO, distributor, new LinkedHashMap<String, Object>());
131 assertEquals("TestContextAlbum", album.getName());
132 assertEquals("TestContextAlbum:0.0.1", album.getKey().getID());
133 assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getID());
134 assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getID());
137 album.containsKey(null);
138 fail("test should throw an exception");
139 } catch (ContextRuntimeException e) {
140 assertEquals("null values are illegal on method parameter \"key\"", e.getMessage());
142 assertEquals(false, album.containsKey("Key0"));
145 album.containsValue(null);
146 fail("test should throw an exception");
147 } catch (ContextRuntimeException e) {
148 assertEquals("null values are illegal on method parameter \"value\"", e.getMessage());
150 assertEquals(false, album.containsValue("some value"));
154 fail("test should throw an exception");
155 } catch (ContextRuntimeException e) {
156 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage());
160 album.put(null, null);
161 fail("test should throw an exception");
162 } catch (ContextRuntimeException e) {
163 assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
167 album.put("KeyNull", null);
168 fail("test should throw an exception");
169 } catch (ContextRuntimeException e) {
170 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()",
175 albumRO.put("KeyReadOnly", "A value for a Read Only Album");
176 fail("test should throw an exception");
177 } catch (ContextRuntimeException e) {
178 assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
179 + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage());
182 Map<String, Object> putAllData = new LinkedHashMap<>();
183 putAllData.put("AllKey0", "vaue of AllKey0");
184 putAllData.put("AllKey1", "vaue of AllKey1");
185 putAllData.put("AllKey2", "vaue of AllKey2");
188 albumRO.putAll(putAllData);
189 fail("test should throw an exception");
190 } catch (ContextRuntimeException e) {
191 assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage());
195 albumRO.remove("AllKey0");
196 fail("test should throw an exception");
197 } catch (ContextRuntimeException e) {
199 "album \"TestContextAlbum:0.0.1\" remove() not allowed on read only albums for key=\"AllKey0\"",
205 fail("test should throw an exception");
206 } catch (ContextRuntimeException e) {
207 assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage());
212 fail("test should throw an exception");
213 } catch (ContextRuntimeException e) {
214 assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage());
217 // The following locking tests pass because the locking protects access to Key0 across all
218 // copies of the distributed album whether the key exists or not
219 album.lockForReading("Key0");
220 assertEquals(null, album.get("Key0"));
221 album.unlockForReading("Key0");
222 assertEquals(null, album.get("Key0"));
224 album.lockForWriting("Key0");
225 assertEquals(null, album.get("Key0"));
226 album.unlockForWriting("Key0");
227 assertEquals(null, album.get("Key0"));
229 // Test write access, trivial test because Integration Test does
230 // a full test of access locking over albums in different JVMs
231 album.lockForWriting("Key0");
232 assertEquals(null, album.get("Key0"));
233 album.put("Key0", "value of Key0");
234 assertEquals("value of Key0", album.get("Key0"));
235 album.unlockForWriting("Key0");
236 assertEquals("value of Key0", album.get("Key0"));
238 // Test read access, trivial test because Integration Test does
239 // a full test of access locking over albums in different JVMs
240 album.lockForReading("Key0");
241 assertEquals("value of Key0", album.get("Key0"));
242 album.unlockForReading("Key0");
244 AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
245 AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
247 AxConcept[] userArtifactStack = { somePolicyKey, somePolicyState };
248 album.setUserArtifactStack(userArtifactStack);
249 assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getID());
250 assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getID());
252 assertEquals(true, album.keySet().contains("Key0"));
253 assertEquals(true, album.values().contains("value of Key0"));
254 assertEquals(1, album.entrySet().size());
256 // The flush() operation fails because the distributor is not initialized with the album which
257 // is fine for unit test
260 fail("test should throw an exception");
261 } catch (ContextException e) {
262 assertEquals("map flush failed, supplied map is null", e.getMessage());
265 assertEquals(1, album.size());
266 assertEquals(false, album.isEmpty());
268 album.put("Key0", "New value of Key0");
269 assertEquals("New value of Key0", album.get("Key0"));
271 album.putAll(putAllData);
273 putAllData.put("AllKey3", null);
275 album.putAll(putAllData);
276 fail("test should throw an exception");
277 } catch (ContextRuntimeException e) {
278 assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()",
282 assertEquals("New value of Key0", album.remove("Key0"));
285 assertTrue(album.isEmpty());
287 ModelService.clear();