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