1e419521f97b7042a6948f492f7afb8a7a23b991
[policy/apex-pdp.git] / testsuites / integration / integration-executor-test / src / test / java / org / onap / policy / apex / testsuites / integration / executor / handling / TestApexModelExport.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.testsuites.integration.executor.handling;
22
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
35 import org.onap.policy.apex.model.policymodel.handling.PolicyModelSplitter;
36 import org.slf4j.ext.XLogger;
37 import org.slf4j.ext.XLoggerFactory;
38
39 /**
40  * The Class TestApexModelReader tests Apex model reading.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  */
44 public class TestApexModelExport {
45     private static final XLogger logger = XLoggerFactory.getXLogger(TestApexModelExport.class);
46
47     private AxPolicyModel model = null;
48
49     @Before
50     public void initApexModelSmall() throws ApexException {
51         model = new TestApexSamplePolicyModelCreator("MVEL").getModel();
52     }
53
54     @Test
55     public void testApexModelExport() throws Exception {
56         logger.info("Starting test: testApexModelExport");
57
58         final List<AxArtifactKey> exportPolicyList = new ArrayList<AxArtifactKey>();
59         exportPolicyList.addAll(model.getPolicies().getPolicyMap().keySet());
60
61         final AxPolicyModel exportedModel0 = PolicyModelSplitter.getSubPolicyModel(model, exportPolicyList);
62
63         // Remove unused schemas and their keys
64         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem000", "0.0.1"));
65         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem001", "0.0.1"));
66         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem002", "0.0.1"));
67         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem003", "0.0.1"));
68         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem004", "0.0.1"));
69         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem005", "0.0.1"));
70         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem006", "0.0.1"));
71         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem007", "0.0.1"));
72         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem008", "0.0.1"));
73         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem009", "0.0.1"));
74         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem00A", "0.0.1"));
75         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem00B", "0.0.1"));
76         model.getSchemas().getSchemasMap().remove(new AxArtifactKey("TestContextItem00C", "0.0.1"));
77
78         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem000", "0.0.1"));
79         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem001", "0.0.1"));
80         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem002", "0.0.1"));
81         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem003", "0.0.1"));
82         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem004", "0.0.1"));
83         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem005", "0.0.1"));
84         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem006", "0.0.1"));
85         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem007", "0.0.1"));
86         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem008", "0.0.1"));
87         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem009", "0.0.1"));
88         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem00A", "0.0.1"));
89         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem00B", "0.0.1"));
90         model.getKeyInformation().getKeyInfoMap().remove(new AxArtifactKey("TestContextItem00C", "0.0.1"));
91
92         assertTrue(model.equals(exportedModel0));
93
94         exportPolicyList.remove(0);
95
96         final AxPolicyModel exportedModel1 = PolicyModelSplitter.getSubPolicyModel(model, exportPolicyList);
97         assertFalse(model.equals(exportedModel1));
98         assertTrue(model.getPolicies().get("Policy1").equals(exportedModel1.getPolicies().get("Policy1")));
99
100         exportPolicyList.clear();
101         exportPolicyList.add(new AxArtifactKey("NonExistentPolicy", "0.0.1"));
102
103         try {
104             final AxPolicyModel emptyExportedModel = PolicyModelSplitter.getSubPolicyModel(model, exportPolicyList);
105             assertNotNull(emptyExportedModel);
106         } catch (final Exception e) {
107             assertTrue(e.getMessage()
108                     .equals("new model is invalid:\n" + "***validation of model failed***\n"
109                             + "AxPolicies:Policies:0.0.1 - policyMap may not be null or empty\n"
110                             + "AxEvents:Events:0.0.1 - eventMap may not be null or empty\n"
111                             + "********************************"));
112         }
113     }
114 }