Remove try/catch blocks with assertj - apex-pdp
[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.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;
29
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;
35
36 /**
37  * Test apex logic.
38  *
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class LogicTest {
42
43     @Test
44     public void testLogic() {
45         final DummyLogicReader logicReader = new DummyLogicReader();
46
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));
52
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));
61
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));
70
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));
79
80         final AxLogic logic = new AxLogic();
81
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());
86
87         logic.setLogicFlavour("LogicFlavour");
88         assertEquals("LogicFlavour", logic.getLogicFlavour());
89
90         logic.setLogic("Logic");
91         assertEquals("Logic", logic.getLogic());
92
93         AxValidationResult result = new AxValidationResult();
94         result = logic.validate(result);
95         assertEquals(AxValidationResult.ValidationResult.VALID, result.getValidationResult());
96
97         logic.setKey(AxReferenceKey.getNullKey());
98         result = new AxValidationResult();
99         result = logic.validate(result);
100         assertEquals(ValidationResult.INVALID, result.getValidationResult());
101
102         logic.setKey(logicKey);
103         result = new AxValidationResult();
104         result = logic.validate(result);
105         assertEquals(ValidationResult.VALID, result.getValidationResult());
106
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());
116
117         logic.setLogicFlavour("LogicFlavour");
118         result = new AxValidationResult();
119         result = logic.validate(result);
120         assertEquals(ValidationResult.VALID, result.getValidationResult());
121
122         assertThatThrownBy(() -> logic.setLogic(null)).hasMessage("logic may not be null");
123         logic.setLogic("");
124         result = new AxValidationResult();
125         result = logic.validate(result);
126         assertEquals(ValidationResult.INVALID, result.getValidationResult());
127
128         logic.setLogic("Logic");
129         result = new AxValidationResult();
130         result = logic.validate(result);
131         assertEquals(ValidationResult.VALID, result.getValidationResult());
132
133         logic.clean();
134
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());
139
140         assertNotEquals(0, logic.hashCode());
141
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"));
150
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")));
159
160         assertNotNull(logic.getKeys());
161     }
162 }