Changes for checkstyle 8.32
[policy/apex-pdp.git] / testsuites / integration / integration-context-test / src / test / java / org / onap / policy / apex / testsuites / integration / context / distribution / ContextUpdate.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.testsuites.integration.context.distribution;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
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.EXCEPTION_MESSAGE;
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;
37
38 import java.util.Date;
39 import java.util.HashMap;
40 import java.util.Locale;
41 import java.util.Map;
42 import org.onap.policy.apex.context.ContextAlbum;
43 import org.onap.policy.apex.context.ContextException;
44 import org.onap.policy.apex.context.ContextRuntimeException;
45 import org.onap.policy.apex.context.Distributor;
46 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
47 import org.onap.policy.apex.context.test.concepts.TestContextDateItem;
48 import org.onap.policy.apex.context.test.concepts.TestContextDateLocaleItem;
49 import org.onap.policy.apex.context.test.concepts.TestContextTreeMapItem;
50 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
51 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
52 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
53 import org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory;
54 import org.slf4j.ext.XLogger;
55 import org.slf4j.ext.XLoggerFactory;
56
57 /**
58  * The Class TestContextUpdate checks context updates.
59  *
60  * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
61  */
62 public class ContextUpdate {
63     // Recurring string constants.
64     private static final String NORMAL_TEST_EXCEPTION = "normal test exception";
65
66     private static final String ZERO = "zero";
67     private static final String NUMBER_ZERO = "0";
68     // Logger for this class
69     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextUpdate.class);
70
71     /**
72      * Test context update.
73      *
74      * @throws ApexException the apex exception
75      */
76     public void testContextUpdate() throws ApexException {
77         LOGGER.debug("Running TestContextUpdate test . . .");
78
79         final Distributor contextDistributor = getDistributor();
80
81         final ContextAlbum longContextAlbum = getContextAlbum(LONG_CONTEXT_ALBUM, contextDistributor);
82         final ContextAlbum dateContextAlbum = getContextAlbum(DATE_CONTEXT_ALBUM, contextDistributor);
83         final ContextAlbum mapContextAlbum = getContextAlbum(MAP_CONTEXT_ALBUM, contextDistributor);
84
85         final TestContextDateLocaleItem tciA = getTestContextDateLocaleItem();
86         final TestContextTreeMapItem tciC = getTestContextTreeMapItem();
87
88         longContextAlbum.put(NUMBER_ZERO, (long) 0);
89         longContextAlbum.put(NUMBER_ZERO, 0);
90         longContextAlbum.put(NUMBER_ZERO, NUMBER_ZERO);
91
92         try {
93             longContextAlbum.put(NUMBER_ZERO, ZERO);
94             fail(EXCEPTION_MESSAGE);
95         } catch (final ContextRuntimeException e) {
96             assertEquals("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\":"
97                 + " LongContextAlbum:0.0.1: object \"zero\" of class \"java.lang.String\" not compatible with"
98                 + " class \"java.lang.Long\"", e.getMessage());
99             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
100         }
101
102         try {
103             longContextAlbum.put(NUMBER_ZERO, "");
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             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
111         }
112
113         try {
114             longContextAlbum.put(NUMBER_ZERO, null);
115             fail(EXCEPTION_MESSAGE);
116         } catch (final ContextRuntimeException e) {
117             assertEquals("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()",
118                 e.getMessage());
119             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
120         }
121
122         try {
123             longContextAlbum.put(null, null);
124             fail(EXCEPTION_MESSAGE);
125         } catch (final ContextRuntimeException e) {
126             assertEquals("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage());
127             LOGGER.trace(NORMAL_TEST_EXCEPTION, e);
128         }
129
130         assertNull(dateContextAlbum.put("date0", tciA));
131         assertTrue(dateContextAlbum.put("date0", tciA).equals(tciA));
132
133         assertNull(mapContextAlbum.put("map0", tciC));
134         assertTrue(mapContextAlbum.put("map0", tciC).equals(tciC));
135
136         contextDistributor.clear();
137     }
138
139     private TestContextTreeMapItem getTestContextTreeMapItem() {
140         final Map<String, String> testHashMap = new HashMap<>();
141         testHashMap.put(NUMBER_ZERO, ZERO);
142         testHashMap.put("1", "one");
143         testHashMap.put("2", "two");
144         testHashMap.put("3", "three");
145         testHashMap.put("4", "four");
146
147         return new TestContextTreeMapItem(testHashMap);
148     }
149
150     private TestContextDateLocaleItem getTestContextDateLocaleItem() {
151         final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem();
152         tciA.setDateValue(new TestContextDateItem(new Date()));
153         tciA.setTzValue(TIME_ZONE.getDisplayName());
154         tciA.setDst(true);
155         tciA.setUtcOffset(-600);
156         tciA.setLocale(Locale.ENGLISH);
157         return tciA;
158     }
159
160     private ContextAlbum getContextAlbum(final String albumKey, final Distributor contextDistributor)
161         throws ContextException {
162         final ContextAlbum longContextAlbum = contextDistributor
163             .createContextAlbum(new AxArtifactKey(albumKey, VERSION));
164         assertNotNull(longContextAlbum);
165         longContextAlbum.setUserArtifactStack(getAxArtifactKeyArray());
166         return longContextAlbum;
167     }
168
169     private Distributor getDistributor() throws ContextException {
170         final AxArtifactKey distributorKey = new AxArtifactKey(APEX_DISTRIBUTOR, VERSION);
171         final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
172
173         final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
174         contextDistributor.registerModel(multiModel);
175         return contextDistributor;
176     }
177 }