cb99fc965b31e30a20c022370031eee044027953
[policy/apex-pdp.git] / context / context-management / src / test / java / org / onap / policy / apex / context / impl / ContextAlbumImplTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.impl;
22
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;
27
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.ContextAlbum;
35 import org.onap.policy.apex.context.ContextException;
36 import org.onap.policy.apex.context.ContextRuntimeException;
37 import org.onap.policy.apex.context.Distributor;
38 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
39 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
40 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
41 import org.onap.policy.apex.context.parameters.ContextParameters;
42 import org.onap.policy.apex.context.parameters.SchemaParameters;
43 import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
47 import org.onap.policy.apex.model.basicmodel.service.ModelService;
48 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
49 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
50 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
51 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
52 import org.onap.policy.common.parameters.ParameterService;
53
54 public class ContextAlbumImplTest {
55     /**
56      * Set ups everything for the test.
57      */
58     @BeforeClass
59     public static void prepareForTest() {
60         final ContextParameters contextParameters = new ContextParameters();
61         contextParameters.getLockManagerParameters()
62                         .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager");
63
64         contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
65         contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
66         contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
67         contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
68
69         ParameterService.register(contextParameters);
70         ParameterService.register(contextParameters.getDistributorParameters());
71         ParameterService.register(contextParameters.getLockManagerParameters());
72         ParameterService.register(contextParameters.getPersistorParameters());
73
74         final SchemaParameters schemaParameters = new SchemaParameters();
75         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
76         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
77
78         ParameterService.register(schemaParameters);
79     }
80
81     /**
82      * Clear down the test data.
83      */
84     @AfterClass
85     public static void cleanUpAfterTest() {
86         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
87         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
88         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
89         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
90         ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
91         ParameterService.clear();
92     }
93
94     @Test
95     public void testNullsOnConstructor() {
96         try {
97             new ContextAlbumImpl(null, null, null);
98             fail("this test should throw an exception");
99         } catch (IllegalArgumentException e) {
100             assertEquals("Context album definition may not be null", e.getMessage());
101         } catch (ContextException e) {
102             fail("this test should throw an IllegalArgumentException");
103         }
104
105         try {
106             new ContextAlbumImpl(new AxContextAlbum(), null, null);
107             fail("this test should throw an exception");
108         } catch (IllegalArgumentException e) {
109             assertEquals("Distributor may not be null", e.getMessage());
110         } catch (ContextException e) {
111             fail("this test should throw an IllegalArgumentException");
112         }
113
114         try {
115             new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null);
116             fail("this test should throw an exception");
117         } catch (IllegalArgumentException e) {
118             assertEquals("Album map may not be null", e.getMessage());
119         } catch (ContextException e) {
120             fail("this test should throw an IllegalArgumentException");
121         }
122
123         try {
124             new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), new LinkedHashMap<String, Object>());
125             fail("this test should throw an exception");
126         } catch (ApexRuntimeException e) {
127             assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
128                             + "not found in model service", e.getMessage());
129         } catch (ContextException e) {
130             fail("this test should throw an ApexRuntimeException");
131         }
132     }
133
134     @Test
135     public void testAlbumInterface() throws ContextException {
136         AxContextSchemas schemas = new AxContextSchemas();
137         AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
138                         "JAVA", "java.lang.String");
139         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
140         ModelService.registerModel(AxContextSchemas.class, schemas);
141
142         AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
143                         true, AxArtifactKey.getNullKey());
144
145         try {
146             new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap<String, Object>());
147             fail("this test should throw an exception");
148         } catch (ContextException e) {
149             assertEquals("could not initiate schema management for context album AxContextAlbum",
150                             e.getMessage().substring(0, 69));
151         }
152
153         axContextAlbum.setItemSchema(simpleStringSchema.getKey());
154         Distributor distributor = new JvmLocalDistributor();
155         distributor.init(axContextAlbum.getKey());
156         ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
157
158         AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
159                         false, simpleStringSchema.getKey());
160         ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());
161
162         assertEquals("TestContextAlbum", album.getName());
163         assertEquals("TestContextAlbum:0.0.1", album.getKey().getId());
164         assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId());
165         assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId());
166
167         try {
168             album.containsKey(null);
169             fail("test should throw an exception");
170         } catch (ContextRuntimeException e) {
171             assertEquals("null values are illegal on method parameter \"key\"", e.getMessage());
172         }
173         assertEquals(false, album.containsKey("Key0"));
174
175         try {
176             album.containsValue(null);
177             fail("test should throw an exception");
178         } catch (ContextRuntimeException e) {
179             assertEquals("null values are illegal on method parameter \"value\"", e.getMessage());
180         }
181         assertEquals(false, album.containsValue("some value"));
182
183         try {
184             album.get(null);
185             fail("test should throw an exception");
186         } catch (ContextRuntimeException e) {
187             assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage());
188         }
189
190         try {
191             album.put(null, null);
192             fail("test should throw an exception");
193         } catch (ContextRuntimeException e) {
194             assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
195         }
196
197         try {
198             album.put("KeyNull", null);
199             fail("test should throw an exception");
200         } catch (ContextRuntimeException e) {
201             assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()",
202                             e.getMessage());
203         }
204
205         try {
206             albumRo.put("KeyReadOnly", "A value for a Read Only Album");
207             fail("test should throw an exception");
208         } catch (ContextRuntimeException e) {
209             assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
210                             + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage());
211         }
212
213         Map<String, Object> putAllData = new LinkedHashMap<>();
214         putAllData.put("AllKey0", "vaue of AllKey0");
215         putAllData.put("AllKey1", "vaue of AllKey1");
216         putAllData.put("AllKey2", "vaue of AllKey2");
217
218         try {
219             albumRo.putAll(putAllData);
220             fail("test should throw an exception");
221         } catch (ContextRuntimeException e) {
222             assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage());
223         }
224
225         try {
226             albumRo.remove("AllKey0");
227             fail("test should throw an exception");
228         } catch (ContextRuntimeException e) {
229             assertEquals("album \"TestContextAlbum:0.0.1\" remove() not allowed "
230                             + "on read only albums for key=\"AllKey0\"", e.getMessage());
231         }
232
233         try {
234             album.remove(null);
235             fail("test should throw an exception");
236         } catch (ContextRuntimeException e) {
237             assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage());
238         }
239
240         try {
241             albumRo.clear();
242             fail("test should throw an exception");
243         } catch (ContextRuntimeException e) {
244             assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage());
245         }
246
247         // The following locking tests pass because the locking protects access to Key0 across all
248         // copies of the distributed album whether the key exists or not
249         album.lockForReading("Key0");
250         assertEquals(null, album.get("Key0"));
251         album.unlockForReading("Key0");
252         assertEquals(null, album.get("Key0"));
253
254         album.lockForWriting("Key0");
255         assertEquals(null, album.get("Key0"));
256         album.unlockForWriting("Key0");
257         assertEquals(null, album.get("Key0"));
258
259         // Test write access, trivial test because Integration Test does
260         // a full test of access locking over albums in different JVMs
261         album.lockForWriting("Key0");
262         assertEquals(null, album.get("Key0"));
263         album.put("Key0", "value of Key0");
264         assertEquals("value of Key0", album.get("Key0"));
265         album.unlockForWriting("Key0");
266         assertEquals("value of Key0", album.get("Key0"));
267
268         // Test read access, trivial test because Integration Test does
269         // a full test of access locking over albums in different JVMs
270         album.lockForReading("Key0");
271         assertEquals("value of Key0", album.get("Key0"));
272         album.unlockForReading("Key0");
273
274         AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
275         AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
276
277         AxConcept[] userArtifactStack =
278             { somePolicyKey, somePolicyState };
279         album.setUserArtifactStack(userArtifactStack);
280         assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId());
281         assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId());
282
283         assertEquals(true, album.keySet().contains("Key0"));
284         assertEquals(true, album.values().contains("value of Key0"));
285         assertEquals(1, album.entrySet().size());
286
287         // The flush() operation fails because the distributor is not initialized with the album which
288         // is fine for unit test
289         try {
290             album.flush();
291             fail("test should throw an exception");
292         } catch (ContextException e) {
293             assertEquals("map flush failed, supplied map is null", e.getMessage());
294         }
295
296         assertEquals(1, album.size());
297         assertEquals(false, album.isEmpty());
298
299         album.put("Key0", "New value of Key0");
300         assertEquals("New value of Key0", album.get("Key0"));
301
302         album.putAll(putAllData);
303
304         putAllData.put("AllKey3", null);
305         try {
306             album.putAll(putAllData);
307             fail("test should throw an exception");
308         } catch (ContextRuntimeException e) {
309             assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()",
310                             e.getMessage());
311         }
312
313         assertEquals("New value of Key0", album.remove("Key0"));
314
315         album.clear();
316         assertTrue(album.isEmpty());
317
318         ModelService.clear();
319     }
320
321     @SuppressWarnings("unlikely-arg-type")
322     @Test
323     public void testCompareToEqualsHash() throws ContextException {
324         AxContextSchemas schemas = new AxContextSchemas();
325         AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
326                         "java.lang.Integer");
327         schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
328         AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
329                         "JAVA", "java.lang.String");
330         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
331         ModelService.registerModel(AxContextSchemas.class, schemas);
332
333         AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
334                         true, AxArtifactKey.getNullKey());
335
336         axContextAlbum.setItemSchema(simpleIntSchema.getKey());
337         Distributor distributor = new JvmLocalDistributor();
338         distributor.init(axContextAlbum.getKey());
339         ContextAlbumImpl album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
340
341         assertTrue(album.hashCode() != 0);
342
343         assertEquals(0, album.compareTo(album));
344         assertEquals(1, album.compareTo(null));
345
346         assertTrue(album.equals(album));
347         assertFalse(album.equals(new DummyContextAlbumImpl()));
348
349         ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor,
350                         new LinkedHashMap<String, Object>());
351         assertTrue(album.equals(otherAlbum));
352
353         otherAlbum.put("Key", 123);
354         assertFalse(album.equals(otherAlbum));
355
356         try {
357             otherAlbum.put("Key", "BadValue");
358             fail("test should throw an exception here");
359         } catch (ContextRuntimeException cre) {
360             assertEquals("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": "
361                             + "TestContextAlbum:0.0.1: object \"BadValue\" of class \"java.lang.String\" "
362                             + "not compatible with class \"java.lang.Integer\"", cre.getMessage());
363         }
364
365         AxContextAlbum otherAxContextAlbum = new AxContextAlbum(new AxArtifactKey("OtherTestContextAlbum", "0.0.1"),
366                         "Policy", true, AxArtifactKey.getNullKey());
367
368         otherAxContextAlbum.setItemSchema(simpleStringSchema.getKey());
369         otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<String, Object>());
370         assertFalse(album.equals(otherAlbum));
371
372         try {
373             album.flush();
374             fail("test should throw an exception here");
375         } catch (ContextException ce) {
376             assertEquals("map flush failed, supplied map is null", ce.getMessage());
377         }
378
379         AxContextAlbums albums = new AxContextAlbums();
380         ModelService.registerModel(AxContextAlbums.class, albums);
381         albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum);
382         distributor.createContextAlbum(album.getKey());
383         
384         album.flush();
385
386         ModelService.clear();
387     }
388 }