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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
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;
29 import java.util.ArrayList;
30 import java.util.List;
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;
43 * DAO test for ToscaProperty.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class ToscaPropertyTest {
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()));
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());
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());
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());
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());
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());
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);
96 tp.setDescription("A Description");
97 assertEquals("A Description", tp.getDescription());
99 tp.setRequired(false);
100 assertFalse(tp.isRequired());
102 PfConceptKey tdefaultKey = new PfConceptKey("defaultKey", "0.0.1");
103 tp.setDefaultValue(tdefaultKey);
105 tp.setStatus(ToscaProperty.Status.SUPPORTED);
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());
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);
119 ToscaProperty tdtClone0 = new ToscaProperty(tp);
120 assertEquals(tp, tdtClone0);
121 assertEquals(0, tp.compareTo(tdtClone0));
123 ToscaProperty tdtClone1 = new ToscaProperty();
124 tp.copyTo(tdtClone1);
125 assertEquals(tp, tdtClone1);
126 assertEquals(0, tp.compareTo(tdtClone1));
128 assertEquals(-1, tp.compareTo(null));
129 assertEquals(0, tp.compareTo(tp));
130 assertFalse(tp.compareTo(tp.getKey()) == 0);
132 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherProperty");
133 ToscaProperty otherDt = new ToscaProperty(otherDtKey);
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));
154 otherDt.setRequired(true);
155 assertFalse(tp.compareTo(otherDt) == 0);
156 otherDt.setRequired(false);
157 assertEquals(0, tp.compareTo(otherDt));
159 otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
160 assertFalse(tp.compareTo(otherDt) == 0);
161 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
162 assertEquals(0, tp.compareTo(otherDt));
166 fail("test should throw an exception");
167 } catch (Exception exc) {
168 assertEquals("target is marked @NonNull but is null", exc.getMessage());
171 assertEquals(6, tp.getKeys().size());
172 assertEquals(2, new ToscaProperty().getKeys().size());
174 new ToscaProperty().clean();
176 assertEquals(tdtClone0, tp);
178 assertFalse(new ToscaProperty().validate(new PfValidationResult()).isValid());
179 assertTrue(tp.validate(new PfValidationResult()).isValid());
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());
189 assertFalse(tp.validate(new PfValidationResult()).isValid());
191 assertTrue(tp.validate(new PfValidationResult()).isValid());
193 tp.setType(PfConceptKey.getNullKey());
194 assertFalse(tp.validate(new PfValidationResult()).isValid());
196 assertTrue(tp.validate(new PfValidationResult()).isValid());
198 tp.setDefaultValue(null);
199 assertTrue(tp.validate(new PfValidationResult()).isValid());
200 tp.setDefaultValue(tdefaultKey);
201 assertTrue(tp.validate(new PfValidationResult()).isValid());
203 tp.setDefaultValue(PfConceptKey.getNullKey());
204 assertFalse(tp.validate(new PfValidationResult()).isValid());
205 tp.setDefaultValue(tdefaultKey);
206 assertTrue(tp.validate(new PfValidationResult()).isValid());
208 tp.getConstraints().add(null);
209 assertFalse(tp.validate(new PfValidationResult()).isValid());
210 tp.getConstraints().remove(null);
211 assertTrue(tp.validate(new PfValidationResult()).isValid());
215 fail("test should throw an exception");
216 } catch (Exception exc) {
217 assertEquals("status is marked @NonNull but is null", exc.getMessage());
222 fail("test should throw an exception");
223 } catch (Exception exc) {
224 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());