2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020,2022 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.testsuites.integration.context.distribution;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.APEX_DISTRIBUTOR;
30 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.DATE_CONTEXT_ALBUM;
31 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.LONG_CONTEXT_ALBUM;
32 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.MAP_CONTEXT_ALBUM;
33 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.TIME_ZONE;
34 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.VERSION;
35 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.getAxArtifactKeyArray;
37 import java.util.Date;
38 import java.util.HashMap;
39 import java.util.Locale;
41 import org.onap.policy.apex.context.ContextAlbum;
42 import org.onap.policy.apex.context.ContextException;
43 import org.onap.policy.apex.context.Distributor;
44 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
45 import org.onap.policy.apex.context.test.concepts.TestContextDateItem;
46 import org.onap.policy.apex.context.test.concepts.TestContextDateLocaleItem;
47 import org.onap.policy.apex.context.test.concepts.TestContextTreeMapItem;
48 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
49 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
50 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
51 import org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory;
52 import org.slf4j.ext.XLogger;
53 import org.slf4j.ext.XLoggerFactory;
56 * The Class TestContextUpdate checks context updates.
58 * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
60 public class ContextUpdate {
61 private static final String ZERO = "zero";
62 private static final String NUMBER_ZERO = "0";
63 // Logger for this class
64 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextUpdate.class);
67 * Test context update.
69 * @throws ApexException the apex exception
71 public void testContextUpdate() throws ApexException {
72 LOGGER.debug("Running TestContextUpdate test . . .");
74 final Distributor contextDistributor = getDistributor();
76 final ContextAlbum longContextAlbum = getContextAlbum(LONG_CONTEXT_ALBUM, contextDistributor);
77 final ContextAlbum dateContextAlbum = getContextAlbum(DATE_CONTEXT_ALBUM, contextDistributor);
78 final ContextAlbum mapContextAlbum = getContextAlbum(MAP_CONTEXT_ALBUM, contextDistributor);
80 final TestContextDateLocaleItem tciA = getTestContextDateLocaleItem();
81 final TestContextTreeMapItem tciC = getTestContextTreeMapItem();
83 longContextAlbum.put(NUMBER_ZERO, (long) 0);
84 longContextAlbum.put(NUMBER_ZERO, 0);
85 longContextAlbum.put(NUMBER_ZERO, NUMBER_ZERO);
87 assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, ZERO))
88 .hasMessage("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\":"
89 + " LongContextAlbum:0.0.1: object \"zero\" of class \"java.lang.String\" not compatible with"
90 + " class \"java.lang.Long\"");
91 assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, ""))
92 .hasMessage("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": "
93 + "LongContextAlbum:0.0.1: object \"\" of class \"java.lang.String\" not "
94 + "compatible with class \"java.lang.Long\"");
95 assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, null))
96 .hasMessage("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()");
97 assertThatThrownBy(() -> longContextAlbum.put(null, null))
98 .hasMessage("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()");
100 assertNull(dateContextAlbum.put("date0", tciA));
101 assertThat(dateContextAlbum.put("date0", tciA)).isEqualTo(tciA);
103 assertNull(mapContextAlbum.put("map0", tciC));
104 assertThat(mapContextAlbum.put("map0", tciC)).isEqualTo(tciC);
106 contextDistributor.clear();
109 private TestContextTreeMapItem getTestContextTreeMapItem() {
110 final Map<String, String> testHashMap = new HashMap<>();
111 testHashMap.put(NUMBER_ZERO, ZERO);
112 testHashMap.put("1", "one");
113 testHashMap.put("2", "two");
114 testHashMap.put("3", "three");
115 testHashMap.put("4", "four");
117 return new TestContextTreeMapItem(testHashMap);
120 private TestContextDateLocaleItem getTestContextDateLocaleItem() {
121 final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem();
122 tciA.setDateValue(new TestContextDateItem(new Date()));
123 tciA.setTzValue(TIME_ZONE.getDisplayName());
125 tciA.setUtcOffset(-600);
126 tciA.setLocale(Locale.ENGLISH);
130 private ContextAlbum getContextAlbum(final String albumKey, final Distributor contextDistributor)
131 throws ContextException {
132 final ContextAlbum longContextAlbum = contextDistributor
133 .createContextAlbum(new AxArtifactKey(albumKey, VERSION));
134 assertNotNull(longContextAlbum);
135 longContextAlbum.setUserArtifactStack(getAxArtifactKeyArray());
136 return longContextAlbum;
139 private Distributor getDistributor() throws ContextException {
140 final AxArtifactKey distributorKey = new AxArtifactKey(APEX_DISTRIBUTOR, VERSION);
141 final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
143 final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
144 contextDistributor.registerModel(multiModel);
145 return contextDistributor;