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