Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Sequence Diagrams and fix JUnit test #207

Merged
merged 1 commit into from
Nov 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/attachments/ActiveRecallSequenceDiagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions docs/uml/ActiveRecallSequenceDiagram.puml
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ activate GameEnvironment
ref over GameEnvironment, DifficultyModifier, Question : updateQuestionDifficulty
|||
end
destroy QuestionGenerator
return

return CommandResult(true)
destroy GameEnvironment

@enduml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ endbox
box "Active Recall" #lightblue
participant ":GameEnvironment" as GameEnvironment #lightskyblue
participant ":QuestionGenerator" as QuestionGenerator #lightskyblue
participant ":DifficultyModifier" as DifficultyModifier #lightskyblue
participant ":Question" as Question #lightskyblue
endbox

-> TestCommand : execute(moduleManager)
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/terminus/Terminus.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public class Terminus {

private ModuleManager moduleManager;
private StorageManager storageManager;
private Path dataDirectory;

private static final Path DATA_DIRECTORY = Path.of(System.getProperty("user.dir"), "data");
private static final String MAIN_JSON = "main.json";
Expand All @@ -36,12 +37,13 @@ public static void main(String[] args) {
}

Terminus() {
this(Ui.getInstance(), MainCommandParser.getInstance());
this(Ui.getInstance(), MainCommandParser.getInstance(), DATA_DIRECTORY);
}

Terminus(Ui ui, CommandParser parser) {
Terminus(Ui ui, CommandParser parser, Path dataDirectory) {
this.ui = ui;
this.parser = parser;
this.dataDirectory = dataDirectory;
}

/**
Expand All @@ -58,7 +60,7 @@ void initialize() {
TerminusLogger.initializeLogger();
TerminusLogger.info("Starting Terminus...");
this.workspace = "";
this.storageManager = new StorageManager(DATA_DIRECTORY, MAIN_JSON);
this.storageManager = new StorageManager(dataDirectory, MAIN_JSON);
this.moduleManager = this.storageManager.initialize();
} catch (IOException e) {
TerminusLogger.warning("Log file loading has failed.", e.fillInStackTrace());
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/terminus/activerecall/GameEnvironment.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private int getUserFeedback() {
difficulty = Integer.parseInt(input);

} while (difficulty == INVALID_DIFFICULTY);
assert difficulty <= HARD_DIFFICULTY && difficulty >= -EASY_DIFFICULTY;
assert difficulty <= HARD_DIFFICULTY && difficulty >= EXIT_CODE;
return difficulty;
}

Expand Down
40 changes: 33 additions & 7 deletions src/test/java/terminus/TerminusTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.stream.Stream;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import terminus.parser.MainCommandParser;
import terminus.ui.Ui;
Expand All @@ -26,6 +34,20 @@ static void afterAll() {
System.setIn(oldInput);
}

@BeforeEach
void setUp() {
TestFilePath.RESOURCE_DATA_FOLDER.toFile().mkdir();
}

@AfterEach
void tearDown() throws IOException {
try (Stream<Path> walk = Files.walk(TestFilePath.RESOURCE_DATA_FOLDER)) {
walk.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}

@Test
void run_exit_success() {
String input = String.format("exit%s", LS);
Expand All @@ -38,7 +60,8 @@ void run_exit_success() {
void run_invalidCommand_exit_success() {
String input = String.format("exita%shelp%sexit%s", LS, LS, LS);
InputStream in = new ByteArrayInputStream(input.getBytes());
Terminus terminus = new Terminus(new Ui(in), MainCommandParser.getInstance());
Terminus terminus = new Terminus(new Ui(in), MainCommandParser.getInstance(),
TestFilePath.RESOURCE_DATA_FOLDER);
terminus.run();
}

Expand All @@ -51,23 +74,26 @@ void safety_test() {

@Test
void handleUserInput_success() {
Terminus terminus = new Terminus();
Terminus terminus = new Terminus(new Ui(System.in), MainCommandParser.getInstance(),
TestFilePath.RESOURCE_DATA_FOLDER);
terminus.initialize();
assertTrue(terminus.handleUserInput("exit").isExit());
}

@Test
void handleUserInput_exceptionThrown_returnNull() {
Terminus terminus = new Terminus();
Terminus terminus = new Terminus(new Ui(System.in), MainCommandParser.getInstance(),
TestFilePath.RESOURCE_DATA_FOLDER);
terminus.initialize();
assertNull(terminus.handleUserInput("exita"));
assertNull(terminus.handleUserInput("module add"));
assertNull(terminus.handleUserInput("module delete 10"));
assertNull(terminus.handleUserInput("module delete " + Integer.MAX_VALUE));
}

@Test
void handleCommandResult_success() {
Terminus terminus = new Terminus();
Terminus terminus = new Terminus(new Ui(System.in), MainCommandParser.getInstance(),
TestFilePath.RESOURCE_DATA_FOLDER);
terminus.initialize();
terminus.handleCommandResult(terminus.handleUserInput("module add \"test\""));
terminus.handleCommandResult(terminus.handleUserInput("go test"));
Expand All @@ -83,10 +109,10 @@ void handleCommandResult_success() {

@Test
void handleStorage_success() {
Terminus terminus = new Terminus();
Terminus terminus = new Terminus(new Ui(System.in), MainCommandParser.getInstance(),
TestFilePath.RESOURCE_DATA_FOLDER);
terminus.initialize();
terminus.handleStorage(terminus.handleUserInput("module add \"test\""));
terminus.handleStorage(terminus.handleUserInput("module delete 1"));
}

}
1 change: 1 addition & 0 deletions src/test/java/terminus/TestFilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public class TestFilePath {

public static final Path RESOURCE_FOLDER = Path.of("src", "test", "resources");
public static final Path RESOURCE_DATA_FOLDER = RESOURCE_FOLDER.resolve("data");
public static final String FAULTY_FOLDER = "faulty";
public static final String SAVE_FILE = "saveFile.json";
public static final String MALFORMED_FILE = "malformedFile.json";
Expand Down