d104900c977c1967e986601dc59235513c1b206c
[policy/apex-pdp.git] /
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.test.distribution;
22
23 import java.io.IOException;
24 import java.util.Date;
25 import java.util.HashMap;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.TimeZone;
29
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;
44
45 /**
46  * The Class TestContextUpdate checks context updates.
47  *
48  * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
49  */
50 public class ContextUpdate {
51     // Logger for this class
52     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextUpdate.class);
53
54     /**
55      * Test context update.
56      *
57      * @throws ApexModelException the apex model exception
58      * @throws IOException the IO exception
59      * @throws ApexException the apex exception
60      */
61     public void testContextUpdate() throws ApexModelException, IOException, ApexException {
62         LOGGER.debug("Running TestContextUpdate test . . .");
63
64         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
65         final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
66
67         // @formatter:off
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")
72         };
73         // @formatter:on
74
75         // CHECKSTYLE:OFF: checkstyle:magicNumber
76
77         final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
78         contextDistributor.registerModel(multiModel);
79
80         final ContextAlbum longContextAlbum =
81                 contextDistributor.createContextAlbum(new AxArtifactKey("LongContextAlbum", "0.0.1"));
82         assert (longContextAlbum != null);
83         longContextAlbum.setUserArtifactStack(usedArtifactStackArray);
84
85         final ContextAlbum dateContextAlbum =
86                 contextDistributor.createContextAlbum(new AxArtifactKey("DateContextAlbum", "0.0.1"));
87         assert (dateContextAlbum != null);
88         longContextAlbum.setUserArtifactStack(usedArtifactStackArray);
89
90         final ContextAlbum mapContextAlbum =
91                 contextDistributor.createContextAlbum(new AxArtifactKey("MapContextAlbum", "0.0.1"));
92         assert (mapContextAlbum != null);
93         mapContextAlbum.setUserArtifactStack(usedArtifactStackArray);
94
95         final TestContextItem00A tciA = new TestContextItem00A();
96         tciA.setDateValue(new TestContextItem008(new Date()));
97         tciA.setTZValue(TimeZone.getTimeZone("Europe/Dublin").getDisplayName());
98         tciA.setDST(true);
99         tciA.setUTCOffset(-600);
100         tciA.setLocale(Locale.ENGLISH);
101
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");
108
109         final TestContextItem00C tciC = new TestContextItem00C(testHashMap);
110
111         longContextAlbum.put("0", (long) 0);
112         longContextAlbum.put("0", 0);
113         longContextAlbum.put("0", "0");
114
115         try {
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\""));
121         }
122
123         try {
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\""));
129         }
130
131         try {
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()"));
137         }
138
139         try {
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()"));
144         }
145
146         Object object = dateContextAlbum.put("date0", tciA);
147         assert (object == null);
148         object = dateContextAlbum.put("date0", tciA);
149         assert (object.equals(tciA));
150
151         object = mapContextAlbum.put("map0", tciC);
152         assert (object == null);
153         object = mapContextAlbum.put("map0", tciC);
154         assert (object.equals(tciC));
155
156         contextDistributor.clear();
157         // CHECKSTYLE:ON: checkstyle:magicNumber
158     }
159 }