2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 Nordix Foundation.
4 * Modifications Copyright (C) 2019-2020 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.assertNotEquals;
28 import static org.junit.Assert.assertNotNull;
29 import static org.junit.Assert.assertTrue;
31 import java.util.ArrayList;
32 import java.util.List;
33 import org.junit.Test;
34 import org.onap.policy.models.base.PfConceptKey;
37 * DAO test for ToscaEntrySchema.
39 * @author Liam Fallon (liam.fallon@est.tech)
41 public class JpaToscaSchemaDefinitionTest {
43 private static final String A_DESCRIPTION = "A Description";
46 public void testEntrySchemaNull() {
47 assertNotNull(new JpaToscaSchemaDefinition(new PfConceptKey()));
48 assertNotNull(new JpaToscaSchemaDefinition(new JpaToscaSchemaDefinition(new PfConceptKey())));
50 assertThatThrownBy(() -> new JpaToscaSchemaDefinition((PfConceptKey) null))
51 .hasMessageMatching("type is marked .*on.*ull but is null");
53 assertThatThrownBy(() -> new JpaToscaSchemaDefinition((JpaToscaSchemaDefinition) null))
54 .hasMessageMatching("copyConcept is marked .*on.*ull but is null");
58 public void testEntrySchema() {
59 PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
60 JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
62 tes.setDescription(A_DESCRIPTION);
63 assertEquals(A_DESCRIPTION, tes.getDescription());
65 List<JpaToscaConstraint> constraints = new ArrayList<>();
66 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
68 tes.setConstraints(constraints);
69 assertEquals(constraints, tes.getConstraints());
71 JpaToscaSchemaDefinition tdtClone0 = new JpaToscaSchemaDefinition(tes);
72 assertEquals(tes, tdtClone0);
73 assertEquals(0, tes.compareTo(tdtClone0));
75 JpaToscaSchemaDefinition tdtClone1 = new JpaToscaSchemaDefinition(tes);
76 assertEquals(tes, tdtClone1);
77 assertEquals(0, tes.compareTo(tdtClone1));
79 assertEquals(-1, tes.compareTo(null));
80 assertEquals(0, tes.compareTo(tes));
82 JpaToscaSchemaDefinition otherEs = new JpaToscaSchemaDefinition(typeKey);
84 assertNotEquals(0, tes.compareTo(otherEs));
85 otherEs.setType(typeKey);
86 assertNotEquals(0, tes.compareTo(otherEs));
87 otherEs.setDescription(A_DESCRIPTION);
88 assertNotEquals(0, tes.compareTo(otherEs));
89 otherEs.setConstraints(constraints);
90 assertEquals(0, tes.compareTo(otherEs));
92 assertThatThrownBy(() -> tes.copyTo(null)).hasMessageMatching("target is marked .*on.*ull but is null");
94 assertEquals(1, tes.getKeys().size());
95 assertEquals(1, new JpaToscaSchemaDefinition(typeKey).getKeys().size());
97 new JpaToscaSchemaDefinition(typeKey).clean();
99 assertEquals(tdtClone0, tes);
103 public void testEntrySchemaValidation() {
104 PfConceptKey typeKey = new PfConceptKey("type", "0.0.1");
105 JpaToscaSchemaDefinition tes = setUpJpaToscaSchemaDefinition(typeKey);
107 assertTrue(new JpaToscaSchemaDefinition(typeKey).validate("").isValid());
108 assertTrue(tes.validate("").isValid());
110 tes.setType(PfConceptKey.getNullKey());
111 assertFalse(tes.validate("").isValid());
113 assertFalse(tes.validate("").isValid());
114 tes.setType(typeKey);
115 assertTrue(tes.validate("").isValid());
117 tes.setDescription("");
119 assertFalse(tes.validate("").isValid());
120 tes.setDescription(A_DESCRIPTION);
121 assertTrue(tes.validate("").isValid());
123 tes.getConstraints().add(null);
124 assertFalse(tes.validate("").isValid());
125 tes.getConstraints().remove(null);
126 assertTrue(tes.validate("").isValid());
128 assertThatThrownBy(() -> tes.validate(null)).hasMessageMatching("fieldName is marked .*on.*ull but is null");
131 private JpaToscaSchemaDefinition setUpJpaToscaSchemaDefinition(PfConceptKey typeKey) {
132 JpaToscaSchemaDefinition tes = new JpaToscaSchemaDefinition(typeKey);
133 tes.setDescription(A_DESCRIPTION);
135 List<JpaToscaConstraint> constraints = new ArrayList<>();
136 JpaToscaConstraintLogical lsc = new JpaToscaConstraintLogical(JpaToscaConstraintOperation.EQ, "hello");
137 constraints.add(lsc);
138 tes.setConstraints(constraints);