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.authorative.concepts.ToscaProperty;
37 import org.onap.policy.models.tosca.simple.concepts.JpaToscaConstraint;
40 * DAO test for ToscaProperty.
42 * @author Liam Fallon (liam.fallon@est.tech)
44 public class JpaToscaPropertyTest {
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()));
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());
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());
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());
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());
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());
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);
93 tp.setDescription("A Description");
94 assertEquals("A Description", tp.getDescription());
96 tp.setRequired(false);
97 assertFalse(tp.isRequired());
99 tp.setDefaultValue("defaultKey");
101 tp.setStatus(ToscaProperty.Status.SUPPORTED);
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());
109 PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
110 JpaToscaEntrySchema tes = new JpaToscaEntrySchema(typeKey);
111 tp.setEntrySchema(tes);
113 JpaToscaProperty tdtClone0 = new JpaToscaProperty(tp);
114 assertEquals(tp, tdtClone0);
115 assertEquals(0, tp.compareTo(tdtClone0));
117 JpaToscaProperty tdtClone1 = new JpaToscaProperty();
118 tp.copyTo(tdtClone1);
119 assertEquals(tp, tdtClone1);
120 assertEquals(0, tp.compareTo(tdtClone1));
122 assertEquals(-1, tp.compareTo(null));
123 assertEquals(0, tp.compareTo(tp));
124 assertFalse(tp.compareTo(tp.getKey()) == 0);
126 PfReferenceKey otherDtKey = new PfReferenceKey("otherDt", "0.0.1", "OtherProperty");
127 JpaToscaProperty otherDt = new JpaToscaProperty(otherDtKey);
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));
148 otherDt.setRequired(true);
149 assertFalse(tp.compareTo(otherDt) == 0);
150 otherDt.setRequired(false);
151 assertEquals(0, tp.compareTo(otherDt));
153 otherDt.setStatus(ToscaProperty.Status.UNSUPPORTED);
154 assertFalse(tp.compareTo(otherDt) == 0);
155 otherDt.setStatus(ToscaProperty.Status.SUPPORTED);
156 assertEquals(0, tp.compareTo(otherDt));
160 fail("test should throw an exception");
161 } catch (Exception exc) {
162 assertEquals("target is marked @NonNull but is null", exc.getMessage());
165 assertEquals(3, tp.getKeys().size());
166 assertEquals(2, new JpaToscaProperty().getKeys().size());
168 new JpaToscaProperty().clean();
170 assertEquals(tdtClone0, tp);
172 assertFalse(new JpaToscaProperty().validate(new PfValidationResult()).isValid());
173 assertTrue(tp.validate(new PfValidationResult()).isValid());
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());
183 assertFalse(tp.validate(new PfValidationResult()).isValid());
185 assertTrue(tp.validate(new PfValidationResult()).isValid());
187 tp.setType(PfConceptKey.getNullKey());
188 assertFalse(tp.validate(new PfValidationResult()).isValid());
190 assertTrue(tp.validate(new PfValidationResult()).isValid());
192 tp.setDefaultValue(null);
193 assertTrue(tp.validate(new PfValidationResult()).isValid());
194 tp.setDefaultValue("defaultKey");
195 assertTrue(tp.validate(new PfValidationResult()).isValid());
197 tp.getConstraints().add(null);
198 assertFalse(tp.validate(new PfValidationResult()).isValid());
199 tp.getConstraints().remove(null);
200 assertTrue(tp.validate(new PfValidationResult()).isValid());
204 fail("test should throw an exception");
205 } catch (Exception exc) {
206 assertEquals("resultIn is marked @NonNull but is null", exc.getMessage());