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