23f9d38206c82969979990b4ccf773985e9040f5
[policy/apex-pdp.git] / model / policy-model / src / test / java / org / onap / policy / apex / model / policymodel / handling / PolicyLogicReaderTest.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.assertTrue;
25 import static org.junit.Assert.fail;
26
27 import org.junit.Test;
28 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
29 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
30 import org.onap.policy.apex.model.policymodel.concepts.AxLogic;
31 import org.onap.policy.apex.model.policymodel.handling.PolicyLogicReader;
32
33 /**
34  * Logic reader for policy tests.
35  * 
36  * @author Liam Fallon (liam.fallon@ericsson.com)
37  */
38 public class PolicyLogicReaderTest {
39
40     @Test
41     public void test() {
42         final AxReferenceKey logicKey = new AxReferenceKey("LogicParent", "0.0.1", "LogicInstanceName");
43
44         final PolicyLogicReader plReader = new PolicyLogicReader();
45
46         plReader.setLogicPackage("somewhere.over.the.rainbow");
47         assertEquals("somewhere.over.the.rainbow", plReader.getLogicPackage());
48
49         plReader.setDefaultLogic("FunkyDefaultLogic");
50         assertEquals("FunkyDefaultLogic", plReader.getDefaultLogic());
51
52         try {
53             new AxLogic(logicKey, "FunkyLogic", plReader);
54             fail("test should throw an exception here");
55         } catch (final Exception e) {
56             assertEquals("logic not found for logic "
57                             + "\"somewhere/over/the/rainbow/funkylogic/FunkyDefaultLogic.funkylogic\"", e.getMessage());
58         }
59
60         plReader.setDefaultLogic(null);
61         try {
62             new AxLogic(logicKey, "FunkyLogic", plReader);
63             fail("test should throw an exception here");
64         } catch (final Exception e) {
65             assertEquals("logic not found for logic "
66                             + "\"somewhere/over/the/rainbow/funkylogic/LogicParentLogicInstanceName.funkylogic\"",
67                             e.getMessage());
68         }
69
70         logicKey.setParentLocalName("LogicParentLocalName");
71         try {
72             new AxLogic(logicKey, "FunkyLogic", plReader);
73             fail("test should throw an exception here");
74         } catch (final Exception e) {
75             assertEquals("logic not found for logic " + "\"somewhere/over/the/rainbow/funkylogic/"
76                             + "LogicParentLogicParentLocalNameLogicInstanceName.funkylogic\"", e.getMessage());
77         }
78
79         plReader.setLogicPackage("path.to.apex.logic");
80         try {
81             final AxLogic logic = new AxLogic(logicKey, "FunkyLogic", plReader);
82             assertTrue(logic.getLogic().endsWith("Way out man, this is funky logic!"));
83         } catch (final Exception e) {
84             fail("test should not throw an exception");
85         }
86
87         plReader.setLogicPackage("somewhere.over.the.rainbow");
88         plReader.setDefaultLogic("JavaLogic");
89
90         try {
91             final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader);
92             assertEquals("somewhere.over.the.rainbow.java.JavaLogic", logic.getLogic());
93         } catch (final Exception e) {
94             fail("test should not throw an exception");
95         }
96
97         plReader.setDefaultLogic(null);
98         try {
99             final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader);
100             assertEquals("somewhere.over.the.rainbow.java.LogicParentLogicParentLocalNameLogicInstanceName",
101                             logic.getLogic());
102         } catch (final Exception e) {
103             fail("test should not throw an exception");
104         }
105
106         logicKey.setParentLocalName(AxKey.NULL_KEY_NAME);
107         try {
108             final AxLogic logic = new AxLogic(logicKey, "JAVA", plReader);
109             assertEquals("somewhere.over.the.rainbow.java.LogicParentLogicInstanceName", logic.getLogic());
110         } catch (final Exception e) {
111             fail("test should not throw an exception");
112         }
113     }
114 }