Finish unit test on policy-models
[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 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.fail;
27
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.junit.Test;
32 import org.onap.policy.models.tosca.authorative.concepts.ToscaConstraint;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical;
34
35 /**
36  * DAO test for ToscaConstraintLogicalString.
37  *
38  * @author Liam Fallon (liam.fallon@est.tech)
39  */
40 public class JpaToscaConstraintTest {
41
42     @Test
43     public void testConstraintLogicalStringPojo() {
44         assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint"));
45
46         try {
47             new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, null);
48             fail("test should throw an exception");
49         } catch (Exception exc) {
50             assertEquals("operation is marked @NonNull but is null", exc.getMessage());
51         }
52
53         try {
54             new JpaToscaConstraintLogical((JpaToscaConstraintOperation) null, "Hello");
55             fail("test should throw an exception");
56         } catch (Exception exc) {
57             assertEquals("operation is marked @NonNull but is null", exc.getMessage());
58         }
59
60         try {
61             new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, null);
62             fail("test should throw an exception");
63         } catch (Exception exc) {
64             assertEquals("compareTo is marked @NonNull but is null", exc.getMessage());
65         }
66
67         assertNotNull(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint"));
68
69         assertEquals(0, new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "")
70                 .compareTo(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "")));
71
72         JpaToscaConstraintOperation op = JpaToscaConstraintOperation.EQ;
73         assertEquals("equal_to", op.getToscaToken());
74
75         List<String> validValues = new ArrayList<>();
76         validValues.add("hello");
77         validValues.add("goodbye");
78         JpaToscaConstraintValidValues cvv0 = new JpaToscaConstraintValidValues(validValues);
79         assertEquals(-1, cvv0.compareTo(null));
80         assertEquals(0, cvv0.compareTo(cvv0));
81         assertNotEquals(0, cvv0.compareTo(new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "Constraint")));
82         JpaToscaConstraintValidValues cvv1 = new JpaToscaConstraintValidValues(validValues);
83         assertEquals(0, cvv0.compareTo(cvv1));
84
85         cvv1.fromAuthorative(new ToscaConstraint());
86         assertNotNull(cvv1.getValidValues());
87     }
88 }