ae0fea7eaf96506791b904a2996a2f4387835b7b
[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 java.io.IOException;
24
25 import org.onap.policy.apex.context.ContextAlbum;
26 import org.onap.policy.apex.context.Distributor;
27 import org.onap.policy.apex.context.impl.distribution.DistributorFactory;
28 import org.onap.policy.apex.context.test.factory.TestContextAlbumFactory;
29 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
32 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
33 import org.onap.policy.apex.model.contextmodel.concepts.AxContextModel;
34 import org.onap.policy.apex.model.utilities.comparison.KeyedMapComparer;
35 import org.onap.policy.apex.model.utilities.comparison.KeyedMapDifference;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
38
39 /**
40  * The Class ContextAlbumUpdate is used to test Context Album updates.
41  */
42 public class ContextAlbumUpdate {
43     // Logger for this class
44     private static final XLogger LOGGER = XLoggerFactory.getXLogger(ContextAlbumUpdate.class);
45
46     /**
47      * Test context album update.
48      *
49      * @throws ApexModelException the apex model exception
50      * @throws IOException the IO exception
51      * @throws ApexException the apex exception
52      */
53     public void testContextAlbumUpdate() throws ApexModelException, IOException, ApexException {
54         LOGGER.debug("Running TestContextAlbumUpdate test . . .");
55
56         final AxArtifactKey distributorKey = new AxArtifactKey("ApexDistributor", "0.0.1");
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", "0.0.1"));
63         final ContextAlbum longAlbum1 = contextDistributor.createContextAlbum(longAlbum1Def.getKey());
64         assert (longAlbum1 != null);
65
66         final AxContextAlbum longAlbum2Def = longModel.getAlbums().get(new AxArtifactKey("LongContextAlbum2", "0.0.1"));
67         final ContextAlbum longAlbum2 = contextDistributor.createContextAlbum(longAlbum2Def.getKey());
68         assert (longAlbum2 != null);
69
70         // CHECKSTYLE:OFF: checkstyle:magicNumber
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         assert (0 == result0.getDifferentValues().size());
80         assert (0 == result0.getIdenticalValues().size());
81         assert (0 == result0.getRightOnly().size());
82         assert (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         assert (0 == result1.getDifferentValues().size());
90         assert (4 == result1.getIdenticalValues().size());
91         assert (0 == result1.getRightOnly().size());
92         assert (0 == result1.getLeftOnly().size());
93
94         longAlbum1.put("4", (long) 4);
95         longAlbum2.put("5", (long) 5);
96         longAlbum1.put("67", (long) 6);
97         longAlbum2.put("67", (long) 7);
98
99         final KeyedMapDifference<String, Object> result2 =
100                 new KeyedMapComparer<String, Object>().compareMaps(longAlbum1, longAlbum2);
101
102         assert (1 == result2.getDifferentValues().size());
103         assert (4 == result2.getIdenticalValues().size());
104         assert (1 == result2.getRightOnly().size());
105         assert (1 == result2.getLeftOnly().size());
106
107         longAlbum1.remove("0");
108         longAlbum2.remove("3");
109         // CHECKSTYLE:ON: checkstyle:magicNumber
110
111         final KeyedMapDifference<String, Object> result3 =
112                 new KeyedMapComparer<String, Object>().compareMaps(longAlbum1, longAlbum2);
113
114         assert (1 == result3.getDifferentValues().size());
115         assert (2 == result3.getIdenticalValues().size());
116         assert (2 == result3.getRightOnly().size());
117         assert (2 == result3.getLeftOnly().size());
118
119         contextDistributor.clear();
120     }
121 }