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.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.fail;
30 import org.junit.Test;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class LogicTest {
44 public void testLogic() {
45 final DummyLogicReader logicReader = new DummyLogicReader();
47 assertNotNull(new AxLogic());
48 assertNotNull(new AxLogic(new AxReferenceKey()));
49 assertNotNull(new AxLogic(new AxReferenceKey(), "LogicFlavour", "Logic"));
50 assertNotNull(new AxLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic"));
51 assertNotNull(new AxLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
53 assertNotNull(new AxTaskLogic());
54 assertNotNull(new AxTaskLogic(new AxReferenceKey()));
55 assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", "Logic"));
56 assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
57 assertNotNull(new AxTaskLogic(new AxLogic()));
58 assertNotNull(new AxTaskLogic(new AxArtifactKey(), "LogicName", "LogicFlavour", logicReader));
59 assertNotNull(new AxTaskLogic(new AxArtifactKey(), "LogicName", "LogicFlavour", "Logic"));
60 assertNotNull(new AxTaskLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
62 assertNotNull(new AxTaskSelectionLogic());
63 assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey()));
64 assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", "Logic"));
65 assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic"));
66 assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
67 assertNotNull(new AxTaskSelectionLogic(new AxLogic()));
68 assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
69 assertNotNull(new AxTaskSelectionLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", logicReader));
71 assertNotNull(new AxStateFinalizerLogic());
72 assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey()));
73 assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", "Logic"));
74 assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", "Logic"));
75 assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
76 assertNotNull(new AxStateFinalizerLogic(new AxLogic()));
77 assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicFlavour", logicReader));
78 assertNotNull(new AxStateFinalizerLogic(new AxReferenceKey(), "LogicName", "LogicFlavour", logicReader));
80 final AxLogic logic = new AxLogic();
82 final AxReferenceKey logicKey = new AxReferenceKey("LogicParentName", "0.0.1", "PLN", "LN");
83 logic.setKey(logicKey);
84 assertEquals("LogicParentName:0.0.1:PLN:LN", logic.getKey().getId());
85 assertEquals("LogicParentName:0.0.1:PLN:LN", logic.getKeys().get(0).getId());
87 logic.setLogicFlavour("LogicFlavour");
88 assertEquals("LogicFlavour", logic.getLogicFlavour());
90 logic.setLogic("Logic");
91 assertEquals("Logic", logic.getLogic());
93 AxValidationResult result = new AxValidationResult();
94 result = logic.validate(result);
95 assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
97 logic.setKey(AxReferenceKey.getNullKey());
98 result = new AxValidationResult();
99 result = logic.validate(result);
100 assertEquals(ValidationResult.INVALID, result.getValidationResult());
102 logic.setKey(logicKey);
103 result = new AxValidationResult();
104 result = logic.validate(result);
105 assertEquals(ValidationResult.VALID, result.getValidationResult());
107 assertThatThrownBy(() -> logic.setLogicFlavour(null))
108 .hasMessageContaining("parameter \"logicFlavour\" is null");
109 assertThatThrownBy(() -> logic.setLogicFlavour(""))
110 .hasMessage("parameter \"logicFlavour\": value \"\", "
111 + "does not match regular expression \"[A-Za-z0-9\\-_]+\"");
112 logic.setLogicFlavour(AxLogic.LOGIC_FLAVOUR_UNDEFINED);
113 result = new AxValidationResult();
114 result = logic.validate(result);
115 assertEquals(ValidationResult.INVALID, result.getValidationResult());
117 logic.setLogicFlavour("LogicFlavour");
118 result = new AxValidationResult();
119 result = logic.validate(result);
120 assertEquals(ValidationResult.VALID, result.getValidationResult());
122 assertThatThrownBy(() -> logic.setLogic(null)).hasMessage("logic may not be null");
124 result = new AxValidationResult();
125 result = logic.validate(result);
126 assertEquals(ValidationResult.INVALID, result.getValidationResult());
128 logic.setLogic("Logic");
129 result = new AxValidationResult();
130 result = logic.validate(result);
131 assertEquals(ValidationResult.VALID, result.getValidationResult());
135 final AxLogic clonedLogic = new AxLogic(logic);
136 assertEquals("AxLogic:(key=AxReferenceKey:(parentKeyName=LogicParentName,parentKeyVersion=0.0.1,"
137 + "parentLocalName=PLN,localName=LN),logicFlavour=LogicFlavour,logic=Logic)",
138 clonedLogic.toString());
140 assertNotEquals(0, logic.hashCode());
142 assertEquals(logic, logic);
143 assertEquals(logic, clonedLogic);
144 assertNotNull(logic);
145 assertNotEquals(logic, (Object) "Hello");
146 assertNotEquals(logic, new AxLogic(AxReferenceKey.getNullKey(), "LogicFlavour", "Logic"));
147 assertNotEquals(logic, new AxLogic(logicKey, "AnotherLogicFlavour", "Logic"));
148 assertNotEquals(logic, new AxLogic(logicKey, "LogicFlavour", "AnotherLogic"));
149 assertEquals(logic, new AxLogic(logicKey, "LogicFlavour", "Logic"));
151 assertEquals(0, logic.compareTo(logic));
152 assertEquals(0, logic.compareTo(clonedLogic));
153 assertNotEquals(0, logic.compareTo(new AxArtifactKey()));
154 assertNotEquals(0, logic.compareTo(null));
155 assertNotEquals(0, logic.compareTo(new AxLogic(AxReferenceKey.getNullKey(), "LogicFlavour", "Logic")));
156 assertNotEquals(0, logic.compareTo(new AxLogic(logicKey, "AnotherLogicFlavour", "Logic")));
157 assertNotEquals(0, logic.compareTo(new AxLogic(logicKey, "LogicFlavour", "AnotherLogic")));
158 assertEquals(0, logic.compareTo(new AxLogic(logicKey, "LogicFlavour", "Logic")));
160 assertNotNull(logic.getKeys());