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.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;
35 import javax.xml.bind.JAXBException;
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;
45 import com.google.common.collect.ImmutableListMultimap;
46 import com.google.common.collect.Lists;
47 import com.google.common.collect.Multimap;
49 public class PojoUtilsTest {
51 private PojoUtils pojoUtils;
55 pojoUtils = new PojoUtils();
59 public void testGetKeyValueList() throws Exception {
60 Entity entity = getEntityObject();
61 Person person = getPojoObject();
63 List<KeyValueList> keyValueLists = pojoUtils.getKeyValueList(entity, person);
65 for (KeyValueList keyValueList : keyValueLists) {
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());
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();
91 pojoUtils.getJsonFromObject(obj);
93 Mockito.verify(pojoUtils, Mockito.times(1)).getJsonFromObject(Mockito.anyString(), Mockito.eq(false), Mockito.eq(true));
97 public void testGetJsonFromObjectClass() throws Exception {
98 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
100 String res = pojoUtils.getJsonFromObject(date);
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"));
116 public void testGetJsonFromObjectClassNull() throws Exception {
117 String res = pojoUtils.getJsonFromObject(null);
120 assertEquals("null", res);
124 public void testGetJsonFromObjectNull() throws Exception {
125 String res = pojoUtils.getJsonFromObject(null, false, true);
128 assertEquals("null", res);
132 public void testGetJsonFromObject() throws Exception {
133 LocalDateTime date = LocalDateTime.of(2017, Month.SEPTEMBER, 18, 10, 55, 0, 300);
135 String res = pojoUtils.getJsonFromObject(date, false, false);
138 res = pojoUtils.getJsonFromObject(date, true, false);
141 res = pojoUtils.getJsonFromObject(date, true, true);
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);
151 Mockito.when(jaxbContext.createMarshaller()).thenReturn(marshaller);
153 String output = pojoUtils.getJsonFromDynamicObject(dynamicEntity, jaxbContext, true);
154 assertEquals("", output);
157 @Test(expected = NullPointerException.class)
158 public void testGetXmlFromObjectNull() throws Exception {
159 pojoUtils.getXmlFromObject(null);
163 public void testGetXmlFromObject() throws JAXBException, IOException {
164 NotificationEvent notificationEvent = new NotificationEvent();
165 notificationEvent.setCambriaPartition("partition");
167 String res = pojoUtils.getXmlFromObject(notificationEvent);
170 assertTrue(res.contains("<NotificationEvent>"));
171 assertTrue(res.contains("<cambria.partition>partition</cambria.partition>"));
172 assertTrue(res.contains("</NotificationEvent>"));
176 public void testGetLookupKeyEmptyKey() {
178 Collection<String> keyProps = new ArrayList<String>();
181 HashMap<String, Object> lookup = new HashMap<String, Object>();
182 lookup.put("key", "val");
183 String expectedLookupKey = "key=val";
185 String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
186 assertEquals(expectedLookupKey, res);
190 public void testGetLookupKey() {
191 String baseKey = "baseKey";
192 Collection<String> keyProps = new ArrayList<String>();
195 HashMap<String, Object> lookup = new HashMap<String, Object>();
196 lookup.put("key", "val");
197 String expectedLookupKey = "baseKey&key=val";
199 String res = pojoUtils.getLookupKey(baseKey, lookup, keyProps);
201 assertEquals(expectedLookupKey, res);
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);
211 Multimap<String, String> multimap = ImmutableListMultimap.of("objectType", "multimapkey");
212 String res = pojoUtils.getLookupKeys(lookupHashes, multimap);
214 String lookupKey = "val";
216 assertEquals(lookupKey, res);
220 public void testGetExampleObject() throws Exception {
221 Person p = getPojoObject();
222 pojoUtils.getExampleObject(p, true);
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());
233 private Entity getEntityObject() {
234 Entity entity = new Entity();
235 KeyValueList list = new KeyValueList();
237 list.setValue("value");
239 entity.setAction("action");
240 entity.setKeyValueList(Lists.newArrayList(list));
241 entity.setEquipmentRole("equipmentRole");
242 entity.setSelfLink("selfLink");
247 private Person getPojoObject() {
248 Person p = new Person("Andrew");
250 p.setHeightcm((short) 190);
252 p.setNickname("Andy");
260 private long weightlb;
261 private short heightcm;
262 private String nickname;
265 private boolean isMarried;
267 public Person(String name) {
271 public int getAge() {
275 public void setAge(int age) {
279 public long getWeightlb() {
283 public void setWeightlb(long weightlb) {
284 this.weightlb = weightlb;
287 public short getHeightcm() {
291 public void setHeightcm(short heightcm) {
292 this.heightcm = heightcm;
295 public String getNickname() {
299 public void setNickname(String nickname) {
300 this.nickname = nickname;
303 public String getName() {
307 public void setName(String name) {
311 public String getPet() {
315 public void setPet(String pet) {
319 public boolean isMarried() {
323 public void setMarried(boolean isMarried) {
324 this.isMarried = isMarried;