2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-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.core.engine.context;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
27 import org.junit.After;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.policy.apex.context.ContextException;
31 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
32 import org.onap.policy.apex.context.parameters.DistributorParameters;
33 import org.onap.policy.apex.context.parameters.LockManagerParameters;
34 import org.onap.policy.apex.context.parameters.PersistorParameters;
35 import org.onap.policy.apex.context.parameters.SchemaParameters;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
39 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
40 import org.onap.policy.common.parameters.ParameterService;
43 * Test the Apex engine internal context class.
45 public class ApexInternalContextTest {
47 private AxPolicyModel policyModel;
48 private AxPolicyModel newVersionPolicyModel;
49 private AxPolicyModel newPolicyModel;
50 private AxContextAlbum album;
51 private AxContextAlbum newAlbum;
52 private AxPolicyModel incompatiblePolicyModel;
55 * Initialize parameters.
58 public void registerParameters() {
59 ParameterService.register(new SchemaParameters());
60 ParameterService.register(new DistributorParameters());
61 ParameterService.register(new LockManagerParameters());
62 ParameterService.register(new PersistorParameters());
66 * Create policy model.
69 public void createPolicyModels() {
70 AxArtifactKey modelKey = new AxArtifactKey("PolicyModel:0.0.1");
71 policyModel = new AxPolicyModel(modelKey);
73 AxArtifactKey schemaKey = new AxArtifactKey("Schema:0.0.1");
74 AxContextSchema schema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
75 policyModel.getSchemas().getSchemasMap().put(schemaKey, schema);
77 AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1");
78 album = new AxContextAlbum(albumKey, "Policy", true, schemaKey);
80 policyModel.getAlbums().getAlbumsMap().put(albumKey, album);
82 AxArtifactKey newVersionModelKey = new AxArtifactKey("PolicyModel:0.0.2");
83 newVersionPolicyModel = new AxPolicyModel(newVersionModelKey);
85 newVersionPolicyModel.getSchemas().getSchemasMap().put(schemaKey, schema);
86 AxContextAlbum compatibleAlbum = new AxContextAlbum(albumKey, "Global", true, schemaKey);
87 newVersionPolicyModel.getAlbums().getAlbumsMap().put(albumKey, compatibleAlbum);
89 AxArtifactKey anotherAlbumKey = new AxArtifactKey("AnotherAlbum:0.0.1");
90 AxContextAlbum anotherAlbum = new AxContextAlbum(anotherAlbumKey, "Policy", true, schemaKey);
92 newVersionPolicyModel.getAlbums().getAlbumsMap().put(anotherAlbumKey, anotherAlbum);
94 AxArtifactKey incompatibleModelKey = new AxArtifactKey("IncompatiblePolicyModel:0.0.2");
95 incompatiblePolicyModel = new AxPolicyModel(incompatibleModelKey);
97 AxArtifactKey incompatibleSchemaKey = new AxArtifactKey("IncompatibleSchema:0.0.1");
98 AxContextSchema incompatibleSchema = new AxContextSchema(incompatibleSchemaKey, "Java", "java.lang.Integer");
99 incompatiblePolicyModel.getSchemas().getSchemasMap().put(incompatibleSchemaKey, incompatibleSchema);
101 AxContextAlbum incompatibleAlbum = new AxContextAlbum(albumKey, "Policy", true, incompatibleSchemaKey);
102 incompatiblePolicyModel.getAlbums().getAlbumsMap().put(albumKey, incompatibleAlbum);
104 AxArtifactKey newModelKey = new AxArtifactKey("NewPolicyModel:0.0.1");
105 newPolicyModel = new AxPolicyModel(newModelKey);
107 AxArtifactKey newSchemaKey = new AxArtifactKey("NewSchema:0.0.1");
108 AxContextSchema newSchema = new AxContextSchema(newSchemaKey, "Java", "java.lang.Integer");
109 newPolicyModel.getSchemas().getSchemasMap().put(newSchemaKey, newSchema);
111 AxArtifactKey newAlbumKey = new AxArtifactKey("NewAlbum:0.0.1");
112 newAlbum = new AxContextAlbum(newAlbumKey, "Policy", true, newSchemaKey);
114 newPolicyModel.getAlbums().getAlbumsMap().put(newAlbumKey, newAlbum);
118 * Deregister parameters.
121 public void deregisterParameters() {
122 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
123 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
124 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
125 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
129 public void testAlbumInit() throws ContextException {
130 assertThatThrownBy(() -> new ApexInternalContext(null))
131 .hasMessage("internal context update failed, supplied model is null");
132 ApexInternalContext context = new ApexInternalContext(policyModel);
134 assertEquals(policyModel.getKey(), context.getKey());
135 assertEquals(1, context.getContextAlbums().size());
137 AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1");
138 assertEquals(album.getId(), context.get(albumKey).getKey().getId());
139 assertEquals(album.getId(), context.get(albumKey.getName()).getKey().getId());
140 assertEquals(album.getId(), context.get(albumKey.getName(), albumKey.getVersion()).getKey().getId());
141 assertEquals(album.getId(), context.getAll(albumKey.getName()).iterator().next().getKey().getId());
142 assertEquals(album.getId(),
143 context.getAll(albumKey.getName(), albumKey.getVersion()).iterator().next().getKey().getId());
146 assertEquals(1, context.getContextAlbums().size());
148 assertEquals("ApexInternalContext [contextAlbums={AxArtifactKey:(name=Album,version=0.0.1)",
149 context.toString().substring(0, 76));
153 public void testAlbumUpdate() throws ContextException {
154 ApexInternalContext context = new ApexInternalContext(policyModel);
155 assertThatThrownBy(() -> context.update(null, false))
156 .hasMessage("internal context update failed, supplied model is null");
158 assertEquals(policyModel.getKey().getId(), context.getKey().getId());
159 assertEquals(1, context.getContextAlbums().size());
161 assertThatThrownBy(() -> context.update(incompatiblePolicyModel, false)).hasMessage(
162 "internal context update failed on context album \"Album:0.0.1\" " + "in model \"PolicyModel:0.0.1\", "
163 + "schema \"Schema:0.0.1\" on existing context model does not equal "
164 + "schema \"IncompatibleSchema:0.0.1\" on incoming model");
166 assertEquals(policyModel.getKey().getId(), context.getKey().getId());
168 context.update(newVersionPolicyModel, false);
169 assertEquals(newVersionPolicyModel.getKey().getId(), context.getKey().getId());
171 context.update(newPolicyModel, true);
172 assertEquals(newPolicyModel.getKey().getId(), context.getKey().getId());