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