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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.policymodel.concepts;
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;
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;
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class LogicTest {
43 public void testLogic() {
44 final DummyLogicReader logicReader = new DummyLogicReader();
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));
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));
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));
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));
79 final AxLogic logic = new AxLogic();
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());
86 logic.setLogicFlavour("LogicFlavour");
87 assertEquals("LogicFlavour", logic.getLogicFlavour());
89 logic.setLogic("Logic");
90 assertEquals("Logic", logic.getLogic());
92 AxValidationResult result = new AxValidationResult();
93 result = logic.validate(result);
94 assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
96 logic.setKey(AxReferenceKey.getNullKey());
97 result = new AxValidationResult();
98 result = logic.validate(result);
99 assertEquals(ValidationResult.INVALID, result.getValidationResult());
101 logic.setKey(logicKey);
102 result = new AxValidationResult();
103 result = logic.validate(result);
104 assertEquals(ValidationResult.VALID, result.getValidationResult());
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());
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());
121 logic.setLogicFlavour(AxLogic.LOGIC_FLAVOUR_UNDEFINED);
122 result = new AxValidationResult();
123 result = logic.validate(result);
124 assertEquals(ValidationResult.INVALID, result.getValidationResult());
126 logic.setLogicFlavour("LogicFlavour");
127 result = new AxValidationResult();
128 result = logic.validate(result);
129 assertEquals(ValidationResult.VALID, result.getValidationResult());
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());
139 result = new AxValidationResult();
140 result = logic.validate(result);
141 assertEquals(ValidationResult.INVALID, result.getValidationResult());
143 logic.setLogic("Logic");
144 result = new AxValidationResult();
145 result = logic.validate(result);
146 assertEquals(ValidationResult.VALID, result.getValidationResult());
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());
155 assertNotEquals(0, logic.hashCode());
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"));
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")));
175 assertNotNull(logic.getKeys());