3658704e8f499491afee9ae49192d030a3c574a3
[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 package org.onap.aai.util;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import java.time.LocalDateTime;
27 import java.time.Month;
28 import java.util.ArrayList;
29 import java.util.Collection;
30 import java.util.HashMap;
31 import java.util.LinkedHashMap;
32 import java.util.List;
33
34 import javax.xml.bind.JAXBException;
35
36 import org.eclipse.persistence.dynamic.DynamicEntity;
37 import org.eclipse.persistence.jaxb.JAXBContext;
38 import org.eclipse.persistence.jaxb.JAXBMarshaller;
39 import org.junit.Before;
40 import org.junit.Test;
41 import org.mockito.Mockito;
42 import org.onap.aai.domain.notificationEvent.NotificationEvent;
43
44 import com.google.common.collect.ImmutableListMultimap;
45 import com.google.common.collect.Lists;
46 import com.google.common.collect.Multimap;
47
48 public class PojoUtilsTest {
49
50         private PojoUtils pojoUtils;
51         
52         @Before
53         public void init() {
54                 pojoUtils = new PojoUtils();
55         }
56
57         @Test
58         public void testGetKeyValueList() throws Exception {
59                 Entity entity = getEntityObject();
60                 Person person = getPojoObject();
61
62                 List<KeyValueList> keyValueLists = pojoUtils.getKeyValueList(entity, person);
63
64                 for (KeyValueList keyValueList : keyValueLists) {
65
66                         if(keyValueList.getKey().equals("key")) {
67                                 assertEquals("value", keyValueList.getValue());
68                         } else if (keyValueList.getKey().equals("name")) {
69                                 assertEquals("Andrew", keyValueList.getValue());
70                         } else if(keyValueList.getKey().equals("nickname")) {
71                                 assertEquals("Andy", keyValueList.getValue());
72                         } else if(keyValueList.getKey().equals("age")) {
73                                 assertEquals("30", keyValueList.getValue());
74                         } else if(keyValueList.getKey().equals("weightlb")) {
75                                 assertEquals("185", keyValueList.getValue());
76                         } else if(keyValueList.getKey().equals("heightcm")) {
77                                 assertEquals("190", keyValueList.getValue());
78                         } else if(keyValueList.getKey().equals("pet")) {
79                                 assertEquals("", keyValueList.getValue());
80                         }
81                 }
82         }
83
84         @Test
85         public void testGetJsonFromObjectClassMockTest() throws Exception {
86                 PojoUtils pojoUtils = Mockito.mock(PojoUtils.class);
87                 String obj = "helloWorld";
88                 Mockito.when(pojoUtils.getJsonFromObject(Mockito.anyString())).thenCallRealMethod();
89
90                 pojoUtils.getJsonFromObject(obj);
91
92                 Mockito.verify(pojoUtils, Mockito.times(1)).getJsonFromObject(Mockito.anyString(), Mockito.eq(false), 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 {
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                 assertNotNull(p.getAge());
227                 assertNotNull(p.getHeightcm());
228                 assertNotNull(p.getWeightlb());
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 }