aboutsummaryrefslogtreecommitdiffstats
path: root/testsuites/integration/integration-context-test/src/test/java/org/onap/policy/apex/testsuites/integration/context/distribution/ContextUpdate.java
blob: ae662f2938e784eb2cac6cc2dffd5cee00e7a8ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.testsuites.integration.context.distribution;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.APEX_DISTRIBUTOR;
import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.DATE_CONTEXT_ALBUM;
import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.LONG_CONTEXT_ALBUM;
import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.MAP_CONTEXT_ALBUM;
import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.TIME_ZONE;
import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.VERSION;
import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.getAxArtifactKeyArray;

import java.util.Date;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import org.onap.policy.apex.context.ContextAlbum;
import org.onap.policy.apex.context.ContextException;
import org.onap.policy.apex.context.Distributor;
import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
import org.onap.policy.apex.context.test.concepts.TestContextDateItem;
import org.onap.policy.apex.context.test.concepts.TestContextDateLocaleItem;
import org.onap.policy.apex.context.test.concepts.TestContextTreeMapItem;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
import org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory;
import org.slf4j.ext.XLogger;
import org.slf4j.ext.XLoggerFactory;

/**
 * The Class TestContextUpdate checks context updates.
 *
 * @author Sergey Sachkov (sergey.sachkov@ericsson.com)
 */
public class ContextUpdate {
    private static final String ZERO = "zero";
    private static final String NUMBER_ZERO = "0";
    // Logger for this class
    private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextUpdate.class);

    /**
     * Test context update.
     *
     * @throws ApexException the apex exception
     */
    public void testContextUpdate() throws ApexException {
        LOGGER.debug("Running TestContextUpdate test . . .");

        final Distributor contextDistributor = getDistributor();

        final ContextAlbum longContextAlbum = getContextAlbum(LONG_CONTEXT_ALBUM, contextDistributor);
        final ContextAlbum dateContextAlbum = getContextAlbum(DATE_CONTEXT_ALBUM, contextDistributor);
        final ContextAlbum mapContextAlbum = getContextAlbum(MAP_CONTEXT_ALBUM, contextDistributor);

        final TestContextDateLocaleItem tciA = getTestContextDateLocaleItem();
        final TestContextTreeMapItem tciC = getTestContextTreeMapItem();

        longContextAlbum.put(NUMBER_ZERO, (long) 0);
        longContextAlbum.put(NUMBER_ZERO, 0);
        longContextAlbum.put(NUMBER_ZERO, NUMBER_ZERO);

        assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, ZERO))
            .hasMessage("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\"");
        assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, ""))
            .hasMessage("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\"");
        assertThatThrownBy(() -> longContextAlbum.put(NUMBER_ZERO, null))
            .hasMessage("album \"LongContextAlbum:0.0.1\" null values are illegal on key \"0\" for put()");
        assertThatThrownBy(() -> longContextAlbum.put(null, null))
            .hasMessage("album \"LongContextAlbum:0.0.1\" null keys are illegal on keys for put()");

        assertNull(dateContextAlbum.put("date0", tciA));
        assertTrue(dateContextAlbum.put("date0", tciA).equals(tciA));

        assertNull(mapContextAlbum.put("map0", tciC));
        assertTrue(mapContextAlbum.put("map0", tciC).equals(tciC));

        contextDistributor.clear();
    }

    private TestContextTreeMapItem getTestContextTreeMapItem() {
        final Map<String, String> testHashMap = new HashMap<>();
        testHashMap.put(NUMBER_ZERO, ZERO);
        testHashMap.put("1", "one");
        testHashMap.put("2", "two");
        testHashMap.put("3", "three");
        testHashMap.put("4", "four");

        return new TestContextTreeMapItem(testHashMap);
    }

    private TestContextDateLocaleItem getTestContextDateLocaleItem() {
        final TestContextDateLocaleItem tciA = new TestContextDateLocaleItem();
        tciA.setDateValue(new TestContextDateItem(new Date()));
        tciA.setTzValue(TIME_ZONE.getDisplayName());
        tciA.setDst(true);
        tciA.setUtcOffset(-600);
        tciA.setLocale(Locale.ENGLISH);
        return tciA;
    }

    private ContextAlbum getContextAlbum(final String albumKey, final Distributor contextDistributor)
        throws ContextException {
        final ContextAlbum longContextAlbum = contextDistributor
            .createContextAlbum(new AxArtifactKey(albumKey, VERSION));
        assertNotNull(longContextAlbum);
        longContextAlbum.setUserArtifactStack(getAxArtifactKeyArray());
        return longContextAlbum;
    }

    private Distributor getDistributor() throws ContextException {
        final AxArtifactKey distributorKey = new AxArtifactKey(APEX_DISTRIBUTOR, VERSION);
        final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);

        final AxContextModel multiModel = TestContextAlbumFactory.createMultiAlbumsContextModel();
        contextDistributor.registerModel(multiModel);
        return contextDistributor;
    }
}