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