Skip to content

Commit

Permalink
Fix dynamic options in UI (#1050)
Browse files Browse the repository at this point in the history
* Fix dynamic options in UI
* Added unit tests

Fixes #1040

Also-by: Christoph Weitkamp <github@christophweitkamp.de>
Signed-off-by: Laurent Garnier <lg.hc@free.fr>
  • Loading branch information
lolodomo authored and cweitkamp committed Sep 28, 2019
1 parent b9d9135 commit 7b49f27
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
Expand Up @@ -187,7 +187,7 @@ public StateDescriptionFragment merge(StateDescriptionFragment fragment) {
if (this.readOnly == null) {
this.readOnly = fragment.isReadOnly();
}
if (this.options == null) {
if (this.options == null || this.options.isEmpty()) {
this.options = fragment.getOptions();
}

Expand Down
Expand Up @@ -12,22 +12,22 @@
*/
package org.eclipse.smarthome.core.internal.types;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;

import org.eclipse.smarthome.core.types.StateDescription;
import org.eclipse.smarthome.core.types.StateDescriptionFragment;
import org.eclipse.smarthome.core.types.StateOption;
import org.junit.Before;
import org.junit.Test;

/**
* Test the {@link StateDescriptionFragmentImpl}.
*
* @author Henning Treu - Initial contribution
*
*/
public class StateDescriptionFragmentImplTest {

Expand All @@ -41,7 +41,7 @@ public void setup() {
source.setStep(BigDecimal.ONE);
source.setPattern("pattern");
source.setReadOnly(Boolean.TRUE);
source.setOptions(new ArrayList<>(0));
source.setOptions(Collections.emptyList());
}

@Test
Expand All @@ -54,6 +54,21 @@ public void mergeFragment() {
assertThat(fragment.getPattern(), is(source.getPattern()));
assertThat(fragment.isReadOnly(), is(source.isReadOnly()));
assertThat(fragment.getOptions(), is(source.getOptions()));

// fragment with empty options should inherit new options
StateDescriptionFragmentImpl sourceWithOptions = new StateDescriptionFragmentImpl();
sourceWithOptions.setOptions(Collections.singletonList(new StateOption("value1", "label1")));

fragment = source.merge(sourceWithOptions);

assertThat(fragment.getOptions(), is(sourceWithOptions.getOptions()));

// fragment with options should NOT inherit new options
sourceWithOptions.setOptions(Collections.singletonList(new StateOption("value2", "label2")));

fragment = source.merge(sourceWithOptions);

assertThat(fragment.getOptions(), is(not(sourceWithOptions.getOptions())));
}

@Test
Expand Down
Expand Up @@ -16,11 +16,9 @@
import static org.junit.Assert.assertThat;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.eclipse.jdt.annotation.NonNull;
import org.junit.Before;
import org.junit.Test;

Expand Down Expand Up @@ -64,15 +62,21 @@ public void builderWithReadOnly() {
}

@Test
public void builderWithIOptions() {
List<@NonNull StateOption> options = new ArrayList<>(0);
public void builderWithEmptyOptions() {
List<StateOption> options = Collections.emptyList();
assertThat(builder.withOptions(options).build().getOptions(), is(options));
}

@Test
public void builderWithOptions() {
List<StateOption> options = Collections.singletonList(new StateOption("value", "label"));
assertThat(builder.withOptions(options).build().getOptions(), is(options));
}

@Test
public void builderWithStateDescription() {
StateDescription source = new StateDescription(BigDecimal.ZERO, BigDecimal.TEN, BigDecimal.ONE, "pattern", true,
Arrays.asList(new StateOption[] { new StateOption("value", "label") }));
Collections.singletonList(new StateOption("value", "label")));
StateDescriptionFragment fragment = StateDescriptionFragmentBuilder.create(source).build();

assertThat(fragment.getMinimum(), is(source.getMinimum()));
Expand All @@ -86,13 +90,18 @@ public void builderWithStateDescription() {
@Test
public void subsequentBuildsCreateIndependentFragments() {
StateDescriptionFragment fragment1 = builder.withMinimum(BigDecimal.ZERO).withMaximum(BigDecimal.TEN)
.withPattern("pattern").build();
.withStep(BigDecimal.ONE).withPattern("pattern").withReadOnly(Boolean.FALSE)
.withOptions(Collections.singletonList(new StateOption("value", "label"))).build();
StateDescriptionFragment fragment2 = builder.withMinimum(BigDecimal.ONE).withMaximum(BigDecimal.ONE)
.withPattern("pattern_new").build();
.withStep(BigDecimal.ZERO).withPattern("pattern_new").withReadOnly(Boolean.TRUE)
.withOptions(Collections.emptyList()).build();

assertThat(fragment1.getMinimum(), is(not(fragment2.getMinimum())));
assertThat(fragment1.getMaximum(), is(not(fragment2.getMaximum())));
assertThat(fragment1.getStep(), is(not(fragment2.getStep())));
assertThat(fragment1.getPattern(), is(not(fragment2.getPattern())));
assertThat(fragment1.isReadOnly(), is(not(fragment2.isReadOnly())));
assertThat(fragment1.getOptions().size(), is(not(fragment2.getOptions().size())));
}

}

0 comments on commit 7b49f27

Please sign in to comment.