2bdf7499fb89465a89dc9e5baa59e2d8b068c744
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 Nordix Foundation.
5  *  Modifications Copyright (C) 2020 Bell Canada. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.policymodel.handling;
24
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28
29 import org.junit.Test;
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.policymodel.concepts.AxPolicyModel;
33 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
34
35 /**
36  * Test model merging.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class PolicyModelMergerTest {
41
42     @Test
43     public void testPolicyModelMerger() throws ApexModelException {
44         final AxPolicyModel leftPolicyModel = new SupportApexPolicyModelCreator().getModel();
45         final AxPolicyModel rightPolicyModel = new SupportApexPolicyModelCreator().getModel();
46
47         AxPolicyModel mergedPolicyModel =
48             PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false);
49         assertEquals(leftPolicyModel, mergedPolicyModel);
50         assertEquals(rightPolicyModel, mergedPolicyModel);
51
52         leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1"));
53         assertThatThrownBy(
54             () -> PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false))
55                 .hasMessageContaining("left model is invalid: \n***validation of model failed");
56
57         leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1"));
58         assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true, false));
59
60         leftPolicyModel.getKeyInformation().generateKeyInfo(leftPolicyModel);
61         mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, true, false);
62         assertNotNull(mergedPolicyModel);
63
64         rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1"));
65         assertThatThrownBy(
66             () -> PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false))
67                 .hasMessageContaining("right model is invalid: \n***validation of model failed");
68
69         rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1"));
70         assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true, false));
71
72         rightPolicyModel.getKeyInformation().generateKeyInfo(rightPolicyModel);
73         mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false);
74         assertNotNull(mergedPolicyModel);
75
76         final AxPolicyModel rightPolicyModel2 = new SupportApexPolicyModelCreator().getAnotherModel();
77         mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel2, true, false);
78         assertNotNull(mergedPolicyModel);
79
80         mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel2, true, true);
81         assertNotNull(mergedPolicyModel);
82
83         final AxPolicyModel rightPolicyModel3 = new SupportApexPolicyModelCreator().getModel();
84         AxArtifactKey taskArtifactKey = new AxArtifactKey("task", "0.0.1");
85         // fail when concepts in two policies have same name but different definition
86         // here make up some change so as to update the definition of the task in second policy
87         rightPolicyModel3.getTasks().getTaskMap().get(taskArtifactKey)
88             .setTaskLogic(new AxTaskLogic(taskArtifactKey, "logicName", "logicFlavour", "logicImpl"));
89         assertThatThrownBy(() -> PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel3, true, true))
90             .hasMessage("\n Same task - task:0.0.1 with different definitions used in different policies");
91     }
92 }