Refactoring GitDBTest

This commit is contained in:
Paul Campbell 2018-06-25 21:51:08 +01:00
parent f5483f419b
commit 2de83ba3bc

View file

@ -13,38 +13,23 @@ import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.file.DirectoryNotEmptyException; import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.NotDirectoryException; import java.nio.file.NotDirectoryException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.UUID; import java.util.UUID;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier; import java.util.function.Supplier;
@ExtendWith(MockitoExtension.class)
class GitDBTest implements WithAssertions { class GitDBTest implements WithAssertions {
private final Supplier<String> stringSupplier = UUID.randomUUID()::toString; private final Supplier<String> stringSupplier = UUID.randomUUID()::toString;
private final String userName = stringSupplier.get(); private final String userName = stringSupplier.get();
private final String userEmailAddress = stringSupplier.get(); private final String userEmailAddress = stringSupplier.get();
private static void tree(final Path dbDir, final PrintStream out) throws IOException {
final Process treeProcess = new ProcessBuilder("tree", dbDir.toString()).start();
try (final BufferedReader reader = new BufferedReader(new InputStreamReader(treeProcess.getInputStream()))) {
String line;
while (null != (line = reader.readLine())) {
out.println("line = " + line);
}
}
}
// When initialising a repo in a dir that doesn't exist then a bare repo is created // When initialising a repo in a dir that doesn't exist then a bare repo is created
@Test @Test
void initRepo_whenDirNotExist_thenCreateBareRepo() throws IOException { void initRepo_whenDirNotExist_thenCreateBareRepo() throws IOException {
@ -53,7 +38,7 @@ class GitDBTest implements WithAssertions {
//when //when
final Result<GitDB> gitDB = GitDB.initLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDB = GitDB.initLocal(dir, userName, userEmailAddress);
//then //then
assertThat(gitDB.isOkay()).isTrue(); assertThatResultIsOkay(gitDB);
assertThatIsBareRepo(dir); assertThatIsBareRepo(dir);
} }
@ -63,6 +48,10 @@ class GitDBTest implements WithAssertions {
return directory; return directory;
} }
private <T> void assertThatResultIsOkay(final Result<T> result) {
assertThat(result.isOkay()).isTrue();
}
private void assertThatIsBareRepo(final Path dbDir) throws IOException { private void assertThatIsBareRepo(final Path dbDir) throws IOException {
final Git git = Git.open(dbDir.toFile()); final Git git = Git.open(dbDir.toFile());
final Repository repository = git.getRepository(); final Repository repository = git.getRepository();
@ -128,7 +117,7 @@ class GitDBTest implements WithAssertions {
//when //when
final Result<GitDB> gitDB = GitDB.initLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDB = GitDB.initLocal(dir, userName, userEmailAddress);
//then //then
assertThat(gitDB.isOkay()).isTrue(); assertThatResultIsOkay(gitDB);
assertThatIsBareRepo(dir); assertThatIsBareRepo(dir);
} }
@ -234,7 +223,7 @@ class GitDBTest implements WithAssertions {
//given //given
final Result<GitDB> gitDb = gitDB(dirDoesNotExist()); final Result<GitDB> gitDb = gitDB(dirDoesNotExist());
//when //when
final Result<Maybe<GitDBBranch>> branch = gitDb.flatMap(db -> db.branch("unknown")); final Result<Maybe<GitDBBranch>> branch = gitDb.flatMap(selectBranch("unknown"));
//then //then
assertThat(branch.orElseThrow().toOptional()).isEmpty(); assertThat(branch.orElseThrow().toOptional()).isEmpty();
} }
@ -243,6 +232,12 @@ class GitDBTest implements WithAssertions {
return GitDB.initLocal(dbDir, userName, userEmailAddress); return GitDB.initLocal(dbDir, userName, userEmailAddress);
} }
private Function<GitDB, Result<Maybe<GitDBBranch>>> selectBranch(final String branchName) {
return db -> db.branch(branchName);
}
// Given a valid GitDbBranch handle
// When select a valid branch then a GitDbBranch is returned // When select a valid branch then a GitDbBranch is returned
@Test @Test
void selectBranch_branchExists_thenReturnBranch() throws Throwable { void selectBranch_branchExists_thenReturnBranch() throws Throwable {
@ -250,13 +245,11 @@ class GitDBTest implements WithAssertions {
final Path dbDir = dirDoesNotExist(); final Path dbDir = dirDoesNotExist();
final Result<GitDB> gitDb = gitDB(dbDir); final Result<GitDB> gitDb = gitDB(dbDir);
//when //when
final Result<Maybe<GitDBBranch>> branch = gitDb.flatMap(db -> db.branch("master")); final Result<Maybe<GitDBBranch>> branch = gitDb.flatMap(selectBranch("master"));
//then //then
assertThat(branch.orElseThrow().toOptional()).as("Branch master exists").isNotEmpty(); assertThat(branch.orElseThrow().toOptional()).as("Branch master exists").isNotEmpty();
} }
// Given a valid GitDbBranch handle
// When getting a key that does not exist then return an empty Optional // When getting a key that does not exist then return an empty Optional
@Test @Test
void getKey_whenKeyNotExist_thenReturnEmptyOptional() throws IOException { void getKey_whenKeyNotExist_thenReturnEmptyOptional() throws IOException {
@ -273,18 +266,9 @@ class GitDBTest implements WithAssertions {
private GitDBBranch gitDBBranch() { private GitDBBranch gitDBBranch() {
try { try {
final Result<GitDB> gitDBResult = gitDB(dirDoesNotExist()); return gitDB(dirDoesNotExist())
System.out.println("gitDBResult = " + gitDBResult); .flatMap(selectBranch("master"))
final Result<Maybe<GitDBBranch>> master = gitDBResult.flatMap(db -> { .orElseThrow().orElse(null);
final Result<Maybe<GitDBBranch>> maybeResult = db.branch("master");
System.out.println("maybeResult = " + maybeResult);
return maybeResult;
});
assert master != null;
System.out.println("master = " + master);
final Maybe<GitDBBranch> gitDBBranchMaybe = master.orElseThrow();
final GitDBBranch gitDBBranch = gitDBBranchMaybe.orElse(null);
return gitDBBranch;
} catch (Throwable throwable) { } catch (Throwable throwable) {
throw new RuntimeException("Couldn't create master branch", throwable); throw new RuntimeException("Couldn't create master branch", throwable);
} }
@ -321,8 +305,6 @@ class GitDBTest implements WithAssertions {
success -> assertThat(success).isNotNull().isNotSameAs(originalBranch), success -> assertThat(success).isNotNull().isNotSameAs(originalBranch),
failOnError() failOnError()
); );
assertThat(updatedBranch).isNotNull()
.isNotSameAs(originalBranch);
} }
// When getting a key that does exist then the value is returned inside an Optional // When getting a key that does exist then the value is returned inside an Optional