Add support for legacy guard policies
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / ToscaConstraintLogicalKeyTest.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.ToscaConstraintLogicalKey;
34
35 /**
36  * DAO test for ToscaConstraintLogicalKey.
37  *
38  * @author Liam Fallon (liam.fallon@est.tech)
39  */
40 public class ToscaConstraintLogicalKeyTest {
41
42     @Test
43     public void testConstraintLogicalKeyPojo() {
44         assertNotNull(new ToscaConstraintLogicalKey());
45         assertNotNull(new ToscaConstraintLogicalKey(new PfReferenceKey()));
46         assertNotNull(new ToscaConstraintLogicalKey(new PfReferenceKey(), ToscaConstraintLogicalKey.Operation.EQ,
47                 new PfConceptKey()));
48
49         try {
50             new ToscaConstraintLogicalKey((PfReferenceKey) null);
51             fail("test should throw an exception");
52         } catch (Exception exc) {
53             assertEquals("key is marked @NonNull but is null", exc.getMessage());
54         }
55
56         try {
57             new ToscaConstraintLogicalKey((ToscaConstraintLogicalKey) null);
58             fail("test should throw an exception");
59         } catch (Exception exc) {
60             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
61         }
62
63         PfConceptKey tclParentKey = new PfConceptKey("tParentKey", "0.0.1");
64         PfReferenceKey tclKey = new PfReferenceKey(tclParentKey, "trigger0");
65         PfConceptKey constraintKey = new PfConceptKey("tParentKey", "0.0.1");
66         ToscaConstraintLogicalKey tcl =
67                 new ToscaConstraintLogicalKey(tclKey, ToscaConstraintLogicalKey.Operation.EQ, constraintKey);
68
69         try {
70             new ToscaConstraintLogicalKey(tcl);
71             fail("test should throw an exception");
72         } catch (Exception exc) {
73             assertEquals("cannot copy an immutable constraint", exc.getMessage());
74         }
75
76         ToscaConstraintLogicalKey tclClone1 = new ToscaConstraintLogicalKey();
77         try {
78             tcl.copyTo(tclClone1);
79             fail("test should throw an exception");
80         } catch (Exception exc) {
81             assertEquals("cannot copy an immutable constraint", exc.getMessage());
82         }
83         tclClone1 = new ToscaConstraintLogicalKey(tclKey, ToscaConstraintLogicalKey.Operation.EQ, constraintKey);
84
85         assertEquals(tcl, tclClone1);
86         assertEquals(0, tcl.compareTo(tclClone1));
87
88         assertEquals(-1, tcl.compareTo(null));
89         assertEquals(0, tcl.compareTo(tcl));
90         assertFalse(tcl.compareTo(tcl.getKey()) == 0);
91
92         ToscaConstraintLogicalKey differentTcl = new ToscaConstraintLogicalKey(new PfReferenceKey(),
93                 ToscaConstraintLogicalKey.Operation.EQ, constraintKey);
94         assertFalse(tcl.compareTo(differentTcl) == 0);
95
96         ToscaConstraintLogicalKey otherTc =
97                 new ToscaConstraintLogicalKey(tclKey, ToscaConstraintLogicalKey.Operation.EQ, constraintKey);
98         assertEquals(0, tcl.compareTo(otherTc));
99
100         try {
101             tcl.copyTo(null);
102             fail("test should throw an exception");
103         } catch (Exception exc) {
104             assertEquals("target is marked @NonNull but is null", exc.getMessage());
105         }
106
107         assertEquals(2, tcl.getKeys().size());
108         assertEquals(2, new ToscaConstraintLogicalKey().getKeys().size());
109
110         new ToscaConstraintLogicalKey().clean();
111         tcl.clean();
112         assertEquals(tclClone1, tcl);
113
114         assertFalse(new ToscaConstraintLogicalKey().validate(new PfValidationResult()).isValid());
115         assertTrue(tcl.validate(new PfValidationResult()).isValid());
116
117         try {
118             tcl.validate(null);
119             fail("test should throw an exception");
120         } catch (Exception exc) {
121             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
122         }
123
124         try {
125             new ToscaConstraintLogicalKey(tclKey, ToscaConstraintLogicalKey.Operation.EQ, null)
126                     .validate(new PfValidationResult());
127             fail("test should throw an exception");
128         } catch (Exception exc) {
129             assertEquals("compareToKey is marked @NonNull but is null", exc.getMessage());
130         }
131
132         assertFalse(new ToscaConstraintLogicalKey(tclKey, ToscaConstraintLogicalKey.Operation.EQ, new PfConceptKey())
133                 .validate(new PfValidationResult()).isValid());
134     }
135 }