8c4f40a5a9c711dd0b17d48533c8219ed255b733
[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         final ContextAlbum policyContextAlbum = contextDistributor
137                         .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 = (TestPolicyContextItem) policyContextAlbum
155                         .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().getFlag());
161         assertEquals(contextItem.getTestPolicyContextItem004().getLongValue(), testDate.getTime());
162         assertEquals(TEST_HASH_MAP, contextItem.getTestPolicyContextItem005().getMapValue());
163
164         final TestGlobalContextItem globalContext = getTestGlobalContextItem(contextDistributor, testDate, tci9,
165                         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 = (TestGlobalContextItem) globalContextAlbum
175                         .get(GLOBAL_CONTEXT_KEY);
176
177         assertFalse(globalContextItem.getTestGlobalContextItem000().getFlag());
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 = (TestExternalContextItem) externalContextAlbum
221                         .get(EXTERNAL_CONTEXT);
222
223         assertFalse(externalContextItem.getTestExternalContextItem000().getFlag());
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 = contextDistributor
368                         .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.getCanonicalName(), "stringValue=" + STRING_VAL),
454                             e.getMessage());
455             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
456         }
457
458         try {
459             policyContextAlbum.putAll(valueMapA);
460             fail(EXCEPTION_MESSAGE);
461         } catch (final ContextRuntimeException e) {
462             assertEquals(getMessage(TEST_POLICY_CONTEXT_ITEM001, "TestContextItem003",
463                             TestContextLongItem.class.getCanonicalName(), "longValue=" + INT_VAL_3), e.getMessage());
464             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
465         }
466     }
467
468     private AxContextModel getAxContextModel() {
469         final AxContextModel policyContextModel = createPolicyContextModel();
470         final AxValidationResult result = new AxValidationResult();
471         policyContextModel.validate(result);
472         LOGGER.debug(result.toString());
473
474         assertTrue(result.isValid());
475         return policyContextModel;
476     }
477
478     private TestContextDateLocaleItem getTestContextDateLocaleItem(final Date testDate) {
479         final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem();
480         tciA.setDateValue(new TestContextDateItem(testDate));
481         tciA.setTzValue(TIME_ZONE.getDisplayName());
482         tciA.setDst(true);
483         tciA.setUtcOffset(-600);
484         tciA.setLocale(Locale.ENGLISH);
485         return tciA;
486     }
487
488     private TestContextDateTzItem getTestContextDateTzItem(final Date testDate) {
489         final TestContextDateTzItem tci9 = new TestContextDateTzItem();
490         tci9.setDateValue(new TestContextDateItem(testDate));
491         tci9.setTzValue(TIME_ZONE.getDisplayName());
492         tci9.setDst(true);
493         return tci9;
494     }
495
496     private TestExternalContextItem getTestExternalContextItem(final Date testDate, final TestContextDateTzItem tci9A,
497                     final TestContextDateLocaleItem tciAa) {
498         final TestExternalContextItem externalContext = new TestExternalContextItem();
499
500         final TestContextBooleanItem testExternalContextItem000 = new TestContextBooleanItem(false);
501         final TestContextByteItem testExternalContextItem001 = new TestContextByteItem(BYTE_VAL);
502         final TestContextIntItem testExternalContextItem002 = new TestContextIntItem(INT_VAL);
503         final TestContextLongItem testExternalContextItem003 = new TestContextLongItem(LONG_VAL);
504         final TestContextFloatItem testExternalContextItem004 = new TestContextFloatItem(new Float(3.14159265359));
505         final TestContextDoubleItem testExternalContextItem005 = new TestContextDoubleItem(PI_VAL);
506         final TestContextStringItem testExternalContextItem006 = new TestContextStringItem(STRING_EXT_VAL);
507         final TestContextLongObjectItem testExternalContextItem007 = new TestContextLongObjectItem(testDate.getTime());
508         final TestContextDateItem testExternalContextItem008 = new TestContextDateItem(testDate);
509         final TestContextTreeSetItem testExternalContextItem00B = new TestContextTreeSetItem(TEST_TREE_SET);
510         final TestContextTreeMapItem testExternalContextItem00C = new TestContextTreeMapItem(TEST_HASH_MAP);
511
512         externalContext.setTestExternalContextItem000(testExternalContextItem000);
513         externalContext.setTestExternalContextItem001(testExternalContextItem001);
514         externalContext.setTestExternalContextItem002(testExternalContextItem002);
515         externalContext.setTestExternalContextItem003(testExternalContextItem003);
516         externalContext.setTestExternalContextItem004(testExternalContextItem004);
517         externalContext.setTestExternalContextItem005(testExternalContextItem005);
518         externalContext.setTestExternalContextItem006(testExternalContextItem006);
519         externalContext.setTestExternalContextItem007(testExternalContextItem007);
520         externalContext.setTestExternalContextItem008(testExternalContextItem008);
521         externalContext.setTestExternalContextItem009(tci9A);
522         externalContext.setTestExternalContextItem00A(tciAa);
523         externalContext.setTestExternalContextItem00B(testExternalContextItem00B);
524         externalContext.setTestExternalContextItem00C(testExternalContextItem00C);
525         return externalContext;
526     }
527
528     private String getMessage(final String key, final String objName, final String clazzName, final String valString) {
529         return getMessage(key, objName, clazzName, valString, TestPolicyContextItem.class.getCanonicalName());
530     }
531
532     private String getMessage(final String key, final String objName, final String clazzName, final String valString,
533                     final String compatibleClazzName) {
534         return "Failed to set context value for key \"" + key + "\" in album \"PolicyContextAlbum:0.0.1\": "
535                         + "PolicyContextAlbum:0.0.1: object \"" + objName + " [" + valString + "]\" " + "of class \""
536                         + clazzName + "\"" + " not compatible with class \"" + compatibleClazzName + "\"";
537     }
538
539     private void assertFloat(final float actual, final float expected) {
540         assertTrue(Float.compare(actual, expected) == 0);
541     }
542
543     private void assertDouble(final double actual, final double expected) {
544         assertTrue(Double.compare(actual, expected) == 0);
545     }
546
547     private Distributor getDistributor() throws ContextException {
548         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributorInit", VERSION);
549         final Distributor distributor = new DistributorFactory().getDistributor(distributorKey);
550         final AxContextModel policyContextModel = getAxContextModel();
551         distributor.registerModel(policyContextModel);
552         return distributor;
553     }
554 }