2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.models.tosca.simple.concepts;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.List;
31 import lombok.NoArgsConstructor;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.onap.policy.common.parameters.annotations.NotNull;
37 import org.onap.policy.models.base.PfConceptKey;
38 import org.onap.policy.models.base.PfKey;
39 import org.onap.policy.models.base.PfReferenceKey;
40 import org.onap.policy.models.tosca.authorative.concepts.ToscaProperty;
41 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithToscaProperties;
43 public class JpaToscaWithToscaPropertiesTest {
44 private static final String SOME_DESCRIPTION = "some description";
45 private static final String KEY1 = "abc";
46 private static final String KEY2 = "def";
47 private static final PfConceptKey CONCEPT_KEY1 = new PfConceptKey("hello", "1.2.3");
48 private static final PfConceptKey CONCEPT_KEY2 = new PfConceptKey("world", "3.2.1");
49 private static final PfReferenceKey REF_KEY1 = new PfReferenceKey(CONCEPT_KEY1);
50 private static final PfReferenceKey REF_KEY2 = new PfReferenceKey(CONCEPT_KEY2);
51 private static final JpaToscaProperty JPA_PROP1 = new JpaToscaProperty(REF_KEY1);
52 private static final JpaToscaProperty JPA_PROP2 = new JpaToscaProperty(REF_KEY2);
53 private static ToscaProperty PROP1;
54 private static ToscaProperty PROP2;
55 private static final String DESCRIPT1 = "description A";
56 private static final String DESCRIPT2 = "description B";
61 * Initializes the properties.
64 public static void setUpBeforeClass() {
65 JPA_PROP1.setDescription(DESCRIPT1);
66 JPA_PROP2.setDescription(DESCRIPT2);
68 PROP1 = JPA_PROP1.toAuthorative();
69 PROP2 = JPA_PROP2.toAuthorative();
78 public void testGetKeys() {
79 PfConceptKey key = new PfConceptKey("bye", "9.8.7");
82 jpa.setDescription(SOME_DESCRIPTION);
83 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
85 // properties should be included
87 List<PfKey> keys = jpa.getKeys();
88 Collections.sort(keys);
90 assertThat(keys).isEqualTo(
91 List.of(PfConceptKey.getNullKey(), PfConceptKey.getNullKey(), key, REF_KEY1, REF_KEY2));
95 public void testClean() {
98 jpa.setDescription(" some description ");
100 JpaToscaProperty prop1 = new JpaToscaProperty(JPA_PROP1);
101 prop1.setDescription(DESCRIPT1 + " ");
103 JpaToscaProperty prop2 = new JpaToscaProperty(JPA_PROP2);
104 prop2.setDescription(" " + DESCRIPT2);
106 jpa.setProperties(Map.of(KEY1, prop1, KEY2, prop2));
109 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
110 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
114 public void testToAuthorative() {
115 jpa.setDescription(SOME_DESCRIPTION);
116 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
118 MyTosca tosca = jpa.toAuthorative();
119 assertEquals(SOME_DESCRIPTION, tosca.getDescription());
120 assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, PROP1, KEY2, PROP2));
124 public void testFromAuthorative() {
125 MyTosca tosca = new MyTosca();
126 tosca.setDescription(SOME_DESCRIPTION);
128 jpa.fromAuthorative(tosca);
129 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
130 assertThat(jpa.getProperties()).isNull();
132 tosca.setProperties(Map.of(KEY1, PROP1, KEY2, PROP2));
134 JpaToscaProperty jpa1 = new JpaToscaProperty(PROP1);
135 jpa1.setKey(new PfReferenceKey(jpa.getKey(), KEY1));
137 JpaToscaProperty jpa2 = new JpaToscaProperty(PROP2);
138 jpa2.setKey(new PfReferenceKey(jpa.getKey(), KEY2));
141 jpa.fromAuthorative(tosca);
142 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
143 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, jpa1, KEY2, jpa2));
147 public void testCompareTo() {
148 jpa.setDescription(SOME_DESCRIPTION);
149 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
151 assertThat(jpa).isNotEqualByComparingTo(null).isEqualByComparingTo(jpa).isNotEqualByComparingTo(new MyJpa2());
153 MyJpa jpa2 = new MyJpa();
154 jpa2.setDescription(SOME_DESCRIPTION);
155 jpa2.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
156 assertThat(jpa).isEqualByComparingTo(jpa2);
158 jpa2.setProperties(Map.of(KEY1, JPA_PROP1));
159 assertThat(jpa).isNotEqualByComparingTo(jpa2);
163 public void testJpaToscaWithToscaProperties() {
164 assertThat(jpa.getProperties()).isNull();
165 assertThat(jpa.getKey().isNullKey()).isTrue();
169 public void testJpaToscaWithToscaPropertiesPfConceptKey() {
170 jpa = new MyJpa(CONCEPT_KEY1);
171 assertEquals(CONCEPT_KEY1, jpa.getKey());
175 public void testJpaToscaWithToscaPropertiesJpaToscaWithToscaPropertiesOfT() {
176 jpa.setDescription(SOME_DESCRIPTION);
177 assertEquals(jpa, new MyJpa(jpa));
179 jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
180 assertEquals(jpa, new MyJpa(jpa));
184 public void testJpaToscaWithToscaPropertiesT() {
185 MyTosca tosca = new MyTosca();
186 tosca.setName("world");
187 tosca.setVersion("3.2.1");
188 tosca.setDescription(SOME_DESCRIPTION);
189 tosca.setProperties(Map.of(KEY1, PROP1, KEY2, PROP2));
191 jpa = new MyJpa(tosca);
192 assertEquals(SOME_DESCRIPTION, jpa.getDescription());
194 JpaToscaProperty jpa1 = new JpaToscaProperty(PROP1);
195 jpa1.setKey(new PfReferenceKey(jpa.getKey(), KEY1));
197 JpaToscaProperty jpa2 = new JpaToscaProperty(PROP2);
198 jpa2.setKey(new PfReferenceKey(jpa.getKey(), KEY2));
200 assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, jpa1, KEY2, jpa2));
202 assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey());
206 public void testValidateWithKey() {
207 // null key - should fail
208 jpa.setText("some text");
209 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
212 jpa.setKey(new PfConceptKey("xyz", "2.3.4"));
213 assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue();
215 // null text - bean validator should fail
217 assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
221 public void testGetReferencedDataTypes() {
222 assertThat(jpa.getReferencedDataTypes()).isEmpty();
225 PfConceptKey schemaKey = new PfConceptKey("schemaZ", "9.8.7");
226 JpaToscaSchemaDefinition schema = new JpaToscaSchemaDefinition();
227 schema.setType(schemaKey);
228 JpaToscaProperty prop1 = new JpaToscaProperty(JPA_PROP1);
229 prop1.setType(CONCEPT_KEY1);
230 prop1.setEntrySchema(schema);
232 // one property without a schema
233 JpaToscaProperty prop2 = new JpaToscaProperty(JPA_PROP2);
234 prop2.setType(CONCEPT_KEY2);
235 prop2.setEntrySchema(null);
237 jpa.setProperties(Map.of(KEY1, prop1, KEY2, prop2));
239 List<PfConceptKey> keys = new ArrayList<>(jpa.getReferencedDataTypes());
240 Collections.sort(keys);
242 assertThat(keys).isEqualTo(List.of(CONCEPT_KEY1, schemaKey, CONCEPT_KEY2));
246 private static class MyTosca extends ToscaWithToscaProperties {
251 protected static class MyJpa extends JpaToscaWithToscaProperties<MyTosca> {
252 private static final long serialVersionUID = 1L;
259 public MyJpa(MyJpa jpa) {
263 public MyJpa(PfConceptKey key) {
267 public MyJpa(MyTosca tosca) {
272 public MyTosca toAuthorative() {
273 this.setToscaEntity(new MyTosca());
274 return super.toAuthorative();
278 private static class MyJpa2 extends MyJpa {
279 private static final long serialVersionUID = 1L;