97bfa9efdb8edda339e4ebc393658c3db1ecb40a
[policy/apex-pdp.git] /
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.onap.policy.apex.testsuites.integration.context.utils.Constants.APEX_DISTRIBUTOR;
27 import static org.onap.policy.apex.testsuites.integration.context.utils.Constants.VERSION;
28
29 import org.onap.policy.apex.context.ContextAlbum;
30 import org.onap.policy.apex.context.Distributor;
31 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
36 import org.onap.policy.apex.model.utilities.comparison.KeyedMapComparer;
37 import org.onap.policy.apex.model.utilities.comparison.KeyedMapDifference;
38 import org.onap.policy.apex.testsuites.integration.context.factory.TestContextAlbumFactory;
39 import org.slf4j.ext.XLogger;
40 import org.slf4j.ext.XLoggerFactory;
41
42 /**
43  * The Class ContextAlbumUpdate is used to test Context Album updates.
44  */
45 public class ContextAlbumUpdate {
46     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumUpdate.class);
47
48     /**
49      * Test context album update.
50      *
51      * @throws ApexException the apex exception
52      */
53     public void testContextAlbumUpdate() throws ApexException {
54         LOGGER.debug("Running TestContextAlbumUpdate test . . .");
55
56         final AxArtifactKey distributorKey = new AxArtifactKey(APEX_DISTRIBUTOR, VERSION);
57         final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
58
59         final AxContextModel longModel = TestContextAlbumFactory.createLongContextModel();
60         contextDistributor.registerModel(longModel);
61
62         final AxContextAlbum longAlbum1Def = longModel.getAlbums().get(new AxArtifactKey("LongContextAlbum1", VERSION));
63         final ContextAlbum longAlbum1 = contextDistributor.createContextAlbum(longAlbum1Def.getKey());
64
65         assertNotNull(longAlbum1);
66
67         final AxContextAlbum longAlbum2Def = longModel.getAlbums().get(new AxArtifactKey("LongContextAlbum2", VERSION));
68         final ContextAlbum longAlbum2 = contextDistributor.createContextAlbum(longAlbum2Def.getKey());
69
70         assertNotNull(longAlbum2);
71
72         longAlbum1.put("0", (long) 0);
73         longAlbum1.put("1", (long) 1);
74         longAlbum1.put("2", (long) 2);
75         longAlbum1.put("3", (long) 3);
76
77         final KeyedMapDifference<String, Object> result0 =
78                 new KeyedMapComparer<String, Object>().compareMaps(longAlbum1, longAlbum2);
79
80         assertEquals(0, result0.getDifferentValues().size());
81         assertEquals(0, result0.getIdenticalValues().size());
82         assertEquals(0, result0.getRightOnly().size());
83         assertEquals(4, result0.getLeftOnly().size());
84
85         longAlbum2.putAll(longAlbum1);
86
87         final KeyedMapDifference<String, Object> result1 =
88                 new KeyedMapComparer<String, Object>().compareMaps(longAlbum1, longAlbum2);
89
90
91         assertEquals(0, result1.getDifferentValues().size());
92         assertEquals(4, result1.getIdenticalValues().size());
93         assertEquals(0, result1.getRightOnly().size());
94         assertEquals(0, result1.getLeftOnly().size());
95
96         longAlbum1.put("4", (long) 4);
97         longAlbum2.put("5", (long) 5);
98         longAlbum1.put("67", (long) 6);
99         longAlbum2.put("67", (long) 7);
100
101         final KeyedMapDifference<String, Object> result2 =
102                 new KeyedMapComparer<String, Object>().compareMaps(longAlbum1, longAlbum2);
103
104         assertEquals(1, result2.getDifferentValues().size());
105         assertEquals(4, result2.getIdenticalValues().size());
106         assertEquals(1, result2.getRightOnly().size());
107         assertEquals(1, result2.getLeftOnly().size());
108
109         longAlbum1.remove("0");
110         longAlbum2.remove("3");
111
112         final KeyedMapDifference<String, Object> result3 =
113                 new KeyedMapComparer<String, Object>().compareMaps(longAlbum1, longAlbum2);
114
115         assertEquals(1, result3.getDifferentValues().size());
116         assertEquals(2, result3.getIdenticalValues().size());
117         assertEquals(2, result3.getRightOnly().size());
118         assertEquals(2, result3.getLeftOnly().size());
119         contextDistributor.clear();
120     }
121 }