2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020,2022 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.policymodel.handling;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
27 import java.io.IOException;
28 import java.util.UUID;
29 import org.junit.Test;
30 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
32 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
33 import org.onap.policy.common.utils.resources.TextFileUtils;
35 public class PolicyModelComparerTest {
38 public void testPolicyComparer() throws IOException {
39 final AxPolicyModel leftApexModel = new SupportApexPolicyModelCreator().getModel();
40 final AxPolicyModel rightApexModel = new AxPolicyModel(leftApexModel);
42 PolicyModelComparer policyModelComparer = new PolicyModelComparer(leftApexModel, rightApexModel);
44 String resultString = policyModelComparer.asString(false, false);
45 String checkString = TextFileUtils
46 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalVerboseValues.txt");
47 assertEquals(resultString.trim().replaceAll("\\s+", ""), checkString.trim().replaceAll("\\s+", ""));
49 resultString = policyModelComparer.asString(false, true);
50 checkString = TextFileUtils
51 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalVerboseKeys.txt");
52 assertEquals(checkString.trim().replaceAll("\\s+", ""), resultString.trim().replaceAll("\\s+", ""));
54 resultString = policyModelComparer.asString(true, false);
56 TextFileUtils.getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalTerse.txt");
57 assertEquals(checkString.trim().replaceAll("\\s+", ""), resultString.trim().replaceAll("\\s+", ""));
59 resultString = policyModelComparer.asString(true, true);
61 TextFileUtils.getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalTerse.txt");
62 assertEquals(checkString.trim().replaceAll("\\s+", ""), resultString.trim().replaceAll("\\s+", ""));
64 final AxKeyInfo leftOnlyKeyInfo = new AxKeyInfo(new AxArtifactKey("LeftOnlyKeyInfo", "0.0.1"),
65 UUID.fromString("ce9168c-e6df-414f-9646-6da464b6f000"), "Left only key info");
66 final AxKeyInfo rightOnlyKeyInfo = new AxKeyInfo(new AxArtifactKey("RightOnlyKeyInfo", "0.0.1"),
67 UUID.fromString("ce9168c-e6df-414f-9646-6da464b6f001"), "Right only key info");
69 leftApexModel.getKeyInformation().getKeyInfoMap().put(leftOnlyKeyInfo.getKey(), leftOnlyKeyInfo);
70 rightApexModel.getKeyInformation().getKeyInfoMap().put(rightOnlyKeyInfo.getKey(), rightOnlyKeyInfo);
72 leftApexModel.getKeyInformation().getKeyInfoMap().get(new AxArtifactKey("inEvent", "0.0.1"))
73 .setDescription("Left InEvent Description");
74 rightApexModel.getKeyInformation().getKeyInfoMap().get(new AxArtifactKey("inEvent", "0.0.1"))
75 .setDescription("Right InEvent Description");
77 policyModelComparer = new PolicyModelComparer(leftApexModel, rightApexModel);
79 resultString = policyModelComparer.asString(false, false);
80 checkString = TextFileUtils
81 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentVerboseValues.txt");
82 assertEquals(resultString.trim().replaceAll("\\s+", ""), checkString.trim().replaceAll("\\s+", ""));
84 resultString = policyModelComparer.asString(false, true);
85 checkString = TextFileUtils
86 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentVerboseKeys.txt");
87 assertEquals(checkString.trim().replaceAll("\\s+", ""), resultString.trim().replaceAll("\\s+", ""));
89 resultString = policyModelComparer.asString(true, false);
90 checkString = TextFileUtils
91 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentTerseValues.txt");
92 assertEquals(checkString.trim().replaceAll("\\s+", ""), resultString.trim().replaceAll("\\s+", ""));
94 resultString = policyModelComparer.asString(true, true);
95 checkString = TextFileUtils
96 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentTerseKeys.txt");
97 assertEquals(checkString.trim().replaceAll("\\s+", ""), resultString.trim().replaceAll("\\s+", ""));
99 assertNotNull(policyModelComparer.getContextAlbumComparisonResult());
100 assertNotNull(policyModelComparer.getContextAlbumKeyDifference());
101 assertNotNull(policyModelComparer.getContextSchemaComparisonResult());
102 assertNotNull(policyModelComparer.getContextSchemaKeyDifference());
103 assertNotNull(policyModelComparer.getEventComparisonResult());
104 assertNotNull(policyModelComparer.getEventKeyDifference());
105 assertNotNull(policyModelComparer.getKeyInfoComparisonResult());
106 assertNotNull(policyModelComparer.getKeyInformationKeyDifference());
107 assertNotNull(policyModelComparer.getPolicyComparisonResult());
108 assertNotNull(policyModelComparer.getPolicykeyDifference());
109 assertNotNull(policyModelComparer.getPolicyModelsKeyDifference());
110 assertNotNull(policyModelComparer.getTaskComparisonResult());
111 assertNotNull(policyModelComparer.getTaskKeyDifference());
113 assertNotNull(new PolicyComparer().compare(leftApexModel.getPolicies(), rightApexModel.getPolicies()));
115 assertEquals("****** policy map differences ******\n*** context s",
116 policyModelComparer.toString().substring(0, 50));