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);
103 assertEquals("\"2017-09-18T10:55:00.0000003\"", res);
108 public void testGetJsonFromObjectClassNull() throws Exception {
109 String res = pojoUtils.getJsonFromObject(null);
112 assertEquals("null", res);
116 public void testGetJsonFromObjectNull() throws Exception {
117 String res = pojoUtils.getJsonFromObject(null, false, true);
120 assertEquals("null", res);
124 public void testGetJsonFromObject() throws Exception {
125 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
127 String res = pojoUtils.getJsonFromObject(date, false, false);
130 res = pojoUtils.getJsonFromObject(date, true, false);
133 res = pojoUtils.getJsonFromObject(date, true, true);
138 public void testGetJsonFromDynamicObject() throws Exception {
139 DynamicEntity dynamicEntity = Mockito.mock(DynamicEntity.class);
140 JAXBContext jaxbContext = Mockito.mock(JAXBContext.class);
141 JAXBMarshaller marshaller = Mockito.mock(JAXBMarshaller.class);
143 Mockito.when(jaxbContext.createMarshaller()).thenReturn(marshaller);
145 String output = pojoUtils.getJsonFromDynamicObject(dynamicEntity, jaxbContext, true);
146 assertEquals("", output);
149 @Test(expected = NullPointerException.class)
150 public void testGetXmlFromObjectNull() throws Exception {
151 pojoUtils.getXmlFromObject(null);
155 public void testGetXmlFromObject() throws JAXBException, IOException {
156 NotificationEvent notificationEvent = new NotificationEvent();
157 notificationEvent.setCambriaPartition("partition");
159 String res = pojoUtils.getXmlFromObject(notificationEvent);
162 assertTrue(res.contains("<NotificationEvent>"));
163 assertTrue(res.contains("<cambria.partition>partition</cambria.partition>"));
164 assertTrue(res.contains("</NotificationEvent>"));
168 public void testGetLookupKeyEmptyKey() {
170 Collection<String> keyProps = new ArrayList<String>();
173 HashMap<String, Object> lookup = new HashMap<String, Object>();
174 lookup.put("key", "val");
175 String expectedLookupKey = "key=val";
177 String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
178 assertEquals(expectedLookupKey, res);
182 public void testGetLookupKey() {
183 String baseKey = "baseKey";
184 Collection<String> keyProps = new ArrayList<String>();
187 HashMap<String, Object> lookup = new HashMap<String, Object>();
188 lookup.put("key", "val");
189 String expectedLookupKey = "baseKey&key=val";
191 String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
193 assertEquals(expectedLookupKey, res);
197 public void testGetLookupKeys() {
198 HashMap<String, Object> lookup = new HashMap<>();
199 lookup.put("multimapkey", "val");
200 LinkedHashMap<String, HashMap<String, Object>> lookupHashes = new LinkedHashMap<>();
201 lookupHashes.put("objectType", lookup);
203 Multimap<String, String> multimap = ImmutableListMultimap.of("objectType", "multimapkey");
204 String res = pojoUtils.getLookupKeys(lookupHashes, multimap);
206 String lookupKey = "val";
208 assertEquals(lookupKey, res);
212 public void testGetExampleObject() throws Exception {
213 Person p = getPojoObject();
214 pojoUtils.getExampleObject(p, true);
216 assertTrue(p.getName().contains("example-name-val-"));
217 assertTrue(p.getNickname().contains("example-nickname-val-"));
218 assertTrue(p.getPet().contains("example-pet-val-"));
219 assertTrue(p.isMarried());
222 private Entity getEntityObject() {
223 Entity entity = new Entity();
224 KeyValueList list = new KeyValueList();
226 list.setValue("value");
228 entity.setAction("action");
229 entity.setKeyValueList(Lists.newArrayList(list));
230 entity.setEquipmentRole("equipmentRole");
231 entity.setSelfLink("selfLink");
236 private Person getPojoObject() {
237 Person p = new Person("Andrew");
239 p.setHeightcm((short) 190);
241 p.setNickname("Andy");
249 private long weightlb;
250 private short heightcm;
251 private String nickname;
254 private boolean isMarried;
256 public Person(String name) {
260 public int getAge() {
264 public void setAge(int age) {
268 public long getWeightlb() {
272 public void setWeightlb(long weightlb) {
273 this.weightlb = weightlb;
276 public short getHeightcm() {
280 public void setHeightcm(short heightcm) {
281 this.heightcm = heightcm;
284 public String getNickname() {
288 public void setNickname(String nickname) {
289 this.nickname = nickname;
292 public String getName() {
296 public void setName(String name) {
300 public String getPet() {
304 public void setPet(String pet) {
308 public boolean isMarried() {
312 public void setMarried(boolean isMarried) {
313 this.isMarried = isMarried;