Fixed potential NPE if 'defaultValue' is null (#1149)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
pull/1150/head
Christoph Weitkamp 2019-10-19 20:08:56 +02:00 committed by Wouter Born
parent 22ba5d92c1
commit 8d6d650d2e
2 changed files with 23 additions and 5 deletions

View File

@ -41,14 +41,14 @@ public class EnrichedConfigDescriptionParameterDTO extends ConfigDescriptionPara
public EnrichedConfigDescriptionParameterDTO(String name, Type type, BigDecimal minimum, BigDecimal maximum,
BigDecimal stepsize, String pattern, Boolean required, Boolean readOnly, Boolean multiple, String context,
String defaultValue, String label, String description, List<ParameterOptionDTO> options,
@Nullable String defaultValue, String label, String description, List<ParameterOptionDTO> options,
List<FilterCriteriaDTO> filterCriteria, String groupName, Boolean advanced, Boolean limitToOptions,
Integer multipleLimit, String unit, String unitLabel, Boolean verify) {
super(name, type, minimum, maximum, stepsize, pattern, required, readOnly, multiple, context, defaultValue,
label, description, options, filterCriteria, groupName, advanced, limitToOptions, multipleLimit, unit,
unitLabel, verify);
if (multiple) {
if (multiple && defaultValue != null) {
if (defaultValue.contains(DEFAULT_LIST_DELIMITER)) {
defaultValues = Arrays.asList(defaultValue.split(DEFAULT_LIST_DELIMITER)).stream().map(v -> v.trim())
.filter(v -> !v.isEmpty()).collect(Collectors.toList());

View File

@ -13,6 +13,7 @@
package org.eclipse.smarthome.io.rest.core.config;
import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
import static org.junit.Assert.assertThat;
import java.net.URI;
@ -35,6 +36,23 @@ public class EnrichedConfigDescriptionDTOMapperTest {
private static final String CONFIG_PARAMETER_NAME = "test";
private static final String CONFIG_PARAMETER_DEFAULT_VALUE = "first value,second value,third value";
@Test
public void testThatDefaultValuesAreEmptyIfMultipleIsTrue() {
ConfigDescriptionParameter configDescriptionParameter = ConfigDescriptionParameterBuilder
.create(CONFIG_PARAMETER_NAME, Type.TEXT).withMultiple(true).build();
ConfigDescription configDescription = new ConfigDescription(CONFIG_URI,
Arrays.asList(configDescriptionParameter));
ConfigDescriptionDTO cddto = EnrichedConfigDescriptionDTOMapper.map(configDescription);
assertThat(cddto.parameters, hasSize(1));
ConfigDescriptionParameterDTO cdpdto = cddto.parameters.get(0);
assertThat(cdpdto, instanceOf(EnrichedConfigDescriptionParameterDTO.class));
assertThat(cdpdto.defaultValue, is(nullValue()));
EnrichedConfigDescriptionParameterDTO ecdpdto = (EnrichedConfigDescriptionParameterDTO) cdpdto;
assertThat(ecdpdto.defaultValues, is(nullValue()));
}
@Test
public void testThatDefaultValueIsNotAList() {
ConfigDescriptionParameter configDescriptionParameter = ConfigDescriptionParameterBuilder
@ -43,7 +61,7 @@ public class EnrichedConfigDescriptionDTOMapperTest {
Arrays.asList(configDescriptionParameter));
ConfigDescriptionDTO cddto = EnrichedConfigDescriptionDTOMapper.map(configDescription);
assertThat(cddto.parameters.size(), is(1));
assertThat(cddto.parameters, hasSize(1));
ConfigDescriptionParameterDTO cdpdto = cddto.parameters.get(0);
assertThat(cdpdto, instanceOf(EnrichedConfigDescriptionParameterDTO.class));
@ -62,14 +80,14 @@ public class EnrichedConfigDescriptionDTOMapperTest {
Arrays.asList(configDescriptionParameter));
ConfigDescriptionDTO cddto = EnrichedConfigDescriptionDTOMapper.map(configDescription);
assertThat(cddto.parameters.size(), is(1));
assertThat(cddto.parameters, hasSize(1));
ConfigDescriptionParameterDTO cdpdto = cddto.parameters.get(0);
assertThat(cdpdto, instanceOf(EnrichedConfigDescriptionParameterDTO.class));
assertThat(cdpdto.defaultValue, is(CONFIG_PARAMETER_DEFAULT_VALUE));
EnrichedConfigDescriptionParameterDTO ecdpdto = (EnrichedConfigDescriptionParameterDTO) cdpdto;
assertThat(ecdpdto.defaultValues, is(notNullValue()));
assertThat(ecdpdto.defaultValues.size(), is(3));
assertThat(ecdpdto.defaultValues, hasSize(3));
assertThat(ecdpdto.defaultValues, is(equalTo(Arrays.asList("first value", "second value", "third value"))));
}