bf363058ba78d1d81851f9350d1679f9e81f26a2
[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.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;
49
50 public class ContextAlbumImplTest {
51     /**
52      * Set ups everything for the test.
53      */
54     @BeforeClass
55     public static void prepareForTest() {
56         final ContextParameters contextParameters = new ContextParameters();
57         contextParameters.getLockManagerParameters()
58                         .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JVMLocalLockManager");
59         ParameterService.registerParameters(ContextParameters.class, contextParameters);
60
61         final SchemaParameters schemaParameters = new SchemaParameters();
62         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
63         ParameterService.registerParameters(SchemaParameters.class, schemaParameters);
64     }
65
66     @Test
67     public void testNullsOnConstructor() {
68         try {
69             new ContextAlbumImpl(null, null, null);
70             fail("this test should throw an exception");
71         } catch (IllegalArgumentException e) {
72             assertEquals("Context album definition may not be null", e.getMessage());
73         } catch (ContextException e) {
74             fail("this test should throw an IllegalArgumentException");
75         }
76
77         try {
78             new ContextAlbumImpl(new AxContextAlbum(), null, null);
79             fail("this test should throw an exception");
80         } catch (IllegalArgumentException e) {
81             assertEquals("Distributor may not be null", e.getMessage());
82         } catch (ContextException e) {
83             fail("this test should throw an IllegalArgumentException");
84         }
85
86         try {
87             new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), null);
88             fail("this test should throw an exception");
89         } catch (IllegalArgumentException e) {
90             assertEquals("Album map may not be null", e.getMessage());
91         } catch (ContextException e) {
92             fail("this test should throw an IllegalArgumentException");
93         }
94
95         try {
96             new ContextAlbumImpl(new AxContextAlbum(), new JVMLocalDistributor(), new LinkedHashMap<String, Object>());
97             fail("this test should throw an exception");
98         } catch (ApexRuntimeException e) {
99             assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
100                     + "not found in model service", e.getMessage());
101         } catch (ContextException e) {
102             fail("this test should throw an ApexRuntimeException");
103         }
104     }
105
106     @Test
107     public void testAlbumInterface() throws ContextException {
108         AxContextSchemas schemas = new AxContextSchemas();
109         AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
110                         "JAVA", "java.lang.String");
111         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
112         ModelService.registerModel(AxContextSchemas.class, schemas);
113
114         AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
115                         true, AxArtifactKey.getNullKey());
116
117         try {
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));
123         }
124
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
130         AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
131                 false, simpleStringSchema.getKey());
132         ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());
133
134         assertEquals("TestContextAlbum", album.getName());
135         assertEquals("TestContextAlbum:0.0.1", album.getKey().getID());
136         assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getID());
137         assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getID());
138
139         try {
140             album.containsKey(null);
141             fail("test should throw an exception");
142         } catch (ContextRuntimeException e) {
143             assertEquals("null values are illegal on method parameter \"key\"", e.getMessage());
144         }
145         assertEquals(false, album.containsKey("Key0"));
146
147         try {
148             album.containsValue(null);
149             fail("test should throw an exception");
150         } catch (ContextRuntimeException e) {
151             assertEquals("null values are illegal on method parameter \"value\"", e.getMessage());
152         }
153         assertEquals(false, album.containsValue("some value"));
154
155         try {
156             album.get(null);
157             fail("test should throw an exception");
158         } catch (ContextRuntimeException e) {
159             assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage());
160         }
161
162         try {
163             album.put(null, null);
164             fail("test should throw an exception");
165         } catch (ContextRuntimeException e) {
166             assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
167         }
168
169         try {
170             album.put("KeyNull", null);
171             fail("test should throw an exception");
172         } catch (ContextRuntimeException e) {
173             assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()",
174                             e.getMessage());
175         }
176
177         try {
178             albumRo.put("KeyReadOnly", "A value for a Read Only Album");
179             fail("test should throw an exception");
180         } catch (ContextRuntimeException e) {
181             assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
182                             + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage());
183         }
184
185         Map<String, Object> putAllData = new LinkedHashMap<>();
186         putAllData.put("AllKey0", "vaue of AllKey0");
187         putAllData.put("AllKey1", "vaue of AllKey1");
188         putAllData.put("AllKey2", "vaue of AllKey2");
189
190         try {
191             albumRo.putAll(putAllData);
192             fail("test should throw an exception");
193         } catch (ContextRuntimeException e) {
194             assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage());
195         }
196
197         try {
198             albumRo.remove("AllKey0");
199             fail("test should throw an exception");
200         } catch (ContextRuntimeException e) {
201             assertEquals(
202                     "album \"TestContextAlbum:0.0.1\" remove() not allowed on read only albums for key=\"AllKey0\"",
203                     e.getMessage());
204         }
205
206         try {
207             album.remove(null);
208             fail("test should throw an exception");
209         } catch (ContextRuntimeException e) {
210             assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage());
211         }
212
213         try {
214             albumRo.clear();
215             fail("test should throw an exception");
216         } catch (ContextRuntimeException e) {
217             assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage());
218         }
219
220         // The following locking tests pass because the locking protects access to Key0 across all
221         // copies of the distributed album whether the key exists or not
222         album.lockForReading("Key0");
223         assertEquals(null, album.get("Key0"));
224         album.unlockForReading("Key0");
225         assertEquals(null, album.get("Key0"));
226
227         album.lockForWriting("Key0");
228         assertEquals(null, album.get("Key0"));
229         album.unlockForWriting("Key0");
230         assertEquals(null, album.get("Key0"));
231
232         // Test write access, trivial test because Integration Test does
233         // a full test of access locking over albums in different JVMs
234         album.lockForWriting("Key0");
235         assertEquals(null, album.get("Key0"));
236         album.put("Key0", "value of Key0");
237         assertEquals("value of Key0", album.get("Key0"));
238         album.unlockForWriting("Key0");
239         assertEquals("value of Key0", album.get("Key0"));
240
241         // Test read access, trivial test because Integration Test does
242         // a full test of access locking over albums in different JVMs
243         album.lockForReading("Key0");
244         assertEquals("value of Key0", album.get("Key0"));
245         album.unlockForReading("Key0");
246
247         AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
248         AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
249
250         AxConcept[] userArtifactStack = { somePolicyKey, somePolicyState };
251         album.setUserArtifactStack(userArtifactStack);
252         assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getID());
253         assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getID());
254
255         assertEquals(true, album.keySet().contains("Key0"));
256         assertEquals(true, album.values().contains("value of Key0"));
257         assertEquals(1, album.entrySet().size());
258
259         // The flush() operation fails because the distributor is not initialized with the album which
260         // is fine for unit test
261         try {
262             album.flush();
263             fail("test should throw an exception");
264         } catch (ContextException e) {
265             assertEquals("map flush failed, supplied map is null", e.getMessage());
266         }
267
268         assertEquals(1, album.size());
269         assertEquals(false, album.isEmpty());
270
271         album.put("Key0", "New value of Key0");
272         assertEquals("New value of Key0", album.get("Key0"));
273
274         album.putAll(putAllData);
275
276         putAllData.put("AllKey3", null);
277         try {
278             album.putAll(putAllData);
279             fail("test should throw an exception");
280         } catch (ContextRuntimeException e) {
281             assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()",
282                             e.getMessage());
283         }
284
285         assertEquals("New value of Key0", album.remove("Key0"));
286         
287         album.clear();
288         assertTrue(album.isEmpty());
289         
290         ModelService.clear();
291     }
292 }