ae662f2938e784eb2cac6cc2dffd5cee00e7a8ab
[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     private static final String ZERO = "zero";
61     private static final String NUMBER_ZERO = "0";
62     // Logger for this class
63     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextUpdate.class);
64
65     /**
66      * Test context update.
67      *
68      * @throws ApexException the apex exception
69      */
70     public void testContextUpdate() throws ApexException {
71         LOGGER.debug("Running TestContextUpdate test . . .");
72
73         final Distributor contextDistributor = getDistributor();
74
75         final ContextAlbum longContextAlbum = getContextAlbum(LONG_CONTEXT_ALBUM, contextDistributor);
76         final ContextAlbum dateContextAlbum = getContextAlbum(DATE_CONTEXT_ALBUM, contextDistributor);
77         final ContextAlbum mapContextAlbum = getContextAlbum(MAP_CONTEXT_ALBUM, contextDistributor);
78
79         final TestContextDateLocaleItem tciA = getTestContextDateLocaleItem();
80         final TestContextTreeMapItem tciC = getTestContextTreeMapItem();
81
82         longContextAlbum.put(NUMBER_ZERO, (long) 0);
83         longContextAlbum.put(NUMBER_ZERO, 0);
84         longContextAlbum.put(NUMBER_ZERO, NUMBER_ZERO);
85
86         assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, ZERO))
87             .hasMessage("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\":"
88                 + " LongContextAlbum:0.0.1: object \"zero\" of class \"java.lang.String\" not compatible with"
89                 + " class \"java.lang.Long\"");
90         assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, ""))
91             .hasMessage("Failed to set context value for key \"0\" in album \"LongContextAlbum:0.0.1\": "
92                 + "LongContextAlbum:0.0.1: object \"\" of class \"java.lang.String\" not "
93                 + "compatible with class \"java.lang.Long\"");
94         assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, null))
95             .hasMessage("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()");
96         assertThatThrownBy(() -> longContextAlbum.put(null, null))
97             .hasMessage("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()");
98
99         assertNull(dateContextAlbum.put("date0", tciA));
100         assertTrue(dateContextAlbum.put("date0", tciA).equals(tciA));
101
102         assertNull(mapContextAlbum.put("map0", tciC));
103         assertTrue(mapContextAlbum.put("map0", tciC).equals(tciC));
104
105         contextDistributor.clear();
106     }
107
108     private TestContextTreeMapItem getTestContextTreeMapItem() {
109         final Map<String, String> testHashMap = new HashMap<>();
110         testHashMap.put(NUMBER_ZERO, ZERO);
111         testHashMap.put("1", "one");
112         testHashMap.put("2", "two");
113         testHashMap.put("3", "three");
114         testHashMap.put("4", "four");
115
116         return new TestContextTreeMapItem(testHashMap);
117     }
118
119     private TestContextDateLocaleItem getTestContextDateLocaleItem() {
120         final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem();
121         tciA.setDateValue(new TestContextDateItem(new Date()));
122         tciA.setTzValue(TIME_ZONE.getDisplayName());
123         tciA.setDst(true);
124         tciA.setUtcOffset(-600);
125         tciA.setLocale(Locale.ENGLISH);
126         return tciA;
127     }
128
129     private ContextAlbum getContextAlbum(final String albumKey, final Distributor contextDistributor)
130         throws ContextException {
131         final ContextAlbum longContextAlbum = contextDistributor
132             .createContextAlbum(new AxArtifactKey(albumKey, VERSION));
133         assertNotNull(longContextAlbum);
134         longContextAlbum.setUserArtifactStack(getAxArtifactKeyArray());
135         return longContextAlbum;
136     }
137
138     private Distributor getDistributor() throws ContextException {
139         final AxArtifactKey distributorKey = new AxArtifactKey(APEX_DISTRIBUTOR, VERSION);
140         final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
141
142         final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
143         contextDistributor.registerModel(multiModel);
144         return contextDistributor;
145     }
146 }