Added unit test for 'CommandDescriptionBuilder' (#1181)

Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
This commit is contained in:
Christoph Weitkamp 2019-11-03 19:16:43 +01:00 committed by Kai Kreuzer
parent bab5ff276d
commit 9ba4f55bb5
4 changed files with 98 additions and 4 deletions

View File

@ -15,6 +15,7 @@ package org.eclipse.smarthome.core.types;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.smarthome.core.internal.types.CommandDescriptionImpl;
/**
@ -22,6 +23,7 @@ import org.eclipse.smarthome.core.internal.types.CommandDescriptionImpl;
*
* @author Henning Treu - Initial contribution
*/
@NonNullByDefault
public class CommandDescriptionBuilder {
private final List<CommandOption> commandOptions = new ArrayList<>();
@ -57,7 +59,7 @@ public class CommandDescriptionBuilder {
* @return this builder.
*/
public CommandDescriptionBuilder withCommandOption(CommandOption commandOption) {
this.commandOptions.add(commandOption);
commandOptions.add(commandOption);
return this;
}
@ -68,7 +70,8 @@ public class CommandDescriptionBuilder {
* @return this builder.
*/
public CommandDescriptionBuilder withCommandOptions(List<CommandOption> commandOptions) {
commandOptions.forEach(co -> this.commandOptions.add(co));
this.commandOptions.clear();
this.commandOptions.addAll(commandOptions);
return this;
}
}

View File

@ -13,6 +13,7 @@
package org.eclipse.smarthome.core.types;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jdt.annotation.NonNullByDefault;
@ -134,6 +135,21 @@ public class StateDescriptionFragmentBuilder {
return this;
}
/**
* Ass a {@link StateOption} for the resulting {@link StateDescriptionFragment}.
*
* @param option a {@link StateOption} for the resulting {@link StateDescriptionFragment}.
* @return this builder.
*/
@SuppressWarnings("null")
public StateDescriptionFragmentBuilder withOption(StateOption option) {
if (options == null) {
options = new ArrayList<>();
}
options.add(option);
return this;
}
/**
* Set the {@link StateOption}s for the resulting {@link StateDescriptionFragment}.
*

View File

@ -0,0 +1,67 @@
/**
* Copyright (c) 2010-2019 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.eclipse.smarthome.core.types;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
/**
* Test the {@link CommandDescriptionBuilder}.
*
* @author Christoph Weitkamp - Initial contribution
*/
public class CommandDescriptionBuilderTest {
private CommandDescriptionBuilder builder;
@Before
public void setup() {
builder = CommandDescriptionBuilder.create();
}
@Test
public void builderWithEmptyOptions() {
List<CommandOption> options = Collections.emptyList();
assertThat(builder.withCommandOptions(options).build().getCommandOptions(), is(options));
}
@Test
public void builderWithOption() {
CommandOption option = new CommandOption("command", "label");
assertThat(builder.withCommandOption(option).build().getCommandOptions(), is(Arrays.asList(option)));
}
@Test
public void builderWithOptions() {
List<CommandOption> options = Arrays.asList(new CommandOption("command1", "label1"),
new CommandOption("command2", "label2"));
assertThat(builder.withCommandOptions(options).build().getCommandOptions(), is(options));
}
@Test
public void subsequentBuildsCreateIndependentFragments() {
CommandDescription commandDescription1 = builder
.withCommandOptions(Collections.singletonList(new CommandOption("command", "label"))).build();
CommandDescription commandDescription2 = builder.withCommandOptions(Collections.emptyList()).build();
assertThat(commandDescription1.getCommandOptions(), is(not(commandDescription2.getCommandOptions())));
}
}

View File

@ -16,6 +16,7 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -67,9 +68,16 @@ public class StateDescriptionFragmentBuilderTest {
assertThat(builder.withOptions(options).build().getOptions(), is(options));
}
@Test
public void builderWithOption() {
StateOption option = new StateOption("value", "label");
assertThat(builder.withOption(option).build().getOptions(), is(Arrays.asList(option)));
}
@Test
public void builderWithOptions() {
List<StateOption> options = Collections.singletonList(new StateOption("value", "label"));
List<StateOption> options = Arrays.asList(new StateOption("value1", "label1"),
new StateOption("value2", "label2"));
assertThat(builder.withOptions(options).build().getOptions(), is(options));
}
@ -101,7 +109,7 @@ public class StateDescriptionFragmentBuilderTest {
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())));
assertThat(fragment1.getOptions(), is(not(fragment2.getOptions())));
}
}