5f2b426bdb28429288830629840eb446b980fcee
[policy/apex-pdp.git] /
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.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29
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;
51
52 public class ContextAlbumImplTest {
53     /**
54      * Set ups everything for the test.
55      */
56     @BeforeClass
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");
61
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);
66
67         ParameterService.register(contextParameters);
68         ParameterService.register(contextParameters.getDistributorParameters());
69         ParameterService.register(contextParameters.getLockManagerParameters());
70         ParameterService.register(contextParameters.getPersistorParameters());
71
72         final SchemaParameters schemaParameters = new SchemaParameters();
73         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
74         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
75
76         ParameterService.register(schemaParameters);
77     }
78
79     @AfterClass
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();
87     }
88
89     @Test
90     public void testNullsOnConstructor() {
91         try {
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");
98         }
99
100         try {
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");
107         }
108
109         try {
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");
116         }
117
118         try {
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");
126         }
127     }
128
129     @Test
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);
136
137         AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
138                         true, AxArtifactKey.getNullKey());
139
140         try {
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));
146         }
147
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>());
152
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>());
156
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());
161
162         try {
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());
167         }
168         assertEquals(false, album.containsKey("Key0"));
169
170         try {
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());
175         }
176         assertEquals(false, album.containsValue("some value"));
177
178         try {
179             album.get(null);
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());
183         }
184
185         try {
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());
190         }
191
192         try {
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()",
197                             e.getMessage());
198         }
199
200         try {
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());
206         }
207
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");
212
213         try {
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());
218         }
219
220         try {
221             albumRo.remove("AllKey0");
222             fail("test should throw an exception");
223         } catch (ContextRuntimeException e) {
224             assertEquals(
225                     "album \"TestContextAlbum:0.0.1\" remove() not allowed on read only albums for key=\"AllKey0\"",
226                     e.getMessage());
227         }
228
229         try {
230             album.remove(null);
231             fail("test should throw an exception");
232         } catch (ContextRuntimeException e) {
233             assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage());
234         }
235
236         try {
237             albumRo.clear();
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());
241         }
242
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"));
249
250         album.lockForWriting("Key0");
251         assertEquals(null, album.get("Key0"));
252         album.unlockForWriting("Key0");
253         assertEquals(null, album.get("Key0"));
254
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"));
263
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");
269
270         AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
271         AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
272
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());
277
278         assertEquals(true, album.keySet().contains("Key0"));
279         assertEquals(true, album.values().contains("value of Key0"));
280         assertEquals(1, album.entrySet().size());
281
282         // The flush() operation fails because the distributor is not initialized with the album which
283         // is fine for unit test
284         try {
285             album.flush();
286             fail("test should throw an exception");
287         } catch (ContextException e) {
288             assertEquals("map flush failed, supplied map is null", e.getMessage());
289         }
290
291         assertEquals(1, album.size());
292         assertEquals(false, album.isEmpty());
293
294         album.put("Key0", "New value of Key0");
295         assertEquals("New value of Key0", album.get("Key0"));
296
297         album.putAll(putAllData);
298
299         putAllData.put("AllKey3", null);
300         try {
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()",
305                             e.getMessage());
306         }
307
308         assertEquals("New value of Key0", album.remove("Key0"));
309         
310         album.clear();
311         assertTrue(album.isEmpty());
312         
313         ModelService.clear();
314     }
315 }