75ac4a1bb3d9fc473bc80a81aee9634a2bc6f48e
[policy/apex-pdp.git] / testsuites / integration / integration-context-test / src / test / java / org / onap / policy / apex / testsuites / integration / context / distribution / ContextInstantiation.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.context.distribution;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 import static org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory.createPolicyContextModel;
30 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.BYTE_VAL;
31 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.EXCEPTION_MESSAGE;
32 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.EXTERNAL_CONTEXT;
33 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.EXTERNAL_CONTEXT_ALBUM;
34 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.FLOAT_VAL;
35 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.GLOBAL_CONTEXT_ALBUM;
36 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.GLOBAL_CONTEXT_KEY;
37 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.INT_VAL;
38 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.INT_VAL_2;
39 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.INT_VAL_3;
40 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.LONG_VAL;
41 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.PI_VAL;
42 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.POLICY_CONTEXT_ALBUM;
43 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.STRING_EXT_VAL;
44 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.STRING_GLOBAL_VAL;
45 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.STRING_VAL;
46 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.TEST_POLICY_CONTEXT_ITEM;
47 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.TIME_ZONE;
48 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.VERSION;
49
50 import java.util.Collection;
51 import java.util.Date;
52 import java.util.HashMap;
53 import java.util.LinkedHashMap;
54 import java.util.Locale;
55 import java.util.Map;
56 import java.util.Map.Entry;
57 import java.util.Set;
58 import java.util.TreeSet;
59
60 import org.onap.policy.apex.context.ContextAlbum;
61 import org.onap.policy.apex.context.ContextException;
62 import org.onap.policy.apex.context.ContextRuntimeException;
63 import org.onap.policy.apex.context.Distributor;
64 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
65 import org.onap.policy.apex.context.test.concepts.TestContextBooleanItem;
66 import org.onap.policy.apex.context.test.concepts.TestContextByteItem;
67 import org.onap.policy.apex.context.test.concepts.TestContextDateItem;
68 import org.onap.policy.apex.context.test.concepts.TestContextDateLocaleItem;
69 import org.onap.policy.apex.context.test.concepts.TestContextDateTzItem;
70 import org.onap.policy.apex.context.test.concepts.TestContextDoubleItem;
71 import org.onap.policy.apex.context.test.concepts.TestContextFloatItem;
72 import org.onap.policy.apex.context.test.concepts.TestContextIntItem;
73 import org.onap.policy.apex.context.test.concepts.TestContextLongItem;
74 import org.onap.policy.apex.context.test.concepts.TestContextLongObjectItem;
75 import org.onap.policy.apex.context.test.concepts.TestContextStringItem;
76 import org.onap.policy.apex.context.test.concepts.TestContextTreeMapItem;
77 import org.onap.policy.apex.context.test.concepts.TestContextTreeSetItem;
78 import org.onap.policy.apex.context.test.concepts.TestExternalContextItem;
79 import org.onap.policy.apex.context.test.concepts.TestGlobalContextItem;
80 import org.onap.policy.apex.context.test.concepts.TestPolicyContextItem;
81 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
82 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
83 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
84 import org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory;
85 import org.onap.policy.apex.testsuites.integration.context.utils.Constants;
86 import org.slf4j.ext.XLogger;
87 import org.slf4j.ext.XLoggerFactory;
88
89 /**
90  * The Class TestContextInstantiation is used to test Apex context insitiation is correct.
91  *
92  * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
93  */
94 public class ContextInstantiation {
95     // Logger for this class
96     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextInstantiation.class);
97
98     // Recurring string constants
99     private static final String TEST_POLICY_CONTEXT_ITEM000 = "TestPolicyContextItem000";
100     private static final String TEST_POLICY_CONTEXT_ITEM005 = "TestPolicyContextItem005";
101     private static final String TEST_POLICY_CONTEXT_ITEM004 = "TestPolicyContextItem004";
102     private static final String TEST_POLICY_CONTEXT_ITEM003 = "TestPolicyContextItem003";
103     private static final String TEST_POLICY_CONTEXT_ITEM002 = "TestPolicyContextItem002";
104     private static final String TEST_POLICY_CONTEXT_ITEM001 = "TestPolicyContextItem001";
105     private static final String NORMAL_TEST_EXCEPTION = "normal test exception";
106     private static final String NULL_VALUES_ILLEGAL_TAG =
107             "album \"ExternalContextAlbum:0.0.1\" null values are illegal on key ";
108
109     private static final TreeSet<String> TEST_TREE_SET = new TreeSet<>();
110     private static final Map<String, String> TEST_HASH_MAP = new HashMap<>();
111
112     static {
113         TEST_TREE_SET.add("one hundred");
114         TEST_TREE_SET.add("one hundred and one");
115         TEST_TREE_SET.add("one hundred and two");
116         TEST_TREE_SET.add("one hundred and three");
117         TEST_TREE_SET.add("one hundred and four");
118
119         TEST_HASH_MAP.put("0", "zero");
120         TEST_HASH_MAP.put("1", "one");
121         TEST_HASH_MAP.put("2", "two");
122         TEST_HASH_MAP.put("3", "three");
123         TEST_HASH_MAP.put("4", "four");
124
125     }
126
127     /**
128      * Test context instantiation.
129      *
130      * @throws ContextException the context exception
131      */
132     public void testContextInstantiation() throws ContextException {
133         LOGGER.debug("Running TestContextInstantiation test . . .");
134
135         final Distributor contextDistributor = getDistributor();
136
137         final ContextAlbum policyContextAlbum =
138                 contextDistributor.createContextAlbum(new AxArtifactKey(POLICY_CONTEXT_ALBUM, VERSION));
139
140         assertNotNull(policyContextAlbum);
141         policyContextAlbum.setUserArtifactStack(Constants.getAxArtifactKeyArray());
142
143         final Date testDate = new Date();
144
145         final TestContextDateTzItem tci9 = getTestContextDateTzItem(testDate);
146         final TestContextDateLocaleItem tciA = getTestContextDateLocaleItem(testDate);
147
148         final TestPolicyContextItem policyContext = getTestPolicyContextItem(policyContextAlbum, testDate);
149
150         final Map<String, Object> valueMap0 = new HashMap<>();
151         valueMap0.put(TEST_POLICY_CONTEXT_ITEM, policyContext);
152
153         policyContextAlbum.putAll(valueMap0);
154
155         final TestPolicyContextItem contextItem =
156                 (TestPolicyContextItem) policyContextAlbum.get(TEST_POLICY_CONTEXT_ITEM);
157         assertEquals(STRING_VAL, contextItem.getTestPolicyContextItem000().getStringValue());
158
159         assertEquals(LONG_VAL, contextItem.getTestPolicyContextItem001().getLongValue());
160         assertDouble(contextItem.getTestPolicyContextItem002().getDoubleValue(), PI_VAL);
161         assertTrue(contextItem.getTestPolicyContextItem003().isFlag());
162         assertEquals(contextItem.getTestPolicyContextItem004().getLongValue(), testDate.getTime());
163         assertEquals(TEST_HASH_MAP, contextItem.getTestPolicyContextItem005().getMapValue());
164
165         final TestGlobalContextItem globalContext = getTestGlobalContextItem(contextDistributor, testDate, tci9, tciA);
166
167         final Map<String, Object> valueMap1 = new HashMap<>();
168         valueMap1.put(GLOBAL_CONTEXT_KEY, globalContext);
169
170         final ContextAlbum globalContextAlbum = getContextAlbum(contextDistributor);
171
172         globalContextAlbum.putAll(valueMap1);
173
174         final TestGlobalContextItem globalContextItem =
175                 (TestGlobalContextItem) globalContextAlbum.get(GLOBAL_CONTEXT_KEY);
176
177         assertFalse(globalContextItem.getTestGlobalContextItem000().isFlag());
178
179         assertEquals(BYTE_VAL, globalContextItem.getTestGlobalContextItem001().getByteValue());
180
181         assertEquals(INT_VAL, globalContextItem.getTestGlobalContextItem002().getIntValue());
182         assertEquals(LONG_VAL, globalContextItem.getTestGlobalContextItem003().getLongValue());
183         assertFloat(FLOAT_VAL, globalContextItem.getTestGlobalContextItem004().getFloatValue());
184
185         assertDouble(PI_VAL, globalContextItem.getTestGlobalContextItem005().getDoubleValue());
186         assertEquals(STRING_GLOBAL_VAL, globalContextItem.getTestGlobalContextItem006().getStringValue());
187
188         assertEquals((Long) testDate.getTime(), globalContextItem.getTestGlobalContextItem007().getLongValue());
189         assertEquals(testDate, globalContextItem.getTestGlobalContextItem008().getDateValue());
190         assertEquals(tci9.getDateValue().getTime(),
191                 globalContextItem.getTestGlobalContextItem009().getDateValue().getTime());
192
193         assertEquals(tciA.getDateValue().getTime(),
194                 globalContextItem.getTestGlobalContextItem00A().getDateValue().getTime());
195
196         assertEquals(TEST_TREE_SET, globalContextItem.getTestGlobalContextItem00B().getSetValue());
197         assertEquals(TEST_HASH_MAP, globalContextItem.getTestGlobalContextItem00C().getMapValue());
198
199         final AxContextModel externalContextModel = TestContextAlbumFactory.createExternalContextModel();
200
201         final TestContextDateTzItem tci9A = new TestContextDateTzItem(tci9);
202         final TestContextDateLocaleItem tciAa = new TestContextDateLocaleItem(tciA);
203         final TestExternalContextItem externalContext = getTestExternalContextItem(testDate, tci9A, tciAa);
204
205         final Map<String, Object> valueMap2 = new HashMap<>();
206         valueMap2.put(EXTERNAL_CONTEXT, externalContext);
207
208         contextDistributor.clear();
209         contextDistributor.init(new AxArtifactKey("ClearedandInittedDistributor", VERSION));
210         contextDistributor.registerModel(externalContextModel);
211
212         final AxArtifactKey axContextAlbumKey = new AxArtifactKey(EXTERNAL_CONTEXT_ALBUM, VERSION);
213         final ContextAlbum externalContextAlbum = contextDistributor.createContextAlbum(axContextAlbumKey);
214         assertNotNull(externalContextAlbum);
215         externalContextAlbum.setUserArtifactStack(Constants.getAxArtifactKeyArray());
216
217         externalContextAlbum.putAll(valueMap2);
218         externalContextAlbum.getAlbumDefinition().setWritable(false);
219
220         TestExternalContextItem externalContextItem =
221                 (TestExternalContextItem) externalContextAlbum.get(EXTERNAL_CONTEXT);
222
223         assertFalse(externalContextItem.getTestExternalContextItem000().isFlag());
224         assertEquals(BYTE_VAL, externalContextItem.getTestExternalContextItem001().getByteValue());
225         assertEquals(INT_VAL, externalContextItem.getTestExternalContextItem002().getIntValue());
226
227         assertFloat(LONG_VAL, externalContextItem.getTestExternalContextItem003().getLongValue());
228         assertFloat(FLOAT_VAL, externalContextItem.getTestExternalContextItem004().getFloatValue());
229
230         assertDouble(PI_VAL, externalContextItem.getTestExternalContextItem005().getDoubleValue());
231         assertEquals(STRING_EXT_VAL, externalContextItem.getTestExternalContextItem006().getStringValue());
232         assertEquals((Long) testDate.getTime(), externalContextItem.getTestExternalContextItem007().getLongValue());
233         assertEquals(testDate, externalContextItem.getTestExternalContextItem008().getDateValue());
234         assertEquals(tci9A.getDateValue().getTime(),
235                 externalContextItem.getTestExternalContextItem009().getDateValue().getTime());
236
237         assertEquals(tciAa.getDateValue().getTime(),
238                 externalContextItem.getTestExternalContextItem00A().getDateValue().getTime());
239         assertEquals(TEST_TREE_SET, externalContextItem.getTestExternalContextItem00B().getSetValue());
240         assertEquals(TEST_HASH_MAP, externalContextItem.getTestExternalContextItem00C().getMapValue());
241
242         final Collection<Object> mapValues = externalContextAlbum.values();
243         assertTrue(externalContextAlbum.values().containsAll(mapValues));
244
245         // Check that clearing does not work
246         try {
247             externalContextAlbum.clear();
248             fail(EXCEPTION_MESSAGE);
249         } catch (final ContextRuntimeException e) {
250             assertEquals("album \"ExternalContextAlbum:0.0.1\" clear() not allowed on read only albums",
251                     e.getMessage());
252             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
253         }
254
255         assertEquals(1, externalContextAlbum.size());
256
257         assertContextAlbumContains(externalContext, externalContextAlbum);
258
259         final Set<Entry<String, Object>> entrySet = externalContextAlbum.entrySet();
260         assertEquals(1, entrySet.size());
261
262         try {
263             externalContextAlbum.get(null);
264         } catch (final ContextRuntimeException e) {
265             assertEquals("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for get()",
266                     e.getMessage());
267             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
268         }
269
270         final Object aObject = externalContextAlbum.get(EXTERNAL_CONTEXT);
271         assertEquals(aObject, externalContext);
272
273         // put null keys should fail, throws a runtime exception
274         try {
275             externalContextAlbum.put(null, null);
276         } catch (final ContextRuntimeException e) {
277             assertEquals("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for put()",
278                     e.getMessage());
279             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
280         }
281
282         try {
283             externalContextAlbum.put("TestExternalContextItem00A", null);
284         } catch (final ContextRuntimeException e) {
285             assertEquals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()", e.getMessage());
286             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
287         }
288         assertEquals(tciAa, externalContextItem.getTestExternalContextItem00A());
289
290         // Should return the hash set
291         assertEquals(TEST_TREE_SET, externalContextItem.getTestExternalContextItem00B().getSetValue());
292
293         assertTrue(externalContextAlbum.values().containsAll(mapValues));
294
295         // Set the write flag back as it should be
296         externalContextAlbum.getAlbumDefinition().setWritable(true);
297
298         // Put should return the previous contextItem
299         final TestExternalContextItem externalContextOther = new TestExternalContextItem();
300         externalContextOther.setTestExternalContextItem002(new TestContextIntItem());
301         externalContextOther.getTestExternalContextItem002().setIntValue(INT_VAL_2);
302
303         assertTrue(externalContextAlbum.put(EXTERNAL_CONTEXT, externalContextOther).equals(externalContext));
304         externalContextItem = (TestExternalContextItem) externalContextAlbum.get(EXTERNAL_CONTEXT);
305         assertEquals(INT_VAL_2, externalContextItem.getTestExternalContextItem002().getIntValue());
306         assertTrue(externalContextAlbum.put(EXTERNAL_CONTEXT, externalContext).equals(externalContextOther));
307         externalContextItem = (TestExternalContextItem) externalContextAlbum.get(EXTERNAL_CONTEXT);
308         assertEquals(INT_VAL_3, externalContextItem.getTestExternalContextItem002().getIntValue());
309
310         try {
311             externalContextAlbum.put("TestExternalContextItem00A", null);
312         } catch (final ContextRuntimeException e) {
313             assert (e.getMessage().equals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()"));
314             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
315         }
316         assertTrue(externalContextAlbum.get(EXTERNAL_CONTEXT).equals(externalContext));
317
318         try {
319             externalContextAlbum.put("TestExternalContextItemFFF", null);
320         } catch (final ContextRuntimeException e) {
321             assert (e.getMessage().equals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()"));
322             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
323         }
324         assertEquals(1, externalContextAlbum.size());
325
326         try {
327             externalContextAlbum.put("TestExternalContextItemFFF", null);
328         } catch (final ContextRuntimeException e) {
329             assertEquals(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()", e.getMessage());
330             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
331         }
332         assertEquals(1, externalContextAlbum.size());
333
334         // Should ignore remove
335         externalContextAlbum.remove("TestExternalContextItem017");
336         assertEquals(1, externalContextAlbum.size());
337         assertEquals(1, externalContextAlbum.values().size());
338         assertTrue(externalContextAlbum.values().containsAll(mapValues));
339
340         contextDistributor.clear();
341     }
342
343     private void assertContextAlbumContains(final TestExternalContextItem externalContext,
344             final ContextAlbum externalContextAlbum) {
345         try {
346             externalContextAlbum.containsKey(null);
347         } catch (final ContextRuntimeException e) {
348             assertEquals("null values are illegal on method parameter \"key\"", e.getMessage());
349             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
350         }
351
352         assertTrue(externalContextAlbum.containsKey(EXTERNAL_CONTEXT));
353         assertTrue(!externalContextAlbum.containsKey(GLOBAL_CONTEXT_KEY));
354
355         try {
356             externalContextAlbum.containsValue(null);
357         } catch (final ContextRuntimeException e) {
358             assertEquals("null values are illegal on method parameter \"value\"", e.getMessage());
359             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
360         }
361
362         assertTrue(externalContextAlbum.containsValue(externalContext));
363         assertFalse(externalContextAlbum.containsValue("Hello"));
364     }
365
366     private ContextAlbum getContextAlbum(final Distributor contextDistributor) throws ContextException {
367         final ContextAlbum globalContextAlbum =
368                 contextDistributor.createContextAlbum(new AxArtifactKey(GLOBAL_CONTEXT_ALBUM, VERSION));
369         assertNotNull(globalContextAlbum);
370         globalContextAlbum.setUserArtifactStack(Constants.getAxArtifactKeyArray());
371         return globalContextAlbum;
372     }
373
374     private TestGlobalContextItem getTestGlobalContextItem(final Distributor contextDistributor, final Date testDate,
375             final TestContextDateTzItem tci9, final TestContextDateLocaleItem tciA) throws ContextException {
376         final AxContextModel globalContextModel = TestContextAlbumFactory.createGlobalContextModel();
377         final TestGlobalContextItem globalContext = getTestGlobalContextItem(testDate, tci9, tciA);
378         contextDistributor.registerModel(globalContextModel);
379         return globalContext;
380     }
381
382     private TestGlobalContextItem getTestGlobalContextItem(final Date testDate, final TestContextDateTzItem tci9,
383             final TestContextDateLocaleItem tciA) {
384         final TestGlobalContextItem globalContext = new TestGlobalContextItem();
385
386         final TestContextBooleanItem testGlobalContextItem000 = new TestContextBooleanItem(false);
387         final TestContextByteItem testGlobalContextItem001 = new TestContextByteItem(BYTE_VAL);
388         final TestContextIntItem testGlobalContextItem002 = new TestContextIntItem(INT_VAL);
389         final TestContextLongItem testGlobalContextItem003 = new TestContextLongItem(LONG_VAL);
390         final TestContextFloatItem testGlobalContextItem004 = new TestContextFloatItem(FLOAT_VAL);
391         final TestContextDoubleItem testGlobalContextItem005 = new TestContextDoubleItem(PI_VAL);
392         final TestContextStringItem testGlobalContextItem006 = new TestContextStringItem(STRING_GLOBAL_VAL);
393         final TestContextLongObjectItem testGlobalContextItem007 = new TestContextLongObjectItem(testDate.getTime());
394
395         final TestContextDateItem testGlobalContextItem008 = new TestContextDateItem(testDate);
396         final TestContextTreeSetItem testGlobalContextItem00B = new TestContextTreeSetItem(TEST_TREE_SET);
397         final TestContextTreeMapItem testGlobalContextItem00C = new TestContextTreeMapItem(TEST_HASH_MAP);
398
399         globalContext.setTestGlobalContextItem000(testGlobalContextItem000);
400         globalContext.setTestGlobalContextItem001(testGlobalContextItem001);
401         globalContext.setTestGlobalContextItem002(testGlobalContextItem002);
402         globalContext.setTestGlobalContextItem003(testGlobalContextItem003);
403         globalContext.setTestGlobalContextItem004(testGlobalContextItem004);
404         globalContext.setTestGlobalContextItem005(testGlobalContextItem005);
405         globalContext.setTestGlobalContextItem006(testGlobalContextItem006);
406         globalContext.setTestGlobalContextItem007(testGlobalContextItem007);
407         globalContext.setTestGlobalContextItem008(testGlobalContextItem008);
408         globalContext.setTestGlobalContextItem009(tci9);
409         globalContext.setTestGlobalContextItem00A(tciA);
410         globalContext.setTestGlobalContextItem00B(testGlobalContextItem00B);
411         globalContext.setTestGlobalContextItem00C(testGlobalContextItem00C);
412         return globalContext;
413     }
414
415     private TestPolicyContextItem getTestPolicyContextItem(final ContextAlbum policyContextAlbum, final Date testDate) {
416         final TestContextStringItem contextStringItem = new TestContextStringItem(STRING_VAL);
417         final TestContextLongItem contextLongItem = new TestContextLongItem(LONG_VAL);
418         final TestContextDoubleItem contextDoubleItem = new TestContextDoubleItem(PI_VAL);
419         final TestContextBooleanItem contextBooleanItem = new TestContextBooleanItem(true);
420         final TestContextLongItem contextLongItem2 = new TestContextLongItem(testDate.getTime());
421         final TestContextTreeMapItem contextTreeMapItem = new TestContextTreeMapItem(TEST_HASH_MAP);
422
423         final Map<String, Object> valueMapA = new LinkedHashMap<>();
424         valueMapA.put(TEST_POLICY_CONTEXT_ITEM001, contextLongItem);
425         valueMapA.put(TEST_POLICY_CONTEXT_ITEM002, contextDoubleItem);
426         valueMapA.put(TEST_POLICY_CONTEXT_ITEM003, contextBooleanItem);
427         valueMapA.put(TEST_POLICY_CONTEXT_ITEM004, contextLongItem2);
428         valueMapA.put(TEST_POLICY_CONTEXT_ITEM005, contextTreeMapItem);
429         valueMapA.put(TEST_POLICY_CONTEXT_ITEM000, contextStringItem);
430
431         assertPutMethods(policyContextAlbum, contextStringItem, valueMapA);
432
433         final TestPolicyContextItem policyContext = new TestPolicyContextItem();
434
435         LOGGER.debug(policyContextAlbum.toString());
436
437         policyContext.setTestPolicyContextItem000(contextStringItem);
438         policyContext.setTestPolicyContextItem001(contextLongItem);
439         policyContext.setTestPolicyContextItem002(contextDoubleItem);
440         policyContext.setTestPolicyContextItem003(contextBooleanItem);
441         policyContext.setTestPolicyContextItem004(contextLongItem2);
442         policyContext.setTestPolicyContextItem005(contextTreeMapItem);
443         return policyContext;
444     }
445
446     private void assertPutMethods(final ContextAlbum policyContextAlbum, final TestContextStringItem contextStringItem,
447             final Map<String, Object> valueMapA) {
448         try {
449             policyContextAlbum.put(TEST_POLICY_CONTEXT_ITEM000, contextStringItem);
450             fail(EXCEPTION_MESSAGE);
451         } catch (final ContextRuntimeException e) {
452             assertEquals(getMessage(TEST_POLICY_CONTEXT_ITEM000, "TestContextItem006",
453                     TestContextStringItem.class.getName(), "stringValue=" + STRING_VAL), e.getMessage());
454             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
455         }
456
457         try {
458             policyContextAlbum.putAll(valueMapA);
459             fail(EXCEPTION_MESSAGE);
460         } catch (final ContextRuntimeException e) {
461             assertEquals(getMessage(TEST_POLICY_CONTEXT_ITEM001, "TestContextItem003",
462                     TestContextLongItem.class.getName(), "longValue=" + INT_VAL_3), e.getMessage());
463             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
464         }
465     }
466
467     private AxContextModel getAxContextModel() {
468         final AxContextModel policyContextModel = createPolicyContextModel();
469         final AxValidationResult result = new AxValidationResult();
470         policyContextModel.validate(result);
471         LOGGER.debug(result.toString());
472
473         assertTrue(result.isValid());
474         return policyContextModel;
475     }
476
477     private TestContextDateLocaleItem getTestContextDateLocaleItem(final Date testDate) {
478         final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem();
479         tciA.setDateValue(new TestContextDateItem(testDate));
480         tciA.setTzValue(TIME_ZONE.getDisplayName());
481         tciA.setDst(true);
482         tciA.setUtcOffset(-600);
483         tciA.setLocale(Locale.ENGLISH);
484         return tciA;
485     }
486
487     private TestContextDateTzItem getTestContextDateTzItem(final Date testDate) {
488         final TestContextDateTzItem tci9 = new TestContextDateTzItem();
489         tci9.setDateValue(new TestContextDateItem(testDate));
490         tci9.setTzValue(TIME_ZONE.getDisplayName());
491         tci9.setDst(true);
492         return tci9;
493     }
494
495     private TestExternalContextItem getTestExternalContextItem(final Date testDate, final TestContextDateTzItem tci9A,
496             final TestContextDateLocaleItem tciAa) {
497         final TestExternalContextItem externalContext = new TestExternalContextItem();
498
499         final TestContextBooleanItem testExternalContextItem000 = new TestContextBooleanItem(false);
500         final TestContextByteItem testExternalContextItem001 = new TestContextByteItem(BYTE_VAL);
501         final TestContextIntItem testExternalContextItem002 = new TestContextIntItem(INT_VAL);
502         final TestContextLongItem testExternalContextItem003 = new TestContextLongItem(LONG_VAL);
503         final TestContextFloatItem testExternalContextItem004 = new TestContextFloatItem(3.14159265359F);
504         final TestContextDoubleItem testExternalContextItem005 = new TestContextDoubleItem(PI_VAL);
505         final TestContextStringItem testExternalContextItem006 = new TestContextStringItem(STRING_EXT_VAL);
506         final TestContextLongObjectItem testExternalContextItem007 = new TestContextLongObjectItem(testDate.getTime());
507         final TestContextDateItem testExternalContextItem008 = new TestContextDateItem(testDate);
508         final TestContextTreeSetItem testExternalContextItem00B = new TestContextTreeSetItem(TEST_TREE_SET);
509         final TestContextTreeMapItem testExternalContextItem00C = new TestContextTreeMapItem(TEST_HASH_MAP);
510
511         externalContext.setTestExternalContextItem000(testExternalContextItem000);
512         externalContext.setTestExternalContextItem001(testExternalContextItem001);
513         externalContext.setTestExternalContextItem002(testExternalContextItem002);
514         externalContext.setTestExternalContextItem003(testExternalContextItem003);
515         externalContext.setTestExternalContextItem004(testExternalContextItem004);
516         externalContext.setTestExternalContextItem005(testExternalContextItem005);
517         externalContext.setTestExternalContextItem006(testExternalContextItem006);
518         externalContext.setTestExternalContextItem007(testExternalContextItem007);
519         externalContext.setTestExternalContextItem008(testExternalContextItem008);
520         externalContext.setTestExternalContextItem009(tci9A);
521         externalContext.setTestExternalContextItem00A(tciAa);
522         externalContext.setTestExternalContextItem00B(testExternalContextItem00B);
523         externalContext.setTestExternalContextItem00C(testExternalContextItem00C);
524         return externalContext;
525     }
526
527     private String getMessage(final String key, final String objName, final String clazzName, final String valString) {
528         return getMessage(key, objName, clazzName, valString, TestPolicyContextItem.class.getName());
529     }
530
531     private String getMessage(final String key, final String objName, final String clazzName, final String valString,
532             final String compatibleClazzName) {
533         return "Failed to set context value for key \"" + key + "\" in album \"PolicyContextAlbum:0.0.1\": "
534                 + "PolicyContextAlbum:0.0.1: object \"" + objName + " [" + valString + "]\" " + "of class \""
535                 + clazzName + "\"" + " not compatible with class \"" + compatibleClazzName + "\"";
536     }
537
538     private void assertFloat(final float actual, final float expected) {
539         assertTrue(Float.compare(actual, expected) == 0);
540     }
541
542     private void assertDouble(final double actual, final double expected) {
543         assertTrue(Double.compare(actual, expected) == 0);
544     }
545
546     private Distributor getDistributor() throws ContextException {
547         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributorInit", VERSION);
548         final Distributor distributor = new DistributorFactory().getDistributor(distributorKey);
549         final AxContextModel policyContextModel = getAxContextModel();
550         distributor.registerModel(policyContextModel);
551         return distributor;
552     }
553 }