2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 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.junit.Assert.assertTrue;
30 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.APEX_DISTRIBUTOR;
31 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.DATE_CONTEXT_ALBUM;
32 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.LONG_CONTEXT_ALBUM;
33 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.MAP_CONTEXT_ALBUM;
34 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.TIME_ZONE;
35 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.VERSION;
36 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.getAxArtifactKeyArray;
38 import java.util.Date;
39 import java.util.HashMap;
40 import java.util.Locale;
42 import org.onap.policy.apex.context.ContextAlbum;
43 import org.onap.policy.apex.context.ContextException;
44 import org.onap.policy.apex.context.Distributor;
45 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
46 import org.onap.policy.apex.context.test.concepts.TestContextDateItem;
47 import org.onap.policy.apex.context.test.concepts.TestContextDateLocaleItem;
48 import org.onap.policy.apex.context.test.concepts.TestContextTreeMapItem;
49 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
50 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
51 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
52 import org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory;
53 import org.slf4j.ext.XLogger;
54 import org.slf4j.ext.XLoggerFactory;
57 * The Class TestContextUpdate checks context updates.
59 * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
61 public class ContextUpdate {
62 private static final String ZERO = "zero";
63 private static final String NUMBER_ZERO = "0";
64 // Logger for this class
65 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextUpdate.class);
68 * Test context update.
70 * @throws ApexException the apex exception
72 public void testContextUpdate() throws ApexException {
73 LOGGER.debug("Running TestContextUpdate test . . .");
75 final Distributor contextDistributor = getDistributor();
77 final ContextAlbum longContextAlbum = getContextAlbum(LONG_CONTEXT_ALBUM, contextDistributor);
78 final ContextAlbum dateContextAlbum = getContextAlbum(DATE_CONTEXT_ALBUM, contextDistributor);
79 final ContextAlbum mapContextAlbum = getContextAlbum(MAP_CONTEXT_ALBUM, contextDistributor);
81 final TestContextDateLocaleItem tciA = getTestContextDateLocaleItem();
82 final TestContextTreeMapItem tciC = getTestContextTreeMapItem();
84 longContextAlbum.put(NUMBER_ZERO, (long) 0);
85 longContextAlbum.put(NUMBER_ZERO, 0);
86 longContextAlbum.put(NUMBER_ZERO, NUMBER_ZERO);
88 assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, ZERO))
89 .hasMessage("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\":"
90 + " LongContextAlbum:0.0.1: object \"zero\" of class \"java.lang.String\" not compatible with"
91 + " class \"java.lang.Long\"");
92 assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, ""))
93 .hasMessage("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": "
94 + "LongContextAlbum:0.0.1: object \"\" of class \"java.lang.String\" not "
95 + "compatible with class \"java.lang.Long\"");
96 assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, null))
97 .hasMessage("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()");
98 assertThatThrownBy(() -> longContextAlbum.put(null, null))
99 .hasMessage("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()");
101 assertNull(dateContextAlbum.put("date0", tciA));
102 assertThat(dateContextAlbum.put("date0", tciA)).isEqualTo(tciA);
104 assertNull(mapContextAlbum.put("map0", tciC));
105 assertThat(mapContextAlbum.put("map0", tciC)).isEqualTo(tciC);
107 contextDistributor.clear();
110 private TestContextTreeMapItem getTestContextTreeMapItem() {
111 final Map<String, String> testHashMap = new HashMap<>();
112 testHashMap.put(NUMBER_ZERO, ZERO);
113 testHashMap.put("1", "one");
114 testHashMap.put("2", "two");
115 testHashMap.put("3", "three");
116 testHashMap.put("4", "four");
118 return new TestContextTreeMapItem(testHashMap);
121 private TestContextDateLocaleItem getTestContextDateLocaleItem() {
122 final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem();
123 tciA.setDateValue(new TestContextDateItem(new Date()));
124 tciA.setTzValue(TIME_ZONE.getDisplayName());
126 tciA.setUtcOffset(-600);
127 tciA.setLocale(Locale.ENGLISH);
131 private ContextAlbum getContextAlbum(final String albumKey, final Distributor contextDistributor)
132 throws ContextException {
133 final ContextAlbum longContextAlbum = contextDistributor
134 .createContextAlbum(new AxArtifactKey(albumKey, VERSION));
135 assertNotNull(longContextAlbum);
136 longContextAlbum.setUserArtifactStack(getAxArtifactKeyArray());
137 return longContextAlbum;
140 private Distributor getDistributor() throws ContextException {
141 final AxArtifactKey distributorKey = new AxArtifactKey(APEX_DISTRIBUTOR, VERSION);
142 final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
144 final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
145 contextDistributor.registerModel(multiModel);
146 return contextDistributor;