f8fc99dd8b5ddf15ae262571afce5a8adaec7b4b
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.policymodel.handling;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.Set;
29 import java.util.TreeSet;
30 import org.junit.Test;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.handling.ApexModelException;
33 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
34
35 public class PolicyModelSplitterTest {
36     @Test
37     public void test() throws ApexModelException {
38         final AxPolicyModel apexModel = new SupportApexPolicyModelCreator().getModel();
39
40         final Set<AxArtifactKey> requiredPolicySet = new TreeSet<AxArtifactKey>();
41         requiredPolicySet.add(new AxArtifactKey("policy", "0.0.1"));
42
43         // There's only one policy so a split of this model on that policy should return the same
44         // model
45         AxPolicyModel splitApexModel = null;
46         splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet);
47
48         // The only difference between the models should be that the unused event outEvent1 should
49         // not be in the split model
50         apexModel.getEvents().getEventMap().remove(new AxArtifactKey("outEvent1", "0.0.1"));
51         apexModel.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("outEvent1", "0.0.1"));
52         assertTrue(apexModel.equals(splitApexModel));
53
54         final Set<AxArtifactKey> requiredMissingPolicySet = new TreeSet<AxArtifactKey>();
55         requiredPolicySet.add(new AxArtifactKey("MissingPolicy", "0.0.1"));
56
57         AxPolicyModel missingSplitApexModel = null;
58         missingSplitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredMissingPolicySet);
59         assertNotNull(missingSplitApexModel);
60
61         splitApexModel = null;
62         splitApexModel = PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet, true);
63
64         // The only difference between the models should be that the unused event outEvent1 should
65         // not be in the split model
66         apexModel.getEvents().getEventMap().remove(new AxArtifactKey("outEvent1", "0.0.1"));
67         apexModel.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("outEvent1", "0.0.1"));
68         assertTrue(apexModel.equals(splitApexModel));
69
70         // There's only one policy so a split of this model on that policy should return the same
71         // model
72         apexModel.getKey().setName("InvalidPolicyModelName");
73         assertThatThrownBy(() -> PolicyModelSplitter.getSubPolicyModel(apexModel, requiredPolicySet))
74             .hasMessageContaining("source model is invalid: \n***validation of model f");
75     }
76 }