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