bed67858129a8ca6f52beee2c3d3084672bb3fa3
[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 "
97                     + "not found in model service", 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(
199                     "album \"TestContextAlbum:0.0.1\" remove() not allowed on read only albums for key=\"AllKey0\"",
200                     e.getMessage());
201         }
202
203         try {
204             album.remove(null);
205             fail("test should throw an exception");
206         } catch (ContextRuntimeException e) {
207             assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage());
208         }
209
210         try {
211             albumRO.clear();
212             fail("test should throw an exception");
213         } catch (ContextRuntimeException e) {
214             assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage());
215         }
216
217         // The following locking tests pass because the locking protects access to Key0 across all
218         // copies of the distributed album whether the key exists or not
219         album.lockForReading("Key0");
220         assertEquals(null, album.get("Key0"));
221         album.unlockForReading("Key0");
222         assertEquals(null, album.get("Key0"));
223
224         album.lockForWriting("Key0");
225         assertEquals(null, album.get("Key0"));
226         album.unlockForWriting("Key0");
227         assertEquals(null, album.get("Key0"));
228
229         // Test write access, trivial test because Integration Test does
230         // a full test of access locking over albums in different JVMs
231         album.lockForWriting("Key0");
232         assertEquals(null, album.get("Key0"));
233         album.put("Key0", "value of Key0");
234         assertEquals("value of Key0", album.get("Key0"));
235         album.unlockForWriting("Key0");
236         assertEquals("value of Key0", album.get("Key0"));
237
238         // Test read access, trivial test because Integration Test does
239         // a full test of access locking over albums in different JVMs
240         album.lockForReading("Key0");
241         assertEquals("value of Key0", album.get("Key0"));
242         album.unlockForReading("Key0");
243
244         AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
245         AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
246
247         AxConcept[] userArtifactStack = { somePolicyKey, somePolicyState };
248         album.setUserArtifactStack(userArtifactStack);
249         assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getID());
250         assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getID());
251
252         assertEquals(true, album.keySet().contains("Key0"));
253         assertEquals(true, album.values().contains("value of Key0"));
254         assertEquals(1, album.entrySet().size());
255
256         // The flush() operation fails because the distributor is not initialized with the album which
257         // is fine for unit test
258         try {
259             album.flush();
260             fail("test should throw an exception");
261         } catch (ContextException e) {
262             assertEquals("map flush failed, supplied map is null", e.getMessage());
263         }
264
265         assertEquals(1, album.size());
266         assertEquals(false, album.isEmpty());
267
268         album.put("Key0", "New value of Key0");
269         assertEquals("New value of Key0", album.get("Key0"));
270
271         album.putAll(putAllData);
272
273         putAllData.put("AllKey3", null);
274         try {
275             album.putAll(putAllData);
276             fail("test should throw an exception");
277         } catch (ContextRuntimeException e) {
278             assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()",
279                             e.getMessage());
280         }
281
282         assertEquals("New value of Key0", album.remove("Key0"));
283         
284         album.clear();
285         assertTrue(album.isEmpty());
286         
287         ModelService.clear();
288     }
289 }