2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30 import static org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory.createPolicyContextModel;
31 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.BYTE_VAL;
32 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.EXCEPTION_MESSAGE;
33 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.EXTERNAL_CONTEXT;
34 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.EXTERNAL_CONTEXT_ALBUM;
35 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.FLOAT_VAL;
36 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.GLOBAL_CONTEXT_ALBUM;
37 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.GLOBAL_CONTEXT_KEY;
38 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.INT_VAL;
39 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.INT_VAL_2;
40 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.INT_VAL_3;
41 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.LONG_VAL;
42 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.PI_VAL;
43 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.POLICY_CONTEXT_ALBUM;
44 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.STRING_EXT_VAL;
45 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.STRING_GLOBAL_VAL;
46 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.STRING_VAL;
47 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.TEST_POLICY_CONTEXT_ITEM;
48 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.TIME_ZONE;
49 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.VERSION;
51 import java.util.Collection;
52 import java.util.Date;
53 import java.util.HashMap;
54 import java.util.LinkedHashMap;
55 import java.util.Locale;
57 import java.util.Map.Entry;
59 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 =
138 contextDistributor.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 =
156 (TestPolicyContextItem) policyContextAlbum.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().isFlag());
162 assertEquals(contextItem.getTestPolicyContextItem004().getLongValue(), testDate.getTime());
163 assertEquals(TEST_HASH_MAP, contextItem.getTestPolicyContextItem005().getMapValue());
165 final TestGlobalContextItem globalContext = getTestGlobalContextItem(contextDistributor, testDate, tci9, tciA);
167 final Map<String, Object> valueMap1 = new HashMap<>();
168 valueMap1.put(GLOBAL_CONTEXT_KEY, globalContext);
170 final ContextAlbum globalContextAlbum = getContextAlbum(contextDistributor);
172 globalContextAlbum.putAll(valueMap1);
174 final TestGlobalContextItem globalContextItem =
175 (TestGlobalContextItem) globalContextAlbum.get(GLOBAL_CONTEXT_KEY);
177 assertFalse(globalContextItem.getTestGlobalContextItem000().isFlag());
179 assertEquals(BYTE_VAL, globalContextItem.getTestGlobalContextItem001().getByteValue());
181 assertEquals(INT_VAL, globalContextItem.getTestGlobalContextItem002().getIntValue());
182 assertEquals(LONG_VAL, globalContextItem.getTestGlobalContextItem003().getLongValue());
183 assertFloat(FLOAT_VAL, globalContextItem.getTestGlobalContextItem004().getFloatValue());
185 assertDouble(PI_VAL, globalContextItem.getTestGlobalContextItem005().getDoubleValue());
186 assertEquals(STRING_GLOBAL_VAL, globalContextItem.getTestGlobalContextItem006().getStringValue());
188 assertEquals((Long) testDate.getTime(), globalContextItem.getTestGlobalContextItem007().getLongValue());
189 assertEquals(testDate, globalContextItem.getTestGlobalContextItem008().getDateValue());
190 assertEquals(tci9.getDateValue().getTime(),
191 globalContextItem.getTestGlobalContextItem009().getDateValue().getTime());
193 assertEquals(tciA.getDateValue().getTime(),
194 globalContextItem.getTestGlobalContextItem00A().getDateValue().getTime());
196 assertEquals(TEST_TREE_SET, globalContextItem.getTestGlobalContextItem00B().getSetValue());
197 assertEquals(TEST_HASH_MAP, globalContextItem.getTestGlobalContextItem00C().getMapValue());
199 final AxContextModel externalContextModel = TestContextAlbumFactory.createExternalContextModel();
201 final TestContextDateTzItem tci9A = new TestContextDateTzItem(tci9);
202 final TestContextDateLocaleItem tciAa = new TestContextDateLocaleItem(tciA);
203 final TestExternalContextItem externalContext = getTestExternalContextItem(testDate, tci9A, tciAa);
205 final Map<String, Object> valueMap2 = new HashMap<>();
206 valueMap2.put(EXTERNAL_CONTEXT, externalContext);
208 contextDistributor.clear();
209 contextDistributor.init(new AxArtifactKey("ClearedandInittedDistributor", VERSION));
210 contextDistributor.registerModel(externalContextModel);
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());
217 externalContextAlbum.putAll(valueMap2);
218 externalContextAlbum.getAlbumDefinition().setWritable(false);
220 TestExternalContextItem externalContextItem =
221 (TestExternalContextItem) externalContextAlbum.get(EXTERNAL_CONTEXT);
223 assertFalse(externalContextItem.getTestExternalContextItem000().isFlag());
224 assertEquals(BYTE_VAL, externalContextItem.getTestExternalContextItem001().getByteValue());
225 assertEquals(INT_VAL, externalContextItem.getTestExternalContextItem002().getIntValue());
227 assertFloat(LONG_VAL, externalContextItem.getTestExternalContextItem003().getLongValue());
228 assertFloat(FLOAT_VAL, externalContextItem.getTestExternalContextItem004().getFloatValue());
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());
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());
242 final Collection<Object> mapValues = externalContextAlbum.values();
243 assertTrue(externalContextAlbum.values().containsAll(mapValues));
245 // Check that clearing does not work
246 assertThatThrownBy(() -> externalContextAlbum.clear()).isInstanceOf(ContextRuntimeException.class)
247 .hasMessageContaining("album \"ExternalContextAlbum:0.0.1\" clear() not allowed on read only albums");
248 assertEquals(1, externalContextAlbum.size());
250 assertContextAlbumContains(externalContext, externalContextAlbum);
252 final Set<Entry<String, Object>> entrySet = externalContextAlbum.entrySet();
253 assertEquals(1, entrySet.size());
255 assertThatThrownBy(() -> externalContextAlbum.get(null)).isInstanceOf(ContextRuntimeException.class)
256 .hasMessageContaining("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for get()");
257 final Object aObject = externalContextAlbum.get(EXTERNAL_CONTEXT);
258 assertEquals(aObject, externalContext);
260 // put null keys should fail, throws a runtime exception
261 assertThatThrownBy(() -> externalContextAlbum.put(null, null))
262 .hasMessageContaining("album \"ExternalContextAlbum:0.0.1\" null keys are illegal on keys for put()");
263 assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItem00A", null))
264 .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()");
265 assertEquals(tciAa, externalContextItem.getTestExternalContextItem00A());
267 // Should return the hash set
268 assertEquals(TEST_TREE_SET, externalContextItem.getTestExternalContextItem00B().getSetValue());
270 assertTrue(externalContextAlbum.values().containsAll(mapValues));
272 // Set the write flag back as it should be
273 externalContextAlbum.getAlbumDefinition().setWritable(true);
275 // Put should return the previous contextItem
276 final TestExternalContextItem externalContextOther = new TestExternalContextItem();
277 externalContextOther.setTestExternalContextItem002(new TestContextIntItem());
278 externalContextOther.getTestExternalContextItem002().setIntValue(INT_VAL_2);
280 assertTrue(externalContextAlbum.put(EXTERNAL_CONTEXT, externalContextOther).equals(externalContext));
281 externalContextItem = (TestExternalContextItem) externalContextAlbum.get(EXTERNAL_CONTEXT);
282 assertEquals(INT_VAL_2, externalContextItem.getTestExternalContextItem002().getIntValue());
283 assertTrue(externalContextAlbum.put(EXTERNAL_CONTEXT, externalContext).equals(externalContextOther));
284 externalContextItem = (TestExternalContextItem) externalContextAlbum.get(EXTERNAL_CONTEXT);
285 assertEquals(INT_VAL_3, externalContextItem.getTestExternalContextItem002().getIntValue());
286 assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItem00A", null))
287 .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItem00A\" for put()");
289 assertTrue(externalContextAlbum.get(EXTERNAL_CONTEXT).equals(externalContext));
291 assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItemFFF", null))
292 .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()");
293 assertEquals(1, externalContextAlbum.size());
295 assertThatThrownBy(() -> externalContextAlbum.put("TestExternalContextItemFFF", null))
296 .hasMessageContaining(NULL_VALUES_ILLEGAL_TAG + "\"TestExternalContextItemFFF\" for put()");
297 assertEquals(1, externalContextAlbum.size());
299 // Should ignore remove
300 externalContextAlbum.remove("TestExternalContextItem017");
301 assertEquals(1, externalContextAlbum.size());
302 assertEquals(1, externalContextAlbum.values().size());
303 assertTrue(externalContextAlbum.values().containsAll(mapValues));
305 contextDistributor.clear();
308 private void assertContextAlbumContains(final TestExternalContextItem externalContext,
309 final ContextAlbum externalContextAlbum) {
310 assertThatThrownBy(() -> externalContextAlbum.containsKey(null))
311 .hasMessageContaining("null values are illegal on method parameter \"key\"");
312 assertTrue(externalContextAlbum.containsKey(EXTERNAL_CONTEXT));
313 assertTrue(!externalContextAlbum.containsKey(GLOBAL_CONTEXT_KEY));
315 assertThatThrownBy(() -> externalContextAlbum.containsValue(null))
316 .hasMessageContaining("null values are illegal on method parameter \"value\"");
317 assertTrue(externalContextAlbum.containsValue(externalContext));
318 assertFalse(externalContextAlbum.containsValue("Hello"));
321 private ContextAlbum getContextAlbum(final Distributor contextDistributor) throws ContextException {
322 final ContextAlbum globalContextAlbum =
323 contextDistributor.createContextAlbum(new AxArtifactKey(GLOBAL_CONTEXT_ALBUM, VERSION));
324 assertNotNull(globalContextAlbum);
325 globalContextAlbum.setUserArtifactStack(Constants.getAxArtifactKeyArray());
326 return globalContextAlbum;
329 private TestGlobalContextItem getTestGlobalContextItem(final Distributor contextDistributor, final Date testDate,
330 final TestContextDateTzItem tci9, final TestContextDateLocaleItem tciA) throws ContextException {
331 final AxContextModel globalContextModel = TestContextAlbumFactory.createGlobalContextModel();
332 final TestGlobalContextItem globalContext = getTestGlobalContextItem(testDate, tci9, tciA);
333 contextDistributor.registerModel(globalContextModel);
334 return globalContext;
337 private TestGlobalContextItem getTestGlobalContextItem(final Date testDate, final TestContextDateTzItem tci9,
338 final TestContextDateLocaleItem tciA) {
339 final TestGlobalContextItem globalContext = new TestGlobalContextItem();
341 final TestContextBooleanItem testGlobalContextItem000 = new TestContextBooleanItem(false);
342 final TestContextByteItem testGlobalContextItem001 = new TestContextByteItem(BYTE_VAL);
343 final TestContextIntItem testGlobalContextItem002 = new TestContextIntItem(INT_VAL);
344 final TestContextLongItem testGlobalContextItem003 = new TestContextLongItem(LONG_VAL);
345 final TestContextFloatItem testGlobalContextItem004 = new TestContextFloatItem(FLOAT_VAL);
346 final TestContextDoubleItem testGlobalContextItem005 = new TestContextDoubleItem(PI_VAL);
347 final TestContextStringItem testGlobalContextItem006 = new TestContextStringItem(STRING_GLOBAL_VAL);
348 final TestContextLongObjectItem testGlobalContextItem007 = new TestContextLongObjectItem(testDate.getTime());
350 final TestContextDateItem testGlobalContextItem008 = new TestContextDateItem(testDate);
351 final TestContextTreeSetItem testGlobalContextItem00B = new TestContextTreeSetItem(TEST_TREE_SET);
352 final TestContextTreeMapItem testGlobalContextItem00C = new TestContextTreeMapItem(TEST_HASH_MAP);
354 globalContext.setTestGlobalContextItem000(testGlobalContextItem000);
355 globalContext.setTestGlobalContextItem001(testGlobalContextItem001);
356 globalContext.setTestGlobalContextItem002(testGlobalContextItem002);
357 globalContext.setTestGlobalContextItem003(testGlobalContextItem003);
358 globalContext.setTestGlobalContextItem004(testGlobalContextItem004);
359 globalContext.setTestGlobalContextItem005(testGlobalContextItem005);
360 globalContext.setTestGlobalContextItem006(testGlobalContextItem006);
361 globalContext.setTestGlobalContextItem007(testGlobalContextItem007);
362 globalContext.setTestGlobalContextItem008(testGlobalContextItem008);
363 globalContext.setTestGlobalContextItem009(tci9);
364 globalContext.setTestGlobalContextItem00A(tciA);
365 globalContext.setTestGlobalContextItem00B(testGlobalContextItem00B);
366 globalContext.setTestGlobalContextItem00C(testGlobalContextItem00C);
367 return globalContext;
370 private TestPolicyContextItem getTestPolicyContextItem(final ContextAlbum policyContextAlbum, final Date testDate) {
371 final TestContextStringItem contextStringItem = new TestContextStringItem(STRING_VAL);
372 final TestContextLongItem contextLongItem = new TestContextLongItem(LONG_VAL);
373 final TestContextDoubleItem contextDoubleItem = new TestContextDoubleItem(PI_VAL);
374 final TestContextBooleanItem contextBooleanItem = new TestContextBooleanItem(true);
375 final TestContextLongItem contextLongItem2 = new TestContextLongItem(testDate.getTime());
376 final TestContextTreeMapItem contextTreeMapItem = new TestContextTreeMapItem(TEST_HASH_MAP);
378 final Map<String, Object> valueMapA = new LinkedHashMap<>();
379 valueMapA.put(TEST_POLICY_CONTEXT_ITEM001, contextLongItem);
380 valueMapA.put(TEST_POLICY_CONTEXT_ITEM002, contextDoubleItem);
381 valueMapA.put(TEST_POLICY_CONTEXT_ITEM003, contextBooleanItem);
382 valueMapA.put(TEST_POLICY_CONTEXT_ITEM004, contextLongItem2);
383 valueMapA.put(TEST_POLICY_CONTEXT_ITEM005, contextTreeMapItem);
384 valueMapA.put(TEST_POLICY_CONTEXT_ITEM000, contextStringItem);
386 assertPutMethods(policyContextAlbum, contextStringItem, valueMapA);
388 final TestPolicyContextItem policyContext = new TestPolicyContextItem();
390 LOGGER.debug(policyContextAlbum.toString());
392 policyContext.setTestPolicyContextItem000(contextStringItem);
393 policyContext.setTestPolicyContextItem001(contextLongItem);
394 policyContext.setTestPolicyContextItem002(contextDoubleItem);
395 policyContext.setTestPolicyContextItem003(contextBooleanItem);
396 policyContext.setTestPolicyContextItem004(contextLongItem2);
397 policyContext.setTestPolicyContextItem005(contextTreeMapItem);
398 return policyContext;
401 private void assertPutMethods(final ContextAlbum policyContextAlbum, final TestContextStringItem contextStringItem,
402 final Map<String, Object> valueMapA) {
403 assertThatThrownBy(() -> policyContextAlbum.put(TEST_POLICY_CONTEXT_ITEM000, contextStringItem))
404 .hasMessageContaining(getMessage(TEST_POLICY_CONTEXT_ITEM000, "TestContextItem006",
405 TestContextStringItem.class.getName(), "stringValue=" + STRING_VAL));
406 assertThatThrownBy(() -> policyContextAlbum.putAll(valueMapA))
407 .hasMessageContaining(getMessage(TEST_POLICY_CONTEXT_ITEM001, "TestContextItem003",
408 TestContextLongItem.class.getName(), "longValue=" + INT_VAL_3));
411 private AxContextModel getAxContextModel() {
412 final AxContextModel policyContextModel = createPolicyContextModel();
413 final AxValidationResult result = new AxValidationResult();
414 policyContextModel.validate(result);
415 LOGGER.debug(result.toString());
417 assertTrue(result.isValid());
418 return policyContextModel;
421 private TestContextDateLocaleItem getTestContextDateLocaleItem(final Date testDate) {
422 final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem();
423 tciA.setDateValue(new TestContextDateItem(testDate));
424 tciA.setTzValue(TIME_ZONE.getDisplayName());
426 tciA.setUtcOffset(-600);
427 tciA.setLocale(Locale.ENGLISH);
431 private TestContextDateTzItem getTestContextDateTzItem(final Date testDate) {
432 final TestContextDateTzItem tci9 = new TestContextDateTzItem();
433 tci9.setDateValue(new TestContextDateItem(testDate));
434 tci9.setTzValue(TIME_ZONE.getDisplayName());
439 private TestExternalContextItem getTestExternalContextItem(final Date testDate, final TestContextDateTzItem tci9A,
440 final TestContextDateLocaleItem tciAa) {
441 final TestExternalContextItem externalContext = new TestExternalContextItem();
443 final TestContextBooleanItem testExternalContextItem000 = new TestContextBooleanItem(false);
444 final TestContextByteItem testExternalContextItem001 = new TestContextByteItem(BYTE_VAL);
445 final TestContextIntItem testExternalContextItem002 = new TestContextIntItem(INT_VAL);
446 final TestContextLongItem testExternalContextItem003 = new TestContextLongItem(LONG_VAL);
447 final TestContextFloatItem testExternalContextItem004 = new TestContextFloatItem(3.14159265359F);
448 final TestContextDoubleItem testExternalContextItem005 = new TestContextDoubleItem(PI_VAL);
449 final TestContextStringItem testExternalContextItem006 = new TestContextStringItem(STRING_EXT_VAL);
450 final TestContextLongObjectItem testExternalContextItem007 = new TestContextLongObjectItem(testDate.getTime());
451 final TestContextDateItem testExternalContextItem008 = new TestContextDateItem(testDate);
452 final TestContextTreeSetItem testExternalContextItem00B = new TestContextTreeSetItem(TEST_TREE_SET);
453 final TestContextTreeMapItem testExternalContextItem00C = new TestContextTreeMapItem(TEST_HASH_MAP);
455 externalContext.setTestExternalContextItem000(testExternalContextItem000);
456 externalContext.setTestExternalContextItem001(testExternalContextItem001);
457 externalContext.setTestExternalContextItem002(testExternalContextItem002);
458 externalContext.setTestExternalContextItem003(testExternalContextItem003);
459 externalContext.setTestExternalContextItem004(testExternalContextItem004);
460 externalContext.setTestExternalContextItem005(testExternalContextItem005);
461 externalContext.setTestExternalContextItem006(testExternalContextItem006);
462 externalContext.setTestExternalContextItem007(testExternalContextItem007);
463 externalContext.setTestExternalContextItem008(testExternalContextItem008);
464 externalContext.setTestExternalContextItem009(tci9A);
465 externalContext.setTestExternalContextItem00A(tciAa);
466 externalContext.setTestExternalContextItem00B(testExternalContextItem00B);
467 externalContext.setTestExternalContextItem00C(testExternalContextItem00C);
468 return externalContext;
471 private String getMessage(final String key, final String objName, final String clazzName, final String valString) {
472 return getMessage(key, objName, clazzName, valString, TestPolicyContextItem.class.getName());
475 private String getMessage(final String key, final String objName, final String clazzName, final String valString,
476 final String compatibleClazzName) {
477 return "Failed to set context value for key \"" + key + "\" in album \"PolicyContextAlbum:0.0.1\": "
478 + "PolicyContextAlbum:0.0.1: object \"" + objName + " [" + valString + "]\" " + "of class \""
479 + clazzName + "\"" + " not compatible with class \"" + compatibleClazzName + "\"";
482 private void assertFloat(final float actual, final float expected) {
483 assertTrue(Float.compare(actual, expected) == 0);
486 private void assertDouble(final double actual, final double expected) {
487 assertTrue(Double.compare(actual, expected) == 0);
490 private Distributor getDistributor() throws ContextException {
491 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributorInit", VERSION);
492 final Distributor distributor = new DistributorFactory().getDistributor(distributorKey);
493 final AxContextModel policyContextModel = getAxContextModel();
494 distributor.registerModel(policyContextModel);