diff options
author | Jim Hahn <jrh3@att.com> | 2021-04-07 16:23:10 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-04-07 16:27:18 -0400 |
commit | 04623fef95f2b164d79ad48de2ef3708d02eda80 (patch) | |
tree | 4405b33881ee796091eb42f03ea617dfbdcb40ed /utils/src/test/java | |
parent | 8ebb34d51adc48d94c2ceb5a32d3341b076b8207 (diff) |
Modify StandardCoder to handle empty source
Currently, if the source string or source file is empty, the
StandardCoder just returns null. Modified it to throw an exception in
those cases. However, if the string reference is null, it will still
return null.
Issue-ID: POLICY-3172
Change-Id: Ie7d5e1b0328a24d94a75af8d18a341faebbe5d5f
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'utils/src/test/java')
-rw-r--r-- | utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java | 35 |
1 files changed, 33 insertions, 2 deletions
diff --git a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java index a468f0b4..be588a40 100644 --- a/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java +++ b/utils/src/test/java/org/onap/policy/common/utils/coder/StandardCoderTest.java @@ -1,8 +1,8 @@ -/* +/*- * ============LICENSE_START======================================================= * ONAP PAP * ================================================================================ - * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved. + * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ package org.onap.policy.common.utils.coder; +import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -263,6 +264,8 @@ public class StandardCoderTest { @Test public void testToJsonTree_testFromJsonJsonElementClassT() throws Exception { + assertThat(coder.fromJson((JsonElement) null, MyMap.class)).isNull(); + MyMap map = new MyMap(); map.props = new LinkedHashMap<>(); map.props.put("jel keyA", "jel valueA"); @@ -278,6 +281,34 @@ public class StandardCoderTest { } @Test + public void testFromJsonStringClassT() throws Exception { + assertThat(coder.fromJson((String) null, MyMap.class)).isNull(); + + String json = "{'props': {'str keyA': 'str valueA', 'str keyB':'str valueB'}}".replace('\'', '"'); + + Object result = coder.fromJson(json, MyMap.class); + + assertNotNull(result); + assertEquals("{str keyA=str valueA, str keyB=str valueB}", result.toString()); + + assertThatThrownBy(() -> coder.fromJson("", MyMap.class)).isInstanceOf(CoderException.class) + .hasMessageContaining("empty"); + } + + @Test + public void testFromJsonReaderClassT() throws Exception { + String json = "{'props': {'str keyA': 'str valueA', 'str keyB':'str valueB'}}".replace('\'', '"'); + + Object result = coder.fromJson(new StringReader(json), MyMap.class); + + assertNotNull(result); + assertEquals("{str keyA=str valueA, str keyB=str valueB}", result.toString()); + + assertThatThrownBy(() -> coder.fromJson(new StringReader(""), MyMap.class)).isInstanceOf(CoderException.class) + .hasMessageContaining("empty"); + } + + @Test public void testConvertFromDouble() throws Exception { String text = "[listA, {keyA=100}, 200]"; assertEquals(text, coder.decode(text, Object.class).toString()); |