a75ad37f62d790534aa1cc89cc666b244bde2659
[policy/apex-pdp.git] /
1 /*-
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
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
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.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.core.engine.context;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.fail;
25
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;
40
41 /**
42  * Test the Apex engine internal context class.
43  */
44 public class ApexInternalContextTest {
45
46     private AxPolicyModel policyModel;
47     private AxPolicyModel newVersionPolicyModel;
48     private AxPolicyModel newPolicyModel;
49     private AxContextAlbum album;
50     private AxContextAlbum newAlbum;
51     private AxPolicyModel incompatiblePolicyModel;
52
53     /**
54      * Initialize parameters.
55      */
56     @Before
57     public void registerParameters() {
58         ParameterService.register(new SchemaParameters());
59         ParameterService.register(new DistributorParameters());
60         ParameterService.register(new LockManagerParameters());
61         ParameterService.register(new PersistorParameters());
62     }
63
64     /**
65      * Create policy model.
66      */
67     @Before
68     public void createPolicyModels() {
69         AxArtifactKey modelKey = new AxArtifactKey("PolicyModel:0.0.1");
70         policyModel = new AxPolicyModel(modelKey);
71
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);
75
76         AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1");
77         album = new AxContextAlbum(albumKey, "Policy", true, schemaKey);
78
79         policyModel.getAlbums().getAlbumsMap().put(albumKey, album);
80
81         AxArtifactKey newVersionModelKey = new AxArtifactKey("PolicyModel:0.0.2");
82         newVersionPolicyModel = new AxPolicyModel(newVersionModelKey);
83
84         newVersionPolicyModel.getSchemas().getSchemasMap().put(schemaKey, schema);
85         AxContextAlbum compatibleAlbum = new AxContextAlbum(albumKey, "Global", true, schemaKey);
86         newVersionPolicyModel.getAlbums().getAlbumsMap().put(albumKey, compatibleAlbum);
87
88         AxArtifactKey anotherAlbumKey = new AxArtifactKey("AnotherAlbum:0.0.1");
89         AxContextAlbum anotherAlbum = new AxContextAlbum(anotherAlbumKey, "Policy", true, schemaKey);
90
91         newVersionPolicyModel.getAlbums().getAlbumsMap().put(anotherAlbumKey, anotherAlbum);
92
93         AxArtifactKey incompatibleModelKey = new AxArtifactKey("IncompatiblePolicyModel:0.0.2");
94         incompatiblePolicyModel = new AxPolicyModel(incompatibleModelKey);
95
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);
99
100         AxContextAlbum incompatibleAlbum = new AxContextAlbum(albumKey, "Policy", true, incompatibleSchemaKey);
101         incompatiblePolicyModel.getAlbums().getAlbumsMap().put(albumKey, incompatibleAlbum);
102
103         AxArtifactKey newModelKey = new AxArtifactKey("NewPolicyModel:0.0.1");
104         newPolicyModel = new AxPolicyModel(newModelKey);
105
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);
109
110         AxArtifactKey newAlbumKey = new AxArtifactKey("NewAlbum:0.0.1");
111         newAlbum = new AxContextAlbum(newAlbumKey, "Policy", true, newSchemaKey);
112
113         newPolicyModel.getAlbums().getAlbumsMap().put(newAlbumKey, newAlbum);
114     }
115
116     /**
117      * Deregister parameters.
118      */
119     @After
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);
125     }
126
127     @Test
128     public void testAlbumInit() throws ContextException {
129         try {
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());
134         }
135
136         ApexInternalContext context = new ApexInternalContext(policyModel);
137
138         assertEquals(policyModel.getKey(), context.getKey());
139         assertEquals(1, context.getContextAlbums().size());
140
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());
148
149         context.clear();
150         assertEquals(1, context.getContextAlbums().size());
151
152         assertEquals("ApexInternalContext [contextAlbums={AxArtifactKey:(name=Album,version=0.0.1)",
153                         context.toString().substring(0, 76));
154     }
155
156     @Test
157     public void testAlbumUpdate() throws ContextException {
158         ApexInternalContext context = new ApexInternalContext(policyModel);
159
160         try {
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());
165         }
166
167         assertEquals(policyModel.getKey().getId(), context.getKey().getId());
168         assertEquals(1, context.getContextAlbums().size());
169
170         try {
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());
178         }
179
180         assertEquals(policyModel.getKey().getId(), context.getKey().getId());
181
182         context.update(newVersionPolicyModel);
183         assertEquals(newVersionPolicyModel.getKey().getId(), context.getKey().getId());
184
185         context.update(newPolicyModel);
186         assertEquals(newPolicyModel.getKey().getId(), context.getKey().getId());
187     }
188 }