Changes for checkstyle 8.32
[policy/apex-pdp.git] / model / policy-model / src / test / java / org / onap / policy / apex / model / policymodel / handling / PolicyModelComparerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 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.model.policymodel.handling;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.IOException;
29 import java.util.UUID;
30 import org.junit.Test;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInfo;
33 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
34 import org.onap.policy.common.utils.resources.TextFileUtils;
35
36 public class PolicyModelComparerTest {
37
38     @Test
39     public void testPolicyComparer() throws IOException {
40         final AxPolicyModel leftApexModel = new SupportApexPolicyModelCreator().getModel();
41         final AxPolicyModel rightApexModel = new AxPolicyModel(leftApexModel);
42
43         PolicyModelComparer policyModelComparer = new PolicyModelComparer(leftApexModel, rightApexModel);
44
45         String resultString = policyModelComparer.asString(false, false);
46         String checkString = TextFileUtils
47                 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalVerboseValues.txt");
48         assertEquals(resultString.trim().replaceAll("[\\r?\\n]+", " "),
49                 checkString.trim().replaceAll("[\\r?\\n]+", " "));
50
51         resultString = policyModelComparer.asString(false, true);
52         checkString = TextFileUtils
53                 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalVerboseKeys.txt");
54         assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ")
55                 .equals(checkString.trim().replaceAll("[\\r?\\n]+", " ")));
56
57         resultString = policyModelComparer.asString(true, false);
58         checkString = TextFileUtils
59                 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalTerse.txt");
60         assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ")
61                 .equals(checkString.trim().replaceAll("[\\r?\\n]+", " ")));
62
63         resultString = policyModelComparer.asString(true, true);
64         checkString = TextFileUtils
65                 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonIdenticalTerse.txt");
66         assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ")
67                 .equals(checkString.trim().replaceAll("[\\r?\\n]+", " ")));
68
69         final AxKeyInfo leftOnlyKeyInfo = new AxKeyInfo(new AxArtifactKey("LeftOnlyKeyInfo", "0.0.1"),
70                 UUID.fromString("ce9168c-e6df-414f-9646-6da464b6f000"), "Left only key info");
71         final AxKeyInfo rightOnlyKeyInfo = new AxKeyInfo(new AxArtifactKey("RightOnlyKeyInfo", "0.0.1"),
72                 UUID.fromString("ce9168c-e6df-414f-9646-6da464b6f001"), "Right only key info");
73
74         leftApexModel.getKeyInformation().getKeyInfoMap().put(leftOnlyKeyInfo.getKey(), leftOnlyKeyInfo);
75         rightApexModel.getKeyInformation().getKeyInfoMap().put(rightOnlyKeyInfo.getKey(), rightOnlyKeyInfo);
76
77         leftApexModel.getKeyInformation().getKeyInfoMap().get(new AxArtifactKey("inEvent", "0.0.1"))
78                 .setDescription("Left InEvent Description");
79         rightApexModel.getKeyInformation().getKeyInfoMap().get(new AxArtifactKey("inEvent", "0.0.1"))
80                 .setDescription("Right InEvent Description");
81
82         policyModelComparer = new PolicyModelComparer(leftApexModel, rightApexModel);
83
84         resultString = policyModelComparer.asString(false, false);
85         checkString = TextFileUtils
86                 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentVerboseValues.txt");
87         assertEquals(resultString.trim().replaceAll("[\\r?\\n]+", " "),
88                 checkString.trim().replaceAll("[\\r?\\n]+", " "));
89
90         resultString = policyModelComparer.asString(false, true);
91         checkString = TextFileUtils
92                 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentVerboseKeys.txt");
93         assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ")
94                 .equals(checkString.trim().replaceAll("[\\r?\\n]+", " ")));
95
96         resultString = policyModelComparer.asString(true, false);
97         checkString = TextFileUtils
98                 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentTerseValues.txt");
99         assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ")
100                 .equals(checkString.trim().replaceAll("[\\r?\\n]+", " ")));
101
102         resultString = policyModelComparer.asString(true, true);
103         checkString = TextFileUtils
104                 .getTextFileAsString("src/test/resources/checkFiles/PolicyModelComparisonDifferentTerseKeys.txt");
105         assertTrue(resultString.trim().replaceAll("[\\r?\\n]+", " ")
106                 .equals(checkString.trim().replaceAll("[\\r?\\n]+", " ")));
107
108         assertNotNull(policyModelComparer.getContextAlbumComparisonResult());
109         assertNotNull(policyModelComparer.getContextAlbumKeyDifference());
110         assertNotNull(policyModelComparer.getContextSchemaComparisonResult());
111         assertNotNull(policyModelComparer.getContextSchemaKeyDifference());
112         assertNotNull(policyModelComparer.getEventComparisonResult());
113         assertNotNull(policyModelComparer.getEventKeyDifference());
114         assertNotNull(policyModelComparer.getKeyInfoComparisonResult());
115         assertNotNull(policyModelComparer.getKeyInformationKeyDifference());
116         assertNotNull(policyModelComparer.getPolicyComparisonResult());
117         assertNotNull(policyModelComparer.getPolicykeyDifference());
118         assertNotNull(policyModelComparer.getPolicyModelsKeyDifference());
119         assertNotNull(policyModelComparer.getTaskComparisonResult());
120         assertNotNull(policyModelComparer.getTaskKeyDifference());
121
122         assertNotNull(new PolicyComparer().compare(leftApexModel.getPolicies(), rightApexModel.getPolicies()));
123
124         assertEquals("****** policy map differences ******\n*** context s",
125                 policyModelComparer.toString().substring(0, 50));
126     }
127 }