Restructure for authorative models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaConstraintLogicalTest.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.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.junit.Assert.fail;
28
29 import org.junit.Test;
30 import org.onap.policy.models.base.PfConceptKey;
31 import org.onap.policy.models.base.PfReferenceKey;
32 import org.onap.policy.models.base.PfValidationResult;
33 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical;
34 import org.onap.policy.models.tosca.simple.concepts.testconcepts.DummyToscaConstraint;
35
36 /**
37  * DAO test for ToscaConstraintLogical.
38  *
39  * @author Liam Fallon (liam.fallon@est.tech)
40  */
41 public class JpaToscaConstraintLogicalTest {
42
43     @Test
44     public void testConstraintLogicalPojo() {
45         assertNotNull(new JpaToscaConstraintLogical());
46         assertNotNull(new JpaToscaConstraintLogical(new PfReferenceKey()));
47
48         try {
49             new JpaToscaConstraintLogical((PfReferenceKey) null);
50             fail("test should throw an exception");
51         } catch (Exception exc) {
52             assertEquals("key is marked @NonNull but is null", exc.getMessage());
53         }
54
55         try {
56             new JpaToscaConstraintLogical((JpaToscaConstraintLogical) null);
57             fail("test should throw an exception");
58         } catch (Exception exc) {
59             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
60         }
61
62         PfConceptKey tclParentKey = new PfConceptKey("tParentKey", "0.0.1");
63         PfReferenceKey tclKey = new PfReferenceKey(tclParentKey, "trigger0");
64         JpaToscaConstraintLogical tcl = new JpaToscaConstraintLogical(tclKey, JpaToscaConstraintLogical.Operation.EQ);
65
66         try {
67             new JpaToscaConstraintLogical(tcl);
68             fail("test should throw an exception");
69         } catch (Exception exc) {
70             assertEquals("cannot copy an immutable constraint", exc.getMessage());
71         }
72
73         JpaToscaConstraintLogical tclClone1 = new JpaToscaConstraintLogical();
74         try {
75             tcl.copyTo(tclClone1);
76             fail("test should throw an exception");
77         } catch (Exception exc) {
78             assertEquals("cannot copy an immutable constraint", exc.getMessage());
79         }
80         tclClone1 = new JpaToscaConstraintLogical(tclKey, JpaToscaConstraintLogical.Operation.EQ);
81
82         assertEquals(tcl, tclClone1);
83         assertEquals(0, tcl.compareTo(tclClone1));
84
85         assertEquals(-1, tcl.compareTo(null));
86         assertEquals(0, tcl.compareTo(tcl));
87         assertFalse(tcl.compareTo(tcl.getKey()) == 0);
88
89         JpaToscaConstraintLogical differentTcl =
90                 new JpaToscaConstraintLogical(new PfReferenceKey(), JpaToscaConstraintLogical.Operation.EQ);
91         assertFalse(tcl.compareTo(differentTcl) == 0);
92
93         JpaToscaConstraintLogical otherTc =
94                 new JpaToscaConstraintLogical(tclKey, JpaToscaConstraintLogical.Operation.EQ);
95         assertEquals(0, tcl.compareTo(otherTc));
96
97         try {
98             tcl.copyTo(null);
99             fail("test should throw an exception");
100         } catch (Exception exc) {
101             assertEquals("target is marked @NonNull but is null", exc.getMessage());
102         }
103
104         assertEquals(1, tcl.getKeys().size());
105         assertEquals(1, new JpaToscaConstraintLogical().getKeys().size());
106
107         JpaToscaConstraintLogical tclClone0 = new JpaToscaConstraintLogical();
108         new JpaToscaConstraintLogical().clean();
109         tcl.clean();
110         assertEquals(tclClone0, tcl);
111
112         assertFalse(new JpaToscaConstraintLogical().validate(new PfValidationResult()).isValid());
113         assertTrue(tcl.validate(new PfValidationResult()).isValid());
114
115         try {
116             tcl.validate(null);
117             fail("test should throw an exception");
118         } catch (Exception exc) {
119             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
120         }
121
122         DummyToscaConstraint dtc = new DummyToscaConstraint();
123         try {
124             new DummyToscaConstraint(dtc);
125             fail("test should throw an exception");
126         } catch (Exception exc) {
127             assertEquals("cannot copy an immutable constraint", exc.getMessage());
128         }
129
130         try {
131             new DummyToscaConstraint((PfReferenceKey) null);
132             fail("test should throw an exception");
133         } catch (Exception exc) {
134             assertEquals("key is marked @NonNull but is null", exc.getMessage());
135         }
136
137         try {
138             new DummyToscaConstraint((DummyToscaConstraint) null);
139             fail("test should throw an exception");
140         } catch (Exception exc) {
141             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
142         }
143
144         DummyToscaConstraint dtcClone = new DummyToscaConstraint();
145
146         assertEquals(dtc, dtcClone);
147         assertEquals(dtc, dtc);
148         assertEquals(0, dtc.compareTo(dtcClone));
149         assertEquals(0, dtc.compareTo(dtc));
150         assertEquals(-1, dtc.compareTo(null));
151         assertEquals(0, dtc.compareTo(dtcClone));
152         assertFalse(dtc.compareTo(dtcClone.getKey()) == 0);
153
154         try {
155             dtc.copyTo(null);
156             fail("test should throw an exception");
157         } catch (Exception exc) {
158             assertEquals("target is marked @NonNull but is null", exc.getMessage());
159         }
160
161         try {
162             dtc.copyTo(dtcClone);
163             fail("target should throw an exception");
164         } catch (Exception exc) {
165             assertEquals("cannot copy an immutable constraint", exc.getMessage());
166         }
167     }
168 }