25928716e9186f2cda70d8cd32202bdcf99be0d9
[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     /**
80      * Clear down the test data.
81      */
82     @AfterClass
83     public static void cleanUpAfterTest() {
84         ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
85         ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
86         ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
87         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
88         ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
89         ParameterService.clear();
90     }
91
92     @Test
93     public void testNullsOnConstructor() {
94         try {
95             new ContextAlbumImpl(null, null, null);
96             fail("this test should throw an exception");
97         } catch (IllegalArgumentException e) {
98             assertEquals("Context album definition may not be null", e.getMessage());
99         } catch (ContextException e) {
100             fail("this test should throw an IllegalArgumentException");
101         }
102
103         try {
104             new ContextAlbumImpl(new AxContextAlbum(), null, null);
105             fail("this test should throw an exception");
106         } catch (IllegalArgumentException e) {
107             assertEquals("Distributor may not be null", e.getMessage());
108         } catch (ContextException e) {
109             fail("this test should throw an IllegalArgumentException");
110         }
111
112         try {
113             new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null);
114             fail("this test should throw an exception");
115         } catch (IllegalArgumentException e) {
116             assertEquals("Album map may not be null", e.getMessage());
117         } catch (ContextException e) {
118             fail("this test should throw an IllegalArgumentException");
119         }
120
121         try {
122             new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), new LinkedHashMap<String, Object>());
123             fail("this test should throw an exception");
124         } catch (ApexRuntimeException e) {
125             assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
126                     + "not found in model service", e.getMessage());
127         } catch (ContextException e) {
128             fail("this test should throw an ApexRuntimeException");
129         }
130     }
131
132     @Test
133     public void testAlbumInterface() throws ContextException {
134         AxContextSchemas schemas = new AxContextSchemas();
135         AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
136                         "JAVA", "java.lang.String");
137         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
138         ModelService.registerModel(AxContextSchemas.class, schemas);
139
140         AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
141                         true, AxArtifactKey.getNullKey());
142
143         try {
144             new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap<String, Object>());
145             fail("this test should throw an exception");
146         } catch (ContextException e) {
147             assertEquals("could not initiate schema management for context album AxContextAlbum",
148                             e.getMessage().substring(0, 69));
149         }
150
151         axContextAlbum.setItemSchema(simpleStringSchema.getKey());
152         Distributor distributor = new JvmLocalDistributor();
153         distributor.init(axContextAlbum.getKey());
154         ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
155
156         AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
157                 false, simpleStringSchema.getKey());
158         ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());
159
160         assertEquals("TestContextAlbum", album.getName());
161         assertEquals("TestContextAlbum:0.0.1", album.getKey().getId());
162         assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId());
163         assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId());
164
165         try {
166             album.containsKey(null);
167             fail("test should throw an exception");
168         } catch (ContextRuntimeException e) {
169             assertEquals("null values are illegal on method parameter \"key\"", e.getMessage());
170         }
171         assertEquals(false, album.containsKey("Key0"));
172
173         try {
174             album.containsValue(null);
175             fail("test should throw an exception");
176         } catch (ContextRuntimeException e) {
177             assertEquals("null values are illegal on method parameter \"value\"", e.getMessage());
178         }
179         assertEquals(false, album.containsValue("some value"));
180
181         try {
182             album.get(null);
183             fail("test should throw an exception");
184         } catch (ContextRuntimeException e) {
185             assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage());
186         }
187
188         try {
189             album.put(null, null);
190             fail("test should throw an exception");
191         } catch (ContextRuntimeException e) {
192             assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
193         }
194
195         try {
196             album.put("KeyNull", null);
197             fail("test should throw an exception");
198         } catch (ContextRuntimeException e) {
199             assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()",
200                             e.getMessage());
201         }
202
203         try {
204             albumRo.put("KeyReadOnly", "A value for a Read Only Album");
205             fail("test should throw an exception");
206         } catch (ContextRuntimeException e) {
207             assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
208                             + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage());
209         }
210
211         Map<String, Object> putAllData = new LinkedHashMap<>();
212         putAllData.put("AllKey0", "vaue of AllKey0");
213         putAllData.put("AllKey1", "vaue of AllKey1");
214         putAllData.put("AllKey2", "vaue of AllKey2");
215
216         try {
217             albumRo.putAll(putAllData);
218             fail("test should throw an exception");
219         } catch (ContextRuntimeException e) {
220             assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage());
221         }
222
223         try {
224             albumRo.remove("AllKey0");
225             fail("test should throw an exception");
226         } catch (ContextRuntimeException e) {
227             assertEquals(
228                     "album \"TestContextAlbum:0.0.1\" remove() not allowed on read only albums for key=\"AllKey0\"",
229                     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 = { somePolicyKey, somePolicyState };
277         album.setUserArtifactStack(userArtifactStack);
278         assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId());
279         assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId());
280
281         assertEquals(true, album.keySet().contains("Key0"));
282         assertEquals(true, album.values().contains("value of Key0"));
283         assertEquals(1, album.entrySet().size());
284
285         // The flush() operation fails because the distributor is not initialized with the album which
286         // is fine for unit test
287         try {
288             album.flush();
289             fail("test should throw an exception");
290         } catch (ContextException e) {
291             assertEquals("map flush failed, supplied map is null", e.getMessage());
292         }
293
294         assertEquals(1, album.size());
295         assertEquals(false, album.isEmpty());
296
297         album.put("Key0", "New value of Key0");
298         assertEquals("New value of Key0", album.get("Key0"));
299
300         album.putAll(putAllData);
301
302         putAllData.put("AllKey3", null);
303         try {
304             album.putAll(putAllData);
305             fail("test should throw an exception");
306         } catch (ContextRuntimeException e) {
307             assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()",
308                             e.getMessage());
309         }
310
311         assertEquals("New value of Key0", album.remove("Key0"));
312         
313         album.clear();
314         assertTrue(album.isEmpty());
315         
316         ModelService.clear();
317     }
318 }