b477c47cdc2139931f58182407139ad6a7b6c014
[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  * Test model merging.
35  * 
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 public class PolicyModelMergerTest {
39
40     @Test
41     public void testPolicyModelMerger() {
42         final AxPolicyModel leftPolicyModel = new SupportApexPolicyModelCreator().getModel();
43         AxPolicyModel rightPolicyModel = new SupportApexPolicyModelCreator().getModel();
44
45         try {
46             final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel,
47                             rightPolicyModel, false);
48             assertEquals(leftPolicyModel, mergedPolicyModel);
49             assertEquals(rightPolicyModel, mergedPolicyModel);
50         } catch (final ApexModelException e) {
51             fail("test should not throw an exception");
52         }
53
54         leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1"));
55         try {
56             PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false);
57             fail("test should throw an exception here");
58         } catch (final ApexModelException e) {
59             assertEquals("left model is invalid: \n***validation of model fai", e.getMessage().substring(0, 50));
60         }
61
62         leftPolicyModel.setKey(new AxArtifactKey("LeftPolicyModel", "0.0.1"));
63         try {
64             assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true));
65         } catch (final ApexModelException e) {
66             fail("test should not throw an exception");
67         }
68
69         leftPolicyModel.getKeyInformation().generateKeyInfo(leftPolicyModel);
70         try {
71             final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel,
72                             rightPolicyModel, true);
73             assertNotNull(mergedPolicyModel);
74         } catch (final ApexModelException e) {
75             fail("test should not throw an exception");
76         }
77
78         rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1"));
79         try {
80             PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false);
81             fail("test should throw an exception here");
82         } catch (final ApexModelException e) {
83             assertEquals("right model is invalid: \n***validation of model fa", e.getMessage().substring(0, 50));
84         }
85
86         rightPolicyModel.setKey(new AxArtifactKey("RightPolicyModel", "0.0.1"));
87         try {
88             assertNotNull(PolicyModelMerger.getMergedPolicyModel(leftPolicyModel, rightPolicyModel, false, true));
89         } catch (final ApexModelException e) {
90             fail("test should not throw an exception");
91         }
92
93         rightPolicyModel.getKeyInformation().generateKeyInfo(rightPolicyModel);
94         try {
95             final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel,
96                             rightPolicyModel, false);
97             assertNotNull(mergedPolicyModel);
98         } catch (final ApexModelException e) {
99             fail("test should not throw an exception");
100         }
101
102         rightPolicyModel = new SupportApexPolicyModelCreator().getAnotherModel();
103         try {
104             final AxPolicyModel mergedPolicyModel = PolicyModelMerger.getMergedPolicyModel(leftPolicyModel,
105                             rightPolicyModel, true);
106             assertNotNull(mergedPolicyModel);
107         } catch (final ApexModelException e) {
108             fail("test should not throw an exception");
109         }
110     }
111 }