2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 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.util.ArrayList;
28 import java.util.Collections;
29 import java.util.List;
32 import lombok.NoArgsConstructor;
34 import org.junit.jupiter.api.BeforeAll;
35 import org.junit.jupiter.api.BeforeEach;
36 import org.junit.jupiter.api.Test;
37 import org.onap.policy.common.parameters.annotations.NotNull;
38 import org.onap.policy.models.base.PfConceptKey;
39 import org.onap.policy.models.base.PfKey;
40 import org.onap.policy.models.base.PfReferenceKey;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithToscaProperties;
44 class JpaToscaWithToscaPropertiesTest {
45 private static final String SOME_DESCRIPTION = "some description";
46 private static final String KEY1 = "abc";
47 private static final String KEY2 = "def";
48 private static final PfConceptKey CONCEPT_KEY1 = new PfConceptKey("hello", "1.2.3");
49 private static final PfConceptKey CONCEPT_KEY2 = new PfConceptKey("world", "3.2.1");
50 private static final PfReferenceKey REF_KEY1 = new PfReferenceKey(CONCEPT_KEY1);
51 private static final PfReferenceKey REF_KEY2 = new PfReferenceKey(CONCEPT_KEY2);
52 private static final JpaToscaProperty JPA_PROP1 = new JpaToscaProperty(REF_KEY1);
53 private static final JpaToscaProperty JPA_PROP2 = new JpaToscaProperty(REF_KEY2);
54 private static ToscaProperty PROP1;
55 private static ToscaProperty PROP2;
56 private static final String DESCRIPT1 = "description A";
57 private static final String DESCRIPT2 = "description B";
62 * Initializes the properties.
65 public static void setUpBeforeClass() {
66 JPA_PROP1.setDescription(DESCRIPT1);
67 JPA_PROP2.setDescription(DESCRIPT2);
69 PROP1 = JPA_PROP1.toAuthorative();
70 PROP2 = JPA_PROP2.toAuthorative();
80 PfConceptKey key = new PfConceptKey("bye", "9.8.7");
83 jpa.setDescription(SOME_DESCRIPTION);
84 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
86 // properties should be included
88 List<PfKey> keys = jpa.getKeys();
89 Collections.sort(keys);
91 assertThat(keys).isEqualTo(
92 List.of(PfConceptKey.getNullKey(), PfConceptKey.getNullKey(), key, REF_KEY1, REF_KEY2));
99 jpa.setDescription(" some description ");
101 JpaToscaProperty prop1 = new JpaToscaProperty(JPA_PROP1);
102 prop1.setDescription(DESCRIPT1 + " ");
104 JpaToscaProperty prop2 = new JpaToscaProperty(JPA_PROP2);
105 prop2.setDescription(" " + DESCRIPT2);
107 jpa.setProperties(Map.of(KEY1, prop1, KEY2, prop2));
110 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
111 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
115 void testToAuthorative() {
116 jpa.setDescription(SOME_DESCRIPTION);
117 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
119 MyTosca tosca = jpa.toAuthorative();
120 assertEquals(SOME_DESCRIPTION, tosca.getDescription());
121 assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, PROP1, KEY2, PROP2));
125 void testFromAuthorative() {
126 MyTosca tosca = new MyTosca();
127 tosca.setDescription(SOME_DESCRIPTION);
129 jpa.fromAuthorative(tosca);
130 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
131 assertThat(jpa.getProperties()).isNull();
133 tosca.setProperties(Map.of(KEY1, PROP1, KEY2, PROP2));
135 JpaToscaProperty jpa1 = new JpaToscaProperty(PROP1);
136 jpa1.setKey(new PfReferenceKey(jpa.getKey(), KEY1));
138 JpaToscaProperty jpa2 = new JpaToscaProperty(PROP2);
139 jpa2.setKey(new PfReferenceKey(jpa.getKey(), KEY2));
142 jpa.fromAuthorative(tosca);
143 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
144 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, jpa1, KEY2, jpa2));
148 void testCompareTo() {
149 jpa.setDescription(SOME_DESCRIPTION);
150 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
152 assertThat(jpa).isNotEqualByComparingTo(null).isEqualByComparingTo(jpa).isNotEqualByComparingTo(new MyJpa2());
154 MyJpa jpa2 = new MyJpa();
155 jpa2.setDescription(SOME_DESCRIPTION);
156 jpa2.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
157 assertThat(jpa).isEqualByComparingTo(jpa2);
159 jpa2.setProperties(Map.of(KEY1, JPA_PROP1));
160 assertThat(jpa).isNotEqualByComparingTo(jpa2);
164 void testJpaToscaWithToscaProperties() {
165 assertThat(jpa.getProperties()).isNull();
166 assertThat(jpa.getKey().isNullKey()).isTrue();
170 void testJpaToscaWithToscaPropertiesPfConceptKey() {
171 jpa = new MyJpa(CONCEPT_KEY1);
172 assertEquals(CONCEPT_KEY1, jpa.getKey());
176 void testJpaToscaWithToscaPropertiesJpaToscaWithToscaPropertiesOfT() {
177 jpa.setDescription(SOME_DESCRIPTION);
178 assertEquals(jpa, new MyJpa(jpa));
180 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
181 assertEquals(jpa, new MyJpa(jpa));
185 void testJpaToscaWithToscaPropertiesT() {
186 MyTosca tosca = new MyTosca();
187 tosca.setName("world");
188 tosca.setVersion("3.2.1");
189 tosca.setDescription(SOME_DESCRIPTION);
190 tosca.setProperties(Map.of(KEY1, PROP1, KEY2, PROP2));
192 jpa = new MyJpa(tosca);
193 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
195 JpaToscaProperty jpa1 = new JpaToscaProperty(PROP1);
196 jpa1.setKey(new PfReferenceKey(jpa.getKey(), KEY1));
198 JpaToscaProperty jpa2 = new JpaToscaProperty(PROP2);
199 jpa2.setKey(new PfReferenceKey(jpa.getKey(), KEY2));
201 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, jpa1, KEY2, jpa2));
203 assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey());
207 void testValidateWithKey() {
208 // null key - should fail
209 jpa.setText("some text");
210 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
213 jpa.setKey(new PfConceptKey("xyz", "2.3.4"));
214 assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue();
216 // null text - bean validator should fail
218 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
222 void testGetReferencedDataTypes() {
223 assertThat(jpa.getReferencedDataTypes()).isEmpty();
226 PfConceptKey schemaKey = new PfConceptKey("schemaZ", "9.8.7");
227 JpaToscaSchemaDefinition schema = new JpaToscaSchemaDefinition();
228 schema.setType(schemaKey);
229 JpaToscaProperty prop1 = new JpaToscaProperty(JPA_PROP1);
230 prop1.setType(CONCEPT_KEY1);
231 prop1.setEntrySchema(schema);
233 // one property without a schema
234 JpaToscaProperty prop2 = new JpaToscaProperty(JPA_PROP2);
235 prop2.setType(CONCEPT_KEY2);
236 prop2.setEntrySchema(null);
238 jpa.setProperties(Map.of(KEY1, prop1, KEY2, prop2));
240 List<PfConceptKey> keys = new ArrayList<>(jpa.getReferencedDataTypes());
241 Collections.sort(keys);
243 assertThat(keys).isEqualTo(List.of(CONCEPT_KEY1, schemaKey, CONCEPT_KEY2));
247 private static class MyTosca extends ToscaWithToscaProperties {
252 protected static class MyJpa extends JpaToscaWithToscaProperties<MyTosca> {
253 private static final long serialVersionUID = 1L;
260 public MyJpa(MyJpa jpa) {
264 public MyJpa(PfConceptKey key) {
268 public MyJpa(MyTosca tosca) {
273 public MyTosca toAuthorative() {
274 this.setToscaEntity(new MyTosca());
275 return super.toAuthorative();
279 private static class MyJpa2 extends MyJpa {
280 private static final long serialVersionUID = 1L;