c8628935cf933a70828b6d492fc7c9b5548c33a3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-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.context.test.persistence;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.sql.Connection;
29 import java.sql.DriverManager;
30 import java.util.Date;
31 import java.util.HashMap;
32 import java.util.Map;
33 import java.util.TimeZone;
34
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.onap.policy.apex.context.ContextAlbum;
39 import org.onap.policy.apex.context.Distributor;
40 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
41 import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
42 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
43 import org.onap.policy.apex.context.parameters.ContextParameters;
44 import org.onap.policy.apex.context.parameters.SchemaParameters;
45 import org.onap.policy.apex.context.test.concepts.TestContextDateItem;
46 import org.onap.policy.apex.context.test.concepts.TestContextDateLocaleItem;
47 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
48 import org.onap.policy.apex.context.test.concepts.TestContextTreeMapItem;
49 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
50 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
51 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
52 import org.onap.policy.apex.model.basicmodel.dao.ApexDao;
53 import org.onap.policy.apex.model.basicmodel.dao.ApexDaoFactory;
54 import org.onap.policy.apex.model.basicmodel.dao.DaoParameters;
55 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
56 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
57 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
58 import org.onap.policy.common.parameters.ParameterService;
59
60 /**
61  * The Class TestContextInstantiation.
62  *
63  * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
64  */
65 public class TestPersistentContextInstantiation {
66     // Logger for this class
67     // private static final XLogger logger =
68     // XLoggerFactory.getXLogger(TestPersistentContextInstantiation.class);
69
70     private Connection connection;
71     private SchemaParameters schemaParameters;
72     private ContextParameters contextParameters;
73
74     @Before
75     public void setup() throws Exception {
76         Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
77         connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true");
78     }
79
80     @After
81     public void teardown() throws Exception {
82         connection.close();
83         new File("derby.log").delete();
84     }
85
86     @Before
87     public void beforeTest() {
88         contextParameters = new ContextParameters();
89
90         contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
91         contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
92         contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
93         contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
94
95         ParameterService.register(contextParameters);
96         ParameterService.register(contextParameters.getDistributorParameters());
97         ParameterService.register(contextParameters.getLockManagerParameters());
98         ParameterService.register(contextParameters.getPersistorParameters());
99         
100         schemaParameters = new SchemaParameters();
101         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
102         schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
103
104         ParameterService.register(schemaParameters);
105     }
106
107     @After
108     public void afterTest() {
109         ParameterService.deregister(schemaParameters);
110
111         ParameterService.deregister(contextParameters.getDistributorParameters());
112         ParameterService.deregister(contextParameters.getLockManagerParameters());
113         ParameterService.deregister(contextParameters.getPersistorParameters());
114         ParameterService.deregister(contextParameters);
115     }
116
117     @Test
118     public void testContextPersistentInstantiation() throws ApexModelException, IOException, ApexException {
119
120         final AxArtifactKey distributorKey = new AxArtifactKey("AbstractDistributor", "0.0.1");
121         final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
122
123         final AxArtifactKey[] usedArtifactStackArray = {new AxArtifactKey("testC-top", "0.0.1"),
124             new AxArtifactKey("testC-next", "0.0.1"), new AxArtifactKey("testC-bot", "0.0.1")};
125
126         final DaoParameters DaoParameters = new DaoParameters();
127         DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao");
128         DaoParameters.setPersistenceUnit("DAOTest");
129         final ApexDao apexDao = new ApexDaoFactory().createApexDao(DaoParameters);
130         apexDao.init(DaoParameters);
131
132         final AxContextModel someContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
133
134         // Context for Storing Map values
135         final AxContextAlbum axContextAlbumForMap =
136                 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("MapContextAlbum", "0.0.1"));
137         apexDao.create(axContextAlbumForMap);
138         contextDistributor.registerModel(someContextModel);
139         final ContextAlbum contextAlbumForMap = contextDistributor.createContextAlbum(axContextAlbumForMap.getKey());
140         assertNotNull(contextAlbumForMap);
141         contextAlbumForMap.setUserArtifactStack(usedArtifactStackArray);
142
143         final Map<String, String> testMap = new HashMap<String, String>();
144         testMap.put("key", "This is a policy context string");
145
146         final Map<String, Object> valueMap0 = new HashMap<String, Object>();
147         valueMap0.put("TestPolicyContextItem000", new TestContextTreeMapItem(testMap));
148
149         contextAlbumForMap.putAll(valueMap0);
150
151         assertEquals(
152                 ((TestContextTreeMapItem) contextAlbumForMap.get("TestPolicyContextItem000")).getMapValue().get("key"),
153                 "This is a policy context string");
154
155         contextAlbumForMap.flush();
156
157         // Context for Storing Date values
158         final AxContextAlbum axContextAlbumForDate =
159                 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("DateContextAlbum", "0.0.1"));
160         apexDao.create(axContextAlbumForDate);
161         contextDistributor.registerModel(someContextModel);
162         final ContextAlbum contextAlbumForDate = contextDistributor.createContextAlbum(axContextAlbumForDate.getKey());
163         assertNotNull(contextAlbumForDate);
164         contextAlbumForDate.setUserArtifactStack(usedArtifactStackArray);
165
166         final TestContextDateItem testDate = new TestContextDateItem(new Date());
167         final TestContextDateLocaleItem tci00A = new TestContextDateLocaleItem();
168         tci00A.setDateValue(testDate);
169         tci00A.setTZValue(TimeZone.getTimeZone("Europe/Dublin").toString());
170         tci00A.setDST(true);
171
172         final Map<String, Object> valueMap1 = new HashMap<String, Object>();
173         valueMap1.put("TestPolicyContextItem00A", tci00A);
174
175         contextAlbumForDate.putAll(valueMap1);
176
177         assertEquals(((TestContextDateLocaleItem) contextAlbumForDate.get("TestPolicyContextItem00A")).getDateValue(),
178                 testDate);
179         assertEquals(((TestContextDateLocaleItem) contextAlbumForDate.get("TestPolicyContextItem00A")).getDST(), true);
180
181         contextAlbumForDate.flush();
182
183         // Context for Storing Long values
184         final AxContextAlbum axContextAlbumForLong =
185                 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
186         apexDao.create(axContextAlbumForLong);
187         contextDistributor.registerModel(someContextModel);
188         final ContextAlbum contextAlbumForLong = contextDistributor.createContextAlbum(axContextAlbumForLong.getKey());
189         assertNotNull(contextAlbumForLong);
190         contextAlbumForLong.setUserArtifactStack(usedArtifactStackArray);
191
192         final Map<String, Object> valueMap2 = new HashMap<String, Object>();
193         valueMap2.put("TestPolicyContextItem0031", new TestContextLongItem(0xFFFFFFFFFFFFFFFFL));
194         valueMap2.put("TestPolicyContextItem0032", new TestContextLongItem(0xFFFFFFFFFFFFFFFEL));
195         valueMap2.put("TestPolicyContextItem0033", new TestContextLongItem(0xFFFFFFFFFFFFFFFDL));
196         valueMap2.put("TestPolicyContextItem0034", new TestContextLongItem(0xFFFFFFFFFFFFFFFCL));
197         valueMap2.put("TestPolicyContextItem0035", new TestContextLongItem(0xFFFFFFFFFFFFFFFBL));
198
199         contextAlbumForLong.putAll(valueMap2);
200
201         assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0031")).getLongValue(),
202                 0xFFFFFFFFFFFFFFFFL);
203         assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0032")).getLongValue(),
204                 0xFFFFFFFFFFFFFFFEL);
205         assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0033")).getLongValue(),
206                 0xFFFFFFFFFFFFFFFDL);
207         assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0034")).getLongValue(),
208                 0xFFFFFFFFFFFFFFFCL);
209         assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0035")).getLongValue(),
210                 0xFFFFFFFFFFFFFFFBL);
211
212         contextAlbumForLong.flush();
213         contextDistributor.clear();
214     }
215 }