2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.util;
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
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;
34 import javax.xml.bind.JAXBException;
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;
44 import com.google.common.collect.ImmutableListMultimap;
45 import com.google.common.collect.Lists;
46 import com.google.common.collect.Multimap;
48 public class PojoUtilsTest {
50 private PojoUtils pojoUtils;
54 pojoUtils = new PojoUtils();
58 public void testGetKeyValueList() throws Exception {
59 Entity entity = getEntityObject();
60 Person person = getPojoObject();
62 List<KeyValueList> keyValueLists = pojoUtils.getKeyValueList(entity, person);
64 for (KeyValueList keyValueList : keyValueLists) {
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());
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();
90 pojoUtils.getJsonFromObject(obj);
92 Mockito.verify(pojoUtils, Mockito.times(1)).getJsonFromObject(Mockito.anyString(), Mockito.eq(false), Mockito.eq(true));
96 public void testGetJsonFromObjectClass() throws Exception {
97 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
99 String res = pojoUtils.getJsonFromObject(date);
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"));
115 public void testGetJsonFromObjectClassNull() throws Exception {
116 String res = pojoUtils.getJsonFromObject(null);
119 assertEquals("null", res);
123 public void testGetJsonFromObjectNull() throws Exception {
124 String res = pojoUtils.getJsonFromObject(null, false, true);
127 assertEquals("null", res);
131 public void testGetJsonFromObject() throws Exception {
132 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
134 String res = pojoUtils.getJsonFromObject(date, false, false);
137 res = pojoUtils.getJsonFromObject(date, true, false);
140 res = pojoUtils.getJsonFromObject(date, true, true);
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);
150 Mockito.when(jaxbContext.createMarshaller()).thenReturn(marshaller);
152 String output = pojoUtils.getJsonFromDynamicObject(dynamicEntity, jaxbContext, true);
153 assertEquals("", output);
156 @Test(expected = NullPointerException.class)
157 public void testGetXmlFromObjectNull() throws Exception {
158 pojoUtils.getXmlFromObject(null);
162 public void testGetXmlFromObject() throws JAXBException {
163 NotificationEvent notificationEvent = new NotificationEvent();
164 notificationEvent.setCambriaPartition("partition");
166 String res = pojoUtils.getXmlFromObject(notificationEvent);
169 assertTrue(res.contains("<NotificationEvent>"));
170 assertTrue(res.contains("<cambria.partition>partition</cambria.partition>"));
171 assertTrue(res.contains("</NotificationEvent>"));
175 public void testGetLookupKeyEmptyKey() {
177 Collection<String> keyProps = new ArrayList<String>();
180 HashMap<String, Object> lookup = new HashMap<String, Object>();
181 lookup.put("key", "val");
182 String expectedLookupKey = "key=val";
184 String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
185 assertEquals(expectedLookupKey, res);
189 public void testGetLookupKey() {
190 String baseKey = "baseKey";
191 Collection<String> keyProps = new ArrayList<String>();
194 HashMap<String, Object> lookup = new HashMap<String, Object>();
195 lookup.put("key", "val");
196 String expectedLookupKey = "baseKey&key=val";
198 String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
200 assertEquals(expectedLookupKey, res);
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);
210 Multimap<String, String> multimap = ImmutableListMultimap.of("objectType", "multimapkey");
211 String res = pojoUtils.getLookupKeys(lookupHashes, multimap);
213 String lookupKey = "val";
215 assertEquals(lookupKey, res);
219 public void testGetExampleObject() throws Exception {
220 Person p = getPojoObject();
221 pojoUtils.getExampleObject(p, true);
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());
232 private Entity getEntityObject() {
233 Entity entity = new Entity();
234 KeyValueList list = new KeyValueList();
236 list.setValue("value");
238 entity.setAction("action");
239 entity.setKeyValueList(Lists.newArrayList(list));
240 entity.setEquipmentRole("equipmentRole");
241 entity.setSelfLink("selfLink");
246 private Person getPojoObject() {
247 Person p = new Person("Andrew");
249 p.setHeightcm((short) 190);
251 p.setNickname("Andy");
259 private long weightlb;
260 private short heightcm;
261 private String nickname;
264 private boolean isMarried;
266 public Person(String name) {
270 public int getAge() {
274 public void setAge(int age) {
278 public long getWeightlb() {
282 public void setWeightlb(long weightlb) {
283 this.weightlb = weightlb;
286 public short getHeightcm() {
290 public void setHeightcm(short heightcm) {
291 this.heightcm = heightcm;
294 public String getNickname() {
298 public void setNickname(String nickname) {
299 this.nickname = nickname;
302 public String getName() {
306 public void setName(String name) {
310 public String getPet() {
314 public void setPet(String pet) {
318 public boolean isMarried() {
322 public void setMarried(boolean isMarried) {
323 this.isMarried = isMarried;