a33da605e6c35629b1bbef4683b2a690bce3949c
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / ToscaPropertyTest.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.ToscaConstraint;
37 import org.onap.policy.models.tosca.simple.concepts.ToscaConstraintLogical.Operation;
38 import org.onap.policy.models.tosca.simple.concepts.ToscaConstraintLogicalString;
39 import org.onap.policy.models.tosca.simple.concepts.ToscaEntrySchema;
40 import org.onap.policy.models.tosca.simple.concepts.ToscaProperty;
41
42 /**
43  * DAO test for ToscaProperty.
44  *
45  * @author Liam Fallon (liam.fallon@est.tech)
46  */
47 public class ToscaPropertyTest {
48
49     @Test
50     public void testPropertyPojo() {
51         assertNotNull(new ToscaProperty());
52         assertNotNull(new ToscaProperty(new PfReferenceKey()));
53         assertNotNull(new ToscaProperty(new PfReferenceKey(), new PfConceptKey()));
54         assertNotNull(new ToscaProperty(new ToscaProperty()));
55
56         try {
57             new ToscaProperty((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 ToscaProperty(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 ToscaProperty(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 ToscaProperty(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 ToscaProperty((ToscaProperty) 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         ToscaProperty tp = new ToscaProperty(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         PfConceptKey tdefaultKey = new PfConceptKey("defaultKey", "0.0.1");
103         tp.setDefaultValue(tdefaultKey);
104
105         tp.setStatus(ToscaProperty.Status.SUPPORTED);
106
107         List<ToscaConstraint> constraints = new ArrayList<>();
108         ToscaConstraintLogicalString lsc =
109                 new ToscaConstraintLogicalString(new PfReferenceKey(pkey, "sc"), Operation.EQ, "hello");
110         constraints.add(lsc);
111         tp.setConstraints(constraints);
112         assertEquals(constraints, tp.getConstraints());
113
114         PfReferenceKey esKey = new PfReferenceKey(pkey, "entrySchema");
115         PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
116         ToscaEntrySchema tes = new ToscaEntrySchema(esKey, typeKey);
117         tp.setEntrySchema(tes);
118
119         ToscaProperty tdtClone0 = new ToscaProperty(tp);
120         assertEquals(tp, tdtClone0);
121         assertEquals(0, tp.compareTo(tdtClone0));
122
123         ToscaProperty tdtClone1 = new ToscaProperty();
124         tp.copyTo(tdtClone1);
125         assertEquals(tp, tdtClone1);
126         assertEquals(0, tp.compareTo(tdtClone1));
127
128         assertEquals(-1, tp.compareTo(null));
129         assertEquals(0, tp.compareTo(tp));
130         assertFalse(tp.compareTo(tp.getKey()) == 0);
131
132         PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherProperty");
133         ToscaProperty otherDt = new ToscaProperty(otherDtKey);
134
135         assertFalse(tp.compareTo(otherDt) == 0);
136         otherDt.setKey(pkey);
137         assertFalse(tp.compareTo(otherDt) == 0);
138         otherDt.setType(ptypeKey);
139         assertFalse(tp.compareTo(otherDt) == 0);
140         otherDt.setDescription("A Description");
141         assertFalse(tp.compareTo(otherDt) == 0);
142         otherDt.setRequired(false);
143         assertFalse(tp.compareTo(otherDt) == 0);
144         otherDt.setDefaultValue(tdefaultKey);
145         assertFalse(tp.compareTo(otherDt) == 0);
146         otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
147         assertFalse(tp.compareTo(otherDt) == 0);
148         assertFalse(tp.compareTo(otherDt) == 0);
149         otherDt.setConstraints(constraints);
150         assertFalse(tp.compareTo(otherDt) == 0);
151         otherDt.setEntrySchema(tes);
152         assertEquals(0, tp.compareTo(otherDt));
153
154         otherDt.setRequired(true);
155         assertFalse(tp.compareTo(otherDt) == 0);
156         otherDt.setRequired(false);
157         assertEquals(0, tp.compareTo(otherDt));
158
159         otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
160         assertFalse(tp.compareTo(otherDt) == 0);
161         otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
162         assertEquals(0, tp.compareTo(otherDt));
163
164         try {
165             tp.copyTo(null);
166             fail("test should throw an exception");
167         } catch (Exception exc) {
168             assertEquals("target is marked @NonNull but is null", exc.getMessage());
169         }
170
171         assertEquals(6, tp.getKeys().size());
172         assertEquals(2, new ToscaProperty().getKeys().size());
173
174         new ToscaProperty().clean();
175         tp.clean();
176         assertEquals(tdtClone0, tp);
177
178         assertFalse(new ToscaProperty().validate(new PfValidationResult()).isValid());
179         assertTrue(tp.validate(new PfValidationResult()).isValid());
180
181         tp.setDescription(null);
182         assertTrue(tp.validate(new PfValidationResult()).isValid());
183         tp.setDescription("");
184         assertFalse(tp.validate(new PfValidationResult()).isValid());
185         tp.setDescription("A Description");
186         assertTrue(tp.validate(new PfValidationResult()).isValid());
187
188         tp.setType(null);
189         assertFalse(tp.validate(new PfValidationResult()).isValid());
190         tp.setType(typeKey);
191         assertTrue(tp.validate(new PfValidationResult()).isValid());
192
193         tp.setType(PfConceptKey.getNullKey());
194         assertFalse(tp.validate(new PfValidationResult()).isValid());
195         tp.setType(typeKey);
196         assertTrue(tp.validate(new PfValidationResult()).isValid());
197
198         tp.setDefaultValue(null);
199         assertTrue(tp.validate(new PfValidationResult()).isValid());
200         tp.setDefaultValue(tdefaultKey);
201         assertTrue(tp.validate(new PfValidationResult()).isValid());
202
203         tp.setDefaultValue(PfConceptKey.getNullKey());
204         assertFalse(tp.validate(new PfValidationResult()).isValid());
205         tp.setDefaultValue(tdefaultKey);
206         assertTrue(tp.validate(new PfValidationResult()).isValid());
207
208         tp.getConstraints().add(null);
209         assertFalse(tp.validate(new PfValidationResult()).isValid());
210         tp.getConstraints().remove(null);
211         assertTrue(tp.validate(new PfValidationResult()).isValid());
212
213         try {
214             tp.setStatus(null);
215             fail("test should throw an exception");
216         } catch (Exception exc) {
217             assertEquals("status is marked @NonNull but is null", exc.getMessage());
218         }
219
220         try {
221             tp.validate(null);
222             fail("test should throw an exception");
223         } catch (Exception exc) {
224             assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());
225         }
226     }
227 }