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