2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020, 2024 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.context.impl;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertNotEquals;
28 import static org.junit.jupiter.api.Assertions.assertNull;
29 import static org.junit.jupiter.api.Assertions.assertTrue;
31 import java.util.LinkedHashMap;
33 import org.junit.jupiter.api.AfterAll;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.Test;
36 import org.onap.policy.apex.context.ContextAlbum;
37 import org.onap.policy.apex.context.ContextException;
38 import org.onap.policy.apex.context.Distributor;
39 import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
40 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
41 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
42 import org.onap.policy.apex.context.parameters.ContextParameters;
43 import org.onap.policy.apex.context.parameters.SchemaParameters;
44 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
45 import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
46 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
47 import org.onap.policy.apex.model.basicmodel.service.ModelService;
48 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
49 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
50 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
51 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
52 import org.onap.policy.common.parameters.ParameterService;
54 class ContextAlbumImplTest {
56 * Set-ups everything for the test.
59 public static void prepareForTest() {
60 final ContextParameters contextParameters = new ContextParameters();
61 contextParameters.getLockManagerParameters()
62 .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager");
64 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
65 contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
66 contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
67 contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
69 ParameterService.register(contextParameters);
70 ParameterService.register(contextParameters.getDistributorParameters());
71 ParameterService.register(contextParameters.getLockManagerParameters());
72 ParameterService.register(contextParameters.getPersistorParameters());
74 final SchemaParameters schemaParameters = new SchemaParameters();
75 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
76 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
78 ParameterService.register(schemaParameters);
82 * Clear down the test data.
85 public static void cleanUpAfterTest() {
86 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
87 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
88 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
89 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
90 ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
91 ParameterService.clear();
95 void testNullsOnConstructor() {
96 assertThatThrownBy(() -> new ContextAlbumImpl(null, null, null))
97 .hasMessage("Context album definition may not be null");
99 assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), null, null))
100 .hasMessage("Distributor may not be null");
102 assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null))
103 .hasMessage("Album map may not be null");
105 assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(),
106 new LinkedHashMap<>()))
107 .hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
108 + "not found in model service");
112 void testAlbumInterface() throws ContextException {
113 AxContextSchemas schemas = new AxContextSchemas();
114 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
115 "JAVA", "java.lang.String");
116 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
117 ModelService.registerModel(AxContextSchemas.class, schemas);
119 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
120 true, AxArtifactKey.getNullKey());
123 () -> new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap<>()))
124 .hasMessageContaining("could not initiate schema management for context album AxContextAlbum");
126 axContextAlbum.setItemSchema(simpleStringSchema.getKey());
127 Distributor distributor = new JvmLocalDistributor();
128 distributor.init(axContextAlbum.getKey());
129 ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<>());
131 assertGetValuesFromContextAlbum(album);
133 assertThatThrownBy(() -> album.containsKey(null))
134 .hasMessage("null values are illegal on method parameter \"key\"");
135 assertFalse(album.containsKey("Key0"));
137 assertUsingNullValues_RaiseExceptions(album);
139 AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
140 false, simpleStringSchema.getKey());
141 ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<>());
143 assertThatThrownBy(() -> albumRo.put("KeyReadOnly", "A value for a Read Only Album"))
144 .hasMessage("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
145 + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album");
147 Map<String, Object> putAllData = new LinkedHashMap<>();
148 putAllData.put("AllKey0", "vaue of AllKey0");
149 putAllData.put("AllKey1", "vaue of AllKey1");
150 putAllData.put("AllKey2", "vaue of AllKey2");
152 assertThatThrownBy(() -> albumRo.putAll(putAllData))
153 .hasMessage("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums");
155 assertThatThrownBy(() -> albumRo.remove("AllKey0")).hasMessage(
156 "album \"TestContextAlbum:0.0.1\" remove() not allowed " + "on read only albums for key=\"AllKey0\"");
158 assertThatThrownBy(() -> album.remove(null))
159 .hasMessage("null values are illegal on method parameter \"keyID\"");
161 assertThatThrownBy(albumRo::clear)
162 .hasMessage("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums");
164 assertLockingUnlocking(album);
166 AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
167 AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
169 AxConcept[] userArtifactStack = {somePolicyKey, somePolicyState};
170 album.setUserArtifactStack(userArtifactStack);
171 assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId());
172 assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId());
174 assertTrue(album.containsKey("Key0"));
175 assertTrue(album.containsValue("value of Key0"));
176 assertEquals(1, album.entrySet().size());
178 // The flush() operation fails because the distributor is not initialized with
179 // the album which is fine for unit test
180 assertThatThrownBy(album::flush).hasMessage("map flush failed, supplied map is null");
181 assertEquals(1, album.size());
182 assertFalse(album.isEmpty());
184 album.put("Key0", "New value of Key0");
185 assertEquals("New value of Key0", album.get("Key0"));
187 album.putAll(putAllData);
189 putAllData.put("AllKey3", null);
190 assertThatThrownBy(() -> album.putAll(putAllData))
191 .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key " + "\"AllKey3\" for put()");
192 assertEquals("New value of Key0", album.remove("Key0"));
196 ModelService.clear();
199 private static void assertUsingNullValues_RaiseExceptions(ContextAlbum album) {
200 assertThatThrownBy(() -> album.containsValue(null))
201 .hasMessage("null values are illegal on method parameter \"value\"");
202 assertFalse(album.containsValue("some value"));
204 assertThatThrownBy(() -> album.get(null))
205 .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()");
207 assertThatThrownBy(() -> album.put(null, null))
208 .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()");
210 assertThatThrownBy(() -> album.put("KeyNull", null))
211 .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\"" + " for put()");
214 private static void assertGetValuesFromContextAlbum(ContextAlbum album) {
215 assertEquals("TestContextAlbum", album.getName());
216 assertEquals("TestContextAlbum:0.0.1", album.getKey().getId());
217 assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId());
218 assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId());
221 private static void assertLockingUnlocking(ContextAlbum album) throws ContextException {
222 // The following locking tests pass because the locking protects access to Key0
224 // copies of the distributed album whether the key exists or not
225 album.lockForReading("Key0");
226 assertNull(album.get("Key0"));
227 album.unlockForReading("Key0");
228 assertNull(album.get("Key0"));
230 album.lockForWriting("Key0");
231 assertNull(album.get("Key0"));
232 album.unlockForWriting("Key0");
233 assertNull(album.get("Key0"));
235 // Test write access, trivial test because Integration Test does
236 // a full test of access locking over albums in different JVMs
237 album.lockForWriting("Key0");
238 assertNull(album.get("Key0"));
239 album.put("Key0", "value of Key0");
240 assertEquals("value of Key0", album.get("Key0"));
241 album.unlockForWriting("Key0");
242 assertEquals("value of Key0", album.get("Key0"));
244 // Test read access, trivial test because Integration Test does
245 // a full test of access locking over albums in different JVMs
246 album.lockForReading("Key0");
247 assertEquals("value of Key0", album.get("Key0"));
248 album.unlockForReading("Key0");
252 void testCompareToEqualsHash() throws ContextException {
253 AxContextSchemas schemas = new AxContextSchemas();
254 AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
255 "java.lang.Integer");
256 schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
257 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
258 "JAVA", "java.lang.String");
259 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
260 ModelService.registerModel(AxContextSchemas.class, schemas);
262 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
263 true, AxArtifactKey.getNullKey());
265 axContextAlbum.setItemSchema(simpleIntSchema.getKey());
266 Distributor distributor = new JvmLocalDistributor();
267 distributor.init(axContextAlbum.getKey());
268 ContextAlbumImpl album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<>());
270 assertNotEquals(0, album.hashCode());
272 assertEquals(1, album.compareTo(null));
274 assertNotEquals(album, new DummyContextAlbumImpl());
276 ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<>());
277 assertEquals(album, otherAlbum);
279 otherAlbum.put("Key", 123);
280 assertNotEquals(album, otherAlbum);
282 assertThatThrownBy(() -> {
283 ContextAlbumImpl otherAlbumBad = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<>());
284 otherAlbumBad.put("Key", "BadValue");
285 }).hasMessage("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": "
286 + "TestContextAlbum:0.0.1: object \"BadValue\" of class \"java.lang.String\" "
287 + "not compatible with class \"java.lang.Integer\"");
288 AxContextAlbum otherAxContextAlbum = new AxContextAlbum(new AxArtifactKey("OtherTestContextAlbum", "0.0.1"),
289 "Policy", true, AxArtifactKey.getNullKey());
291 otherAxContextAlbum.setItemSchema(simpleStringSchema.getKey());
292 otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<>());
293 assertNotEquals(album, otherAlbum);
295 assertThatThrownBy(album::flush).hasMessage("map flush failed, supplied map is null");
296 AxContextAlbums albums = new AxContextAlbums();
297 ModelService.registerModel(AxContextAlbums.class, albums);
298 albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum);
299 distributor.createContextAlbum(album.getKey());
303 ModelService.clear();