refactor Subcommand to abstract class, add add link and tag subcommands
Some checks failed
continuous-integration/drone/push Build is failing
Some checks failed
continuous-integration/drone/push Build is failing
This commit is contained in:
parent
4e74f8b10e
commit
78730bc69f
8
0-Plugin/src/main/java/cli/CliError.java
Normal file
8
0-Plugin/src/main/java/cli/CliError.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
package cli;
|
||||
|
||||
public class CliError extends RuntimeException {
|
||||
|
||||
public CliError(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -1,11 +1,34 @@
|
|||
package cli;
|
||||
|
||||
public interface Subcommand {
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
public String getSubcommand();
|
||||
abstract public class Subcommand {
|
||||
|
||||
public String executeSubcommand(String[] args);
|
||||
/**
|
||||
* Einfach ein Träumchen, dass das hier public sein muss, damit die Kinderklassen darauf zugreifen können
|
||||
*/
|
||||
|
||||
final public HashMap<String, Function<String[], String>> commands = new HashMap<>();
|
||||
|
||||
abstract public String getSubcommand();
|
||||
|
||||
abstract public String getUsage();
|
||||
|
||||
public String executeSubcommand(String[] args) {
|
||||
|
||||
try {
|
||||
commandExits(args[0]);
|
||||
return commands.get(args[0]).apply(args);
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
throw new CliError("Missing a value! " + getUsage());
|
||||
}
|
||||
}
|
||||
|
||||
private void commandExits(String command) {
|
||||
if (commands.get(command) == null) {
|
||||
throw new CliError("Subcommand does not exist! " + getUsage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,13 +2,9 @@ package cli.category;
|
|||
|
||||
import cli.Subcommand;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class CategoryCommands implements Subcommand {
|
||||
public class CategoryCommands extends Subcommand {
|
||||
|
||||
final private CategoryCliAdapter categoryCliAdapter;
|
||||
final private HashMap<String, Function<String[], String>> commands = new HashMap<>();
|
||||
|
||||
public CategoryCommands(CategoryCliAdapter categoryCliAdapter) {
|
||||
this.categoryCliAdapter = categoryCliAdapter;
|
||||
|
@ -22,8 +18,9 @@ public class CategoryCommands implements Subcommand {
|
|||
}
|
||||
|
||||
@Override
|
||||
public String executeSubcommand(String[] args) {
|
||||
return commands.get(args[0]).apply(args);
|
||||
public String getUsage() {
|
||||
return "Usage:" + System.lineSeparator() + getSubcommand() + "add categoryName" + System.lineSeparator() + getSubcommand() + "get";
|
||||
|
||||
}
|
||||
|
||||
private String addCategory(String[] args) {
|
||||
|
|
35
0-Plugin/src/main/java/cli/link/LinkCommands.java
Normal file
35
0-Plugin/src/main/java/cli/link/LinkCommands.java
Normal file
|
@ -0,0 +1,35 @@
|
|||
package cli.link;
|
||||
|
||||
import cli.Subcommand;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class LinkCommands extends Subcommand {
|
||||
|
||||
private final LinkCliAdapter linkCliAdapter;
|
||||
|
||||
public LinkCommands(LinkCliAdapter linkCliAdapter) {
|
||||
this.linkCliAdapter = linkCliAdapter;
|
||||
commands.put("add", this::addLink);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSubcommand() {
|
||||
return "link";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "Usage: " + System.lineSeparator() +
|
||||
getSubcommand() + "add http://example.org yourUsername [category1 category2 .. categoryN]";
|
||||
}
|
||||
|
||||
private String addLink(String[] args) {
|
||||
|
||||
linkCliAdapter.addLink(args[1],
|
||||
Arrays.stream(Arrays.copyOfRange(args, 3, args.length)).collect(Collectors.toSet()),
|
||||
args[2]);
|
||||
return "Added the new Link";
|
||||
}
|
||||
}
|
34
0-Plugin/src/main/java/cli/tag/TagCommands.java
Normal file
34
0-Plugin/src/main/java/cli/tag/TagCommands.java
Normal file
|
@ -0,0 +1,34 @@
|
|||
package cli.tag;
|
||||
|
||||
import cli.Subcommand;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class TagCommands extends Subcommand {
|
||||
|
||||
final private CustomTagsCliAdapter customTagsCliAdapter;
|
||||
|
||||
final private HashMap<String, Function<String[], String>> commands = new HashMap<>();
|
||||
|
||||
public TagCommands(CustomTagsCliAdapter customTagsCliAdapter) {
|
||||
this.customTagsCliAdapter = customTagsCliAdapter;
|
||||
commands.put("add", this::addCustomTag);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getSubcommand() {
|
||||
return "tag";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getUsage() {
|
||||
return "Usage: " + System.lineSeparator()+
|
||||
getSubcommand() + "add tagName tagRegexExpression";
|
||||
}
|
||||
|
||||
private String addCustomTag(String[] args) {
|
||||
customTagsCliAdapter.addCustomTagMatcher(args[1], args[2]);
|
||||
return "Added the new Tag matcher";
|
||||
}
|
||||
}
|
63
0-Plugin/src/test/java/cli/link/LinkCommandsTest.java
Normal file
63
0-Plugin/src/test/java/cli/link/LinkCommandsTest.java
Normal file
|
@ -0,0 +1,63 @@
|
|||
package cli.link;
|
||||
|
||||
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;
|
||||
|
||||
class LinkCommandsTest {
|
||||
|
||||
LinkCliAdapter mockAdapter = mock(LinkCliAdapter.class);
|
||||
|
||||
@Test
|
||||
void addCommandWorks() {
|
||||
var url = "http://tea.filefighter.de";
|
||||
var username = "mario";
|
||||
var category1 = "funStuff";
|
||||
var category2 = "workStuff";
|
||||
|
||||
ArgumentCaptor<String> captureUrl = ArgumentCaptor.forClass(String.class);
|
||||
ArgumentCaptor<String> captureUsername = ArgumentCaptor.forClass(String.class);
|
||||
ArgumentCaptor<Set<String>> captureCategories = ArgumentCaptor.forClass(Set.class);
|
||||
|
||||
doNothing()
|
||||
.when(mockAdapter)
|
||||
.addLink(captureUrl.capture(), captureCategories.capture(), captureUsername.capture());
|
||||
|
||||
var sut = new LinkCommands(mockAdapter);
|
||||
var returnValue = sut.executeSubcommand(new String[]{"add", url, username, category1, category2});
|
||||
|
||||
assertEquals("Added the new Link", returnValue);
|
||||
|
||||
assertEquals(url, captureUrl.getValue());
|
||||
assertEquals(username, captureUsername.getValue());
|
||||
assertEquals(Set.of(category1, category2), captureCategories.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addCommandWorksNocategories() {
|
||||
var url = "http://tea.filefighter.de";
|
||||
var username = "mario";
|
||||
|
||||
ArgumentCaptor<String> captureUrl = ArgumentCaptor.forClass(String.class);
|
||||
ArgumentCaptor<String> captureUsername = ArgumentCaptor.forClass(String.class);
|
||||
ArgumentCaptor<Set<String>> captureCategories = ArgumentCaptor.forClass(Set.class);
|
||||
|
||||
doNothing()
|
||||
.when(mockAdapter)
|
||||
.addLink(captureUrl.capture(), captureCategories.capture(), captureUsername.capture());
|
||||
|
||||
var sut = new LinkCommands(mockAdapter);
|
||||
var returnValue = sut.executeSubcommand(new String[]{"add", url, username});
|
||||
|
||||
assertEquals("Added the new Link", returnValue);
|
||||
|
||||
assertEquals(url, captureUrl.getValue());
|
||||
assertEquals(username, captureUsername.getValue());
|
||||
assertEquals(Set.of(), captureCategories.getValue());
|
||||
}
|
||||
}
|
|
@ -14,7 +14,6 @@ public class CustomTagsCliAdapter {
|
|||
|
||||
public void addCustomTagMatcher(String name, String regexString) {
|
||||
customTagsUseCase.addCustomTagMatcher(new CustomTagMatcher(new TagName(name), regexString));
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue