From 68eb7d4f494338205aa5809342cdf0a60451664d Mon Sep 17 00:00:00 2001 From: LouisLouis19 Date: Tue, 12 Oct 2021 21:08:31 +0800 Subject: [PATCH 1/2] Add JUnit test for view link command --- .../command/link/DeleteLinkCommandTest.java | 2 +- .../command/link/ViewLinkCommandTest.java | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 src/test/java/terminus/command/link/ViewLinkCommandTest.java diff --git a/src/test/java/terminus/command/link/DeleteLinkCommandTest.java b/src/test/java/terminus/command/link/DeleteLinkCommandTest.java index 58d15b4fab..685f5baf58 100644 --- a/src/test/java/terminus/command/link/DeleteLinkCommandTest.java +++ b/src/test/java/terminus/command/link/DeleteLinkCommandTest.java @@ -31,7 +31,7 @@ void setUp() { } @Test - void execute_success() throws InvalidCommandException, InvalidArgumentException { + void execute_deleteLink_success() throws InvalidCommandException, InvalidArgumentException { for (int i = 0; i < 3; i++) { Command addLinkCommand = linkCommandParser.parseCommand("add \"test_desc\" \"Monday\" \"12:00\" \"https://zoom.us/test\""); CommandResult addResult = addLinkCommand.execute(ui, nusModule); diff --git a/src/test/java/terminus/command/link/ViewLinkCommandTest.java b/src/test/java/terminus/command/link/ViewLinkCommandTest.java new file mode 100644 index 0000000000..855bc34989 --- /dev/null +++ b/src/test/java/terminus/command/link/ViewLinkCommandTest.java @@ -0,0 +1,81 @@ +package terminus.command.link; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import terminus.command.Command; +import terminus.command.CommandResult; +import terminus.content.Link; +import terminus.exception.InvalidArgumentException; +import terminus.exception.InvalidCommandException; +import terminus.module.NusModule; +import terminus.parser.LinkCommandParser; +import terminus.ui.Ui; + +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class ViewLinkCommandTest { + + private LinkCommandParser linkCommandParser; + private NusModule nusModule; + private Ui ui; + + Class type = Link.class; + + @BeforeEach + void setUp() { + this.linkCommandParser = LinkCommandParser.getInstance(); + this.nusModule = new NusModule(); + this.ui = new Ui(); + } + + @Test + void execute_viewAll_success() throws InvalidCommandException, InvalidArgumentException { + for (int i = 0; i < 5; i++) { + Command addLinkCommand = linkCommandParser.parseCommand( + "add \"test\" \"Saturday\" \"00:00\" \"https://zoom.us/test\""); + CommandResult addLinkResult = addLinkCommand.execute(ui, nusModule); + assertTrue(addLinkResult.isOk()); + } + assertEquals(5, nusModule.getContentManager(type).getTotalContents()); + + Command viewLinkCommand = linkCommandParser.parseCommand("view"); + CommandResult viewLinkResult = viewLinkCommand.execute(ui, nusModule); + assertTrue(viewLinkResult.isOk()); + } + + @Test + void execute_viewLink_success() throws InvalidCommandException, InvalidArgumentException { + for (int i = 0; i < 5; i++) { + Command addLinkCommand = linkCommandParser.parseCommand( + "add \"test\" \"Saturday\" \"00:00\" \"https://zoom.us/test\""); + CommandResult addLinkResult = addLinkCommand.execute(ui, nusModule); + assertTrue(addLinkResult.isOk()); + } + assertEquals(5, nusModule.getContentManager(type).getTotalContents()); + + Command viewLinkCommand = linkCommandParser.parseCommand("view 1"); + CommandResult viewLinkResult = viewLinkCommand.execute(ui, nusModule); + assertTrue(viewLinkResult.isOk()); + + viewLinkCommand = linkCommandParser.parseCommand("view 5"); + viewLinkResult = viewLinkCommand.execute(ui, nusModule); + assertTrue(viewLinkResult.isOk()); + } + + @Test + void execute_viewLink_exceptionThrown() throws InvalidCommandException, InvalidArgumentException { + for (int i = 0; i < 5; i++) { + Command addLinkCommand = linkCommandParser.parseCommand( + "add \"test\" \"Saturday\" \"00:00\" \"https://zoom.us/test\""); + CommandResult addLinkResult = addLinkCommand.execute(ui, nusModule); + assertTrue(addLinkResult.isOk()); + } + assertEquals(5, nusModule.getContentManager(type).getTotalContents()); + + assertThrows(InvalidArgumentException.class, () -> linkCommandParser.parseCommand("view -1")); + assertThrows(InvalidArgumentException.class, () -> linkCommandParser.parseCommand("view X")); + assertThrows(InvalidCommandException.class, () -> linkCommandParser.parseCommand("viewwwww")); + } +} \ No newline at end of file From 81235a600a79bda37f2b2b80551bfb7841080ace Mon Sep 17 00:00:00 2001 From: LouisLouis19 Date: Tue, 12 Oct 2021 21:11:35 +0800 Subject: [PATCH 2/2] Rename Tests --- src/test/java/terminus/command/link/DeleteLinkCommandTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/terminus/command/link/DeleteLinkCommandTest.java b/src/test/java/terminus/command/link/DeleteLinkCommandTest.java index 685f5baf58..575bdabe79 100644 --- a/src/test/java/terminus/command/link/DeleteLinkCommandTest.java +++ b/src/test/java/terminus/command/link/DeleteLinkCommandTest.java @@ -54,7 +54,7 @@ void execute_deleteLink_success() throws InvalidCommandException, InvalidArgumen } @Test - void execute_throwsException() throws InvalidCommandException, InvalidArgumentException { + void execute_deleteLink_throwsException() throws InvalidCommandException, InvalidArgumentException { Command deleteLinkCommand = linkCommandParser.parseCommand("delete 20"); assertThrows(InvalidArgumentException.class, () -> deleteLinkCommand.execute(ui, nusModule)); }