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 java.io.IOException;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.Locale;
28 import java.util.TimeZone;
30 import org.onap.policy.apex.context.ContextAlbum;
31 import org.onap.policy.apex.context.ContextRuntimeException;
32 import org.onap.policy.apex.context.Distributor;
33 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
34 import org.onap.policy.apex.context.test.concepts.TestContextItem008;
35 import org.onap.policy.apex.context.test.concepts.TestContextItem00A;
36 import org.onap.policy.apex.context.test.concepts.TestContextItem00C;
37 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
38 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
39 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
40 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
41 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
42 import org.slf4j.ext.XLogger;
43 import org.slf4j.ext.XLoggerFactory;
46 * The Class TestContextUpdate checks context updates.
48 * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
50 public class ContextUpdate {
51 // Logger for this class
52 private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextUpdate.class);
55 * Test context update.
57 * @throws ApexModelException the apex model exception
58 * @throws IOException the IO exception
59 * @throws ApexException the apex exception
61 public void testContextUpdate() throws ApexModelException, IOException, ApexException {
62 LOGGER.debug("Running TestContextUpdate test . . .");
64 final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
65 final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
68 final AxArtifactKey[] usedArtifactStackArray = {
69 new AxArtifactKey("testC-top", "0.0.1"),
70 new AxArtifactKey("testC-next", "0.0.1"),
71 new AxArtifactKey("testC-bot", "0.0.1")
75 // CHECKSTYLE:OFF: checkstyle:magicNumber
77 final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
78 contextDistributor.registerModel(multiModel);
80 final ContextAlbum longContextAlbum =
81 contextDistributor.createContextAlbum(new AxArtifactKey("LongContextAlbum", "0.0.1"));
82 assert (longContextAlbum != null);
83 longContextAlbum.setUserArtifactStack(usedArtifactStackArray);
85 final ContextAlbum dateContextAlbum =
86 contextDistributor.createContextAlbum(new AxArtifactKey("DateContextAlbum", "0.0.1"));
87 assert (dateContextAlbum != null);
88 longContextAlbum.setUserArtifactStack(usedArtifactStackArray);
90 final ContextAlbum mapContextAlbum =
91 contextDistributor.createContextAlbum(new AxArtifactKey("MapContextAlbum", "0.0.1"));
92 assert (mapContextAlbum != null);
93 mapContextAlbum.setUserArtifactStack(usedArtifactStackArray);
95 final TestContextItem00A tciA = new TestContextItem00A();
96 tciA.setDateValue(new TestContextItem008(new Date()));
97 tciA.setTZValue(TimeZone.getTimeZone("Europe/Dublin").getDisplayName());
99 tciA.setUTCOffset(-600);
100 tciA.setLocale(Locale.ENGLISH);
102 final Map<String, String> testHashMap = new HashMap<>();
103 testHashMap.put("0", "zero");
104 testHashMap.put("1", "one");
105 testHashMap.put("2", "two");
106 testHashMap.put("3", "three");
107 testHashMap.put("4", "four");
109 final TestContextItem00C tciC = new TestContextItem00C(testHashMap);
111 longContextAlbum.put("0", (long) 0);
112 longContextAlbum.put("0", 0);
113 longContextAlbum.put("0", "0");
116 longContextAlbum.put("0", "zero");
117 assert ("Test should throw an exception".equals(""));
118 } catch (final ContextRuntimeException e) {
119 assert (e.getMessage().equals(
120 "Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": LongContextAlbum:0.0.1: object \"zero\" of class \"java.lang.String\" not compatible with class \"java.lang.Long\""));
124 longContextAlbum.put("0", "");
125 assert ("Test should throw an exception".equals(""));
126 } catch (final ContextRuntimeException e) {
127 assert (e.getMessage().equals(
128 "Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": LongContextAlbum:0.0.1: object \"\" of class \"java.lang.String\" not compatible with class \"java.lang.Long\""));
132 longContextAlbum.put("0", null);
133 assert ("Test should throw an exception".equals(""));
134 } catch (final ContextRuntimeException e) {
135 assert (e.getMessage()
136 .equals("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()"));
140 longContextAlbum.put(null, null);
141 assert ("Test should throw an exception".equals(""));
142 } catch (final ContextRuntimeException e) {
143 assert (e.getMessage().equals("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()"));
146 Object object = dateContextAlbum.put("date0", tciA);
147 assert (object == null);
148 object = dateContextAlbum.put("date0", tciA);
149 assert (object.equals(tciA));
151 object = mapContextAlbum.put("map0", tciC);
152 assert (object == null);
153 object = mapContextAlbum.put("map0", tciC);
154 assert (object.equals(tciC));
156 contextDistributor.clear();
157 // CHECKSTYLE:ON: checkstyle:magicNumber