add CLI interface and impl for category
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
cca611a1b0
commit
fa8b6a4377
11
0-Plugin/src/main/java/cli/Subcommand.java
Normal file
11
0-Plugin/src/main/java/cli/Subcommand.java
Normal file
|
@ -0,0 +1,11 @@
|
|||
package cli;
|
||||
|
||||
public interface Subcommand {
|
||||
|
||||
public String getSubcommand();
|
||||
|
||||
public String executeSubcommand(String[] args);
|
||||
|
||||
|
||||
|
||||
}
|
39
0-Plugin/src/main/java/cli/category/CategoryCommands.java
Normal file
39
0-Plugin/src/main/java/cli/category/CategoryCommands.java
Normal 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());
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Reference in a new issue