-
-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[config] Changed 'ConfigDescriptionParameterDTO' field serialization …
…'defaultValue' -> 'default' (#2383) * Changed ConfigParameterDTO field serialization 'defaultValue' -> 'default' Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
- Loading branch information
Showing
10 changed files
with
335 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
....config.core/src/test/java/org/openhab/core/config/core/dto/ConfigDescriptionDTOTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.config.core.dto; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize; | ||
|
||
import java.math.BigDecimal; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.junit.jupiter.api.Test; | ||
import org.openhab.core.config.core.ConfigDescription; | ||
import org.openhab.core.config.core.ConfigDescriptionBuilder; | ||
import org.openhab.core.config.core.ConfigDescriptionParameter; | ||
import org.openhab.core.config.core.ConfigDescriptionParameter.Type; | ||
import org.openhab.core.config.core.ConfigDescriptionParameterBuilder; | ||
import org.openhab.core.config.core.ConfigDescriptionParameterGroup; | ||
import org.openhab.core.config.core.ConfigDescriptionParameterGroupBuilder; | ||
import org.openhab.core.config.core.FilterCriteria; | ||
import org.openhab.core.config.core.ParameterOption; | ||
|
||
/** | ||
* This is the test class for {@link ConfigDescriptionDTO}. | ||
* | ||
* @author Christoph Weitkamp - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
class ConfigDescriptionDTOTest { | ||
|
||
private static final String CONFIG_DESCRIPTION_SYSTEM_I18N_URI = "system:i18n"; | ||
|
||
private static final String PARAM_NAME = "name"; | ||
private static final String PARAMETER_NAME_CONTEXT_NETWORK_ADDRESS = "network-address"; | ||
private static final String PARAMETER_NAME_DEFAULT_VALUE = "test"; | ||
private static final String PARAMETER_NAME_DESCRIPTION = "description"; | ||
private static final List<FilterCriteria> PARAMETER_NAME_FILTER = List.of(new FilterCriteria("name", "value")); | ||
private static final String PARAMETER_NAME_GROUP = "group"; | ||
private static final String PARAMETER_NAME_LABEL = "label"; | ||
private static final List<ParameterOption> PARAMETER_NAME_OPTIONS = List.of(new ParameterOption("value", "label")); | ||
private static final String PARAMETER_NAME_PATTERN = "%.1f"; | ||
private static final String PARAMETER_NAME_UNIT = "ms"; | ||
private static final String PARAMETER_NAME_UNIT_LABEL = "milliseconds"; | ||
|
||
private static final String PARAMETER_GROUP_NAME = "name"; | ||
private static final String PARAMETER_GROUP_CONTEXT = "context"; | ||
private static final String PARAMETER_GROUP_DESCRIPTION = "description"; | ||
private static final String PARAMETER_GROUP_LABEL = "label"; | ||
|
||
@Test | ||
public void testConfigDescriptionDTOMappingContainsDecodedURIString() throws URISyntaxException { | ||
final URI systemI18nURI = new URI(CONFIG_DESCRIPTION_SYSTEM_I18N_URI); | ||
ConfigDescription subject = ConfigDescriptionBuilder.create(systemI18nURI).build(); | ||
|
||
ConfigDescriptionDTO result = ConfigDescriptionDTOMapper.map(subject); | ||
|
||
assertThat(result.uri, is(CONFIG_DESCRIPTION_SYSTEM_I18N_URI)); | ||
} | ||
|
||
@Test | ||
public void testConfigDescriptionParameterDTOMappingIsBidirectional() { | ||
List<ConfigDescriptionParameter> subject = List.of(ConfigDescriptionParameterBuilder | ||
.create(PARAM_NAME, Type.INTEGER).withAdvanced(true).withContext(PARAMETER_NAME_CONTEXT_NETWORK_ADDRESS) | ||
.withDefault(PARAMETER_NAME_DEFAULT_VALUE).withDescription(PARAMETER_NAME_DESCRIPTION) | ||
.withFilterCriteria(PARAMETER_NAME_FILTER).withGroupName(PARAMETER_NAME_GROUP) | ||
.withLabel(PARAMETER_NAME_LABEL).withLimitToOptions(false).withMaximum(BigDecimal.TEN) | ||
.withMinimum(BigDecimal.ZERO).withMultiple(true).withMultipleLimit(Integer.valueOf(3)) | ||
.withOptions(PARAMETER_NAME_OPTIONS).withPattern(PARAMETER_NAME_PATTERN).withReadOnly(false) | ||
.withRequired(true).withStepSize(BigDecimal.ONE).withUnit(PARAMETER_NAME_UNIT) | ||
.withUnitLabel(PARAMETER_NAME_UNIT_LABEL).withVerify(true).build()); | ||
|
||
List<ConfigDescriptionParameter> result = ConfigDescriptionDTOMapper | ||
.map(ConfigDescriptionDTOMapper.mapParameters(subject)); | ||
assertThat(result, hasSize(1)); | ||
|
||
ConfigDescriptionParameter parameter = result.get(0); | ||
assertThat(parameter.getName(), is(PARAM_NAME)); | ||
assertThat(parameter.getType(), is(Type.INTEGER)); | ||
assertThat(parameter.isAdvanced(), is(true)); | ||
assertThat(parameter.getContext(), is(PARAMETER_NAME_CONTEXT_NETWORK_ADDRESS)); | ||
assertThat(parameter.getDefault(), is(PARAMETER_NAME_DEFAULT_VALUE)); | ||
assertThat(parameter.getDescription(), is(PARAMETER_NAME_DESCRIPTION)); | ||
assertThat(parameter.getFilterCriteria(), is(PARAMETER_NAME_FILTER)); | ||
assertThat(parameter.getGroupName(), is(PARAMETER_NAME_GROUP)); | ||
assertThat(parameter.getLabel(), is(PARAMETER_NAME_LABEL)); | ||
assertThat(parameter.getLimitToOptions(), is(false)); | ||
assertThat(parameter.getMaximum(), is(BigDecimal.TEN)); | ||
assertThat(parameter.getMinimum(), is(BigDecimal.ZERO)); | ||
assertThat(parameter.isMultiple(), is(true)); | ||
assertThat(parameter.getMultipleLimit(), is(Integer.valueOf(3))); | ||
assertThat(parameter.getOptions(), is(PARAMETER_NAME_OPTIONS)); | ||
assertThat(parameter.getPattern(), is(PARAMETER_NAME_PATTERN)); | ||
assertThat(parameter.isReadOnly(), is(false)); | ||
assertThat(parameter.isRequired(), is(true)); | ||
assertThat(parameter.getStepSize(), is(BigDecimal.ONE)); | ||
assertThat(parameter.getUnit(), is(PARAMETER_NAME_UNIT)); | ||
assertThat(parameter.getUnitLabel(), is(PARAMETER_NAME_UNIT_LABEL)); | ||
assertThat(parameter.isVerifyable(), is(true)); | ||
} | ||
|
||
@Test | ||
public void testConfigDescriptionParameterGroupDTOMappingIsBidirectional() { | ||
List<ConfigDescriptionParameterGroup> subject = List.of(ConfigDescriptionParameterGroupBuilder | ||
.create(PARAMETER_GROUP_NAME).withAdvanced(true).withContext(PARAMETER_GROUP_CONTEXT) | ||
.withDescription(PARAMETER_GROUP_DESCRIPTION).withLabel(PARAMETER_GROUP_LABEL).build()); | ||
|
||
List<ConfigDescriptionParameterGroup> result = ConfigDescriptionDTOMapper | ||
.mapParameterGroupsDTO(ConfigDescriptionDTOMapper.mapParameterGroups(subject)); | ||
assertThat(result, hasSize(1)); | ||
|
||
ConfigDescriptionParameterGroup parameterGroup = result.get(0); | ||
assertThat(parameterGroup.getName(), is(PARAMETER_GROUP_NAME)); | ||
assertThat(parameterGroup.isAdvanced(), is(true)); | ||
assertThat(parameterGroup.getContext(), is(PARAMETER_GROUP_CONTEXT)); | ||
assertThat(parameterGroup.getDescription(), is(PARAMETER_GROUP_DESCRIPTION)); | ||
assertThat(parameterGroup.getLabel(), is(PARAMETER_GROUP_LABEL)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...est/java/org/openhab/core/io/rest/core/internal/config/ConfigDescriptionResourceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/** | ||
* Copyright (c) 2010-2021 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.io.rest.core.internal.config; | ||
|
||
import static org.hamcrest.CoreMatchers.is; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.mockito.ArgumentMatchers.*; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
import javax.ws.rs.core.Response; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.mockito.junit.jupiter.MockitoSettings; | ||
import org.mockito.quality.Strictness; | ||
import org.openhab.core.config.core.ConfigDescription; | ||
import org.openhab.core.config.core.ConfigDescriptionBuilder; | ||
import org.openhab.core.config.core.ConfigDescriptionParameter.Type; | ||
import org.openhab.core.config.core.ConfigDescriptionParameterBuilder; | ||
import org.openhab.core.config.core.ConfigDescriptionRegistry; | ||
import org.openhab.core.io.rest.LocaleService; | ||
|
||
/** | ||
* @author Christoph Weitkamp - Initial contribution | ||
*/ | ||
@ExtendWith(MockitoExtension.class) | ||
@MockitoSettings(strictness = Strictness.WARN) | ||
public class ConfigDescriptionResourceTest { | ||
|
||
private static final String PARAM_NAME = "name"; | ||
private static final String PARAMETER_NAME_DEFAULT_VALUE = "test"; | ||
private static final String PARAM_COUNTRY = "country"; | ||
|
||
private static final String CONFIG_DESCRIPTION_SYSTEM_I18N_URI = "system:i18n"; | ||
|
||
private ConfigDescriptionResource resource; | ||
|
||
private @Mock ConfigDescriptionRegistry mockedConfigDescriptionRegistry; | ||
private @Mock LocaleService mockedLocaleService; | ||
|
||
@BeforeEach | ||
public void beforeEach() throws URISyntaxException { | ||
final URI systemI18nURI = new URI(CONFIG_DESCRIPTION_SYSTEM_I18N_URI); | ||
final ConfigDescription systemI18n = ConfigDescriptionBuilder.create(systemI18nURI) | ||
.withParameter(ConfigDescriptionParameterBuilder.create(PARAM_NAME, Type.TEXT) | ||
.withDefault(PARAMETER_NAME_DEFAULT_VALUE).build()) | ||
.build(); | ||
final ConfigDescription systemEphemeris = ConfigDescriptionBuilder.create(new URI("system:ephemeris")) | ||
.withParameter(ConfigDescriptionParameterBuilder.create(PARAM_COUNTRY, Type.TEXT).build()).build(); | ||
when(mockedConfigDescriptionRegistry.getConfigDescriptions(any())) | ||
.thenReturn(List.of(systemI18n, systemEphemeris)); | ||
when(mockedConfigDescriptionRegistry.getConfigDescription(eq(systemI18nURI), any())).thenReturn(systemI18n); | ||
|
||
resource = new ConfigDescriptionResource(mockedConfigDescriptionRegistry, mockedLocaleService); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAllConfigDescriptions() throws IOException { | ||
Response response = resource.getAll(null, null); | ||
assertThat(response.getStatus(), is(200)); | ||
assertThat(new String(((InputStream) response.getEntity()).readAllBytes(), StandardCharsets.UTF_8), is( | ||
"[{\"uri\":\"system:i18n\",\"parameters\":[{\"default\":\"test\",\"name\":\"name\",\"required\":false,\"type\":\"TEXT\",\"readOnly\":false,\"multiple\":false,\"advanced\":false,\"verify\":false,\"limitToOptions\":true,\"options\":[],\"filterCriteria\":[]}],\"parameterGroups\":[]},{\"uri\":\"system:ephemeris\",\"parameters\":[{\"name\":\"country\",\"required\":false,\"type\":\"TEXT\",\"readOnly\":false,\"multiple\":false,\"advanced\":false,\"verify\":false,\"limitToOptions\":true,\"options\":[],\"filterCriteria\":[]}],\"parameterGroups\":[]}]")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnAConfigDescription() throws IOException { | ||
Response response = resource.getByURI(null, CONFIG_DESCRIPTION_SYSTEM_I18N_URI); | ||
assertThat(response.getStatus(), is(200)); | ||
assertThat(new String(((InputStream) response.getEntity()).readAllBytes(), StandardCharsets.UTF_8), is( | ||
"{\"uri\":\"system:i18n\",\"parameters\":[{\"default\":\"test\",\"name\":\"name\",\"required\":false,\"type\":\"TEXT\",\"readOnly\":false,\"multiple\":false,\"advanced\":false,\"verify\":false,\"limitToOptions\":true,\"options\":[],\"filterCriteria\":[]}],\"parameterGroups\":[]}")); | ||
} | ||
|
||
@Test | ||
public void shouldReturnStatus404() { | ||
Response response = resource.getByURI(null, "uri:invalid"); | ||
assertThat(response.getStatus(), is(404)); | ||
} | ||
} |
Oops, something went wrong.