19828f6e2b2e29d15b4f05f8308fb5a9b126cb59
[policy/apex-pdp.git] /
1 /*-
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
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
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;
41
42 /**
43  * Test the Apex engine internal context class.
44  */
45 public class ApexInternalContextTest {
46
47     private AxPolicyModel policyModel;
48     private AxPolicyModel newVersionPolicyModel;
49     private AxPolicyModel newPolicyModel;
50     private AxContextAlbum album;
51     private AxContextAlbum newAlbum;
52     private AxPolicyModel incompatiblePolicyModel;
53
54     /**
55      * Initialize parameters.
56      */
57     @Before
58     public void registerParameters() {
59         ParameterService.register(new SchemaParameters());
60         ParameterService.register(new DistributorParameters());
61         ParameterService.register(new LockManagerParameters());
62         ParameterService.register(new PersistorParameters());
63     }
64
65     /**
66      * Create policy model.
67      */
68     @Before
69     public void createPolicyModels() {
70         AxArtifactKey modelKey = new AxArtifactKey("PolicyModel:0.0.1");
71         policyModel = new AxPolicyModel(modelKey);
72
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);
76
77         AxArtifactKey albumKey = new AxArtifactKey("Album:0.0.1");
78         album = new AxContextAlbum(albumKey, "Policy", true, schemaKey);
79
80         policyModel.getAlbums().getAlbumsMap().put(albumKey, album);
81
82         AxArtifactKey newVersionModelKey = new AxArtifactKey("PolicyModel:0.0.2");
83         newVersionPolicyModel = new AxPolicyModel(newVersionModelKey);
84
85         newVersionPolicyModel.getSchemas().getSchemasMap().put(schemaKey, schema);
86         AxContextAlbum compatibleAlbum = new AxContextAlbum(albumKey, "Global", true, schemaKey);
87         newVersionPolicyModel.getAlbums().getAlbumsMap().put(albumKey, compatibleAlbum);
88
89         AxArtifactKey anotherAlbumKey = new AxArtifactKey("AnotherAlbum:0.0.1");
90         AxContextAlbum anotherAlbum = new AxContextAlbum(anotherAlbumKey, "Policy", true, schemaKey);
91
92         newVersionPolicyModel.getAlbums().getAlbumsMap().put(anotherAlbumKey, anotherAlbum);
93
94         AxArtifactKey incompatibleModelKey = new AxArtifactKey("IncompatiblePolicyModel:0.0.2");
95         incompatiblePolicyModel = new AxPolicyModel(incompatibleModelKey);
96
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);
100
101         AxContextAlbum incompatibleAlbum = new AxContextAlbum(albumKey, "Policy", true, incompatibleSchemaKey);
102         incompatiblePolicyModel.getAlbums().getAlbumsMap().put(albumKey, incompatibleAlbum);
103
104         AxArtifactKey newModelKey = new AxArtifactKey("NewPolicyModel:0.0.1");
105         newPolicyModel = new AxPolicyModel(newModelKey);
106
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);
110
111         AxArtifactKey newAlbumKey = new AxArtifactKey("NewAlbum:0.0.1");
112         newAlbum = new AxContextAlbum(newAlbumKey, "Policy", true, newSchemaKey);
113
114         newPolicyModel.getAlbums().getAlbumsMap().put(newAlbumKey, newAlbum);
115     }
116
117     /**
118      * Deregister parameters.
119      */
120     @After
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);
126     }
127
128     @Test
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);
133
134         assertEquals(policyModel.getKey(), context.getKey());
135         assertEquals(1, context.getContextAlbums().size());
136
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());
144
145         context.clear();
146         assertEquals(1, context.getContextAlbums().size());
147
148         assertEquals("ApexInternalContext [contextAlbums={AxArtifactKey:(name=Album,version=0.0.1)",
149                         context.toString().substring(0, 76));
150     }
151
152     @Test
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");
157
158         assertEquals(policyModel.getKey().getId(), context.getKey().getId());
159         assertEquals(1, context.getContextAlbums().size());
160
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");
165
166         assertEquals(policyModel.getKey().getId(), context.getKey().getId());
167
168         context.update(newVersionPolicyModel, false);
169         assertEquals(newVersionPolicyModel.getKey().getId(), context.getKey().getId());
170
171         context.update(newPolicyModel, true);
172         assertEquals(newPolicyModel.getKey().getId(), context.getKey().getId());
173     }
174 }