2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.core.engine.context;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.apex.context.ContextException;
30 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
31 import org.onap.policy.apex.context.parameters.DistributorParameters;
32 import org.onap.policy.apex.context.parameters.LockManagerParameters;
33 import org.onap.policy.apex.context.parameters.PersistorParameters;
34 import org.onap.policy.apex.context.parameters.SchemaParameters;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
38 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
39 import org.onap.policy.common.parameters.ParameterService;
42 * Test the Apex engine internal context class.
44 public class ApexInternalContextTest {
46 private AxPolicyModel policyModel;
47 private AxPolicyModel newVersionPolicyModel;
48 private AxPolicyModel newPolicyModel;
49 private AxContextAlbum album;
50 private AxContextAlbum newAlbum;
51 private AxPolicyModel incompatiblePolicyModel;
54 * Initialize parameters.
57 public void registerParameters() {
58 ParameterService.register(new SchemaParameters());
59 ParameterService.register(new DistributorParameters());
60 ParameterService.register(new LockManagerParameters());
61 ParameterService.register(new PersistorParameters());
65 * Create policy model.
68 public void createPolicyModels() {
69 AxArtifactKey modelKey = new AxArtifactKey("PolicyModel:0.0.1");
70 policyModel = new AxPolicyModel(modelKey);
72 AxArtifactKey schemaKey = new AxArtifactKey("Schema:0.0.1");
73 AxContextSchema schema = new AxContextSchema(schemaKey, "Java", "java.lang.String");
74 policyModel.getSchemas().getSchemasMap().put(schemaKey, schema);
76 AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1");
77 album = new AxContextAlbum(albumKey, "Policy", true, schemaKey);
79 policyModel.getAlbums().getAlbumsMap().put(albumKey, album);
81 AxArtifactKey newVersionModelKey = new AxArtifactKey("PolicyModel:0.0.2");
82 newVersionPolicyModel = new AxPolicyModel(newVersionModelKey);
84 newVersionPolicyModel.getSchemas().getSchemasMap().put(schemaKey, schema);
85 AxContextAlbum compatibleAlbum = new AxContextAlbum(albumKey, "Global", true, schemaKey);
86 newVersionPolicyModel.getAlbums().getAlbumsMap().put(albumKey, compatibleAlbum);
88 AxArtifactKey anotherAlbumKey = new AxArtifactKey("AnotherAlbum:0.0.1");
89 AxContextAlbum anotherAlbum = new AxContextAlbum(anotherAlbumKey, "Policy", true, schemaKey);
91 newVersionPolicyModel.getAlbums().getAlbumsMap().put(anotherAlbumKey, anotherAlbum);
93 AxArtifactKey incompatibleModelKey = new AxArtifactKey("IncompatiblePolicyModel:0.0.2");
94 incompatiblePolicyModel = new AxPolicyModel(incompatibleModelKey);
96 AxArtifactKey incompatibleSchemaKey = new AxArtifactKey("IncompatibleSchema:0.0.1");
97 AxContextSchema incompatibleSchema = new AxContextSchema(incompatibleSchemaKey, "Java", "java.lang.Integer");
98 incompatiblePolicyModel.getSchemas().getSchemasMap().put(incompatibleSchemaKey, incompatibleSchema);
100 AxContextAlbum incompatibleAlbum = new AxContextAlbum(albumKey, "Policy", true, incompatibleSchemaKey);
101 incompatiblePolicyModel.getAlbums().getAlbumsMap().put(albumKey, incompatibleAlbum);
103 AxArtifactKey newModelKey = new AxArtifactKey("NewPolicyModel:0.0.1");
104 newPolicyModel = new AxPolicyModel(newModelKey);
106 AxArtifactKey newSchemaKey = new AxArtifactKey("NewSchema:0.0.1");
107 AxContextSchema newSchema = new AxContextSchema(newSchemaKey, "Java", "java.lang.Integer");
108 newPolicyModel.getSchemas().getSchemasMap().put(newSchemaKey, newSchema);
110 AxArtifactKey newAlbumKey = new AxArtifactKey("NewAlbum:0.0.1");
111 newAlbum = new AxContextAlbum(newAlbumKey, "Policy", true, newSchemaKey);
113 newPolicyModel.getAlbums().getAlbumsMap().put(newAlbumKey, newAlbum);
117 * Deregister parameters.
120 public void deregisterParameters() {
121 ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
122 ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
123 ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
124 ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
128 public void testAlbumInit() throws ContextException {
130 new ApexInternalContext(null);
131 fail("test should throw an exception");
132 } catch (ContextException ce) {
133 assertEquals("internal context update failed, supplied model is null", ce.getMessage());
136 ApexInternalContext context = new ApexInternalContext(policyModel);
138 assertEquals(policyModel.getKey(), context.getKey());
139 assertEquals(1, context.getContextAlbums().size());
141 AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1");
142 assertEquals(album.getId(), context.get(albumKey).getKey().getId());
143 assertEquals(album.getId(), context.get(albumKey.getName()).getKey().getId());
144 assertEquals(album.getId(), context.get(albumKey.getName(), albumKey.getVersion()).getKey().getId());
145 assertEquals(album.getId(), context.getAll(albumKey.getName()).iterator().next().getKey().getId());
146 assertEquals(album.getId(),
147 context.getAll(albumKey.getName(), albumKey.getVersion()).iterator().next().getKey().getId());
150 assertEquals(1, context.getContextAlbums().size());
152 assertEquals("ApexInternalContext [contextAlbums={AxArtifactKey:(name=Album,version=0.0.1)",
153 context.toString().substring(0, 76));
157 public void testAlbumUpdate() throws ContextException {
158 ApexInternalContext context = new ApexInternalContext(policyModel);
161 context.update(null);
162 fail("test should throw an exception");
163 } catch (ContextException ce) {
164 assertEquals("internal context update failed, supplied model is null", ce.getMessage());
167 assertEquals(policyModel.getKey().getId(), context.getKey().getId());
168 assertEquals(1, context.getContextAlbums().size());
171 context.update(incompatiblePolicyModel);
172 fail("test should throw an exception here");
173 } catch (ContextException ce) {
174 assertEquals("internal context update failed on context album \"Album:0.0.1\" "
175 + "in model \"PolicyModel:0.0.1\", "
176 + "schema \"Schema:0.0.1\" on existing context model does not equal "
177 + "schema \"IncompatibleSchema:0.0.1\" on incoming model", ce.getMessage());
180 assertEquals(policyModel.getKey().getId(), context.getKey().getId());
182 context.update(newVersionPolicyModel);
183 assertEquals(newVersionPolicyModel.getKey().getId(), context.getKey().getId());
185 context.update(newPolicyModel);
186 assertEquals(newPolicyModel.getKey().getId(), context.getKey().getId());