8f066c8e301fb8a913b05ce5437f5e95ef12de7a
[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 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;
36
37 import java.io.IOException;
38 import java.util.Date;
39 import java.util.HashMap;
40 import java.util.Locale;
41 import java.util.Map;
42
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;
58
59 /**
60  * The Class TestContextUpdate checks context updates.
61  *
62  * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
63  */
64 public class ContextUpdate {
65     private static final String ZERO = "zero";
66     private static final String _0 = "0";
67     // Logger for this class
68     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextUpdate.class);
69
70     /**
71      * Test context update.
72      *
73      * @throws ApexModelException the apex model exception
74      * @throws IOException the IO exception
75      * @throws ApexException the apex exception
76      */
77     public void testContextUpdate() throws ApexModelException, IOException, ApexException {
78         LOGGER.debug("Running TestContextUpdate test . . .");
79
80         final Distributor contextDistributor = getDistributor();
81
82         final ContextAlbum longContextAlbum = getContextAlbum(LONG_CONTEXT_ALBUM, contextDistributor);
83         final ContextAlbum dateContextAlbum = getContextAlbum(DATE_CONTEXT_ALBUM, contextDistributor);
84         final ContextAlbum mapContextAlbum = getContextAlbum(MAP_CONTEXT_ALBUM, contextDistributor);
85
86         final TestContextDateLocaleItem tciA = getTestContextDateLocaleItem();
87         final TestContextTreeMapItem tciC = getTestContextTreeMapItem();
88
89         longContextAlbum.put(_0, (long) 0);
90         longContextAlbum.put(_0, 0);
91         longContextAlbum.put(_0, _0);
92
93         try {
94             longContextAlbum.put(_0, ZERO);
95             fail(EXCEPTION_MESSAGE);
96         } catch (final ContextRuntimeException e) {
97             assertEquals("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\":"
98                     + " LongContextAlbum:0.0.1: object \"zero\" of class \"java.lang.String\" not compatible with"
99                     + " class \"java.lang.Long\"", e.getMessage());
100         }
101
102         try {
103             longContextAlbum.put(_0, "");
104             fail(EXCEPTION_MESSAGE);
105         } catch (final ContextRuntimeException e) {
106             assertEquals(
107                     "Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": LongContextAlbum"
108                     + ":0.0.1: object \"\" of class \"java.lang.String\" not compatible with class \"java.lang.Long\"",
109                     e.getMessage());
110         }
111
112         try {
113             longContextAlbum.put(_0, null);
114             fail(EXCEPTION_MESSAGE);
115         } catch (final ContextRuntimeException e) {
116             assertEquals("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()",
117                     e.getMessage());
118         }
119
120         try {
121             longContextAlbum.put(null, null);
122             fail(EXCEPTION_MESSAGE);
123         } catch (final ContextRuntimeException e) {
124             assertEquals("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
125         }
126
127         assertNull(dateContextAlbum.put("date0", tciA));
128         assertTrue(dateContextAlbum.put("date0", tciA).equals(tciA));
129
130
131         assertNull(mapContextAlbum.put("map0", tciC));
132         assertTrue(mapContextAlbum.put("map0", tciC).equals(tciC));
133
134         contextDistributor.clear();
135     }
136
137     private TestContextTreeMapItem getTestContextTreeMapItem() {
138         final Map<String, String> testHashMap = new HashMap<>();
139         testHashMap.put(_0, ZERO);
140         testHashMap.put("1", "one");
141         testHashMap.put("2", "two");
142         testHashMap.put("3", "three");
143         testHashMap.put("4", "four");
144
145         final TestContextTreeMapItem tciC = new TestContextTreeMapItem(testHashMap);
146         return tciC;
147     }
148
149     private TestContextDateLocaleItem getTestContextDateLocaleItem() {
150         final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem();
151         tciA.setDateValue(new TestContextDateItem(new Date()));
152         tciA.setTZValue(TIME_ZONE.getDisplayName());
153         tciA.setDST(true);
154         tciA.setUTCOffset(-600);
155         tciA.setLocale(Locale.ENGLISH);
156         return tciA;
157     }
158
159     private ContextAlbum getContextAlbum(final String albumKey, final Distributor contextDistributor)
160             throws ContextException {
161         final ContextAlbum longContextAlbum =
162                 contextDistributor.createContextAlbum(new AxArtifactKey(albumKey, VERSION));
163         assertNotNull(longContextAlbum);
164         longContextAlbum.setUserArtifactStack(getAxArtifactKeyArray());
165         return longContextAlbum;
166     }
167
168     private Distributor getDistributor() throws ContextException {
169         final AxArtifactKey distributorKey = new AxArtifactKey(APEX_DISTRIBUTOR, VERSION);
170         final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
171
172
173         final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
174         contextDistributor.registerModel(multiModel);
175         return contextDistributor;
176     }
177 }