Merge "[AAI] Fix doc config files"
[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
104         assertNotNull(res);
105         assertTrue(res.contains("\"dayOfMonth\" : 18"));
106         assertTrue(res.contains("\"dayOfWeek\" : \"MONDAY\""));
107         assertTrue(res.contains("\"dayOfYear\" : 261"));
108         assertTrue(res.contains("\"hour\" : 10"));
109         assertTrue(res.contains("\"minute\" : 55"));
110         assertTrue(res.contains("\"month\" : \"SEPTEMBER\""));
111         assertTrue(res.contains("\"monthValue\" : 9"));
112         assertTrue(res.contains("\"nano\" : 300"));
113         assertTrue(res.contains("\"second\" : 0"));
114         assertTrue(res.contains("\"year\" : 2017"));
115     }
116
117     @Test
118     public void testGetJsonFromObjectClassNull() throws Exception {
119         String res = pojoUtils.getJsonFromObject(null);
120
121         assertNotNull(res);
122         assertEquals("null", res);
123     }
124
125     @Test
126     public void testGetJsonFromObjectNull() throws Exception {
127         String res = pojoUtils.getJsonFromObject(null, false, true);
128
129         assertNotNull(res);
130         assertEquals("null", res);
131     }
132
133     @Test
134     public void testGetJsonFromObject() throws Exception {
135         LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
136
137         String res = pojoUtils.getJsonFromObject(date, false, false);
138         assertNotNull(res);
139
140         res = pojoUtils.getJsonFromObject(date, true, false);
141         assertNotNull(res);
142
143         res = pojoUtils.getJsonFromObject(date, true, true);
144         assertNotNull(res);
145     }
146
147     @Test
148     public void testGetJsonFromDynamicObject() throws Exception {
149         DynamicEntity dynamicEntity = Mockito.mock(DynamicEntity.class);
150         JAXBContext jaxbContext = Mockito.mock(JAXBContext.class);
151         JAXBMarshaller marshaller = Mockito.mock(JAXBMarshaller.class);
152
153         Mockito.when(jaxbContext.createMarshaller()).thenReturn(marshaller);
154
155         String output = pojoUtils.getJsonFromDynamicObject(dynamicEntity, jaxbContext, true);
156         assertEquals("", output);
157     }
158
159     @Test(expected = NullPointerException.class)
160     public void testGetXmlFromObjectNull() throws Exception {
161         pojoUtils.getXmlFromObject(null);
162     }
163
164     @Test
165     public void testGetXmlFromObject() throws JAXBException, IOException {
166         NotificationEvent notificationEvent = new NotificationEvent();
167         notificationEvent.setCambriaPartition("partition");
168
169         String res = pojoUtils.getXmlFromObject(notificationEvent);
170
171         assertNotNull(res);
172         assertTrue(res.contains("<NotificationEvent>"));
173         assertTrue(res.contains("<cambria.partition>partition</cambria.partition>"));
174         assertTrue(res.contains("</NotificationEvent>"));
175     }
176
177     @Test
178     public void testGetLookupKeyEmptyKey() {
179         String baseKey = "";
180         Collection<String> keyProps = new ArrayList<String>();
181         keyProps.add("key");
182
183         HashMap<String, Object> lookup = new HashMap<String, Object>();
184         lookup.put("key", "val");
185         String expectedLookupKey = "key=val";
186
187         String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
188         assertEquals(expectedLookupKey, res);
189     }
190
191     @Test
192     public void testGetLookupKey() {
193         String baseKey = "baseKey";
194         Collection<String> keyProps = new ArrayList<String>();
195         keyProps.add("key");
196
197         HashMap<String, Object> lookup = new HashMap<String, Object>();
198         lookup.put("key", "val");
199         String expectedLookupKey = "baseKey&key=val";
200
201         String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
202
203         assertEquals(expectedLookupKey, res);
204     }
205
206     @Test
207     public void testGetLookupKeys() {
208         HashMap<String, Object> lookup = new HashMap<>();
209         lookup.put("multimapkey", "val");
210         LinkedHashMap<String, HashMap<String, Object>> lookupHashes = new LinkedHashMap<>();
211         lookupHashes.put("objectType", lookup);
212
213         Multimap<String, String> multimap = ImmutableListMultimap.of("objectType", "multimapkey");
214         String res = pojoUtils.getLookupKeys(lookupHashes, multimap);
215
216         String lookupKey = "val";
217         assertNotNull(res);
218         assertEquals(lookupKey, res);
219     }
220
221     @Test
222     public void testGetExampleObject() throws Exception {
223         Person p = getPojoObject();
224         pojoUtils.getExampleObject(p, true);
225         assertNotNull(p);
226         assertTrue(p.getName().contains("example-name-val-"));
227         assertTrue(p.getNickname().contains("example-nickname-val-"));
228         assertTrue(p.getPet().contains("example-pet-val-"));
229         assertTrue(p.isMarried());
230     }
231
232     private Entity getEntityObject() {
233         Entity entity = new Entity();
234         KeyValueList list = new KeyValueList();
235         list.setKey("key");
236         list.setValue("value");
237
238         entity.setAction("action");
239         entity.setKeyValueList(Lists.newArrayList(list));
240         entity.setEquipmentRole("equipmentRole");
241         entity.setSelfLink("selfLink");
242
243         return entity;
244     }
245
246     private Person getPojoObject() {
247         Person p = new Person("Andrew");
248         p.setAge(30);
249         p.setHeightcm((short) 190);
250         p.setWeightlb(185);
251         p.setNickname("Andy");
252         p.setPet(null);
253         return p;
254     }
255
256     class Person {
257
258         private int age;
259         private long weightlb;
260         private short heightcm;
261         private String nickname;
262         private String name;
263         private String pet;
264         private boolean isMarried;
265
266         public Person(String name) {
267             this.name = name;
268         }
269
270         public int getAge() {
271             return age;
272         }
273
274         public void setAge(int age) {
275             this.age = age;
276         }
277
278         public long getWeightlb() {
279             return weightlb;
280         }
281
282         public void setWeightlb(long weightlb) {
283             this.weightlb = weightlb;
284         }
285
286         public short getHeightcm() {
287             return heightcm;
288         }
289
290         public void setHeightcm(short heightcm) {
291             this.heightcm = heightcm;
292         }
293
294         public String getNickname() {
295             return nickname;
296         }
297
298         public void setNickname(String nickname) {
299             this.nickname = nickname;
300         }
301
302         public String getName() {
303             return name;
304         }
305
306         public void setName(String name) {
307             this.name = name;
308         }
309
310         public String getPet() {
311             return pet;
312         }
313
314         public void setPet(String pet) {
315             this.pet = pet;
316         }
317
318         public boolean isMarried() {
319             return isMarried;
320         }
321
322         public void setMarried(boolean isMarried) {
323             this.isMarried = isMarried;
324         }
325
326     }
327 }