3d0d377bf63fbe6e4e74964613a8a19dd81659fc
[policy/apex-pdp.git] /
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.core.engine.context;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
27
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;
42
43 /**
44  * Test the Apex engine internal context class.
45  */
46 public class ApexInternalContextTest {
47
48     private AxPolicyModel policyModel;
49     private AxPolicyModel newVersionPolicyModel;
50     private AxPolicyModel newPolicyModel;
51     private AxContextAlbum album;
52     private AxContextAlbum newAlbum;
53     private AxPolicyModel incompatiblePolicyModel;
54
55     /**
56      * Initialize parameters.
57      */
58     @Before
59     public void registerParameters() {
60         ParameterService.register(new SchemaParameters());
61         ParameterService.register(new DistributorParameters());
62         ParameterService.register(new LockManagerParameters());
63         ParameterService.register(new PersistorParameters());
64     }
65
66     /**
67      * Create policy model.
68      */
69     @Before
70     public void createPolicyModels() {
71         AxArtifactKey modelKey = new AxArtifactKey("PolicyModel:0.0.1");
72         policyModel = new AxPolicyModel(modelKey);
73
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);
77
78         AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1");
79         album = new AxContextAlbum(albumKey, "Policy", true, schemaKey);
80
81         policyModel.getAlbums().getAlbumsMap().put(albumKey, album);
82
83         AxArtifactKey newVersionModelKey = new AxArtifactKey("PolicyModel:0.0.2");
84         newVersionPolicyModel = new AxPolicyModel(newVersionModelKey);
85
86         newVersionPolicyModel.getSchemas().getSchemasMap().put(schemaKey, schema);
87         AxContextAlbum compatibleAlbum = new AxContextAlbum(albumKey, "Global", true, schemaKey);
88         newVersionPolicyModel.getAlbums().getAlbumsMap().put(albumKey, compatibleAlbum);
89
90         AxArtifactKey anotherAlbumKey = new AxArtifactKey("AnotherAlbum:0.0.1");
91         AxContextAlbum anotherAlbum = new AxContextAlbum(anotherAlbumKey, "Policy", true, schemaKey);
92
93         newVersionPolicyModel.getAlbums().getAlbumsMap().put(anotherAlbumKey, anotherAlbum);
94
95         AxArtifactKey incompatibleModelKey = new AxArtifactKey("IncompatiblePolicyModel:0.0.2");
96         incompatiblePolicyModel = new AxPolicyModel(incompatibleModelKey);
97
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);
101
102         AxContextAlbum incompatibleAlbum = new AxContextAlbum(albumKey, "Policy", true, incompatibleSchemaKey);
103         incompatiblePolicyModel.getAlbums().getAlbumsMap().put(albumKey, incompatibleAlbum);
104
105         AxArtifactKey newModelKey = new AxArtifactKey("NewPolicyModel:0.0.1");
106         newPolicyModel = new AxPolicyModel(newModelKey);
107
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);
111
112         AxArtifactKey newAlbumKey = new AxArtifactKey("NewAlbum:0.0.1");
113         newAlbum = new AxContextAlbum(newAlbumKey, "Policy", true, newSchemaKey);
114
115         newPolicyModel.getAlbums().getAlbumsMap().put(newAlbumKey, newAlbum);
116     }
117
118     /**
119      * Deregister parameters.
120      */
121     @After
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);
127     }
128
129     @Test
130     public void testAlbumInit() throws ContextException {
131         try {
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());
136         }
137
138         ApexInternalContext context = new ApexInternalContext(policyModel);
139
140         assertEquals(policyModel.getKey(), context.getKey());
141         assertEquals(1, context.getContextAlbums().size());
142
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());
150
151         context.clear();
152         assertEquals(1, context.getContextAlbums().size());
153
154         assertEquals("ApexInternalContext [contextAlbums={AxArtifactKey:(name=Album,version=0.0.1)",
155                         context.toString().substring(0, 76));
156     }
157
158     @Test
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");
163
164         assertEquals(policyModel.getKey().getId(), context.getKey().getId());
165         assertEquals(1, context.getContextAlbums().size());
166
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");
171
172         assertEquals(policyModel.getKey().getId(), context.getKey().getId());
173
174         context.update(newVersionPolicyModel, false);
175         assertEquals(newVersionPolicyModel.getKey().getId(), context.getKey().getId());
176
177         context.update(newPolicyModel, true);
178         assertEquals(newPolicyModel.getKey().getId(), context.getKey().getId());
179     }
180 }