6c39737f18e269cb01cb173d2e3adc2abab92024
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraintTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019-2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
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.models.tosca.simple.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
29 import java.util.ArrayList;
30 import java.util.List;
31 import org.junit.Test;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
33
34 /**
35  * DAO test for ToscaConstraintLogicalString.
36  *
37  * @author Liam Fallon (liam.fallon@est.tech)
38  */
39 public class JpaToscaConstraintTest {
40
41     private static final String CONSTRAINT = "Constraint";
42
43     @Test
44     public void testConstraintLogicalStringPojo() {
45         assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, CONSTRAINT));
46
47         assertThatThrownBy(() -> new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, null))
48                 .hasMessageMatching("operation is marked .*on.*ull but is null");
49
50         assertThatThrownBy(() -> new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, "Hello"))
51                 .hasMessageMatching("operation is marked .*on.*ull but is null");
52
53         assertThatThrownBy(() -> new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, null))
54                 .hasMessageMatching("compareTo is marked .*on.*ull but is null");
55
56         assertThatThrownBy(() -> new JpaToscaConstraintInRange((List<String>) null))
57                 .hasMessageMatching("rangeValues is marked .*on.*ull but is null");
58
59         assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, CONSTRAINT));
60
61         assertEquals(0, new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "")
62                 .compareTo(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "")));
63
64         JpaToscaConstraintOperation op = JpaToscaConstraintOperation.EQ;
65         assertEquals("equal_to", op.getToscaToken());
66
67         List<String> validValues = new ArrayList<>();
68         validValues.add("hello");
69         validValues.add("goodbye");
70         JpaToscaConstraintValidValues cvv0 = new JpaToscaConstraintValidValues(validValues);
71         assertEquals(-1, cvv0.compareTo(null));
72         assertEquals(0, cvv0.compareTo(cvv0));
73         assertNotEquals(0, cvv0.compareTo(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, CONSTRAINT)));
74         JpaToscaConstraintValidValues cvv1 = new JpaToscaConstraintValidValues(validValues);
75         assertEquals(0, cvv0.compareTo(cvv1));
76
77         cvv1.fromAuthorative(new ToscaConstraint());
78         assertNotNull(cvv1.getValidValues());
79
80         List<String> rangeValues = new ArrayList<>();
81         rangeValues.add("hello");
82         rangeValues.add("goodbye");
83         JpaToscaConstraintInRange cir0 = new JpaToscaConstraintInRange(rangeValues);
84         assertEquals(-1, cir0.compareTo(null));
85         assertEquals(0, cir0.compareTo(cir0));
86         assertNotEquals(0, cir0.compareTo(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, CONSTRAINT)));
87         JpaToscaConstraintInRange cir1 = new JpaToscaConstraintInRange(rangeValues);
88         assertEquals(0, cir0.compareTo(cir1));
89
90         ToscaConstraint tc0 = new ToscaConstraint();
91         tc0.setRangeValues(rangeValues);
92         JpaToscaConstraintInRange cir2 = new JpaToscaConstraintInRange(tc0);
93         assertEquals(0, cir0.compareTo(cir2));
94
95         cir1.fromAuthorative(new ToscaConstraint());
96         assertNotNull(cir1.getRangeValues());
97
98         ToscaConstraint tc1 = cir2.toAuthorative();
99         assertEquals(tc0, tc1);
100     }
101 }