Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[config] Changed 'ConfigDescriptionParameterDTO' field serialization 'defaultValue' -> 'default' #2383

Merged
merged 2 commits into from
May 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,42 @@ public String getValue() {
public String toString() {
return this.getClass().getSimpleName() + " [name=\"" + name + "\", value=\"" + value + "\"]";
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((name == null) ? 0 : name.hashCode());
result = prime * result + ((value == null) ? 0 : value.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
FilterCriteria other = (FilterCriteria) obj;
if (name == null) {
if (other.name != null) {
return false;
}
} else if (!name.equals(other.name)) {
return false;
}
if (value == null) {
if (other.value != null) {
return false;
}
} else if (!value.equals(other.value)) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.core.config.core.ConfigDescription;
import org.openhab.core.config.core.ConfigDescriptionParameter;
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;

Expand All @@ -31,6 +33,7 @@
* @author Dennis Nobel - Initial contribution
* @author Ana Dimova - converting ConfigDescriptionParameterDTO to ConfigDescriptionParameter
*/
@NonNullByDefault
public class ConfigDescriptionDTOMapper {

/**
Expand Down Expand Up @@ -64,13 +67,27 @@ public static List<ConfigDescriptionParameter> map(List<ConfigDescriptionParamet
.withPattern(parameter.pattern).withReadOnly(parameter.readOnly).withMultiple(parameter.multiple)
.withMultipleLimit(parameter.multipleLimit).withGroupName(parameter.groupName)
.withAdvanced(parameter.advanced).withVerify(parameter.verify)
.withLimitToOptions(parameter.limitToOptions).withUnit(parameter.unitLabel)
.withLimitToOptions(parameter.limitToOptions).withUnit(parameter.unit)
.withUnitLabel(parameter.unitLabel).withOptions(mapOptionsDTO(parameter.options))
.withFilterCriteria(mapFilterCriteriaDTO(parameter.filterCriteria)).build());
}
return result;
}

public static List<ConfigDescriptionParameterGroup> mapParameterGroupsDTO(
List<ConfigDescriptionParameterGroupDTO> parameterGroups) {
if (parameterGroups == null) {
return null;
}
final List<ConfigDescriptionParameterGroup> result = new ArrayList<>(parameterGroups.size());
for (ConfigDescriptionParameterGroupDTO parameterGroup : parameterGroups) {
result.add(ConfigDescriptionParameterGroupBuilder.create(parameterGroup.name)
.withAdvanced(parameterGroup.advanced).withContext(parameterGroup.context)
.withDescription(parameterGroup.description).withLabel(parameterGroup.label).build());
}
return result;
}

private static List<FilterCriteria> mapFilterCriteriaDTO(List<FilterCriteriaDTO> filterCriteria) {
if (filterCriteria == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

import org.openhab.core.config.core.ConfigDescriptionParameter.Type;

import com.google.gson.annotations.SerializedName;

/**
* This is a data transfer object that is used to serialize parameter of a
* configuration description.
* This is a data transfer object that is used to serialize parameter of a configuration description.
*
* @author Dennis Nobel - Initial contribution
* @author Alex Tugarev - Extended for options and filter criteria
Expand All @@ -29,6 +30,7 @@
public class ConfigDescriptionParameterDTO {

public String context;
@SerializedName(value = "default", alternate = "defaultValue")
public String defaultValue;
public String description;
public String label;
Expand Down
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public Response getByURI(
URI uriObject = UriBuilder.fromPath(uri).build();
ConfigDescription configDescription = configDescriptionRegistry.getConfigDescription(uriObject, locale);
return configDescription != null
? Response.ok(EnrichedConfigDescriptionDTOMapper.map(configDescription)).build()
? JSONResponse.createResponse(Status.OK, EnrichedConfigDescriptionDTOMapper.map(configDescription),
null)
: JSONResponse.createErrorResponse(Status.NOT_FOUND, "Configuration not found: " + uri);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public Response getItems(final @Context UriInfo uriInfo, final @Context HttpHead
@GET
@RolesAllowed({ Role.USER, Role.ADMIN })
@Path("/{itemname: [a-zA-Z_0-9]+}")
@Produces({ MediaType.APPLICATION_JSON })
@Produces(MediaType.APPLICATION_JSON)
@Operation(operationId = "getItemByName", summary = "Gets a single item.", responses = {
@ApiResponse(responseCode = "200", description = "OK", content = @Content(schema = @Schema(implementation = EnrichedItemDTO.class))),
@ApiResponse(responseCode = "404", description = "Item not found") })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ public void testThatDefaultValueIsNotAList() {
assertThat(ecdpdto.defaultValues, is(nullValue()));
}

@SuppressWarnings("null")
@Test
public void testThatDefaultValuesAreAList() {
ConfigDescriptionParameter configDescriptionParameter = ConfigDescriptionParameterBuilder
Expand Down
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));
}
}
Loading