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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.policymodel.handling;
25 import static org.assertj.core.api.Assertions.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
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;
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class PolicyModelMergerTest {
43 public void testPolicyModelMerger() throws ApexModelException {
44 final AxPolicyModel leftPolicyModel = new SupportApexPolicyModelCreator().getModel();
45 final AxPolicyModel rightPolicyModel = new SupportApexPolicyModelCreator().getModel();
47 AxPolicyModel mergedPolicyModel =
48 PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false);
49 assertEquals(leftPolicyModel, mergedPolicyModel);
50 assertEquals(rightPolicyModel, mergedPolicyModel);
52 leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1"));
54 () -> PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false))
55 .hasMessageContaining("left model is invalid: \n***validation of model failed");
57 leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1"));
58 assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true, false));
60 leftPolicyModel.getKeyInformation().generateKeyInfo(leftPolicyModel);
61 mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, true, false);
62 assertNotNull(mergedPolicyModel);
64 rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1"));
66 () -> PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false))
67 .hasMessageContaining("right model is invalid: \n***validation of model failed");
69 rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1"));
70 assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true, false));
72 rightPolicyModel.getKeyInformation().generateKeyInfo(rightPolicyModel);
73 mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, false);
74 assertNotNull(mergedPolicyModel);
76 final AxPolicyModel rightPolicyModel2 = new SupportApexPolicyModelCreator().getAnotherModel();
77 mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel2, true, false);
78 assertNotNull(mergedPolicyModel);
80 mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel2, true, true);
81 assertNotNull(mergedPolicyModel);
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");