Migrate JAXB to jakarta
[aai/aai-common.git] / aai-core / src / test / java / org / onap / aai / util / PojoUtilsTest.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 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.aai.util;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26
27 import com.google.common.collect.ImmutableListMultimap;
28 import com.google.common.collect.Lists;
29 import com.google.common.collect.Multimap;
30
31 import java.io.IOException;
32 import java.time.LocalDateTime;
33 import java.time.Month;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.HashMap;
37 import java.util.LinkedHashMap;
38 import java.util.List;
39
40 import javax.xml.bind.JAXBException;
41
42 import org.eclipse.persistence.dynamic.DynamicEntity;
43 import org.eclipse.persistence.jaxb.JAXBContext;
44 import org.eclipse.persistence.jaxb.JAXBMarshaller;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.mockito.Mockito;
48 import org.onap.aai.domain.notificationEvent.NotificationEvent;
49
50 public class PojoUtilsTest {
51
52     private PojoUtils pojoUtils;
53
54     @Before
55     public void init() {
56         pojoUtils = new PojoUtils();
57     }
58
59     @Test
60     public void testGetKeyValueList() throws Exception {
61         Entity entity = getEntityObject();
62         Person person = getPojoObject();
63
64         List<KeyValueList> keyValueLists = pojoUtils.getKeyValueList(entity, person);
65
66         for (KeyValueList keyValueList : keyValueLists) {
67
68             if (keyValueList.getKey().equals("key")) {
69                 assertEquals("value", keyValueList.getValue());
70             } else if (keyValueList.getKey().equals("name")) {
71                 assertEquals("Andrew", keyValueList.getValue());
72             } else if (keyValueList.getKey().equals("nickname")) {
73                 assertEquals("Andy", keyValueList.getValue());
74             } else if (keyValueList.getKey().equals("age")) {
75                 assertEquals("30", keyValueList.getValue());
76             } else if (keyValueList.getKey().equals("weightlb")) {
77                 assertEquals("185", keyValueList.getValue());
78             } else if (keyValueList.getKey().equals("heightcm")) {
79                 assertEquals("190", keyValueList.getValue());
80             } else if (keyValueList.getKey().equals("pet")) {
81                 assertEquals("", keyValueList.getValue());
82             }
83         }
84     }
85
86     @Test
87     public void testGetJsonFromObjectClassMockTest() throws Exception {
88         PojoUtils pojoUtils = Mockito.mock(PojoUtils.class);
89         String obj = "helloWorld";
90         Mockito.when(pojoUtils.getJsonFromObject(Mockito.anyString())).thenCallRealMethod();
91
92         pojoUtils.getJsonFromObject(obj);
93
94         Mockito.verify(pojoUtils, Mockito.times(1)).getJsonFromObject(Mockito.anyString(), Mockito.eq(false),
95                 Mockito.eq(true));
96     }
97
98     @Test
99     public void testGetJsonFromObjectClass() throws Exception {
100         LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
101
102         String res = pojoUtils.getJsonFromObject(date);
103         assertEquals("\"2017-09-18T10:55:00.0000003\"", res);
104         assertNotNull(res);
105     }
106
107     @Test
108     public void testGetJsonFromObjectClassNull() throws Exception {
109         String res = pojoUtils.getJsonFromObject(null);
110
111         assertNotNull(res);
112         assertEquals("null", res);
113     }
114
115     @Test
116     public void testGetJsonFromObjectNull() throws Exception {
117         String res = pojoUtils.getJsonFromObject(null, false, true);
118
119         assertNotNull(res);
120         assertEquals("null", res);
121     }
122
123     @Test
124     public void testGetJsonFromObject() throws Exception {
125         LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
126
127         String res = pojoUtils.getJsonFromObject(date, false, false);
128         assertNotNull(res);
129
130         res = pojoUtils.getJsonFromObject(date, true, false);
131         assertNotNull(res);
132
133         res = pojoUtils.getJsonFromObject(date, true, true);
134         assertNotNull(res);
135     }
136
137     @Test
138     public void testGetJsonFromDynamicObject() throws Exception {
139         DynamicEntity dynamicEntity = Mockito.mock(DynamicEntity.class);
140         JAXBContext jaxbContext = Mockito.mock(JAXBContext.class);
141         JAXBMarshaller marshaller = Mockito.mock(JAXBMarshaller.class);
142
143         Mockito.when(jaxbContext.createMarshaller()).thenReturn(marshaller);
144
145         String output = pojoUtils.getJsonFromDynamicObject(dynamicEntity, jaxbContext, true);
146         assertEquals("", output);
147     }
148
149     @Test(expected = NullPointerException.class)
150     public void testGetXmlFromObjectNull() throws Exception {
151         pojoUtils.getXmlFromObject(null);
152     }
153
154     @Test
155     public void testGetXmlFromObject() throws JAXBException, IOException {
156         NotificationEvent notificationEvent = new NotificationEvent();
157         notificationEvent.setCambriaPartition("partition");
158
159         String res = pojoUtils.getXmlFromObject(notificationEvent);
160
161         assertNotNull(res);
162         assertTrue(res.contains("<NotificationEvent>"));
163         assertTrue(res.contains("<cambria.partition>partition</cambria.partition>"));
164         assertTrue(res.contains("</NotificationEvent>"));
165     }
166
167     @Test
168     public void testGetLookupKeyEmptyKey() {
169         String baseKey = "";
170         Collection<String> keyProps = new ArrayList<String>();
171         keyProps.add("key");
172
173         HashMap<String, Object> lookup = new HashMap<String, Object>();
174         lookup.put("key", "val");
175         String expectedLookupKey = "key=val";
176
177         String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
178         assertEquals(expectedLookupKey, res);
179     }
180
181     @Test
182     public void testGetLookupKey() {
183         String baseKey = "baseKey";
184         Collection<String> keyProps = new ArrayList<String>();
185         keyProps.add("key");
186
187         HashMap<String, Object> lookup = new HashMap<String, Object>();
188         lookup.put("key", "val");
189         String expectedLookupKey = "baseKey&key=val";
190
191         String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
192
193         assertEquals(expectedLookupKey, res);
194     }
195
196     @Test
197     public void testGetLookupKeys() {
198         HashMap<String, Object> lookup = new HashMap<>();
199         lookup.put("multimapkey", "val");
200         LinkedHashMap<String, HashMap<String, Object>> lookupHashes = new LinkedHashMap<>();
201         lookupHashes.put("objectType", lookup);
202
203         Multimap<String, String> multimap = ImmutableListMultimap.of("objectType", "multimapkey");
204         String res = pojoUtils.getLookupKeys(lookupHashes, multimap);
205
206         String lookupKey = "val";
207         assertNotNull(res);
208         assertEquals(lookupKey, res);
209     }
210
211     @Test
212     public void testGetExampleObject() throws Exception {
213         Person p = getPojoObject();
214         pojoUtils.getExampleObject(p, true);
215         assertNotNull(p);
216         assertTrue(p.getName().contains("example-name-val-"));
217         assertTrue(p.getNickname().contains("example-nickname-val-"));
218         assertTrue(p.getPet().contains("example-pet-val-"));
219         assertTrue(p.isMarried());
220     }
221
222     private Entity getEntityObject() {
223         Entity entity = new Entity();
224         KeyValueList list = new KeyValueList();
225         list.setKey("key");
226         list.setValue("value");
227
228         entity.setAction("action");
229         entity.setKeyValueList(Lists.newArrayList(list));
230         entity.setEquipmentRole("equipmentRole");
231         entity.setSelfLink("selfLink");
232
233         return entity;
234     }
235
236     private Person getPojoObject() {
237         Person p = new Person("Andrew");
238         p.setAge(30);
239         p.setHeightcm((short) 190);
240         p.setWeightlb(185);
241         p.setNickname("Andy");
242         p.setPet(null);
243         return p;
244     }
245
246     class Person {
247
248         private int age;
249         private long weightlb;
250         private short heightcm;
251         private String nickname;
252         private String name;
253         private String pet;
254         private boolean isMarried;
255
256         public Person(String name) {
257             this.name = name;
258         }
259
260         public int getAge() {
261             return age;
262         }
263
264         public void setAge(int age) {
265             this.age = age;
266         }
267
268         public long getWeightlb() {
269             return weightlb;
270         }
271
272         public void setWeightlb(long weightlb) {
273             this.weightlb = weightlb;
274         }
275
276         public short getHeightcm() {
277             return heightcm;
278         }
279
280         public void setHeightcm(short heightcm) {
281             this.heightcm = heightcm;
282         }
283
284         public String getNickname() {
285             return nickname;
286         }
287
288         public void setNickname(String nickname) {
289             this.nickname = nickname;
290         }
291
292         public String getName() {
293             return name;
294         }
295
296         public void setName(String name) {
297             this.name = name;
298         }
299
300         public String getPet() {
301             return pet;
302         }
303
304         public void setPet(String pet) {
305             this.pet = pet;
306         }
307
308         public boolean isMarried() {
309             return isMarried;
310         }
311
312         public void setMarried(boolean isMarried) {
313             this.isMarried = isMarried;
314         }
315
316     }
317 }