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