add CLI interface and impl for category
continuous-integration/drone/push Build is passing Details

This commit is contained in:
qvalentin 2022-05-15 11:39:03 +02:00
parent cca611a1b0
commit fa8b6a4377
Signed by: qvalentin
GPG Key ID: C979FA1EAFCABF1C
3 changed files with 91 additions and 0 deletions

View File

@ -0,0 +1,11 @@
package cli;
public interface Subcommand {
public String getSubcommand();
public String executeSubcommand(String[] args);
}

View File

@ -0,0 +1,39 @@
package cli.category;
import cli.Subcommand;
import java.util.HashMap;
import java.util.function.Function;
public class CategoryCommands implements Subcommand {
final private CategoryCliAdapter categoryCliAdapter;
final private HashMap<String, Function<String[], String>> commands = new HashMap<>();
public CategoryCommands(CategoryCliAdapter categoryCliAdapter) {
this.categoryCliAdapter = categoryCliAdapter;
commands.put("add", this::addCategory);
commands.put("get", this::getCategories);
}
@Override
public String getSubcommand() {
return "category";
}
@Override
public String executeSubcommand(String[] args) {
return commands.get(args[0]).apply(args);
}
private String addCategory(String[] args) {
categoryCliAdapter.addCategory(args[1]);
return "Added the new category";
}
private String getCategories(String[] _ignored) {
return "Available Categories:" + System.lineSeparator() + String.join(System.lineSeparator(),
categoryCliAdapter.getCategories());
}
}

View File

@ -0,0 +1,41 @@
package cli.category;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class CategoryCommandsTest {
CategoryCliAdapter mockAdapter = mock(CategoryCliAdapter.class);
@Test
void addCommandWorks() {
var categoryName = "funStuff";
ArgumentCaptor<String> valueCapture = ArgumentCaptor.forClass(String.class);
doNothing().when(mockAdapter).addCategory(valueCapture.capture());
var sut = new CategoryCommands(mockAdapter);
var returnValue = sut.executeSubcommand(new String[]{"add", categoryName});
assertEquals(categoryName, valueCapture.getValue());
assertEquals("Added the new category", returnValue);
}
@Test
void getCommandWorks() {
var sut = new CategoryCommands(mockAdapter);
when(mockAdapter.getCategories()).thenReturn(Set.of("funStuff", "workStuff"));
var returnValue = sut.executeSubcommand(new String[]{"get"});
var expected =
"Available Categories:" + System.lineSeparator() + "funStuff" + System.lineSeparator() + "workStuff";
assertEquals(expected, returnValue);
}
}