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