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
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.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertTrue;
29 import java.util.LinkedHashMap;
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;
52 public class ContextAlbumImplTest {
54 * Set ups everything for the test.
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");
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);
67 ParameterService.register(contextParameters);
68 ParameterService.register(contextParameters.getDistributorParameters());
69 ParameterService.register(contextParameters.getLockManagerParameters());
70 ParameterService.register(contextParameters.getPersistorParameters());
72 final SchemaParameters schemaParameters = new SchemaParameters();
73 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
74 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
76 ParameterService.register(schemaParameters);
80 * Clear down the test data.
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();
93 public void testNullsOnConstructor() {
94 assertThatThrownBy(() -> new ContextAlbumImpl(null, null, null))
95 .hasMessage("Context album definition may not be null");
97 assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), null, null))
98 .hasMessage("Distributor may not be null");
100 assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null))
101 .hasMessage("Album map may not be null");
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");
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);
117 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
118 true, AxArtifactKey.getNullKey());
120 assertThatThrownBy(() -> new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(),
121 new LinkedHashMap<String, Object>()))
122 .hasMessageContaining("could not initiate schema management for context album AxContextAlbum");
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>());
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());
134 assertThatThrownBy(() -> album.containsKey(null))
135 .hasMessage("null values are illegal on method parameter \"key\"");
136 assertEquals(false, album.containsKey("Key0"));
138 assertThatThrownBy(() -> album.containsValue(null))
139 .hasMessage("null values are illegal on method parameter \"value\"");
140 assertEquals(false, album.containsValue("some value"));
142 assertThatThrownBy(() -> album.get(null))
143 .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()");
145 assertThatThrownBy(() -> album.put(null, null))
146 .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()");
148 assertThatThrownBy(() -> album.put("KeyNull", null))
149 .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\""
152 AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
153 false, simpleStringSchema.getKey());
154 ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());
156 assertThatThrownBy(() -> albumRo.put("KeyReadOnly", "A value for a Read Only Album"))
157 .hasMessage("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
158 + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album");
160 Map<String, Object> putAllData = new LinkedHashMap<>();
161 putAllData.put("AllKey0", "vaue of AllKey0");
162 putAllData.put("AllKey1", "vaue of AllKey1");
163 putAllData.put("AllKey2", "vaue of AllKey2");
165 assertThatThrownBy(() -> albumRo.putAll(putAllData))
166 .hasMessage("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums");
168 assertThatThrownBy(() -> albumRo.remove("AllKey0"))
169 .hasMessage("album \"TestContextAlbum:0.0.1\" remove() not allowed "
170 + "on read only albums for key=\"AllKey0\"");
172 assertThatThrownBy(() -> album.remove(null))
173 .hasMessage("null values are illegal on method parameter \"keyID\"");
175 assertThatThrownBy(albumRo::clear)
176 .hasMessage("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums");
178 // The following locking tests pass because the locking protects access to Key0 across all
179 // copies of the distributed album whether the key exists or not
180 album.lockForReading("Key0");
181 assertEquals(null, album.get("Key0"));
182 album.unlockForReading("Key0");
183 assertEquals(null, album.get("Key0"));
185 album.lockForWriting("Key0");
186 assertEquals(null, album.get("Key0"));
187 album.unlockForWriting("Key0");
188 assertEquals(null, album.get("Key0"));
190 // Test write access, trivial test because Integration Test does
191 // a full test of access locking over albums in different JVMs
192 album.lockForWriting("Key0");
193 assertEquals(null, album.get("Key0"));
194 album.put("Key0", "value of Key0");
195 assertEquals("value of Key0", album.get("Key0"));
196 album.unlockForWriting("Key0");
197 assertEquals("value of Key0", album.get("Key0"));
199 // Test read access, trivial test because Integration Test does
200 // a full test of access locking over albums in different JVMs
201 album.lockForReading("Key0");
202 assertEquals("value of Key0", album.get("Key0"));
203 album.unlockForReading("Key0");
205 AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
206 AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
208 AxConcept[] userArtifactStack =
209 { somePolicyKey, somePolicyState };
210 album.setUserArtifactStack(userArtifactStack);
211 assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId());
212 assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId());
214 assertEquals(true, album.keySet().contains("Key0"));
215 assertEquals(true, album.values().contains("value of Key0"));
216 assertEquals(1, album.entrySet().size());
218 // The flush() operation fails because the distributor is not initialized with the album which
219 // is fine for unit test
220 assertThatThrownBy(album::flush)
221 .hasMessage("map flush failed, supplied map is null");
222 assertEquals(1, album.size());
223 assertEquals(false, album.isEmpty());
225 album.put("Key0", "New value of Key0");
226 assertEquals("New value of Key0", album.get("Key0"));
228 album.putAll(putAllData);
230 putAllData.put("AllKey3", null);
231 assertThatThrownBy(() -> album.putAll(putAllData))
232 .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key "
233 + "\"AllKey3\" for put()");
234 assertEquals("New value of Key0", album.remove("Key0"));
237 assertTrue(album.isEmpty());
239 ModelService.clear();
242 @SuppressWarnings("unlikely-arg-type")
244 public void testCompareToEqualsHash() throws ContextException {
245 AxContextSchemas schemas = new AxContextSchemas();
246 AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
247 "java.lang.Integer");
248 schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
249 AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
250 "JAVA", "java.lang.String");
251 schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
252 ModelService.registerModel(AxContextSchemas.class, schemas);
254 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
255 true, AxArtifactKey.getNullKey());
257 axContextAlbum.setItemSchema(simpleIntSchema.getKey());
258 Distributor distributor = new JvmLocalDistributor();
259 distributor.init(axContextAlbum.getKey());
260 ContextAlbumImpl album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());
262 assertNotEquals(0, album.hashCode());
264 assertEquals(0, album.compareTo(album));
265 assertEquals(1, album.compareTo(null));
267 assertEquals(album, album);
268 assertNotEquals(album, new DummyContextAlbumImpl());
270 ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor,
271 new LinkedHashMap<String, Object>());
272 assertEquals(album, otherAlbum);
274 otherAlbum.put("Key", 123);
275 assertNotEquals(album, otherAlbum);
277 assertThatThrownBy(() -> {
278 ContextAlbumImpl otherAlbumBad = new ContextAlbumImpl(axContextAlbum, distributor,
279 new LinkedHashMap<String, Object>());
280 otherAlbumBad.put("Key", "BadValue");
281 }).hasMessage("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": "
282 + "TestContextAlbum:0.0.1: object \"BadValue\" of class \"java.lang.String\" "
283 + "not compatible with class \"java.lang.Integer\"");
284 AxContextAlbum otherAxContextAlbum = new AxContextAlbum(new AxArtifactKey("OtherTestContextAlbum", "0.0.1"),
285 "Policy", true, AxArtifactKey.getNullKey());
287 otherAxContextAlbum.setItemSchema(simpleStringSchema.getKey());
288 otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<String, Object>());
289 assertNotEquals(album, otherAlbum);
291 assertThatThrownBy(album::flush)
292 .hasMessage("map flush failed, supplied map is null");
293 AxContextAlbums albums = new AxContextAlbums();
294 ModelService.registerModel(AxContextAlbums.class, albums);
295 albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum);
296 distributor.createContextAlbum(album.getKey());
300 ModelService.clear();