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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.context.test.distribution;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28 import static org.onap.policy.apex.context.test.utils.Constants.APEX_DISTRIBUTOR;
29 import static org.onap.policy.apex.context.test.utils.Constants.DATE_CONTEXT_ALBUM;
30 import static org.onap.policy.apex.context.test.utils.Constants.EXCEPTION_MESSAGE;
31 import static org.onap.policy.apex.context.test.utils.Constants.LONG_CONTEXT_ALBUM;
32 import static org.onap.policy.apex.context.test.utils.Constants.MAP_CONTEXT_ALBUM;
33 import static org.onap.policy.apex.context.test.utils.Constants.TIME_ZONE;
34 import static org.onap.policy.apex.context.test.utils.Constants.VERSION;
35 import static org.onap.policy.apex.context.test.utils.Constants.getAxArtifactKeyArray;
37 import java.io.IOException;
38 import java.util.Date;
39 import java.util.HashMap;
40 import java.util.Locale;
43 import org.onap.policy.apex.context.ContextAlbum;
44 import org.onap.policy.apex.context.ContextException;
45 import org.onap.policy.apex.context.ContextRuntimeException;
46 import org.onap.policy.apex.context.Distributor;
47 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
48 import org.onap.policy.apex.context.test.concepts.TestContextDateItem;
49 import org.onap.policy.apex.context.test.concepts.TestContextDateLocaleItem;
50 import org.onap.policy.apex.context.test.concepts.TestContextTreeMapItem;
51 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
52 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
53 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
54 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
55 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
56 import org.slf4j.ext.XLogger;
57 import org.slf4j.ext.XLoggerFactory;
60 * The Class TestContextUpdate checks context updates.
62 * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
64 public class ContextUpdate {
65 // Recurring string constants.
66 private static final String NORMAL_TEST_EXCEPTION = "normal test exception";
68 private static final String ZERO = "zero";
69 private static final String NUMBER_ZERO = "0";
70 // Logger for this class
71 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextUpdate.class);
74 * Test context update.
76 * @throws ApexModelException the apex model exception
77 * @throws IOException the IO exception
78 * @throws ApexException the apex exception
80 public void testContextUpdate() throws IOException, ApexException {
81 LOGGER.debug("Running TestContextUpdate test . . .");
83 final Distributor contextDistributor = getDistributor();
85 final ContextAlbum longContextAlbum = getContextAlbum(LONG_CONTEXT_ALBUM, contextDistributor);
86 final ContextAlbum dateContextAlbum = getContextAlbum(DATE_CONTEXT_ALBUM, contextDistributor);
87 final ContextAlbum mapContextAlbum = getContextAlbum(MAP_CONTEXT_ALBUM, contextDistributor);
89 final TestContextDateLocaleItem tciA = getTestContextDateLocaleItem();
90 final TestContextTreeMapItem tciC = getTestContextTreeMapItem();
92 longContextAlbum.put(NUMBER_ZERO, (long) 0);
93 longContextAlbum.put(NUMBER_ZERO, 0);
94 longContextAlbum.put(NUMBER_ZERO, NUMBER_ZERO);
97 longContextAlbum.put(NUMBER_ZERO, ZERO);
98 fail(EXCEPTION_MESSAGE);
99 } catch (final ContextRuntimeException e) {
100 assertEquals("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\":"
101 + " LongContextAlbum:0.0.1: object \"zero\" of class \"java.lang.String\" not compatible with"
102 + " class \"java.lang.Long\"", e.getMessage());
103 LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
107 longContextAlbum.put(NUMBER_ZERO, "");
108 fail(EXCEPTION_MESSAGE);
109 } catch (final ContextRuntimeException e) {
111 "Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": LongContextAlbum"
112 + ":0.0.1: object \"\" of class \"java.lang.String\" not compatible with class \"java.lang.Long\"",
114 LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
118 longContextAlbum.put(NUMBER_ZERO, null);
119 fail(EXCEPTION_MESSAGE);
120 } catch (final ContextRuntimeException e) {
121 assertEquals("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()",
123 LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
127 longContextAlbum.put(null, null);
128 fail(EXCEPTION_MESSAGE);
129 } catch (final ContextRuntimeException e) {
130 assertEquals("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
131 LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
134 assertNull(dateContextAlbum.put("date0", tciA));
135 assertTrue(dateContextAlbum.put("date0", tciA).equals(tciA));
138 assertNull(mapContextAlbum.put("map0", tciC));
139 assertTrue(mapContextAlbum.put("map0", tciC).equals(tciC));
141 contextDistributor.clear();
144 private TestContextTreeMapItem getTestContextTreeMapItem() {
145 final Map<String, String> testHashMap = new HashMap<>();
146 testHashMap.put(NUMBER_ZERO, ZERO);
147 testHashMap.put("1", "one");
148 testHashMap.put("2", "two");
149 testHashMap.put("3", "three");
150 testHashMap.put("4", "four");
152 return new TestContextTreeMapItem(testHashMap);
155 private TestContextDateLocaleItem getTestContextDateLocaleItem() {
156 final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem();
157 tciA.setDateValue(new TestContextDateItem(new Date()));
158 tciA.setTzValue(TIME_ZONE.getDisplayName());
160 tciA.setUtcOffset(-600);
161 tciA.setLocale(Locale.ENGLISH);
165 private ContextAlbum getContextAlbum(final String albumKey, final Distributor contextDistributor)
166 throws ContextException {
167 final ContextAlbum longContextAlbum =
168 contextDistributor.createContextAlbum(new AxArtifactKey(albumKey, VERSION));
169 assertNotNull(longContextAlbum);
170 longContextAlbum.setUserArtifactStack(getAxArtifactKeyArray());
171 return longContextAlbum;
174 private Distributor getDistributor() throws ContextException {
175 final AxArtifactKey distributorKey = new AxArtifactKey(APEX_DISTRIBUTOR, VERSION);
176 final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
179 final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
180 contextDistributor.registerModel(multiModel);
181 return contextDistributor;