Restructure for authorative models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaPropertyTest.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 java.util.ArrayList;
30 import java.util.List;
31
32 import org.junit.Test;
33 import org.onap.policy.models.base.PfConceptKey;
34 import org.onap.policy.models.base.PfReferenceKey;
35 import org.onap.policy.models.base.PfValidationResult;
36 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogical.Operation;
38 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraintLogicalString;
39 import org.onap.policy.models.tosca.simple.concepts.JpaToscaEntrySchema;
40 import org.onap.policy.models.tosca.simple.concepts.JpaToscaProperty;
41
42 /**
43  * DAO test for ToscaProperty.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class JpaToscaPropertyTest {
48
49     @Test
50     public void testPropertyPojo() {
51         assertNotNull(new JpaToscaProperty());
52         assertNotNull(new JpaToscaProperty(new PfReferenceKey()));
53         assertNotNull(new JpaToscaProperty(new PfReferenceKey(), new PfConceptKey()));
54         assertNotNull(new JpaToscaProperty(new JpaToscaProperty()));
55
56         try {
57             new JpaToscaProperty((PfReferenceKey) null);
58             fail("test should throw an exception");
59         } catch (Exception exc) {
60             assertEquals("key is marked @NonNull but is null", exc.getMessage());
61         }
62
63         try {
64             new JpaToscaProperty(null, null);
65             fail("test should throw an exception");
66         } catch (Exception exc) {
67             assertEquals("key is marked @NonNull but is null", exc.getMessage());
68         }
69
70         try {
71             new JpaToscaProperty(null, new PfConceptKey());
72             fail("test should throw an exception");
73         } catch (Exception exc) {
74             assertEquals("key is marked @NonNull but is null", exc.getMessage());
75         }
76
77         try {
78             new JpaToscaProperty(new PfReferenceKey(), null);
79             fail("test should throw an exception");
80         } catch (Exception exc) {
81             assertEquals("type is marked @NonNull but is null", exc.getMessage());
82         }
83
84         try {
85             new JpaToscaProperty((JpaToscaProperty) null);
86             fail("test should throw an exception");
87         } catch (Exception exc) {
88             assertEquals("copyConcept is marked @NonNull but is null", exc.getMessage());
89         }
90
91         PfConceptKey pparentKey = new PfConceptKey("tParentKey", "0.0.1");
92         PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
93         PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", "0.0.1");
94         JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
95
96         tp.setDescription("A Description");
97         assertEquals("A Description", tp.getDescription());
98
99         tp.setRequired(false);
100         assertFalse(tp.isRequired());
101
102         tp.setDefaultValue("defaultKey");
103
104         tp.setStatus(JpaToscaProperty.Status.SUPPORTED);
105
106         List<JpaToscaConstraint> constraints = new ArrayList<>();
107         JpaToscaConstraintLogicalString lsc =
108                 new JpaToscaConstraintLogicalString(new PfReferenceKey(pkey, "sc"), Operation.EQ, "hello");
109         constraints.add(lsc);
110         tp.setConstraints(constraints);
111         assertEquals(constraints, tp.getConstraints());
112
113         PfReferenceKey esKey = new PfReferenceKey(pkey, "entrySchema");
114         PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
115         JpaToscaEntrySchema tes = new JpaToscaEntrySchema(esKey, typeKey);
116         tp.setEntrySchema(tes);
117
118         JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
119         assertEquals(tp, tdtClone0);
120         assertEquals(0, tp.compareTo(tdtClone0));
121
122         JpaToscaProperty tdtClone1 = new JpaToscaProperty();
123         tp.copyTo(tdtClone1);
124         assertEquals(tp, tdtClone1);
125         assertEquals(0, tp.compareTo(tdtClone1));
126
127         assertEquals(-1, tp.compareTo(null));
128         assertEquals(0, tp.compareTo(tp));
129         assertFalse(tp.compareTo(tp.getKey()) == 0);
130
131         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherProperty");
132         JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
133
134         assertFalse(tp.compareTo(otherDt) == 0);
135         otherDt.setKey(pkey);
136         assertFalse(tp.compareTo(otherDt) == 0);
137         otherDt.setType(ptypeKey);
138         assertFalse(tp.compareTo(otherDt) == 0);
139         otherDt.setDescription("A Description");
140         assertFalse(tp.compareTo(otherDt) == 0);
141         otherDt.setRequired(false);
142         assertFalse(tp.compareTo(otherDt) == 0);
143         otherDt.setDefaultValue("defaultKey");
144         assertFalse(tp.compareTo(otherDt) == 0);
145         otherDt.setStatus(JpaToscaProperty.Status.SUPPORTED);
146         assertFalse(tp.compareTo(otherDt) == 0);
147         assertFalse(tp.compareTo(otherDt) == 0);
148         otherDt.setConstraints(constraints);
149         assertFalse(tp.compareTo(otherDt) == 0);
150         otherDt.setEntrySchema(tes);
151         assertEquals(0, tp.compareTo(otherDt));
152
153         otherDt.setRequired(true);
154         assertFalse(tp.compareTo(otherDt) == 0);
155         otherDt.setRequired(false);
156         assertEquals(0, tp.compareTo(otherDt));
157
158         otherDt.setStatus(JpaToscaProperty.Status.UNSUPPORTED);
159         assertFalse(tp.compareTo(otherDt) == 0);
160         otherDt.setStatus(JpaToscaProperty.Status.SUPPORTED);
161         assertEquals(0, tp.compareTo(otherDt));
162
163         try {
164             tp.copyTo(null);
165             fail("test should throw an exception");
166         } catch (Exception exc) {
167             assertEquals("target is marked @NonNull but is null", exc.getMessage());
168         }
169
170         assertEquals(5, tp.getKeys().size());
171         assertEquals(2, new JpaToscaProperty().getKeys().size());
172
173         new JpaToscaProperty().clean();
174         tp.clean();
175         assertEquals(tdtClone0, tp);
176
177         assertFalse(new JpaToscaProperty().validate(new PfValidationResult()).isValid());
178         assertTrue(tp.validate(new PfValidationResult()).isValid());
179
180         tp.setDescription(null);
181         assertTrue(tp.validate(new PfValidationResult()).isValid());
182         tp.setDescription("");
183         assertFalse(tp.validate(new PfValidationResult()).isValid());
184         tp.setDescription("A Description");
185         assertTrue(tp.validate(new PfValidationResult()).isValid());
186
187         tp.setType(null);
188         assertFalse(tp.validate(new PfValidationResult()).isValid());
189         tp.setType(typeKey);
190         assertTrue(tp.validate(new PfValidationResult()).isValid());
191
192         tp.setType(PfConceptKey.getNullKey());
193         assertFalse(tp.validate(new PfValidationResult()).isValid());
194         tp.setType(typeKey);
195         assertTrue(tp.validate(new PfValidationResult()).isValid());
196
197         tp.setDefaultValue(null);
198         assertTrue(tp.validate(new PfValidationResult()).isValid());
199         tp.setDefaultValue("defaultKey");
200         assertTrue(tp.validate(new PfValidationResult()).isValid());
201
202         tp.getConstraints().add(null);
203         assertFalse(tp.validate(new PfValidationResult()).isValid());
204         tp.getConstraints().remove(null);
205         assertTrue(tp.validate(new PfValidationResult()).isValid());
206
207         try {
208             tp.setStatus(null);
209             fail("test should throw an exception");
210         } catch (Exception exc) {
211             assertEquals("status is marked @NonNull but is null", exc.getMessage());
212         }
213
214         try {
215             tp.validate(null);
216             fail("test should throw an exception");
217         } catch (Exception exc) {
218             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
219         }
220     }
221 }