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