Add support for legacy guard policies
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / ToscaConstraintLogicalTest.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.ToscaConstraintLogical;
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 ToscaConstraintLogicalTest {
42
43     @Test
44     public void testConstraintLogicalPojo() {
45         assertNotNull(new ToscaConstraintLogical());
46         assertNotNull(new ToscaConstraintLogical(new PfReferenceKey()));
47
48         try {
49             new ToscaConstraintLogical((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 ToscaConstraintLogical((ToscaConstraintLogical) 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         ToscaConstraintLogical tcl = new ToscaConstraintLogical(tclKey, ToscaConstraintLogical.Operation.EQ);
65
66         try {
67             new ToscaConstraintLogical(tcl);
68             fail("test should throw an exception");
69         } catch (Exception exc) {
70             assertEquals("cannot copy an immutable constraint", exc.getMessage());
71         }
72
73         ToscaConstraintLogical tclClone1 = new ToscaConstraintLogical();
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 ToscaConstraintLogical(tclKey, ToscaConstraintLogical.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         ToscaConstraintLogical differentTcl =
90                 new ToscaConstraintLogical(new PfReferenceKey(), ToscaConstraintLogical.Operation.EQ);
91         assertFalse(tcl.compareTo(differentTcl) == 0);
92
93         ToscaConstraintLogical otherTc = new ToscaConstraintLogical(tclKey, ToscaConstraintLogical.Operation.EQ);
94         assertEquals(0, tcl.compareTo(otherTc));
95
96         try {
97             tcl.copyTo(null);
98             fail("test should throw an exception");
99         } catch (Exception exc) {
100             assertEquals("target is marked @NonNull but is null", exc.getMessage());
101         }
102
103         assertEquals(1, tcl.getKeys().size());
104         assertEquals(1, new ToscaConstraintLogical().getKeys().size());
105
106         ToscaConstraintLogical tclClone0 = new ToscaConstraintLogical();
107         new ToscaConstraintLogical().clean();
108         tcl.clean();
109         assertEquals(tclClone0, tcl);
110
111         assertFalse(new ToscaConstraintLogical().validate(new PfValidationResult()).isValid());
112         assertTrue(tcl.validate(new PfValidationResult()).isValid());
113
114         try {
115             tcl.validate(null);
116             fail("test should throw an exception");
117         } catch (Exception exc) {
118             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
119         }
120
121         DummyToscaConstraint dtc = new DummyToscaConstraint();
122         try {
123             new DummyToscaConstraint(dtc);
124             fail("test should throw an exception");
125         } catch (Exception exc) {
126             assertEquals("cannot copy an immutable constraint", exc.getMessage());
127         }
128
129         try {
130             new DummyToscaConstraint((PfReferenceKey)null);
131             fail("test should throw an exception");
132         } catch (Exception exc) {
133             assertEquals("key is marked @NonNull but is null", exc.getMessage());
134         }
135
136         try {
137             new DummyToscaConstraint((DummyToscaConstraint)null);
138             fail("test should throw an exception");
139         } catch (Exception exc) {
140             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
141         }
142
143         DummyToscaConstraint dtcClone = new DummyToscaConstraint();
144
145         assertEquals(dtc, dtcClone);
146         assertEquals(dtc, dtc);
147         assertEquals(0, dtc.compareTo(dtcClone));
148         assertEquals(0, dtc.compareTo(dtc));
149         assertEquals(-1, dtc.compareTo(null));
150         assertEquals(0, dtc.compareTo(dtcClone));
151         assertFalse(dtc.compareTo(dtcClone.getKey()) == 0);
152
153         try {
154             dtc.copyTo(null);
155             fail("test should throw an exception");
156         } catch (Exception exc) {
157             assertEquals("target is marked @NonNull but is null", exc.getMessage());
158         }
159
160         try {
161             dtc.copyTo(dtcClone);
162             fail("target should throw an exception");
163         } catch (Exception exc) {
164             assertEquals("cannot copy an immutable constraint", exc.getMessage());
165         }
166     }
167 }