23e0e6a7eab977aee10db6fcb2af072fc8419c34
[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  * ================================================================================
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.models.tosca.simple.concepts;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25
26 import java.util.List;
27 import java.util.Map;
28 import lombok.Getter;
29 import lombok.NoArgsConstructor;
30 import lombok.Setter;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.common.parameters.annotations.NotNull;
34 import org.onap.policy.models.base.PfConceptKey;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaWithObjectProperties;
36
37 public class JpaToscaWithStringPropertiesTest {
38     private static final String SOME_DESCRIPTION = "some description";
39     private static final String KEY1 = "abc";
40     private static final String KEY2 = "def";
41     private static final String STRING1 = "10";
42     private static final String STRING2 = "20";
43     private static final int INT1 = 10;
44     private static final int INT2 = 20;
45
46     private MyJpa jpa;
47
48     @Before
49     public void setUp() {
50         jpa = new MyJpa();
51     }
52
53     @Test
54     public void testGetKeys() {
55         PfConceptKey key = new PfConceptKey("bye", "9.8.7");
56
57         jpa = new MyJpa(key);
58         jpa.setDescription(SOME_DESCRIPTION);
59         jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
60
61         // properties should be ignored
62         assertThat(jpa.getKeys()).isEqualTo(List.of(key));
63     }
64
65     @Test
66     public void testClean() {
67         jpa.setDescription("  some description  ");
68         jpa.setProperties(Map.of(KEY1, "10 ", KEY2, " 20"));
69
70         jpa.clean();
71         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
72         assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
73     }
74
75     @Test
76     public void testToAuthorative() {
77         jpa.setDescription(SOME_DESCRIPTION);
78         jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
79
80         MyTosca tosca = jpa.toAuthorative();
81         assertEquals(SOME_DESCRIPTION, tosca.getDescription());
82         assertThat(tosca.getProperties()).isEqualTo(Map.of(KEY1, INT1, KEY2, INT2));
83     }
84
85     @Test
86     public void testFromAuthorative() {
87         MyTosca tosca = new MyTosca();
88         tosca.setDescription(SOME_DESCRIPTION);
89
90         jpa.fromAuthorative(tosca);
91         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
92         assertThat(jpa.getProperties()).isNull();
93
94         tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2));
95
96         jpa = new MyJpa();
97         jpa.fromAuthorative(tosca);
98         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
99         assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
100     }
101
102     @Test
103     public void testCompareTo() {
104         jpa.setDescription(SOME_DESCRIPTION);
105         jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
106
107         assertThat(jpa.compareTo(null)).isNegative();
108         assertThat(jpa.compareTo(jpa)).isZero();
109         assertThat(jpa.compareTo(new PfConceptKey())).isNotZero();
110
111         MyJpa jpa2 = new MyJpa();
112         jpa2.setDescription(SOME_DESCRIPTION);
113         jpa2.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
114         assertThat(jpa.compareTo(jpa2)).isZero();
115
116         jpa2.setProperties(Map.of(KEY1, STRING1));
117         assertThat(jpa.compareTo(jpa2)).isNotZero();
118     }
119
120     @Test
121     public void testJpaToscaWithStringProperties() {
122         assertThat(jpa.getProperties()).isNull();
123         assertThat(jpa.getKey().isNullKey()).isTrue();
124
125     }
126
127     @Test
128     public void testJpaToscaWithStringPropertiesPfConceptKey() {
129         PfConceptKey key = new PfConceptKey("hello", "1.2.3");
130
131         jpa = new MyJpa(key);
132         assertEquals(key, jpa.getKey());
133     }
134
135     @Test
136     public void testJpaToscaWithStringPropertiesJpaToscaWithStringPropertiesOfT() {
137         jpa.setDescription(SOME_DESCRIPTION);
138         assertEquals(jpa, new MyJpa(jpa));
139
140         jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
141         assertEquals(jpa, new MyJpa(jpa));
142     }
143
144     @Test
145     public void testJpaToscaWithStringPropertiesT() {
146         MyTosca tosca = new MyTosca();
147         tosca.setName("world");
148         tosca.setVersion("3.2.1");
149         tosca.setDescription(SOME_DESCRIPTION);
150         tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2));
151
152         jpa = new MyJpa(tosca);
153         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
154         assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
155         assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey());
156     }
157
158     @Test
159     public void testValidateWithKey() {
160         // null key - should fail
161         jpa.setText("some text");
162         assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
163
164         // valid
165         jpa.setKey(new PfConceptKey("xyz", "2.3.4"));
166         assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue();
167
168         // null text - bean validator should fail
169         jpa.setText(null);
170         assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
171     }
172
173     private static class MyTosca extends ToscaWithObjectProperties {
174
175     }
176
177     @NoArgsConstructor
178     protected static class MyJpa extends JpaToscaWithStringProperties<MyTosca> {
179         private static final long serialVersionUID = 1L;
180
181         @NotNull
182         @Getter
183         @Setter
184         private String text;
185
186         public MyJpa(MyJpa jpa) {
187             super(jpa);
188         }
189
190         public MyJpa(PfConceptKey key) {
191             super(key);
192         }
193
194         public MyJpa(MyTosca tosca) {
195             super(tosca);
196         }
197
198         @Override
199         public MyTosca toAuthorative() {
200             this.setToscaEntity(new MyTosca());
201             return super.toAuthorative();
202         }
203
204         @Override
205         protected Object deserializePropertyValue(String propValue) {
206             return Integer.parseInt(propValue);
207         }
208
209         @Override
210         protected String serializePropertyValue(Object propValue) {
211             return propValue.toString();
212         }
213     }
214 }