Fix trailing space in Info.yaml models
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaWithToscaPropertiesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.models.tosca.simple.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26
27 import java.io.Serial;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.List;
31 import java.util.Map;
32 import lombok.Getter;
33 import lombok.NoArgsConstructor;
34 import lombok.Setter;
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;
44
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";
59
60     private MyJpa jpa;
61
62     /**
63      * Initializes the properties.
64      */
65     @BeforeAll
66     public static void setUpBeforeClass() {
67         JPA_PROP1.setDescription(DESCRIPT1);
68         JPA_PROP2.setDescription(DESCRIPT2);
69
70         toscaPropertyOne = JPA_PROP1.toAuthorative();
71         toscaPropertyTwo = JPA_PROP2.toAuthorative();
72     }
73
74     @BeforeEach
75     void setUp() {
76         jpa = new MyJpa();
77     }
78
79     @Test
80     void testGetKeys() {
81         PfConceptKey key = new PfConceptKey("bye", "9.8.7");
82
83         jpa = new MyJpa(key);
84         jpa.setDescription(SOME_DESCRIPTION);
85         jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
86
87         // properties should be included
88
89         List<PfKey> keys = jpa.getKeys();
90         Collections.sort(keys);
91
92         assertThat(keys).isEqualTo(
93                         List.of(PfConceptKey.getNullKey(), PfConceptKey.getNullKey(), key, REF_KEY1, REF_KEY2));
94     }
95
96     @Test
97     void testClean() {
98         jpa.clean();
99
100         jpa.setDescription("  some description  ");
101
102         JpaToscaProperty prop1 = new JpaToscaProperty(JPA_PROP1);
103         prop1.setDescription(DESCRIPT1 + " ");
104
105         JpaToscaProperty prop2 = new JpaToscaProperty(JPA_PROP2);
106         prop2.setDescription(" " + DESCRIPT2);
107
108         jpa.setProperties(Map.of(KEY1, prop1, KEY2, prop2));
109
110         jpa.clean();
111         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
112         assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
113     }
114
115     @Test
116     void testToAuthorative() {
117         jpa.setDescription(SOME_DESCRIPTION);
118         jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
119
120         MyTosca tosca = jpa.toAuthorative();
121         assertEquals(SOME_DESCRIPTION, tosca.getDescription());
122         assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, toscaPropertyOne, KEY2, toscaPropertyTwo));
123     }
124
125     @Test
126     void testFromAuthorative() {
127         MyTosca tosca = new MyTosca();
128         tosca.setDescription(SOME_DESCRIPTION);
129
130         jpa.fromAuthorative(tosca);
131         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
132         assertThat(jpa.getProperties()).isNull();
133
134         tosca.setProperties(Map.of(KEY1, toscaPropertyOne, KEY2, toscaPropertyTwo));
135
136         JpaToscaProperty jpa1 = new JpaToscaProperty(toscaPropertyOne);
137         jpa1.setKey(new PfReferenceKey(jpa.getKey(), KEY1));
138
139         JpaToscaProperty jpa2 = new JpaToscaProperty(toscaPropertyTwo);
140         jpa2.setKey(new PfReferenceKey(jpa.getKey(), KEY2));
141
142         jpa = new MyJpa();
143         jpa.fromAuthorative(tosca);
144         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
145         assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, jpa1, KEY2, jpa2));
146     }
147
148     @Test
149     void testCompareTo() {
150         jpa.setDescription(SOME_DESCRIPTION);
151         jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
152
153         assertThat(jpa).isNotEqualByComparingTo(null).isEqualByComparingTo(jpa).isNotEqualByComparingTo(new MyJpa2());
154
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);
159
160         jpa2.setProperties(Map.of(KEY1, JPA_PROP1));
161         assertThat(jpa).isNotEqualByComparingTo(jpa2);
162     }
163
164     @Test
165     void testJpaToscaWithToscaProperties() {
166         assertThat(jpa.getProperties()).isNull();
167         assertThat(jpa.getKey().isNullKey()).isTrue();
168     }
169
170     @Test
171     void testJpaToscaWithToscaPropertiesPfConceptKey() {
172         jpa = new MyJpa(CONCEPT_KEY1);
173         assertEquals(CONCEPT_KEY1, jpa.getKey());
174     }
175
176     @Test
177     void testJpaToscaWithToscaPropertiesJpaToscaWithToscaPropertiesOfT() {
178         jpa.setDescription(SOME_DESCRIPTION);
179         assertEquals(jpa, new MyJpa(jpa));
180
181         jpa.setProperties(Map.of(KEY1, JPA_PROP1, KEY2, JPA_PROP2));
182         assertEquals(jpa, new MyJpa(jpa));
183     }
184
185     @Test
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));
192
193         jpa = new MyJpa(tosca);
194         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
195
196         JpaToscaProperty jpa1 = new JpaToscaProperty(toscaPropertyOne);
197         jpa1.setKey(new PfReferenceKey(jpa.getKey(), KEY1));
198
199         JpaToscaProperty jpa2 = new JpaToscaProperty(toscaPropertyTwo);
200         jpa2.setKey(new PfReferenceKey(jpa.getKey(), KEY2));
201
202         assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, jpa1, KEY2, jpa2));
203
204         assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey());
205     }
206
207     @Test
208     void testValidateWithKey() {
209         // null key - should fail
210         jpa.setText("some text");
211         assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
212
213         // valid
214         jpa.setKey(new PfConceptKey("xyz", "2.3.4"));
215         assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue();
216
217         // null text - bean validator should fail
218         jpa.setText(null);
219         assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
220     }
221
222     @Test
223     void testGetReferencedDataTypes() {
224         assertThat(jpa.getReferencedDataTypes()).isEmpty();
225
226         // one with a schema
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);
233
234         // one property without a schema
235         JpaToscaProperty prop2 = new JpaToscaProperty(JPA_PROP2);
236         prop2.setType(CONCEPT_KEY2);
237         prop2.setEntrySchema(null);
238
239         jpa.setProperties(Map.of(KEY1, prop1, KEY2, prop2));
240
241         List<PfConceptKey> keys = new ArrayList<>(jpa.getReferencedDataTypes());
242         Collections.sort(keys);
243
244         assertThat(keys).isEqualTo(List.of(CONCEPT_KEY1, schemaKey, CONCEPT_KEY2));
245     }
246
247
248     protected static class MyTosca extends ToscaWithToscaProperties {
249
250     }
251
252     @Setter
253     @Getter
254     @NoArgsConstructor
255     protected static class MyJpa extends JpaToscaWithToscaProperties<MyTosca> {
256         @Serial
257         private static final long serialVersionUID = 1L;
258
259         @NotNull
260         private String text;
261
262         public MyJpa(MyJpa jpa) {
263             super(jpa);
264         }
265
266         public MyJpa(PfConceptKey key) {
267             super(key);
268         }
269
270         public MyJpa(MyTosca tosca) {
271             super(tosca);
272         }
273
274         @Override
275         public MyTosca toAuthorative() {
276             this.setToscaEntity(new MyTosca());
277             return super.toAuthorative();
278         }
279     }
280
281     private static class MyJpa2 extends MyJpa {
282         @Serial
283         private static final long serialVersionUID = 1L;
284     }
285 }