f9f4248803133bf8ebaa7baa318b0aabe74d0bc5
[policy/apex-pdp.git] / model / policy-model / src / test / java / org / onap / policy / apex / model / policymodel / concepts / LogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019 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.concepts;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.junit.Assert.fail;
30
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
36 import org.onap.policy.apex.model.policymodel.concepts.AxLogic;
37 import org.onap.policy.apex.model.policymodel.concepts.AxStateFinalizerLogic;
38 import org.onap.policy.apex.model.policymodel.concepts.AxTaskLogic;
39 import org.onap.policy.apex.model.policymodel.concepts.AxTaskSelectionLogic;
40
41 /**
42  * Test apex logic.
43  * 
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public class LogicTest {
47
48     @Test
49     public void testLogic() {
50         final DummyLogicReader logicReader = new DummyLogicReader();
51
52         assertNotNull(new AxLogic());
53         assertNotNull(new AxLogic(new AxReferenceKey()));
54         assertNotNull(new AxLogic(new AxReferenceKey(), "LogicFlavour", "Logic"));
55         assertNotNull(new AxLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic"));
56         assertNotNull(new AxLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
57
58         assertNotNull(new AxTaskLogic());
59         assertNotNull(new AxTaskLogic(new AxReferenceKey()));
60         assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", "Logic"));
61         assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
62         assertNotNull(new AxTaskLogic(new AxLogic()));
63         assertNotNull(new AxTaskLogic(new AxArtifactKey(), "LogicName", "LogicFlavour", logicReader));
64         assertNotNull(new AxTaskLogic(new AxArtifactKey(), "LogicName", "LogicFlavour", "Logic"));
65         assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
66
67         assertNotNull(new AxTaskSelectionLogic());
68         assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey()));
69         assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", "Logic"));
70         assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic"));
71         assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
72         assertNotNull(new AxTaskSelectionLogic(new AxLogic()));
73         assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
74         assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", logicReader));
75
76         assertNotNull(new AxStateFinalizerLogic());
77         assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey()));
78         assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", "Logic"));
79         assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic"));
80         assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
81         assertNotNull(new AxStateFinalizerLogic(new AxLogic()));
82         assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
83         assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", logicReader));
84
85         final AxLogic logic = new AxLogic();
86
87         final AxReferenceKey logicKey = new AxReferenceKey("LogicParentName", "0.0.1", "PLN", "LN");
88         logic.setKey(logicKey);
89         assertEquals("LogicParentName:0.0.1:PLN:LN", logic.getKey().getId());
90         assertEquals("LogicParentName:0.0.1:PLN:LN", logic.getKeys().get(0).getId());
91
92         logic.setLogicFlavour("LogicFlavour");
93         assertEquals("LogicFlavour", logic.getLogicFlavour());
94
95         logic.setLogic("Logic");
96         assertEquals("Logic", logic.getLogic());
97
98         AxValidationResult result = new AxValidationResult();
99         result = logic.validate(result);
100         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
101
102         logic.setKey(AxReferenceKey.getNullKey());
103         result = new AxValidationResult();
104         result = logic.validate(result);
105         assertEquals(ValidationResult.INVALID, result.getValidationResult());
106
107         logic.setKey(logicKey);
108         result = new AxValidationResult();
109         result = logic.validate(result);
110         assertEquals(ValidationResult.VALID, result.getValidationResult());
111
112         try {
113             logic.setLogicFlavour(null);
114             fail("test shold throw an exception here");
115         } catch (final Exception e) {
116             assertEquals("parameter \"logicFlavour\" is null", e.getMessage());
117         }
118
119         try {
120             logic.setLogicFlavour("");
121             fail("test shold throw an exception here");
122         } catch (final Exception e) {
123             assertEquals("parameter \"logicFlavour\": value \"\", "
124                             + "does not match regular expression \"[A-Za-z0-9\\-_]+\"", e.getMessage());
125         }
126
127         logic.setLogicFlavour(AxLogic.LOGIC_FLAVOUR_UNDEFINED);
128         result = new AxValidationResult();
129         result = logic.validate(result);
130         assertEquals(ValidationResult.INVALID, result.getValidationResult());
131
132         logic.setLogicFlavour("LogicFlavour");
133         result = new AxValidationResult();
134         result = logic.validate(result);
135         assertEquals(ValidationResult.VALID, result.getValidationResult());
136
137         try {
138             logic.setLogic(null);
139             fail("test shold throw an exception here");
140         } catch (final Exception e) {
141             assertEquals("logic may not be null", e.getMessage());
142         }
143
144         logic.setLogic("");
145         result = new AxValidationResult();
146         result = logic.validate(result);
147         assertEquals(ValidationResult.INVALID, result.getValidationResult());
148
149         logic.setLogic("Logic");
150         result = new AxValidationResult();
151         result = logic.validate(result);
152         assertEquals(ValidationResult.VALID, result.getValidationResult());
153
154         logic.clean();
155
156         final AxLogic clonedLogic = new AxLogic(logic);
157         assertEquals("AxLogic:(key=AxReferenceKey:(parentKeyName=LogicParentName,parentKeyVersion=0.0.1,"
158                         + "parentLocalName=PLN,localName=LN),logicFlavour=LogicFlavour,logic=Logic)",
159                         clonedLogic.toString());
160
161         assertFalse(logic.hashCode() == 0);
162
163         assertTrue(logic.equals(logic));
164         assertTrue(logic.equals(clonedLogic));
165         assertFalse(logic.equals(null));
166         assertFalse(logic.equals((Object)"Hello"));
167         assertFalse(logic.equals(new AxLogic(AxReferenceKey.getNullKey(), "LogicFlavour", "Logic")));
168         assertFalse(logic.equals(new AxLogic(logicKey, "AnotherLogicFlavour", "Logic")));
169         assertFalse(logic.equals(new AxLogic(logicKey, "LogicFlavour", "AnotherLogic")));
170         assertTrue(logic.equals(new AxLogic(logicKey, "LogicFlavour", "Logic")));
171
172         assertEquals(0, logic.compareTo(logic));
173         assertEquals(0, logic.compareTo(clonedLogic));
174         assertNotEquals(0, logic.compareTo(new AxArtifactKey()));
175         assertNotEquals(0, logic.compareTo(null));
176         assertNotEquals(0, logic.compareTo(new AxLogic(AxReferenceKey.getNullKey(), "LogicFlavour", "Logic")));
177         assertNotEquals(0, logic.compareTo(new AxLogic(logicKey, "AnotherLogicFlavour", "Logic")));
178         assertNotEquals(0, logic.compareTo(new AxLogic(logicKey, "LogicFlavour", "AnotherLogic")));
179         assertEquals(0, logic.compareTo(new AxLogic(logicKey, "LogicFlavour", "Logic")));
180
181         assertNotNull(logic.getKeys());
182     }
183 }