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