92d1dc32a9d20725cacda8f245f65fde972ddc4e
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * 
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.model.policymodel.handling;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import org.junit.Test;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
29 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
30 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
31 import org.onap.policy.apex.model.policymodel.handling.PolicyModelMerger;
32
33 /**
34  * @author Liam Fallon (liam.fallon@ericsson.com)
35  */
36 public class TestPolicyModelMerger {
37
38     @Test
39     public void testPolicyModelMerger() {
40         final AxPolicyModel leftPolicyModel = new TestApexPolicyModelCreator().getModel();
41         AxPolicyModel rightPolicyModel = new TestApexPolicyModelCreator().getModel();
42
43         try {
44             final AxPolicyModel mergedPolicyModel =
45                     PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false);
46             assertEquals(leftPolicyModel, mergedPolicyModel);
47             assertEquals(rightPolicyModel, mergedPolicyModel);
48         } catch (final ApexModelException e) {
49             fail("test should not throw an exception");
50         }
51
52         leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1"));
53         try {
54             PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false);
55             fail("test should throw an exception here");
56         } catch (final ApexModelException e) {
57             assertEquals("left model is invalid: \n***validation of model fai", e.getMessage().substring(0, 50));
58         }
59
60         leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1"));
61         try {
62             assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true));
63         } catch (final ApexModelException e) {
64             fail("test should not throw an exception");
65         }
66
67         leftPolicyModel.getKeyInformation().generateKeyInfo(leftPolicyModel);
68         try {
69             final AxPolicyModel mergedPolicyModel =
70                     PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, true);
71             assertNotNull(mergedPolicyModel);
72         } catch (final ApexModelException e) {
73             fail("test should not throw an exception");
74         }
75
76         rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1"));
77         try {
78             PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false);
79             fail("test should throw an exception here");
80         } catch (final ApexModelException e) {
81             assertEquals("right model is invalid: \n***validation of model fa", e.getMessage().substring(0, 50));
82         }
83
84         rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1"));
85         try {
86             assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true));
87         } catch (final ApexModelException e) {
88             fail("test should not throw an exception");
89         }
90
91         rightPolicyModel.getKeyInformation().generateKeyInfo(rightPolicyModel);
92         try {
93             final AxPolicyModel mergedPolicyModel =
94                     PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false);
95             assertNotNull(mergedPolicyModel);
96         } catch (final ApexModelException e) {
97             fail("test should not throw an exception");
98         }
99
100         rightPolicyModel = new TestApexPolicyModelCreator().getAnotherModel();
101         try {
102             final AxPolicyModel mergedPolicyModel =
103                     PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, true);
104             assertNotNull(mergedPolicyModel);
105         } catch (final ApexModelException e) {
106             fail("test should not throw an exception");
107         }
108     }
109 }