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.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;
43 * DAO test for ToscaProperty.
45 * @author Liam Fallon (liam.fallon@est.tech)
47 public class JpaToscaPropertyTest {
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()));
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());
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());
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());
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());
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());
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);
96 tp.setDescription("A Description");
97 assertEquals("A Description", tp.getDescription());
99 tp.setRequired(false);
100 assertFalse(tp.isRequired());
102 tp.setDefaultValue("defaultKey");
104 tp.setStatus(JpaToscaProperty.Status.SUPPORTED);
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());
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);
118 JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
119 assertEquals(tp, tdtClone0);
120 assertEquals(0, tp.compareTo(tdtClone0));
122 JpaToscaProperty tdtClone1 = new JpaToscaProperty();
123 tp.copyTo(tdtClone1);
124 assertEquals(tp, tdtClone1);
125 assertEquals(0, tp.compareTo(tdtClone1));
127 assertEquals(-1, tp.compareTo(null));
128 assertEquals(0, tp.compareTo(tp));
129 assertFalse(tp.compareTo(tp.getKey()) == 0);
131 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherProperty");
132 JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
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));
153 otherDt.setRequired(true);
154 assertFalse(tp.compareTo(otherDt) == 0);
155 otherDt.setRequired(false);
156 assertEquals(0, tp.compareTo(otherDt));
158 otherDt.setStatus(JpaToscaProperty.Status.UNSUPPORTED);
159 assertFalse(tp.compareTo(otherDt) == 0);
160 otherDt.setStatus(JpaToscaProperty.Status.SUPPORTED);
161 assertEquals(0, tp.compareTo(otherDt));
165 fail("test should throw an exception");
166 } catch (Exception exc) {
167 assertEquals("target is marked @NonNull but is null", exc.getMessage());
170 assertEquals(5, tp.getKeys().size());
171 assertEquals(2, new JpaToscaProperty().getKeys().size());
173 new JpaToscaProperty().clean();
175 assertEquals(tdtClone0, tp);
177 assertFalse(new JpaToscaProperty().validate(new PfValidationResult()).isValid());
178 assertTrue(tp.validate(new PfValidationResult()).isValid());
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());
188 assertFalse(tp.validate(new PfValidationResult()).isValid());
190 assertTrue(tp.validate(new PfValidationResult()).isValid());
192 tp.setType(PfConceptKey.getNullKey());
193 assertFalse(tp.validate(new PfValidationResult()).isValid());
195 assertTrue(tp.validate(new PfValidationResult()).isValid());
197 tp.setDefaultValue(null);
198 assertTrue(tp.validate(new PfValidationResult()).isValid());
199 tp.setDefaultValue("defaultKey");
200 assertTrue(tp.validate(new PfValidationResult()).isValid());
202 tp.getConstraints().add(null);
203 assertFalse(tp.validate(new PfValidationResult()).isValid());
204 tp.getConstraints().remove(null);
205 assertTrue(tp.validate(new PfValidationResult()).isValid());
209 fail("test should throw an exception");
210 } catch (Exception exc) {
211 assertEquals("status is marked @NonNull but is null", exc.getMessage());
216 fail("test should throw an exception");
217 } catch (Exception exc) {
218 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());