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