5ca71cf33abd6cb5f522349a7eb475f368987a06
[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.plugins.executor.test.script.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.io.IOException;
29
30 import org.junit.Test;
31 import org.onap.policy.apex.context.ContextAlbum;
32 import org.onap.policy.apex.core.engine.EngineParameters;
33 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineFactory;
34 import org.onap.policy.apex.core.engine.engine.impl.ApexEngineImpl;
35 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
37 import org.onap.policy.apex.model.policymodel.concepts.AxPolicyModel;
38 import org.onap.policy.apex.plugins.executor.mvel.MVELExecutorParameters;
39 import org.onap.policy.apex.plugins.executor.test.script.engine.TestApexActionListener;
40 import org.onap.policy.apex.test.common.model.SampleDomainModelFactory;
41 import org.slf4j.ext.XLogger;
42 import org.slf4j.ext.XLoggerFactory;
43
44 /**
45  * The Class TestApexEngine.
46  *
47  * @author Liam Fallon (liam.fallon@ericsson.com)
48  */
49 public class TestContextUpdateDifferentModels {
50     // Logger for this class
51     private static final XLogger logger = XLoggerFactory.getXLogger(TestContextUpdateDifferentModels.class);
52
53     @Test
54     public void testContextUpdateDifferentModels() throws ApexException, InterruptedException, IOException {
55         logger.debug("Running test testContextUpdateDifferentModels . . .");
56
57         final AxPolicyModel apexModelSample = new SampleDomainModelFactory().getSamplePolicyModel("MVEL");
58         assertNotNull(apexModelSample);
59
60         final EngineParameters parameters = new EngineParameters();
61         parameters.getExecutorParameterMap().put("MVEL", new MVELExecutorParameters());
62
63         final ApexEngineImpl apexEngine =
64                 (ApexEngineImpl) new ApexEngineFactory().createApexEngine(new AxArtifactKey("TestApexEngine", "0.0.1"));
65         final TestApexActionListener listener = new TestApexActionListener("Test");
66         apexEngine.addEventListener("listener", listener);
67         apexEngine.updateModel(apexModelSample);
68         apexEngine.start();
69
70         apexEngine.stop();
71
72         final AxPolicyModel someSpuriousModel = new AxPolicyModel(new AxArtifactKey("SomeSpuriousModel", "0.0.1"));
73         assertNotNull(someSpuriousModel);
74
75         try {
76             apexEngine.updateModel(null);
77             fail("null model should throw an exception");
78         } catch (final ApexException e) {
79             assertEquals("updateModel()<-TestApexEngine:0.0.1, Apex model is not defined, it has a null value",
80                     e.getMessage());
81         }
82         assertEquals(apexEngine.getInternalContext().getContextAlbums().size(),
83                 apexModelSample.getAlbums().getAlbumsMap().size());
84         for (final ContextAlbum contextAlbum : apexEngine.getInternalContext().getContextAlbums().values()) {
85             assertTrue(
86                     contextAlbum.getAlbumDefinition().equals(apexModelSample.getAlbums().get(contextAlbum.getKey())));
87         }
88
89         apexEngine.updateModel(someSpuriousModel);
90         assertEquals(apexEngine.getInternalContext().getContextAlbums().size(),
91                 someSpuriousModel.getAlbums().getAlbumsMap().size());
92         for (final ContextAlbum contextAlbum : apexEngine.getInternalContext().getContextAlbums().values()) {
93             assertTrue(
94                     contextAlbum.getAlbumDefinition().equals(someSpuriousModel.getAlbums().get(contextAlbum.getKey())));
95         }
96
97         apexEngine.clear();
98
99         logger.debug("Ran test testContextUpdateDifferentModels");
100     }
101
102 }