2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
30 import java.util.ArrayList;
31 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.authorative.concepts.ToscaProperty;
39 * DAO test for ToscaProperty.
41 * @author Liam Fallon (liam.fallon@est.tech)
43 public class JpaToscaPropertyTest {
45 private static final String KEY_IS_NULL = "key is marked @NonNull but is null";
46 private static final String DEFAULT_KEY = "defaultKey";
47 private static final String A_DESCRIPTION = "A Description";
48 private static final String VERSION_001 = "0.0.1";
51 public void testPropertyPojo() {
52 assertNotNull(new JpaToscaProperty());
53 assertNotNull(new JpaToscaProperty(new PfReferenceKey()));
54 assertNotNull(new JpaToscaProperty(new PfReferenceKey(), new PfConceptKey()));
55 assertNotNull(new JpaToscaProperty(new JpaToscaProperty()));
57 assertThatThrownBy(() -> new JpaToscaProperty((PfReferenceKey) null)).hasMessage(KEY_IS_NULL);
59 assertThatThrownBy(() -> new JpaToscaProperty(null, null)).hasMessage(KEY_IS_NULL);
61 assertThatThrownBy(() -> new JpaToscaProperty(null, new PfConceptKey())).hasMessage(KEY_IS_NULL);
63 assertThatThrownBy(() -> new JpaToscaProperty(new PfReferenceKey(), null))
64 .hasMessage("type is marked @NonNull but is null");
66 PfConceptKey pparentKey = new PfConceptKey("tParentKey", VERSION_001);
67 PfReferenceKey pkey = new PfReferenceKey(pparentKey, "trigger0");
68 PfConceptKey ptypeKey = new PfConceptKey("TTypeKey", VERSION_001);
69 JpaToscaProperty tp = new JpaToscaProperty(pkey, ptypeKey);
71 tp.setDescription(A_DESCRIPTION);
72 assertEquals(A_DESCRIPTION, tp.getDescription());
74 tp.setRequired(false);
75 assertFalse(tp.isRequired());
77 tp.setDefaultValue(DEFAULT_KEY);
79 tp.setStatus(ToscaProperty.Status.SUPPORTED);
81 List<JpaToscaConstraint> constraints = new ArrayList<>();
82 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
84 tp.setConstraints(constraints);
85 assertEquals(constraints, tp.getConstraints());
87 PfConceptKey typeKey = new PfConceptKey("type", VERSION_001);
88 JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
89 tp.setEntrySchema(tes);
91 JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
92 assertEquals(tp, tdtClone0);
93 assertEquals(0, tp.compareTo(tdtClone0));
95 JpaToscaProperty tdtClone1 = new JpaToscaProperty(tp);
96 assertEquals(tp, tdtClone1);
97 assertEquals(0, tp.compareTo(tdtClone1));
99 assertEquals(-1, tp.compareTo(null));
100 assertEquals(0, tp.compareTo(tp));
101 assertFalse(tp.compareTo(tp.getKey()) == 0);
103 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", VERSION_001, "OtherProperty");
104 JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
106 assertFalse(tp.compareTo(otherDt) == 0);
107 otherDt.setKey(pkey);
108 assertFalse(tp.compareTo(otherDt) == 0);
109 otherDt.setType(ptypeKey);
110 assertFalse(tp.compareTo(otherDt) == 0);
111 otherDt.setDescription(A_DESCRIPTION);
112 assertFalse(tp.compareTo(otherDt) == 0);
113 otherDt.setRequired(false);
114 assertFalse(tp.compareTo(otherDt) == 0);
115 otherDt.setDefaultValue(DEFAULT_KEY);
116 assertFalse(tp.compareTo(otherDt) == 0);
117 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
118 assertFalse(tp.compareTo(otherDt) == 0);
119 assertFalse(tp.compareTo(otherDt) == 0);
120 otherDt.setConstraints(constraints);
121 assertFalse(tp.compareTo(otherDt) == 0);
122 otherDt.setEntrySchema(tes);
123 assertEquals(0, tp.compareTo(otherDt));
125 otherDt.setRequired(true);
126 assertFalse(tp.compareTo(otherDt) == 0);
127 otherDt.setRequired(false);
128 assertEquals(0, tp.compareTo(otherDt));
130 otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
131 assertFalse(tp.compareTo(otherDt) == 0);
132 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
133 assertEquals(0, tp.compareTo(otherDt));
135 assertThatThrownBy(() -> new JpaToscaProperty((JpaToscaProperty) null))
136 .isInstanceOf(NullPointerException.class);
138 assertEquals(3, tp.getKeys().size());
139 assertEquals(2, new JpaToscaProperty().getKeys().size());
141 new JpaToscaProperty().clean();
143 assertEquals(tdtClone0, tp);
145 assertFalse(new JpaToscaProperty().validate(new PfValidationResult()).isValid());
146 assertTrue(tp.validate(new PfValidationResult()).isValid());
148 tp.setDescription(null);
149 assertTrue(tp.validate(new PfValidationResult()).isValid());
150 tp.setDescription("");
151 assertFalse(tp.validate(new PfValidationResult()).isValid());
152 tp.setDescription(A_DESCRIPTION);
153 assertTrue(tp.validate(new PfValidationResult()).isValid());
156 assertFalse(tp.validate(new PfValidationResult()).isValid());
158 assertTrue(tp.validate(new PfValidationResult()).isValid());
160 tp.setType(PfConceptKey.getNullKey());
161 assertFalse(tp.validate(new PfValidationResult()).isValid());
163 assertTrue(tp.validate(new PfValidationResult()).isValid());
165 tp.setDefaultValue(null);
166 assertTrue(tp.validate(new PfValidationResult()).isValid());
167 tp.setDefaultValue("");
168 assertFalse(tp.validate(new PfValidationResult()).isValid());
169 tp.setDefaultValue(DEFAULT_KEY);
170 assertTrue(tp.validate(new PfValidationResult()).isValid());
172 tp.getConstraints().add(null);
173 assertFalse(tp.validate(new PfValidationResult()).isValid());
174 tp.getConstraints().remove(null);
175 assertTrue(tp.validate(new PfValidationResult()).isValid());
177 assertThatThrownBy(() -> tp.validate(null)).hasMessage("resultIn is marked @NonNull but is null");