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=========================================================
21 package org.onap.aai.util;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import com.google.common.collect.ImmutableListMultimap;
28 import com.google.common.collect.Lists;
29 import com.google.common.collect.Multimap;
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;
40 import javax.xml.bind.JAXBException;
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;
50 public class PojoUtilsTest {
52 private PojoUtils pojoUtils;
56 pojoUtils = new PojoUtils();
60 public void testGetKeyValueList() throws Exception {
61 Entity entity = getEntityObject();
62 Person person = getPojoObject();
64 List<KeyValueList> keyValueLists = pojoUtils.getKeyValueList(entity, person);
66 for (KeyValueList keyValueList : keyValueLists) {
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());
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();
92 pojoUtils.getJsonFromObject(obj);
94 Mockito.verify(pojoUtils, Mockito.times(1)).getJsonFromObject(Mockito.anyString(), Mockito.eq(false),
99 public void testGetJsonFromObjectClass() throws Exception {
100 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
102 String res = pojoUtils.getJsonFromObject(date);
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"));
118 public void testGetJsonFromObjectClassNull() throws Exception {
119 String res = pojoUtils.getJsonFromObject(null);
122 assertEquals("null", res);
126 public void testGetJsonFromObjectNull() throws Exception {
127 String res = pojoUtils.getJsonFromObject(null, false, true);
130 assertEquals("null", res);
134 public void testGetJsonFromObject() throws Exception {
135 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
137 String res = pojoUtils.getJsonFromObject(date, false, false);
140 res = pojoUtils.getJsonFromObject(date, true, false);
143 res = pojoUtils.getJsonFromObject(date, true, true);
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);
153 Mockito.when(jaxbContext.createMarshaller()).thenReturn(marshaller);
155 String output = pojoUtils.getJsonFromDynamicObject(dynamicEntity, jaxbContext, true);
156 assertEquals("", output);
159 @Test(expected = NullPointerException.class)
160 public void testGetXmlFromObjectNull() throws Exception {
161 pojoUtils.getXmlFromObject(null);
165 public void testGetXmlFromObject() throws JAXBException, IOException {
166 NotificationEvent notificationEvent = new NotificationEvent();
167 notificationEvent.setCambriaPartition("partition");
169 String res = pojoUtils.getXmlFromObject(notificationEvent);
172 assertTrue(res.contains("<NotificationEvent>"));
173 assertTrue(res.contains("<cambria.partition>partition</cambria.partition>"));
174 assertTrue(res.contains("</NotificationEvent>"));
178 public void testGetLookupKeyEmptyKey() {
180 Collection<String> keyProps = new ArrayList<String>();
183 HashMap<String, Object> lookup = new HashMap<String, Object>();
184 lookup.put("key", "val");
185 String expectedLookupKey = "key=val";
187 String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
188 assertEquals(expectedLookupKey, res);
192 public void testGetLookupKey() {
193 String baseKey = "baseKey";
194 Collection<String> keyProps = new ArrayList<String>();
197 HashMap<String, Object> lookup = new HashMap<String, Object>();
198 lookup.put("key", "val");
199 String expectedLookupKey = "baseKey&key=val";
201 String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
203 assertEquals(expectedLookupKey, res);
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);
213 Multimap<String, String> multimap = ImmutableListMultimap.of("objectType", "multimapkey");
214 String res = pojoUtils.getLookupKeys(lookupHashes, multimap);
216 String lookupKey = "val";
218 assertEquals(lookupKey, res);
222 public void testGetExampleObject() throws Exception {
223 Person p = getPojoObject();
224 pojoUtils.getExampleObject(p, true);
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());
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;