2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019 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;
26 import static org.junit.Assert.fail;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.apex.context.ContextException;
32 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
33 import org.onap.policy.apex.context.parameters.DistributorParameters;
34 import org.onap.policy.apex.context.parameters.LockManagerParameters;
35 import org.onap.policy.apex.context.parameters.PersistorParameters;
36 import org.onap.policy.apex.context.parameters.SchemaParameters;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
40 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
41 import org.onap.policy.common.parameters.ParameterService;
44 * Test the Apex engine internal context class.
46 public class ApexInternalContextTest {
48 private AxPolicyModel policyModel;
49 private AxPolicyModel newVersionPolicyModel;
50 private AxPolicyModel newPolicyModel;
51 private AxContextAlbum album;
52 private AxContextAlbum newAlbum;
53 private AxPolicyModel incompatiblePolicyModel;
56 * Initialize parameters.
59 public void registerParameters() {
60 ParameterService.register(new SchemaParameters());
61 ParameterService.register(new DistributorParameters());
62 ParameterService.register(new LockManagerParameters());
63 ParameterService.register(new PersistorParameters());
67 * Create policy model.
70 public void createPolicyModels() {
71 AxArtifactKey modelKey = new AxArtifactKey("PolicyModel:0.0.1");
72 policyModel = new AxPolicyModel(modelKey);
74 AxArtifactKey schemaKey = new AxArtifactKey("Schema:0.0.1");
75 AxContextSchema schema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
76 policyModel.getSchemas().getSchemasMap().put(schemaKey, schema);
78 AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1");
79 album = new AxContextAlbum(albumKey, "Policy", true, schemaKey);
81 policyModel.getAlbums().getAlbumsMap().put(albumKey, album);
83 AxArtifactKey newVersionModelKey = new AxArtifactKey("PolicyModel:0.0.2");
84 newVersionPolicyModel = new AxPolicyModel(newVersionModelKey);
86 newVersionPolicyModel.getSchemas().getSchemasMap().put(schemaKey, schema);
87 AxContextAlbum compatibleAlbum = new AxContextAlbum(albumKey, "Global", true, schemaKey);
88 newVersionPolicyModel.getAlbums().getAlbumsMap().put(albumKey, compatibleAlbum);
90 AxArtifactKey anotherAlbumKey = new AxArtifactKey("AnotherAlbum:0.0.1");
91 AxContextAlbum anotherAlbum = new AxContextAlbum(anotherAlbumKey, "Policy", true, schemaKey);
93 newVersionPolicyModel.getAlbums().getAlbumsMap().put(anotherAlbumKey, anotherAlbum);
95 AxArtifactKey incompatibleModelKey = new AxArtifactKey("IncompatiblePolicyModel:0.0.2");
96 incompatiblePolicyModel = new AxPolicyModel(incompatibleModelKey);
98 AxArtifactKey incompatibleSchemaKey = new AxArtifactKey("IncompatibleSchema:0.0.1");
99 AxContextSchema incompatibleSchema = new AxContextSchema(incompatibleSchemaKey, "Java", "java.lang.Integer");
100 incompatiblePolicyModel.getSchemas().getSchemasMap().put(incompatibleSchemaKey, incompatibleSchema);
102 AxContextAlbum incompatibleAlbum = new AxContextAlbum(albumKey, "Policy", true, incompatibleSchemaKey);
103 incompatiblePolicyModel.getAlbums().getAlbumsMap().put(albumKey, incompatibleAlbum);
105 AxArtifactKey newModelKey = new AxArtifactKey("NewPolicyModel:0.0.1");
106 newPolicyModel = new AxPolicyModel(newModelKey);
108 AxArtifactKey newSchemaKey = new AxArtifactKey("NewSchema:0.0.1");
109 AxContextSchema newSchema = new AxContextSchema(newSchemaKey, "Java", "java.lang.Integer");
110 newPolicyModel.getSchemas().getSchemasMap().put(newSchemaKey, newSchema);
112 AxArtifactKey newAlbumKey = new AxArtifactKey("NewAlbum:0.0.1");
113 newAlbum = new AxContextAlbum(newAlbumKey, "Policy", true, newSchemaKey);
115 newPolicyModel.getAlbums().getAlbumsMap().put(newAlbumKey, newAlbum);
119 * Deregister parameters.
122 public void deregisterParameters() {
123 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
124 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
125 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
126 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
130 public void testAlbumInit() throws ContextException {
132 new ApexInternalContext(null);
133 fail("test should throw an exception");
134 } catch (ContextException ce) {
135 assertEquals("internal context update failed, supplied model is null", ce.getMessage());
138 ApexInternalContext context = new ApexInternalContext(policyModel);
140 assertEquals(policyModel.getKey(), context.getKey());
141 assertEquals(1, context.getContextAlbums().size());
143 AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1");
144 assertEquals(album.getId(), context.get(albumKey).getKey().getId());
145 assertEquals(album.getId(), context.get(albumKey.getName()).getKey().getId());
146 assertEquals(album.getId(), context.get(albumKey.getName(), albumKey.getVersion()).getKey().getId());
147 assertEquals(album.getId(), context.getAll(albumKey.getName()).iterator().next().getKey().getId());
148 assertEquals(album.getId(),
149 context.getAll(albumKey.getName(), albumKey.getVersion()).iterator().next().getKey().getId());
152 assertEquals(1, context.getContextAlbums().size());
154 assertEquals("ApexInternalContext [contextAlbums={AxArtifactKey:(name=Album,version=0.0.1)",
155 context.toString().substring(0, 76));
159 public void testAlbumUpdate() throws ContextException {
160 ApexInternalContext context = new ApexInternalContext(policyModel);
161 assertThatThrownBy(() -> context.update(null, false))
162 .hasMessage("internal context update failed, supplied model is null");
164 assertEquals(policyModel.getKey().getId(), context.getKey().getId());
165 assertEquals(1, context.getContextAlbums().size());
167 assertThatThrownBy(() -> context.update(incompatiblePolicyModel, false)).hasMessage(
168 "internal context update failed on context album \"Album:0.0.1\" " + "in model \"PolicyModel:0.0.1\", "
169 + "schema \"Schema:0.0.1\" on existing context model does not equal "
170 + "schema \"IncompatibleSchema:0.0.1\" on incoming model");
172 assertEquals(policyModel.getKey().getId(), context.getKey().getId());
174 context.update(newVersionPolicyModel, false);
175 assertEquals(newVersionPolicyModel.getKey().getId(), context.getKey().getId());
177 context.update(newPolicyModel, true);
178 assertEquals(newPolicyModel.getKey().getId(), context.getKey().getId());