2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024-2025 Nordix Foundation
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.models.tosca.simple.concepts;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import java.io.Serial;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
33 import lombok.NoArgsConstructor;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.onap.policy.common.parameters.annotations.NotNull;
39 import org.onap.policy.models.base.PfConceptKey;
40 import org.onap.policy.models.base.PfKey;
41 import org.onap.policy.models.base.PfReferenceKey;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithToscaProperties;
45 class JpaToscaWithToscaPropertiesTest {
46 private static final String SOME_DESCRIPTION = "some description";
47 private static final String KEY1 = "abc";
48 private static final String KEY2 = "def";
49 private static final PfConceptKey CONCEPT_KEY1 = new PfConceptKey("hello", "1.2.3");
50 private static final PfConceptKey CONCEPT_KEY2 = new PfConceptKey("world", "3.2.1");
51 private static final PfReferenceKey REF_KEY1 = new PfReferenceKey(CONCEPT_KEY1);
52 private static final PfReferenceKey REF_KEY2 = new PfReferenceKey(CONCEPT_KEY2);
53 private static final JpaToscaProperty JPA_PROP1 = new JpaToscaProperty(REF_KEY1);
54 private static final JpaToscaProperty JPA_PROP2 = new JpaToscaProperty(REF_KEY2);
55 private static ToscaProperty toscaPropertyOne;
56 private static ToscaProperty toscaPropertyTwo;
57 private static final String DESCRIPT1 = "description A";
58 private static final String DESCRIPT2 = "description B";
63 * Initializes the properties.
66 public static void setUpBeforeClass() {
67 JPA_PROP1.setDescription(DESCRIPT1);
68 JPA_PROP2.setDescription(DESCRIPT2);
70 toscaPropertyOne = JPA_PROP1.toAuthorative();
71 toscaPropertyTwo = JPA_PROP2.toAuthorative();
81 PfConceptKey key = new PfConceptKey("bye", "9.8.7");
84 jpa.setDescription(SOME_DESCRIPTION);
85 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
87 // properties should be included
89 List<PfKey> keys = jpa.getKeys();
90 Collections.sort(keys);
92 assertThat(keys).isEqualTo(
93 List.of(PfConceptKey.getNullKey(), PfConceptKey.getNullKey(), key, REF_KEY1, REF_KEY2));
100 jpa.setDescription(" some description ");
102 JpaToscaProperty prop1 = new JpaToscaProperty(JPA_PROP1);
103 prop1.setDescription(DESCRIPT1 + " ");
105 JpaToscaProperty prop2 = new JpaToscaProperty(JPA_PROP2);
106 prop2.setDescription(" " + DESCRIPT2);
108 jpa.setProperties(Map.of(KEY1, prop1, KEY2, prop2));
111 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
112 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
116 void testToAuthorative() {
117 jpa.setDescription(SOME_DESCRIPTION);
118 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
120 MyTosca tosca = jpa.toAuthorative();
121 assertEquals(SOME_DESCRIPTION, tosca.getDescription());
122 assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, toscaPropertyOne, KEY2, toscaPropertyTwo));
126 void testFromAuthorative() {
127 MyTosca tosca = new MyTosca();
128 tosca.setDescription(SOME_DESCRIPTION);
130 jpa.fromAuthorative(tosca);
131 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
132 assertThat(jpa.getProperties()).isNull();
134 tosca.setProperties(Map.of(KEY1, toscaPropertyOne, KEY2, toscaPropertyTwo));
136 JpaToscaProperty jpa1 = new JpaToscaProperty(toscaPropertyOne);
137 jpa1.setKey(new PfReferenceKey(jpa.getKey(), KEY1));
139 JpaToscaProperty jpa2 = new JpaToscaProperty(toscaPropertyTwo);
140 jpa2.setKey(new PfReferenceKey(jpa.getKey(), KEY2));
143 jpa.fromAuthorative(tosca);
144 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
145 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, jpa1, KEY2, jpa2));
149 void testCompareTo() {
150 jpa.setDescription(SOME_DESCRIPTION);
151 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
153 assertThat(jpa).isNotEqualByComparingTo(null).isEqualByComparingTo(jpa).isNotEqualByComparingTo(new MyJpa2());
155 MyJpa jpa2 = new MyJpa();
156 jpa2.setDescription(SOME_DESCRIPTION);
157 jpa2.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
158 assertThat(jpa).isEqualByComparingTo(jpa2);
160 jpa2.setProperties(Map.of(KEY1, JPA_PROP1));
161 assertThat(jpa).isNotEqualByComparingTo(jpa2);
165 void testJpaToscaWithToscaProperties() {
166 assertThat(jpa.getProperties()).isNull();
167 assertThat(jpa.getKey().isNullKey()).isTrue();
171 void testJpaToscaWithToscaPropertiesPfConceptKey() {
172 jpa = new MyJpa(CONCEPT_KEY1);
173 assertEquals(CONCEPT_KEY1, jpa.getKey());
177 void testJpaToscaWithToscaPropertiesJpaToscaWithToscaPropertiesOfT() {
178 jpa.setDescription(SOME_DESCRIPTION);
179 assertEquals(jpa, new MyJpa(jpa));
181 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
182 assertEquals(jpa, new MyJpa(jpa));
186 void testJpaToscaWithToscaPropertiesT() {
187 MyTosca tosca = new MyTosca();
188 tosca.setName("world");
189 tosca.setVersion("3.2.1");
190 tosca.setDescription(SOME_DESCRIPTION);
191 tosca.setProperties(Map.of(KEY1, toscaPropertyOne, KEY2, toscaPropertyTwo));
193 jpa = new MyJpa(tosca);
194 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
196 JpaToscaProperty jpa1 = new JpaToscaProperty(toscaPropertyOne);
197 jpa1.setKey(new PfReferenceKey(jpa.getKey(), KEY1));
199 JpaToscaProperty jpa2 = new JpaToscaProperty(toscaPropertyTwo);
200 jpa2.setKey(new PfReferenceKey(jpa.getKey(), KEY2));
202 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, jpa1, KEY2, jpa2));
204 assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey());
208 void testValidateWithKey() {
209 // null key - should fail
210 jpa.setText("some text");
211 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
214 jpa.setKey(new PfConceptKey("xyz", "2.3.4"));
215 assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue();
217 // null text - bean validator should fail
219 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
223 void testGetReferencedDataTypes() {
224 assertThat(jpa.getReferencedDataTypes()).isEmpty();
227 PfConceptKey schemaKey = new PfConceptKey("schemaZ", "9.8.7");
228 JpaToscaSchemaDefinition schema = new JpaToscaSchemaDefinition();
229 schema.setType(schemaKey);
230 JpaToscaProperty prop1 = new JpaToscaProperty(JPA_PROP1);
231 prop1.setType(CONCEPT_KEY1);
232 prop1.setEntrySchema(schema);
234 // one property without a schema
235 JpaToscaProperty prop2 = new JpaToscaProperty(JPA_PROP2);
236 prop2.setType(CONCEPT_KEY2);
237 prop2.setEntrySchema(null);
239 jpa.setProperties(Map.of(KEY1, prop1, KEY2, prop2));
241 List<PfConceptKey> keys = new ArrayList<>(jpa.getReferencedDataTypes());
242 Collections.sort(keys);
244 assertThat(keys).isEqualTo(List.of(CONCEPT_KEY1, schemaKey, CONCEPT_KEY2));
248 protected static class MyTosca extends ToscaWithToscaProperties {
255 protected static class MyJpa extends JpaToscaWithToscaProperties<MyTosca> {
257 private static final long serialVersionUID = 1L;
262 public MyJpa(MyJpa jpa) {
266 public MyJpa(PfConceptKey key) {
270 public MyJpa(MyTosca tosca) {
275 public MyTosca toAuthorative() {
276 this.setToscaEntity(new MyTosca());
277 return super.toAuthorative();
281 private static class MyJpa2 extends MyJpa {
283 private static final long serialVersionUID = 1L;