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