7cd6facf031da8c4ba7d71b2bbdc5e2f5d83c537
[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).isNotEqualByComparingTo(null).isEqualByComparingTo(jpa).isNotEqualByComparingTo(new MyJpa2());
108
109         MyJpa jpa2 = new MyJpa();
110         jpa2.setDescription(SOME_DESCRIPTION);
111         jpa2.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
112         assertThat(jpa).isEqualByComparingTo(jpa2);
113
114         jpa2.setProperties(Map.of(KEY1, STRING1));
115         assertThat(jpa).isNotEqualByComparingTo(jpa2);
116     }
117
118     @Test
119     public void testJpaToscaWithStringProperties() {
120         assertThat(jpa.getProperties()).isNull();
121         assertThat(jpa.getKey().isNullKey()).isTrue();
122
123     }
124
125     @Test
126     public void testJpaToscaWithStringPropertiesPfConceptKey() {
127         PfConceptKey key = new PfConceptKey("hello", "1.2.3");
128
129         jpa = new MyJpa(key);
130         assertEquals(key, jpa.getKey());
131     }
132
133     @Test
134     public void testJpaToscaWithStringPropertiesJpaToscaWithStringPropertiesOfT() {
135         jpa.setDescription(SOME_DESCRIPTION);
136         assertEquals(jpa, new MyJpa(jpa));
137
138         jpa.setProperties(Map.of(KEY1, STRING1, KEY2, STRING2));
139         assertEquals(jpa, new MyJpa(jpa));
140     }
141
142     @Test
143     public void testJpaToscaWithStringPropertiesT() {
144         MyTosca tosca = new MyTosca();
145         tosca.setName("world");
146         tosca.setVersion("3.2.1");
147         tosca.setDescription(SOME_DESCRIPTION);
148         tosca.setProperties(Map.of(KEY1, INT1, KEY2, INT2));
149
150         jpa = new MyJpa(tosca);
151         assertEquals(SOME_DESCRIPTION, jpa.getDescription());
152         assertThat(jpa.getProperties()).isEqualTo(Map.of(KEY1, STRING1, KEY2, STRING2));
153         assertEquals(new PfConceptKey("world", "3.2.1"), jpa.getKey());
154     }
155
156     @Test
157     public void testValidateWithKey() {
158         // null key - should fail
159         jpa.setText("some text");
160         assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
161
162         // valid
163         jpa.setKey(new PfConceptKey("xyz", "2.3.4"));
164         assertThat(jpa.validateWithKey("fieldB").isValid()).isTrue();
165
166         // null text - bean validator should fail
167         jpa.setText(null);
168         assertThat(jpa.validateWithKey("fieldA").isValid()).isFalse();
169     }
170
171     private static class MyTosca extends ToscaWithObjectProperties {
172
173     }
174
175     @NoArgsConstructor
176     protected static class MyJpa extends JpaToscaWithStringProperties<MyTosca> {
177         private static final long serialVersionUID = 1L;
178
179         @NotNull
180         @Getter
181         @Setter
182         private String text;
183
184         public MyJpa(MyJpa jpa) {
185             super(jpa);
186         }
187
188         public MyJpa(PfConceptKey key) {
189             super(key);
190         }
191
192         public MyJpa(MyTosca tosca) {
193             super(tosca);
194         }
195
196         @Override
197         public MyTosca toAuthorative() {
198             this.setToscaEntity(new MyTosca());
199             return super.toAuthorative();
200         }
201
202         @Override
203         protected Object deserializePropertyValue(String propValue) {
204             return Integer.parseInt(propValue);
205         }
206
207         @Override
208         protected String serializePropertyValue(Object propValue) {
209             return propValue.toString();
210         }
211     }
212
213     private static class MyJpa2 extends MyJpa {
214         private static final long serialVersionUID = 1L;
215     }
216 }