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.parameters.ContextParameters;
42 import org.onap.policy.apex.context.parameters.PersistorParameters;
43 import org.onap.policy.apex.context.test.concepts.TestContextDateItem;
44 import org.onap.policy.apex.context.test.concepts.TestContextDateLocaleItem;
45 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
46 import org.onap.policy.apex.context.test.concepts.TestContextTreeMapItem;
47 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
48 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
50 import org.onap.policy.apex.model.basicmodel.dao.ApexDao;
51 import org.onap.policy.apex.model.basicmodel.dao.ApexDaoFactory;
52 import org.onap.policy.apex.model.basicmodel.dao.DAOParameters;
53 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
54 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
55 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
58 * The Class TestContextInstantiation.
60 * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
62 public class TestPersistentContextInstantiation {
63 // Logger for this class
64 // private static final XLogger logger =
65 // XLoggerFactory.getXLogger(TestPersistentContextInstantiation.class);
67 private Connection connection;
70 public void setup() throws Exception {
71 Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
72 connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true");
76 public void teardown() throws Exception {
78 new File("derby.log").delete();
82 public void afterTest() throws IOException {}
85 public void testContextPersistentInstantiation() throws ApexModelException, IOException, ApexException {
87 final ContextParameters contextParameters = new ContextParameters();
88 contextParameters.setPersistorParameters(new PersistorParameters());
90 final AxArtifactKey distributorKey = new AxArtifactKey("AbstractDistributor", "0.0.1");
91 final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
93 final AxArtifactKey[] usedArtifactStackArray = {new AxArtifactKey("testC-top", "0.0.1"),
94 new AxArtifactKey("testC-next", "0.0.1"), new AxArtifactKey("testC-bot", "0.0.1")};
96 final DAOParameters daoParameters = new DAOParameters();
97 daoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao");
98 daoParameters.setPersistenceUnit("DAOTest");
99 final ApexDao apexDao = new ApexDaoFactory().createApexDao(daoParameters);
100 apexDao.init(daoParameters);
102 final AxContextModel someContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
104 // Context for Storing Map values
105 final AxContextAlbum axContextAlbumForMap =
106 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("MapContextAlbum", "0.0.1"));
107 apexDao.create(axContextAlbumForMap);
108 contextDistributor.registerModel(someContextModel);
109 final ContextAlbum contextAlbumForMap = contextDistributor.createContextAlbum(axContextAlbumForMap.getKey());
110 assertNotNull(contextAlbumForMap);
111 contextAlbumForMap.setUserArtifactStack(usedArtifactStackArray);
113 final Map<String, String> testMap = new HashMap<String, String>();
114 testMap.put("key", "This is a policy context string");
116 final Map<String, Object> valueMap0 = new HashMap<String, Object>();
117 valueMap0.put("TestPolicyContextItem000", new TestContextTreeMapItem(testMap));
119 contextAlbumForMap.putAll(valueMap0);
122 ((TestContextTreeMapItem) contextAlbumForMap.get("TestPolicyContextItem000")).getMapValue().get("key"),
123 "This is a policy context string");
125 contextAlbumForMap.flush();
127 // Context for Storing Date values
128 final AxContextAlbum axContextAlbumForDate =
129 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("DateContextAlbum", "0.0.1"));
130 apexDao.create(axContextAlbumForDate);
131 contextDistributor.registerModel(someContextModel);
132 final ContextAlbum contextAlbumForDate = contextDistributor.createContextAlbum(axContextAlbumForDate.getKey());
133 assertNotNull(contextAlbumForDate);
134 contextAlbumForDate.setUserArtifactStack(usedArtifactStackArray);
136 final TestContextDateItem testDate = new TestContextDateItem(new Date());
137 final TestContextDateLocaleItem tci00A = new TestContextDateLocaleItem();
138 tci00A.setDateValue(testDate);
139 tci00A.setTZValue(TimeZone.getTimeZone("Europe/Dublin").toString());
142 final Map<String, Object> valueMap1 = new HashMap<String, Object>();
143 valueMap1.put("TestPolicyContextItem00A", tci00A);
145 contextAlbumForDate.putAll(valueMap1);
147 assertEquals(((TestContextDateLocaleItem) contextAlbumForDate.get("TestPolicyContextItem00A")).getDateValue(),
149 assertEquals(((TestContextDateLocaleItem) contextAlbumForDate.get("TestPolicyContextItem00A")).getDST(), true);
151 contextAlbumForDate.flush();
153 // Context for Storing Long values
154 final AxContextAlbum axContextAlbumForLong =
155 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
156 apexDao.create(axContextAlbumForLong);
157 contextDistributor.registerModel(someContextModel);
158 final ContextAlbum contextAlbumForLong = contextDistributor.createContextAlbum(axContextAlbumForLong.getKey());
159 assertNotNull(contextAlbumForLong);
160 contextAlbumForLong.setUserArtifactStack(usedArtifactStackArray);
162 final Map<String, Object> valueMap2 = new HashMap<String, Object>();
163 valueMap2.put("TestPolicyContextItem0031", new TestContextLongItem(0xFFFFFFFFFFFFFFFFL));
164 valueMap2.put("TestPolicyContextItem0032", new TestContextLongItem(0xFFFFFFFFFFFFFFFEL));
165 valueMap2.put("TestPolicyContextItem0033", new TestContextLongItem(0xFFFFFFFFFFFFFFFDL));
166 valueMap2.put("TestPolicyContextItem0034", new TestContextLongItem(0xFFFFFFFFFFFFFFFCL));
167 valueMap2.put("TestPolicyContextItem0035", new TestContextLongItem(0xFFFFFFFFFFFFFFFBL));
169 contextAlbumForLong.putAll(valueMap2);
171 assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0031")).getLongValue(),
172 0xFFFFFFFFFFFFFFFFL);
173 assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0032")).getLongValue(),
174 0xFFFFFFFFFFFFFFFEL);
175 assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0033")).getLongValue(),
176 0xFFFFFFFFFFFFFFFDL);
177 assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0034")).getLongValue(),
178 0xFFFFFFFFFFFFFFFCL);
179 assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0035")).getLongValue(),
180 0xFFFFFFFFFFFFFFFBL);
182 contextAlbumForLong.flush();
183 contextDistributor.clear();