-/*-
+/*
* ============LICENSE_START=======================================================
* ONAP
* ================================================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 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.
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
-import org.apache.commons.lang3.StringUtils;
import org.onap.policy.common.gson.DoubleConverter;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
* @param json json string to be decoded
* @param clazz class of object to be decoded
* @return the object represented by the given json string
- * @throws CoderException if the source is empty
*/
- protected <T> T fromJson(String json, Class<T> clazz) throws CoderException {
- if (json == null) {
- return null;
- }
-
- if (StringUtils.isBlank(json)) {
- throw new CoderException("source string is empty");
- }
-
+ protected <T> T fromJson(String json, Class<T> clazz) {
return convertFromDouble(clazz, gson.fromJson(json, clazz));
}
* @param source source from which to read the json string to be decoded
* @param clazz class of object to be decoded
* @return the object represented by the given json string
- * @throws CoderException if the source is empty
*/
- protected <T> T fromJson(Reader source, Class<T> clazz) throws CoderException {
- T object = gson.fromJson(source, clazz);
-
- if (object == null) {
- throw new CoderException("source is empty");
- }
-
- return convertFromDouble(clazz, object);
+ protected <T> T fromJson(Reader source, Class<T> clazz) {
+ return convertFromDouble(clazz, gson.fromJson(source, clazz));
}
/**
-/*-
+/*
* ============LICENSE_START=======================================================
* ONAP PAP
* ================================================================================
- * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 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.
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;
@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");
assertEquals("{jel keyA=jel valueA, jel keyB=jel valueB}", result.toString());
}
- @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]";