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());
121 () -> new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), 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\"" + " for put()");
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>());
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");
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");
164 assertThatThrownBy(() -> albumRo.putAll(putAllData))
165 .hasMessage("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums");
167 assertThatThrownBy(() -> albumRo.remove("AllKey0")).hasMessage(
168 "album \"TestContextAlbum:0.0.1\" remove() not allowed " + "on read only albums for key=\"AllKey0\"");
170 assertThatThrownBy(() -> album.remove(null))
171 .hasMessage("null values are illegal on method parameter \"keyID\"");
173 assertThatThrownBy(albumRo::clear)
174 .hasMessage("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums");
176 // The following locking tests pass because the locking protects access to Key0
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"));
184 album.lockForWriting("Key0");
185 assertEquals(null, album.get("Key0"));
186 album.unlockForWriting("Key0");
187 assertEquals(null, album.get("Key0"));
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"));
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");
204 AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
205 AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");
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());
212 assertEquals(true, album.keySet().contains("Key0"));
213 assertEquals(true, album.values().contains("value of Key0"));
214 assertEquals(1, album.entrySet().size());
216 // The flush() operation fails because the distributor is not initialized with
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());
223 album.put("Key0", "New value of Key0");
224 assertEquals("New value of Key0", album.get("Key0"));
226 album.putAll(putAllData);
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"));
234 assertTrue(album.isEmpty());
236 ModelService.clear();
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);
250 AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
251 true, AxArtifactKey.getNullKey());
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>());
258 assertNotEquals(0, album.hashCode());
260 assertEquals(0, album.compareTo(album));
261 assertEquals(1, album.compareTo(null));
263 assertEquals(album, album);
264 assertNotEquals(album, new DummyContextAlbumImpl());
266 ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor,
267 new LinkedHashMap<String, Object>());
268 assertEquals(album, otherAlbum);
270 otherAlbum.put("Key", 123);
271 assertNotEquals(album, otherAlbum);
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());
283 otherAxContextAlbum.setItemSchema(simpleStringSchema.getKey());
284 otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<String, Object>());
285 assertNotEquals(album, otherAlbum);
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());
295 ModelService.clear();