db1ac47fe0d1d7477fa9e3b878a4ac3f7d9a5fce
[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.TestContextItem003;
44 import org.onap.policy.apex.context.test.concepts.TestContextItem008;
45 import org.onap.policy.apex.context.test.concepts.TestContextItem00A;
46 import org.onap.policy.apex.context.test.concepts.TestContextItem00C;
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 = XLoggerFactory.getXLogger(TestPersistentContextInstantiation.class);
65
66     private Connection connection;
67
68     @Before
69     public void setup() throws Exception {
70         Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
71         connection = DriverManager.getConnection("jdbc:derby:memory:apex_test;create=true");
72     }
73
74     @After
75     public void teardown() throws Exception {
76         connection.close();
77         new File("derby.log").delete();
78     }
79
80     @After
81     public void afterTest() throws IOException {}
82
83     @Test
84     public void testContextPersistentInstantiation() throws ApexModelException, IOException, ApexException {
85
86         final ContextParameters contextParameters = new ContextParameters();
87         contextParameters.setPersistorParameters(new PersistorParameters());
88
89         final AxArtifactKey distributorKey = new AxArtifactKey("AbstractDistributor", "0.0.1");
90         final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
91
92         final AxArtifactKey[] usedArtifactStackArray = { new AxArtifactKey("testC-top", "0.0.1"),
93                 new AxArtifactKey("testC-next", "0.0.1"), new AxArtifactKey("testC-bot", "0.0.1") };
94
95         final DAOParameters daoParameters = new DAOParameters();
96         daoParameters.setPluginClass("org.onap.policy.apex.model.basicmodel.dao.impl.DefaultApexDao");
97         daoParameters.setPersistenceUnit("DAOTest");
98         final ApexDao apexDao = new ApexDaoFactory().createApexDao(daoParameters);
99         apexDao.init(daoParameters);
100
101         final AxContextModel someContextModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
102
103         // Context for Storing Map values
104         final AxContextAlbum axContextAlbumForMap =
105                 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("MapContextAlbum", "0.0.1"));
106         apexDao.create(axContextAlbumForMap);
107         contextDistributor.registerModel(someContextModel);
108         final ContextAlbum contextAlbumForMap = contextDistributor.createContextAlbum(axContextAlbumForMap.getKey());
109         assertNotNull(contextAlbumForMap);
110         contextAlbumForMap.setUserArtifactStack(usedArtifactStackArray);
111
112         final Map<String, String> testMap = new HashMap<String, String>();
113         testMap.put("key", "This is a policy context string");
114
115         final Map<String, Object> valueMap0 = new HashMap<String, Object>();
116         valueMap0.put("TestPolicyContextItem000", new TestContextItem00C(testMap));
117
118         contextAlbumForMap.putAll(valueMap0);
119
120         assertEquals(((TestContextItem00C) contextAlbumForMap.get("TestPolicyContextItem000")).getMapValue().get("key"),
121                 "This is a policy context string");
122
123         contextAlbumForMap.flush();
124
125         // Context for Storing Date values
126         final AxContextAlbum axContextAlbumForDate =
127                 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("DateContextAlbum", "0.0.1"));
128         apexDao.create(axContextAlbumForDate);
129         contextDistributor.registerModel(someContextModel);
130         final ContextAlbum contextAlbumForDate = contextDistributor.createContextAlbum(axContextAlbumForDate.getKey());
131         assertNotNull(contextAlbumForDate);
132         contextAlbumForDate.setUserArtifactStack(usedArtifactStackArray);
133
134         final TestContextItem008 testDate = new TestContextItem008(new Date());
135         final TestContextItem00A tci00A = new TestContextItem00A();
136         tci00A.setDateValue(testDate);
137         tci00A.setTZValue(TimeZone.getTimeZone("Europe/Dublin").toString());
138         tci00A.setDST(true);
139
140         final Map<String, Object> valueMap1 = new HashMap<String, Object>();
141         valueMap1.put("TestPolicyContextItem00A", tci00A);
142
143         contextAlbumForDate.putAll(valueMap1);
144
145         assertEquals(((TestContextItem00A) contextAlbumForDate.get("TestPolicyContextItem00A")).getDateValue(),
146                 testDate);
147         assertEquals(((TestContextItem00A) contextAlbumForDate.get("TestPolicyContextItem00A")).getDST(), true);
148
149         contextAlbumForDate.flush();
150
151         // Context for Storing Long values
152         final AxContextAlbum axContextAlbumForLong =
153                 someContextModel.getAlbums().getAlbumsMap().get(new AxArtifactKey("LTypeContextAlbum", "0.0.1"));
154         apexDao.create(axContextAlbumForLong);
155         contextDistributor.registerModel(someContextModel);
156         final ContextAlbum contextAlbumForLong = contextDistributor.createContextAlbum(axContextAlbumForLong.getKey());
157         assertNotNull(contextAlbumForLong);
158         contextAlbumForLong.setUserArtifactStack(usedArtifactStackArray);
159
160         final Map<String, Object> valueMap2 = new HashMap<String, Object>();
161         valueMap2.put("TestPolicyContextItem0031", new TestContextItem003(0xFFFFFFFFFFFFFFFFL));
162         valueMap2.put("TestPolicyContextItem0032", new TestContextItem003(0xFFFFFFFFFFFFFFFEL));
163         valueMap2.put("TestPolicyContextItem0033", new TestContextItem003(0xFFFFFFFFFFFFFFFDL));
164         valueMap2.put("TestPolicyContextItem0034", new TestContextItem003(0xFFFFFFFFFFFFFFFCL));
165         valueMap2.put("TestPolicyContextItem0035", new TestContextItem003(0xFFFFFFFFFFFFFFFBL));
166
167         contextAlbumForLong.putAll(valueMap2);
168
169         assertEquals(((TestContextItem003) contextAlbumForLong.get("TestPolicyContextItem0031")).getLongValue(),
170                 0xFFFFFFFFFFFFFFFFL);
171         assertEquals(((TestContextItem003) contextAlbumForLong.get("TestPolicyContextItem0032")).getLongValue(),
172                 0xFFFFFFFFFFFFFFFEL);
173         assertEquals(((TestContextItem003) contextAlbumForLong.get("TestPolicyContextItem0033")).getLongValue(),
174                 0xFFFFFFFFFFFFFFFDL);
175         assertEquals(((TestContextItem003) contextAlbumForLong.get("TestPolicyContextItem0034")).getLongValue(),
176                 0xFFFFFFFFFFFFFFFCL);
177         assertEquals(((TestContextItem003) contextAlbumForLong.get("TestPolicyContextItem0035")).getLongValue(),
178                 0xFFFFFFFFFFFFFFFBL);
179
180         contextAlbumForLong.flush();
181         contextDistributor.clear();
182
183     }
184 }