6ebb79f4377beecd9fce390522d12ae5ba464caa
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.context.impl;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.LinkedHashMap;
30 import java.util.Map;
31 import org.junit.AfterClass;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.onap.policy.apex.context.ContextAlbum;
35 import org.onap.policy.apex.context.ContextException;
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.AxArtifactKey;
43 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
45 import org.onap.policy.apex.model.basicmodel.service.ModelService;
46 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
47 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
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         assertThatThrownBy(() -> new ContextAlbumImpl(null, null, null))
95             .hasMessage("Context album definition may not be null");
96
97         assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), null, null))
98             .hasMessage("Distributor may not be null");
99
100         assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null))
101             .hasMessage("Album map may not be null");
102
103         assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(),
104             new LinkedHashMap<String, Object>()))
105                 .hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
106                     + "not found in model service");
107     }
108
109     @Test
110     public void testAlbumInterface() throws ContextException {
111         AxContextSchemas schemas = new AxContextSchemas();
112         AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
113             "JAVA", "java.lang.String");
114         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
115         ModelService.registerModel(AxContextSchemas.class, schemas);
116
117         AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
118             true, AxArtifactKey.getNullKey());
119
120         assertThatThrownBy(
121             () -> new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap<String, Object>()))
122                 .hasMessageContaining("could not initiate schema management for context album AxContextAlbum");
123
124         axContextAlbum.setItemSchema(simpleStringSchema.getKey());
125         Distributor distributor = new JvmLocalDistributor();
126         distributor.init(axContextAlbum.getKey());
127         ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
128
129         assertEquals("TestContextAlbum", album.getName());
130         assertEquals("TestContextAlbum:0.0.1", album.getKey().getId());
131         assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId());
132         assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId());
133
134         assertThatThrownBy(() -> album.containsKey(null))
135             .hasMessage("null values are illegal on method parameter \"key\"");
136         assertEquals(false, album.containsKey("Key0"));
137
138         assertThatThrownBy(() -> album.containsValue(null))
139             .hasMessage("null values are illegal on method parameter \"value\"");
140         assertEquals(false, album.containsValue("some value"));
141
142         assertThatThrownBy(() -> album.get(null))
143             .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()");
144
145         assertThatThrownBy(() -> album.put(null, null))
146             .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()");
147
148         assertThatThrownBy(() -> album.put("KeyNull", null))
149             .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\"" + " for put()");
150
151         AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
152             false, simpleStringSchema.getKey());
153         ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());
154
155         assertThatThrownBy(() -> albumRo.put("KeyReadOnly", "A value for a Read Only Album"))
156             .hasMessage("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
157                 + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album");
158
159         Map<String, Object> putAllData = new LinkedHashMap<>();
160         putAllData.put("AllKey0", "vaue of AllKey0");
161         putAllData.put("AllKey1", "vaue of AllKey1");
162         putAllData.put("AllKey2", "vaue of AllKey2");
163
164         assertThatThrownBy(() -> albumRo.putAll(putAllData))
165             .hasMessage("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums");
166
167         assertThatThrownBy(() -> albumRo.remove("AllKey0")).hasMessage(
168             "album \"TestContextAlbum:0.0.1\" remove() not allowed " + "on read only albums for key=\"AllKey0\"");
169
170         assertThatThrownBy(() -> album.remove(null))
171             .hasMessage("null values are illegal on method parameter \"keyID\"");
172
173         assertThatThrownBy(albumRo::clear)
174             .hasMessage("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums");
175
176         // The following locking tests pass because the locking protects access to Key0
177         // across all
178         // copies of the distributed album whether the key exists or not
179         album.lockForReading("Key0");
180         assertEquals(null, album.get("Key0"));
181         album.unlockForReading("Key0");
182         assertEquals(null, album.get("Key0"));
183
184         album.lockForWriting("Key0");
185         assertEquals(null, album.get("Key0"));
186         album.unlockForWriting("Key0");
187         assertEquals(null, album.get("Key0"));
188
189         // Test write access, trivial test because Integration Test does
190         // a full test of access locking over albums in different JVMs
191         album.lockForWriting("Key0");
192         assertEquals(null, album.get("Key0"));
193         album.put("Key0", "value of Key0");
194         assertEquals("value of Key0", album.get("Key0"));
195         album.unlockForWriting("Key0");
196         assertEquals("value of Key0", album.get("Key0"));
197
198         // Test read access, trivial test because Integration Test does
199         // a full test of access locking over albums in different JVMs
200         album.lockForReading("Key0");
201         assertEquals("value of Key0", album.get("Key0"));
202         album.unlockForReading("Key0");
203
204         AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
205         AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
206
207         AxConcept[] userArtifactStack = { somePolicyKey, somePolicyState };
208         album.setUserArtifactStack(userArtifactStack);
209         assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId());
210         assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId());
211
212         assertEquals(true, album.keySet().contains("Key0"));
213         assertEquals(true, album.values().contains("value of Key0"));
214         assertEquals(1, album.entrySet().size());
215
216         // The flush() operation fails because the distributor is not initialized with
217         // the album which
218         // is fine for unit test
219         assertThatThrownBy(album::flush).hasMessage("map flush failed, supplied map is null");
220         assertEquals(1, album.size());
221         assertEquals(false, album.isEmpty());
222
223         album.put("Key0", "New value of Key0");
224         assertEquals("New value of Key0", album.get("Key0"));
225
226         album.putAll(putAllData);
227
228         putAllData.put("AllKey3", null);
229         assertThatThrownBy(() -> album.putAll(putAllData))
230             .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key " + "\"AllKey3\" for put()");
231         assertEquals("New value of Key0", album.remove("Key0"));
232
233         album.clear();
234         assertTrue(album.isEmpty());
235
236         ModelService.clear();
237     }
238
239     @Test
240     public void testCompareToEqualsHash() throws ContextException {
241         AxContextSchemas schemas = new AxContextSchemas();
242         AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
243             "java.lang.Integer");
244         schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
245         AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
246             "JAVA", "java.lang.String");
247         schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
248         ModelService.registerModel(AxContextSchemas.class, schemas);
249
250         AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
251             true, AxArtifactKey.getNullKey());
252
253         axContextAlbum.setItemSchema(simpleIntSchema.getKey());
254         Distributor distributor = new JvmLocalDistributor();
255         distributor.init(axContextAlbum.getKey());
256         ContextAlbumImpl album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
257
258         assertNotEquals(0, album.hashCode());
259
260         assertEquals(0, album.compareTo(album));
261         assertEquals(1, album.compareTo(null));
262
263         assertEquals(album, album);
264         assertNotEquals(album, new DummyContextAlbumImpl());
265
266         ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor,
267             new LinkedHashMap<String, Object>());
268         assertEquals(album, otherAlbum);
269
270         otherAlbum.put("Key", 123);
271         assertNotEquals(album, otherAlbum);
272
273         assertThatThrownBy(() -> {
274             ContextAlbumImpl otherAlbumBad = new ContextAlbumImpl(axContextAlbum, distributor,
275                 new LinkedHashMap<String, Object>());
276             otherAlbumBad.put("Key", "BadValue");
277         }).hasMessage("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": "
278             + "TestContextAlbum:0.0.1: object \"BadValue\" of class \"java.lang.String\" "
279             + "not compatible with class \"java.lang.Integer\"");
280         AxContextAlbum otherAxContextAlbum = new AxContextAlbum(new AxArtifactKey("OtherTestContextAlbum", "0.0.1"),
281             "Policy", true, AxArtifactKey.getNullKey());
282
283         otherAxContextAlbum.setItemSchema(simpleStringSchema.getKey());
284         otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<String, Object>());
285         assertNotEquals(album, otherAlbum);
286
287         assertThatThrownBy(album::flush).hasMessage("map flush failed, supplied map is null");
288         AxContextAlbums albums = new AxContextAlbums();
289         ModelService.registerModel(AxContextAlbums.class, albums);
290         albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum);
291         distributor.createContextAlbum(album.getKey());
292
293         album.flush();
294
295         ModelService.clear();
296     }
297 }