a05928591ca4c3bec70fcb858f8bd6f63342642a
[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.parameters.ContextParameters;
42 import org.onap.policy.apex.context.parameters.PersistorParameters;
43 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
44 import org.onap.policy.apex.context.test.concepts.TestContextDateItem;
45 import org.onap.policy.apex.context.test.concepts.TestContextDateLocaleItem;
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;
56
57 /**
58  * The Class TestContextInstantiation.
59  *
60  * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
61  */
62 public class TestPersistentContextInstantiation {
63     // Logger for this class
64     // private static final XLogger logger =
65     // XLoggerFactory.getXLogger(TestPersistentContextInstantiation.class);
66
67     private Connection connection;
68
69     @Before
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");
73     }
74
75     @After
76     public void teardown() throws Exception {
77         connection.close();
78         new File("derby.log").delete();
79     }
80
81     @After
82     public void afterTest() throws IOException {}
83
84     @Test
85     public void testContextPersistentInstantiation() throws ApexModelException, IOException, ApexException {
86
87         final ContextParameters contextParameters = new ContextParameters();
88         contextParameters.setPersistorParameters(new PersistorParameters());
89
90         final AxArtifactKey distributorKey = new AxArtifactKey("AbstractDistributor", "0.0.1");
91         final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
92
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")};
95
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);
101
102         final AxContextModel someContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
103
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);
112
113         final Map<String, String> testMap = new HashMap<String, String>();
114         testMap.put("key", "This is a policy context string");
115
116         final Map<String, Object> valueMap0 = new HashMap<String, Object>();
117         valueMap0.put("TestPolicyContextItem000", new TestContextTreeMapItem(testMap));
118
119         contextAlbumForMap.putAll(valueMap0);
120
121         assertEquals(((TestContextTreeMapItem) contextAlbumForMap.get("TestPolicyContextItem000")).getMapValue().get("key"),
122                 "This is a policy context string");
123
124         contextAlbumForMap.flush();
125
126         // Context for Storing Date values
127         final AxContextAlbum axContextAlbumForDate =
128                 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("DateContextAlbum", "0.0.1"));
129         apexDao.create(axContextAlbumForDate);
130         contextDistributor.registerModel(someContextModel);
131         final ContextAlbum contextAlbumForDate = contextDistributor.createContextAlbum(axContextAlbumForDate.getKey());
132         assertNotNull(contextAlbumForDate);
133         contextAlbumForDate.setUserArtifactStack(usedArtifactStackArray);
134
135         final TestContextDateItem testDate = new TestContextDateItem(new Date());
136         final TestContextDateLocaleItem tci00A = new TestContextDateLocaleItem();
137         tci00A.setDateValue(testDate);
138         tci00A.setTZValue(TimeZone.getTimeZone("Europe/Dublin").toString());
139         tci00A.setDST(true);
140
141         final Map<String, Object> valueMap1 = new HashMap<String, Object>();
142         valueMap1.put("TestPolicyContextItem00A", tci00A);
143
144         contextAlbumForDate.putAll(valueMap1);
145
146         assertEquals(((TestContextDateLocaleItem) contextAlbumForDate.get("TestPolicyContextItem00A")).getDateValue(),
147                 testDate);
148         assertEquals(((TestContextDateLocaleItem) contextAlbumForDate.get("TestPolicyContextItem00A")).getDST(), true);
149
150         contextAlbumForDate.flush();
151
152         // Context for Storing Long values
153         final AxContextAlbum axContextAlbumForLong =
154                 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
155         apexDao.create(axContextAlbumForLong);
156         contextDistributor.registerModel(someContextModel);
157         final ContextAlbum contextAlbumForLong = contextDistributor.createContextAlbum(axContextAlbumForLong.getKey());
158         assertNotNull(contextAlbumForLong);
159         contextAlbumForLong.setUserArtifactStack(usedArtifactStackArray);
160
161         final Map<String, Object> valueMap2 = new HashMap<String, Object>();
162         valueMap2.put("TestPolicyContextItem0031", new TestContextLongItem(0xFFFFFFFFFFFFFFFFL));
163         valueMap2.put("TestPolicyContextItem0032", new TestContextLongItem(0xFFFFFFFFFFFFFFFEL));
164         valueMap2.put("TestPolicyContextItem0033", new TestContextLongItem(0xFFFFFFFFFFFFFFFDL));
165         valueMap2.put("TestPolicyContextItem0034", new TestContextLongItem(0xFFFFFFFFFFFFFFFCL));
166         valueMap2.put("TestPolicyContextItem0035", new TestContextLongItem(0xFFFFFFFFFFFFFFFBL));
167
168         contextAlbumForLong.putAll(valueMap2);
169
170         assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0031")).getLongValue(),
171                 0xFFFFFFFFFFFFFFFFL);
172         assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0032")).getLongValue(),
173                 0xFFFFFFFFFFFFFFFEL);
174         assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0033")).getLongValue(),
175                 0xFFFFFFFFFFFFFFFDL);
176         assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0034")).getLongValue(),
177                 0xFFFFFFFFFFFFFFFCL);
178         assertEquals(((TestContextLongItem) contextAlbumForLong.get("TestPolicyContextItem0035")).getLongValue(),
179                 0xFFFFFFFFFFFFFFFBL);
180
181         contextAlbumForLong.flush();
182         contextDistributor.clear();
183
184     }
185 }