eb6904a045ad090491cbca631744a95a2ff9b16b
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.context.test.distribution;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.onap.policy.apex.context.test.utils.Constants.APEX_DISTRIBUTOR;
26 import static org.onap.policy.apex.context.test.utils.Constants.VERSION;
27
28 import org.onap.policy.apex.context.ContextAlbum;
29 import org.onap.policy.apex.context.Distributor;
30 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
31 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
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.slf4j.ext.XLogger;
39 import org.slf4j.ext.XLoggerFactory;
40
41 /**
42  * The Class ContextAlbumUpdate is used to test Context Album updates.
43  */
44 public class ContextAlbumUpdate {
45     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumUpdate.class);
46
47     /**
48      * Test context album update.
49      *
50      * @throws ApexException the apex exception
51      */
52     public void testContextAlbumUpdate() throws ApexException {
53         LOGGER.debug("Running TestContextAlbumUpdate test . . .");
54
55         final AxArtifactKey distributorKey = new AxArtifactKey(APEX_DISTRIBUTOR, VERSION);
56         final Distributor contextDistributor = new DistributorFactory().getDistributor(distributorKey);
57
58         final AxContextModel longModel = TestContextAlbumFactory.createLongContextModel();
59         contextDistributor.registerModel(longModel);
60
61         final AxContextAlbum longAlbum1Def = longModel.getAlbums().get(new AxArtifactKey("LongContextAlbum1", VERSION));
62         final ContextAlbum longAlbum1 = contextDistributor.createContextAlbum(longAlbum1Def.getKey());
63
64         assertNotNull(longAlbum1);
65
66         final AxContextAlbum longAlbum2Def = longModel.getAlbums().get(new AxArtifactKey("LongContextAlbum2", VERSION));
67         final ContextAlbum longAlbum2 = contextDistributor.createContextAlbum(longAlbum2Def.getKey());
68
69         assertNotNull(longAlbum2);
70
71         longAlbum1.put("0", (long) 0);
72         longAlbum1.put("1", (long) 1);
73         longAlbum1.put("2", (long) 2);
74         longAlbum1.put("3", (long) 3);
75
76         final KeyedMapDifference<String, Object> result0 =
77                 new KeyedMapComparer<String, Object>().compareMaps(longAlbum1, longAlbum2);
78
79         assertEquals(0, result0.getDifferentValues().size());
80         assertEquals(0, result0.getIdenticalValues().size());
81         assertEquals(0, result0.getRightOnly().size());
82         assertEquals(4, result0.getLeftOnly().size());
83
84         longAlbum2.putAll(longAlbum1);
85
86         final KeyedMapDifference<String, Object> result1 =
87                 new KeyedMapComparer<String, Object>().compareMaps(longAlbum1, longAlbum2);
88
89
90         assertEquals(0, result1.getDifferentValues().size());
91         assertEquals(4, result1.getIdenticalValues().size());
92         assertEquals(0, result1.getRightOnly().size());
93         assertEquals(0, result1.getLeftOnly().size());
94
95         longAlbum1.put("4", (long) 4);
96         longAlbum2.put("5", (long) 5);
97         longAlbum1.put("67", (long) 6);
98         longAlbum2.put("67", (long) 7);
99
100         final KeyedMapDifference<String, Object> result2 =
101                 new KeyedMapComparer<String, Object>().compareMaps(longAlbum1, longAlbum2);
102
103         assertEquals(1, result2.getDifferentValues().size());
104         assertEquals(4, result2.getIdenticalValues().size());
105         assertEquals(1, result2.getRightOnly().size());
106         assertEquals(1, result2.getLeftOnly().size());
107
108         longAlbum1.remove("0");
109         longAlbum2.remove("3");
110
111         final KeyedMapDifference<String, Object> result3 =
112                 new KeyedMapComparer<String, Object>().compareMaps(longAlbum1, longAlbum2);
113
114         assertEquals(1, result3.getDifferentValues().size());
115         assertEquals(2, result3.getIdenticalValues().size());
116         assertEquals(2, result3.getRightOnly().size());
117         assertEquals(2, result3.getLeftOnly().size());
118         contextDistributor.clear();
119     }
120 }