Refactor models for common type handling
[policy/models.git] / models-tosca / src / test / java / org / onap / policy / models / tosca / simple / concepts / JpaToscaWithStringPropertiesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 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.Assert.assertEquals;
26
27 import java.util.List;
28 import java.util.Map;
29 import lombok.Getter;
30 import lombok.NoArgsConstructor;
31 import lombok.Setter;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.parameters.annotations.NotNull;
35 import org.onap.policy.models.base.PfConceptKey;
36 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithTypeAndObjectProperties;
37
38 public class JpaToscaWithStringPropertiesTest {
39     private static final String SOME_DESCRIPTION = "some description";
40     private static final String KEY1 = "abc";
41     private static final String KEY2 = "def";
42     private static final String STRING1 = "10";
43     private static final String STRING2 = "20";
44     private static final int INT1 = 10;
45     private static final int INT2 = 20;
46
47     private MyJpa jpa;
48
49     @Before
50     public void setUp() {
51         jpa = new MyJpa();
52     }
53
54     @Test
55     public void testGetKeys() {
56         PfConceptKey key = new PfConceptKey("bye", "9.8.7");
57         PfConceptKey typeKey = new PfConceptKey("type", "6.5.4");
58
59         jpa = new MyJpa(key, typeKey);
60         jpa.setDescription(SOME_DESCRIPTION);
61         jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
62
63         // properties should be ignored
64         assertThat(jpa.getKeys()).isEqualTo(List.of(key, typeKey));
65     }
66
67     @Test
68     public void testClean() {
69         jpa.setDescription("  some description  ");
70         jpa.setProperties(Map.of(KEY1, "10 ", KEY2, " 20"));
71
72         jpa.clean();
73         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
74         assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
75     }
76
77     @Test
78     public void testToAuthorative() {
79         jpa.setDescription(SOME_DESCRIPTION);
80         jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
81
82         MyTosca tosca = jpa.toAuthorative();
83         assertEquals(SOME_DESCRIPTION, tosca.getDescription());
84         assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, INT1, KEY2, INT2));
85     }
86
87     @Test
88     public void testFromAuthorative() {
89         MyTosca tosca = new MyTosca();
90         tosca.setType("type");
91         tosca.setTypeVersion("1.2.3");
92         tosca.setDescription(SOME_DESCRIPTION);
93
94         jpa.fromAuthorative(tosca);
95         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
96         assertThat(jpa.getProperties()).isNull();
97
98         tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2));
99
100         jpa = new MyJpa();
101         jpa.fromAuthorative(tosca);
102         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
103         assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
104     }
105
106     @Test
107     public void testCompareTo() {
108         jpa.setDescription(SOME_DESCRIPTION);
109         jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
110
111         assertThat(jpa).isNotEqualByComparingTo(null).isEqualByComparingTo(jpa).isNotEqualByComparingTo(new MyJpa2());
112
113         MyJpa jpa2 = new MyJpa();
114         jpa2.setDescription(SOME_DESCRIPTION);
115         jpa2.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
116         assertThat(jpa).isEqualByComparingTo(jpa2);
117
118         jpa2.setProperties(Map.of(KEY1, STRING1));
119         assertThat(jpa).isNotEqualByComparingTo(jpa2);
120     }
121
122     @Test
123     public void testJpaToscaWithStringProperties() {
124         assertThat(jpa.getProperties()).isNull();
125         assertThat(jpa.getKey().isNullKey()).isTrue();
126
127     }
128
129     @Test
130     public void testJpaToscaWithStringPropertiesPfConceptKey() {
131         PfConceptKey key = new PfConceptKey("hello", "1.2.3");
132
133         jpa = new MyJpa(key);
134         assertEquals(key, jpa.getKey());
135     }
136
137     @Test
138     public void testJpaToscaWithStringPropertiesJpaToscaWithStringPropertiesOfT() {
139         jpa.setDescription(SOME_DESCRIPTION);
140         assertEquals(jpa, new MyJpa(jpa));
141
142         jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
143         assertEquals(jpa, new MyJpa(jpa));
144     }
145
146     @Test
147     public void testJpaToscaWithStringPropertiesT() {
148         MyTosca tosca = new MyTosca();
149         tosca.setName("world");
150         tosca.setVersion("3.2.1");
151         tosca.setType("planet");
152         tosca.setTypeVersion("6.5.4");
153         tosca.setDescription(SOME_DESCRIPTION);
154         tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2));
155
156         jpa = new MyJpa(tosca);
157         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
158         assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
159         assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey());
160         assertEquals(new PfConceptKey("planet", "6.5.4"), jpa.getType());
161     }
162
163     @Test
164     public void testValidateWithKey() {
165         // null key - should fail
166         jpa.setText("some text");
167         assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
168
169         // not valid, type is not set
170         jpa.setKey(new PfConceptKey("xyz", "2.3.4"));
171         assertThat(jpa.validateWithKey("fieldB").isValid()).isFalse();
172
173         // valid, type is set
174         jpa.setType(new PfConceptKey("uvw", "5.6.7"));
175         assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue();
176
177         // null text - bean validator should fail
178         jpa.setText(null);
179         assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
180     }
181
182     private static class MyTosca extends ToscaWithTypeAndObjectProperties {
183     }
184
185     @NoArgsConstructor
186     protected static class MyJpa extends JpaToscaWithTypeAndStringProperties<MyTosca> {
187         private static final long serialVersionUID = 1L;
188
189         @NotNull
190         @Getter
191         @Setter
192         private String text;
193
194         public MyJpa(MyJpa jpa) {
195             super(jpa);
196         }
197
198         public MyJpa(PfConceptKey key) {
199             super(key);
200         }
201
202         public MyJpa(PfConceptKey key, PfConceptKey type) {
203             super(key, type);
204         }
205
206         public MyJpa(MyTosca tosca) {
207             super(tosca);
208         }
209
210         @Override
211         public MyTosca toAuthorative() {
212             this.setToscaEntity(new MyTosca());
213             return super.toAuthorative();
214         }
215
216         @Override
217         protected Object deserializePropertyValue(String propValue) {
218             return Integer.parseInt(propValue);
219         }
220
221         @Override
222         protected String serializePropertyValue(Object propValue) {
223             return propValue.toString();
224         }
225     }
226
227     private static class MyJpa2 extends MyJpa {
228         private static final long serialVersionUID = 1L;
229     }
230 }