c20a40736b106e0cc5b58f9fe4664e79233b829e
[policy/apex-pdp.git] / model / policy-model / src / test / java / org / onap / policy / apex / model / policymodel / handling / PolicyModelSplitterTest.java
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.assertTrue;
26 import static org.junit.Assert.fail;
27
28 import java.util.Set;
29 import java.util.TreeSet;
30
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
34 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
35 import org.onap.policy.apex.model.policymodel.handling.PolicyModelSplitter;
36
37 public class PolicyModelSplitterTest {
38     @Test
39     public void test() {
40         final AxPolicyModel apexModel = new SupportApexPolicyModelCreator().getModel();
41
42         final Set<AxArtifactKey> requiredPolicySet = new TreeSet<AxArtifactKey>();
43         requiredPolicySet.add(new AxArtifactKey("policy", "0.0.1"));
44
45         // There's only one policy so a split of this model on that policy should return the same
46         // model
47         AxPolicyModel splitApexModel = null;
48         try {
49             splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet);
50         } catch (final ApexModelException e) {
51             fail(e.getMessage());
52         }
53
54         // The only difference between the models should be that the unused event outEvent1 should
55         // not be in the split model
56         apexModel.getEvents().getEventMap().remove(new AxArtifactKey("outEvent1", "0.0.1"));
57         apexModel.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("outEvent1", "0.0.1"));
58         assertTrue(apexModel.equals(splitApexModel));
59
60         final Set<AxArtifactKey> requiredMissingPolicySet = new TreeSet<AxArtifactKey>();
61         requiredPolicySet.add(new AxArtifactKey("MissingPolicy", "0.0.1"));
62
63         AxPolicyModel missingSplitApexModel = null;
64         try {
65             missingSplitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredMissingPolicySet);
66         } catch (final ApexModelException e) {
67             fail(e.getMessage());
68         }
69         assertNotNull(missingSplitApexModel);
70
71         splitApexModel = null;
72         try {
73             splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet, true);
74         } catch (final ApexModelException e) {
75             fail(e.getMessage());
76         }
77
78         // The only difference between the models should be that the unused event outEvent1 should
79         // not be in the split model
80         apexModel.getEvents().getEventMap().remove(new AxArtifactKey("outEvent1", "0.0.1"));
81         apexModel.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("outEvent1", "0.0.1"));
82         assertTrue(apexModel.equals(splitApexModel));
83
84         // There's only one policy so a split of this model on that policy should return the same
85         // model
86         try {
87             apexModel.getKey().setName("InvalidPolicyModelName");
88             PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet);
89             fail("test should throw an exception here");
90         } catch (final Exception e) {
91             assertEquals("source model is invalid: \n***validation of model f", e.getMessage().substring(0, 50));
92         }
93
94     }
95 }