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
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.context.test.persistence;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
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;
33 import java.util.TimeZone;
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;
61 * The Class TestContextInstantiation.
63 * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
65 public class PersistentContextInstantiationTest {
66 // Logger for this class
67 // private static final XLogger logger =
68 // XLoggerFactory.getXLogger(TestPersistentContextInstantiation.class);
70 private Connection connection;
71 private SchemaParameters schemaParameters;
72 private ContextParameters contextParameters;
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");
81 public void teardown() throws Exception {
83 new File("derby.log").delete();
87 * Set up context for tests.
90 public void beforeTest() {
91 contextParameters = new ContextParameters();
93 contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
94 contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
95 contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
96 contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
98 ParameterService.register(contextParameters);
99 ParameterService.register(contextParameters.getDistributorParameters());
100 ParameterService.register(contextParameters.getLockManagerParameters());
101 ParameterService.register(contextParameters.getPersistorParameters());
103 schemaParameters = new SchemaParameters();
104 schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
105 schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());
107 ParameterService.register(schemaParameters);
111 * Clear down context for tests.
114 public void afterTest() {
115 ParameterService.deregister(schemaParameters);
117 ParameterService.deregister(contextParameters.getDistributorParameters());
118 ParameterService.deregister(contextParameters.getLockManagerParameters());
119 ParameterService.deregister(contextParameters.getPersistorParameters());
120 ParameterService.deregister(contextParameters);
124 public void testContextPersistentInstantiation() throws ApexModelException, IOException, ApexException {
126 final AxArtifactKey distributorKey = new AxArtifactKey("AbstractDistributor", "0.0.1");
127 final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
129 final AxArtifactKey[] usedArtifactStackArray = {
130 new AxArtifactKey("testC-top", "0.0.1"),
131 new AxArtifactKey("testC-next", "0.0.1"),
132 new AxArtifactKey("testC-bot", "0.0.1")
135 final DaoParameters DaoParameters = new DaoParameters();
136 DaoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao");
137 DaoParameters.setPersistenceUnit("DAOTest");
138 final ApexDao apexDao = new ApexDaoFactory().createApexDao(DaoParameters);
139 apexDao.init(DaoParameters);
141 final AxContextModel someContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
143 // Context for Storing Map values
144 final AxContextAlbum axContextAlbumForMap = someContextModel.getAlbums().getAlbumsMap()
145 .get(new AxArtifactKey("MapContextAlbum", "0.0.1"));
146 apexDao.create(axContextAlbumForMap);
147 contextDistributor.registerModel(someContextModel);
148 final ContextAlbum contextAlbumForMap = contextDistributor.createContextAlbum(axContextAlbumForMap.getKey());
149 assertNotNull(contextAlbumForMap);
150 contextAlbumForMap.setUserArtifactStack(usedArtifactStackArray);
152 final Map<String, String> testMap = new HashMap<String, String>();
153 testMap.put("key", "This is a policy context string");
155 final Map<String, Object> valueMap0 = new HashMap<String, Object>();
156 valueMap0.put("TestPolicyContextItem000", new TestContextTreeMapItem(testMap));
158 contextAlbumForMap.putAll(valueMap0);
160 assertEquals("This is a policy context string",
161 ((TestContextTreeMapItem) contextAlbumForMap.get("TestPolicyContextItem000")).getMapValue()
164 contextAlbumForMap.flush();
166 // Context for Storing Date values
167 final AxContextAlbum axContextAlbumForDate = someContextModel.getAlbums().getAlbumsMap()
168 .get(new AxArtifactKey("DateContextAlbum", "0.0.1"));
169 apexDao.create(axContextAlbumForDate);
170 contextDistributor.registerModel(someContextModel);
171 final ContextAlbum contextAlbumForDate = contextDistributor.createContextAlbum(axContextAlbumForDate.getKey());
172 assertNotNull(contextAlbumForDate);
173 contextAlbumForDate.setUserArtifactStack(usedArtifactStackArray);
175 final TestContextDateItem testDate = new TestContextDateItem(new Date());
176 final TestContextDateLocaleItem tci00A = new TestContextDateLocaleItem();
177 tci00A.setDateValue(testDate);
178 tci00A.setTzValue(TimeZone.getTimeZone("Europe/Dublin").toString());
181 final Map<String, Object> valueMap1 = new HashMap<String, Object>();
182 valueMap1.put("TestPolicyContextItem00A", tci00A);
184 contextAlbumForDate.putAll(valueMap1);
186 assertEquals(testDate, ((TestContextDateLocaleItem) contextAlbumForDate.get("TestPolicyContextItem00A"))
188 assertEquals(true, ((TestContextDateLocaleItem) contextAlbumForDate.get("TestPolicyContextItem00A")).getDst());
190 contextAlbumForDate.flush();
192 // Context for Storing Long values
193 final AxContextAlbum axContextAlbumForLong = someContextModel.getAlbums().getAlbumsMap()
194 .get(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
195 apexDao.create(axContextAlbumForLong);
196 contextDistributor.registerModel(someContextModel);
197 final ContextAlbum contextAlbumForLong = contextDistributor.createContextAlbum(axContextAlbumForLong.getKey());
198 assertNotNull(contextAlbumForLong);
199 contextAlbumForLong.setUserArtifactStack(usedArtifactStackArray);
201 final Map<String, Object> valueMap2 = new HashMap<String, Object>();
202 valueMap2.put("TestPolicyContextItem0031", new TestContextLongItem(0xFFFFFFFFFFFFFFFFL));
203 valueMap2.put("TestPolicyContextItem0032", new TestContextLongItem(0xFFFFFFFFFFFFFFFEL));
204 valueMap2.put("TestPolicyContextItem0033", new TestContextLongItem(0xFFFFFFFFFFFFFFFDL));
205 valueMap2.put("TestPolicyContextItem0034", new TestContextLongItem(0xFFFFFFFFFFFFFFFCL));
206 valueMap2.put("TestPolicyContextItem0035", new TestContextLongItem(0xFFFFFFFFFFFFFFFBL));
208 contextAlbumForLong.putAll(valueMap2);
210 assertEquals(0xFFFFFFFFFFFFFFFFL,
211 ((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0031")).getLongValue());
212 assertEquals(0xFFFFFFFFFFFFFFFEL,
213 ((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0032")).getLongValue());
214 assertEquals(0xFFFFFFFFFFFFFFFDL,
215 ((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0033")).getLongValue());
216 assertEquals(0xFFFFFFFFFFFFFFFCL,
217 ((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0034")).getLongValue());
218 assertEquals(0xFFFFFFFFFFFFFFFBL,
219 ((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0035")).getLongValue());
221 contextAlbumForLong.flush();
222 contextDistributor.clear();