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