GitDB returns Result<T> values

This commit is contained in:
Paul Campbell 2018-06-24 09:16:05 +01:00
parent 6a3687466f
commit 193a0f380b
5 changed files with 175 additions and 117 deletions

View file

@ -22,10 +22,10 @@
package net.kemitix.gitdb; package net.kemitix.gitdb;
import net.kemitix.gitdb.impl.LocalGitDB; import net.kemitix.gitdb.impl.LocalGitDB;
import net.kemitix.mon.maybe.Maybe;
import net.kemitix.mon.result.Result;
import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Optional;
/** /**
* Main API for connecting to a Git repo as a database. * Main API for connecting to a Git repo as a database.
@ -41,13 +41,12 @@ public interface GitDB {
* @param userName the user name * @param userName the user name
* @param userEmailAddress the user email address * @param userEmailAddress the user email address
* @return a GitDB instance for the created local gitdb * @return a GitDB instance for the created local gitdb
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
*/ */
static GitDB initLocal( static Result<GitDB> initLocal(
final Path dbDir, final Path dbDir,
final String userName, final String userName,
final String userEmailAddress final String userEmailAddress
) throws IOException { ) {
return LocalGitDB.init(dbDir, userName, userEmailAddress); return LocalGitDB.init(dbDir, userName, userEmailAddress);
} }
@ -59,7 +58,7 @@ public interface GitDB {
* @param userEmailAddress the user email address * @param userEmailAddress the user email address
* @return a GitDB instance for the local gitdb * @return a GitDB instance for the local gitdb
*/ */
static GitDB openLocal(final Path dbDir, final String userName, final String userEmailAddress) { static Result<GitDB> openLocal(final Path dbDir, final String userName, final String userEmailAddress) {
return LocalGitDB.open(dbDir, userName, userEmailAddress); return LocalGitDB.open(dbDir, userName, userEmailAddress);
} }
@ -67,9 +66,8 @@ public interface GitDB {
* Select the named branch. * Select the named branch.
* *
* @param name the branch to select * @param name the branch to select
* @return an Optional containing the branch if it exists * @return an Result Maybe containing the branch if it exists
* @throws IOException if there is an error accessing the branch name
*/ */
Optional<GitDBBranch> branch(String name) throws IOException; Result<Maybe<GitDBBranch>> branch(String name);
} }

View file

@ -22,6 +22,7 @@
package net.kemitix.gitdb.impl; package net.kemitix.gitdb.impl;
import net.kemitix.gitdb.FormatVersion; import net.kemitix.gitdb.FormatVersion;
import net.kemitix.mon.result.Result;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.lib.RepositoryCache; import org.eclipse.jgit.lib.RepositoryCache;
@ -52,14 +53,30 @@ class InitGitDBRepo {
* @param dbDir the directory to initialise the repo in * @param dbDir the directory to initialise the repo in
* @throws IOException if there is an error in creating the repo files * @throws IOException if there is an error in creating the repo files
*/ */
static void create(final Path dbDir) throws IOException { static Result<Void> create(final Path dbDir) {
final InitGitDBRepo initRepo = new InitGitDBRepo(); final InitGitDBRepo initRepo = new InitGitDBRepo();
final File validDbDir = initRepo.validDbDir(dbDir.toFile()); final File validDbDir;
try {
validDbDir = initRepo.validDbDir(dbDir.toFile());
} catch (IOException e) {
return Result.error(e);
}
validDbDir.mkdirs(); validDbDir.mkdirs();
try (Repository repository = RepositoryCache.FileKey.exact(validDbDir, FS.DETECTED).open(false)) { try (Repository repository = RepositoryCache.FileKey.exact(validDbDir, FS.DETECTED).open(false)) {
repository.create(true); repository.create(true);
initRepo.createInitialBranchOnMaster(repository); initRepo.createInitialBranchOnMaster(repository);
} catch (IOException e) {
return Result.error(e);
} }
return Result.ok(null);
}
private File validDbDir(final File dbDir) throws IOException {
verifyIsNotAFile(dbDir);
if (dbDir.exists()) {
verifyIsEmpty(dbDir);
}
return dbDir;
} }
private void createInitialBranchOnMaster(final Repository repository) throws IOException { private void createInitialBranchOnMaster(final Repository repository) throws IOException {
@ -71,6 +88,20 @@ class InitGitDBRepo {
createBranch(repository, commitId, MASTER); createBranch(repository, commitId, MASTER);
} }
private void verifyIsNotAFile(final File dbDir) throws NotDirectoryException {
if (dbDir.isFile()) {
throw new NotDirectoryException(dbDir.toString());
}
}
private void verifyIsEmpty(final File dbDir) throws IOException {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dbDir.toPath())) {
if (directoryStream.iterator().hasNext()) {
throw new DirectoryNotEmptyException(dbDir.toString());
}
}
}
private void createBranch( private void createBranch(
final Repository repository, final Repository repository,
final ObjectId commitId, final ObjectId commitId,
@ -90,26 +121,4 @@ class InitGitDBRepo {
.resolve(String.format(REFS_HEADS_FORMAT, branchName)) .resolve(String.format(REFS_HEADS_FORMAT, branchName))
.toAbsolutePath(); .toAbsolutePath();
} }
private File validDbDir(final File dbDir) throws IOException {
verifyIsNotAFile(dbDir);
if (dbDir.exists()) {
verifyIsEmpty(dbDir);
}
return dbDir;
}
private void verifyIsEmpty(final File dbDir) throws IOException {
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dbDir.toPath())) {
if (directoryStream.iterator().hasNext()) {
throw new DirectoryNotEmptyException(dbDir.toString());
}
}
}
private void verifyIsNotAFile(final File dbDir) throws NotDirectoryException {
if (dbDir.isFile()) {
throw new NotDirectoryException(dbDir.toString());
}
}
} }

View file

@ -22,8 +22,8 @@
package net.kemitix.gitdb.impl; package net.kemitix.gitdb.impl;
import net.kemitix.gitdb.GitDB; import net.kemitix.gitdb.GitDB;
import net.kemitix.mon.result.Result;
import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
/** /**
@ -40,9 +40,8 @@ public interface LocalGitDB extends GitDB {
* @param userName the user name * @param userName the user name
* @param userEmailAddress the user email address * @param userEmailAddress the user email address
* @return a GitDB instance for the created local gitdb * @return a GitDB instance for the created local gitdb
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
*/ */
static GitDB init(final Path dbDir, final String userName, final String userEmailAddress) throws IOException { static Result<GitDB> init(final Path dbDir, final String userName, final String userEmailAddress) {
return LocalGitDBImpl.init(dbDir, userName, userEmailAddress); return LocalGitDBImpl.init(dbDir, userName, userEmailAddress);
} }
@ -54,7 +53,7 @@ public interface LocalGitDB extends GitDB {
* @param userEmailAddress the user email address * @param userEmailAddress the user email address
* @return a GitDB instance for the created local gitdb * @return a GitDB instance for the created local gitdb
*/ */
static GitDB open(final Path dbDir, final String userName, final String userEmailAddress) { static Result<GitDB> open(final Path dbDir, final String userName, final String userEmailAddress) {
return LocalGitDBImpl.open(dbDir, userName, userEmailAddress); return LocalGitDBImpl.open(dbDir, userName, userEmailAddress);
} }

View file

@ -24,12 +24,14 @@ package net.kemitix.gitdb.impl;
import net.kemitix.gitdb.GitDB; import net.kemitix.gitdb.GitDB;
import net.kemitix.gitdb.GitDBBranch; import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.gitdb.InvalidRepositoryException; import net.kemitix.gitdb.InvalidRepositoryException;
import net.kemitix.mon.maybe.Maybe;
import net.kemitix.mon.result.Result;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.*; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
/** /**
@ -43,8 +45,6 @@ final class LocalGitDBImpl implements GitDB, LocalGitDB {
private static final String ERROR_OPENING_REPOSITORY = "Error opening repository"; private static final String ERROR_OPENING_REPOSITORY = "Error opening repository";
private final Repository repository; private final Repository repository;
private final String userName;
private final String userEmailAddress;
private final Function<Ref, GitDBBranch> branchInit; private final Function<Ref, GitDBBranch> branchInit;
@ -54,9 +54,7 @@ final class LocalGitDBImpl implements GitDB, LocalGitDB {
final String userEmailAddress final String userEmailAddress
) { ) {
this.repository = repository; this.repository = repository;
this.userName = userName; branchInit = GitDBBranchImpl.init(this.repository, userName, userEmailAddress);
this.userEmailAddress = userEmailAddress;
branchInit = GitDBBranchImpl.init(this.repository, this.userName, this.userEmailAddress);
} }
/** /**
@ -66,15 +64,14 @@ final class LocalGitDBImpl implements GitDB, LocalGitDB {
* @param userName the user name * @param userName the user name
* @param userEmailAddress the user email address * @param userEmailAddress the user email address
* @return a GitDB instance for the created local gitdb * @return a GitDB instance for the created local gitdb
* @throws IOException if there {@code dbDir} is a file or a non-empty directory
*/ */
static GitDB init( static Result<GitDB> init(
final Path dbDir, final Path dbDir,
final String userName, final String userName,
final String userEmailAddress final String userEmailAddress
) throws IOException { ) {
InitGitDBRepo.create(dbDir); return InitGitDBRepo.create(dbDir)
return open(dbDir, userName, userEmailAddress); .flatMap(c -> open(dbDir, userName, userEmailAddress));
} }
/** /**
@ -85,25 +82,41 @@ final class LocalGitDBImpl implements GitDB, LocalGitDB {
* @param userEmailAddress the user email address * @param userEmailAddress the user email address
* @return a GitDB instance for the created local gitdb * @return a GitDB instance for the created local gitdb
*/ */
static GitDB open( static Result<GitDB> open(
final Path dbDir, final Path dbDir,
final String userName, final String userName,
final String userEmailAddress final String userEmailAddress
) { ) {
try { return gitOpen(dbDir)
return Optional.of(Git.open(dbDir.toFile()))
.map(Git::getRepository) .map(Git::getRepository)
.filter(Repository::isBare) .maybe(Repository::isBare)
.map(repository -> new LocalGitDBImpl(repository, userName, userEmailAddress)) .flatMap(asErrorIfNotBare(dbDir))
.orElseThrow(() -> new InvalidRepositoryException(NOT_A_BARE_REPO, dbDir)); .map(toLocalGitDB(userName, userEmailAddress));
} catch (IOException e) {
throw new InvalidRepositoryException(ERROR_OPENING_REPOSITORY, dbDir, e);
} }
private static Result<Git> gitOpen(Path dbDir) {
try {
return Result.ok(Git.open(dbDir.toFile()));
} catch (IOException e) {
return Result.error(new InvalidRepositoryException(ERROR_OPENING_REPOSITORY, dbDir, e));
}
}
private static Function<Maybe<Repository>, Result<Repository>> asErrorIfNotBare(final Path dbDir) {
return maybe -> Result.fromMaybe(maybe, () -> new InvalidRepositoryException(NOT_A_BARE_REPO, dbDir));
}
private static Function<Repository, GitDB> toLocalGitDB(final String userName, final String userEmailAddress) {
return repository -> new LocalGitDBImpl(repository, userName, userEmailAddress);
} }
@Override @Override
public Optional<GitDBBranch> branch(final String name) throws IOException { public Result<Maybe<GitDBBranch>> branch(final String name) {
return Optional.ofNullable(repository.findRef(name)).map(branchInit); try {
return Result.ok(Maybe.maybe(repository.findRef(name)).map(branchInit));
} catch (IOException e) {
return Result.error(e);
}
} }
} }

View file

@ -5,6 +5,8 @@ import net.kemitix.gitdb.FormatVersion;
import net.kemitix.gitdb.GitDB; import net.kemitix.gitdb.GitDB;
import net.kemitix.gitdb.GitDBBranch; import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.gitdb.InvalidRepositoryException; import net.kemitix.gitdb.InvalidRepositoryException;
import net.kemitix.mon.maybe.Maybe;
import net.kemitix.mon.result.Result;
import org.assertj.core.api.WithAssertions; import org.assertj.core.api.WithAssertions;
import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.api.errors.GitAPIException;
@ -26,8 +28,6 @@ import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.function.Supplier; import java.util.function.Supplier;
import static org.assertj.core.api.Assumptions.assumeThat;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
class GitDBTest implements WithAssertions { class GitDBTest implements WithAssertions {
@ -51,12 +51,18 @@ class GitDBTest implements WithAssertions {
//given //given
final Path dir = dirDoesNotExist(); final Path dir = dirDoesNotExist();
//when //when
final GitDB gitDB = GitDB.initLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDB = GitDB.initLocal(dir, userName, userEmailAddress);
//then //then
assertThat(gitDB).isNotNull(); assertThat(gitDB.isOkay()).isTrue();
assertThatIsBareRepo(dir); assertThatIsBareRepo(dir);
} }
private Path dirDoesNotExist() throws IOException {
final Path directory = Files.createTempDirectory("gitdb");
Files.delete(directory);
return directory;
}
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();
@ -65,21 +71,20 @@ class GitDBTest implements WithAssertions {
assertThat(repository.getDirectory()).isEqualTo(dbDir.toFile()); assertThat(repository.getDirectory()).isEqualTo(dbDir.toFile());
} }
private Path dirDoesNotExist() throws IOException {
final Path directory = Files.createTempDirectory("gitdb");
Files.delete(directory);
return directory;
}
// When initialising a repo in a dir that is a file then an exception is thrown // When initialising a repo in a dir that is a file then an exception is thrown
@Test @Test
void initRepo_whenDirIsFile_thenThrowException() throws IOException { void initRepo_whenDirIsFile_thenThrowException() throws IOException {
//given //given
final Path dir = fileExists(); final Path dir = fileExists();
//when
final Result<GitDB> gitDBResult = GitDB.initLocal(dir, userName, userEmailAddress);
//then //then
assertThatExceptionOfType(NotDirectoryException.class) gitDBResult.match(
.isThrownBy(() -> GitDB.initLocal(dir, userName, userEmailAddress)) success -> fail("Is a file not a directory"),
.withMessageContaining(dir.toString()); error -> assertThat(error)
.isInstanceOf(NotDirectoryException.class)
.hasMessageContaining(dir.toString())
);
} }
private Path fileExists() throws IOException { private Path fileExists() throws IOException {
@ -92,29 +97,34 @@ class GitDBTest implements WithAssertions {
//given //given
final Path dir = dirExists(); final Path dir = dirExists();
filesExistIn(dir); filesExistIn(dir);
//when
final Result<GitDB> gitDBResult = GitDB.initLocal(dir, userName, userEmailAddress);
//then //then
assertThatExceptionOfType(DirectoryNotEmptyException.class) gitDBResult.match(
.isThrownBy(() -> GitDB.initLocal(dir, userName, userEmailAddress)) success -> fail("Directory is not empty"),
.withMessageContaining(dir.toString()); error -> assertThat(error)
} .isInstanceOf(DirectoryNotEmptyException.class)
.hasMessageContaining(dir.toString())
private void filesExistIn(final Path dir) throws IOException { );
Files.createTempFile(dir, "gitdb", "file");
} }
private Path dirExists() throws IOException { private Path dirExists() throws IOException {
return Files.createTempDirectory("gitdb"); return Files.createTempDirectory("gitdb");
} }
private void filesExistIn(final Path dir) throws IOException {
Files.createTempFile(dir, "gitdb", "file");
}
// When initialising a repo in a empty dir then a bare repo is created // When initialising a repo in a empty dir then a bare repo is created
@Test @Test
void initRepo_whenEmptyDir_thenCreateBareRepo() throws IOException { void initRepo_whenEmptyDir_thenCreateBareRepo() throws IOException {
//given //given
final Path dir = dirExists(); final Path dir = dirExists();
//when //when
final GitDB gitDB = GitDB.initLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDB = GitDB.initLocal(dir, userName, userEmailAddress);
//then //then
assertThat(gitDB).isNotNull(); assertThat(gitDB.isOkay()).isTrue();
assertThatIsBareRepo(dir); assertThatIsBareRepo(dir);
} }
@ -123,10 +133,16 @@ class GitDBTest implements WithAssertions {
void openRepo_NotBareRepo_thenThrowException() throws IOException { void openRepo_NotBareRepo_thenThrowException() throws IOException {
//given //given
final Path dir = dirExists(); final Path dir = dirExists();
//when
final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress);
//then //then
assertThatExceptionOfType(InvalidRepositoryException.class) gitDBResult.match(
.isThrownBy(() -> GitDB.openLocal(dir, userName, userEmailAddress)) success -> fail("Not a bare repo"),
.withMessageContaining(dir.toString()); error -> assertThat(error)
.isInstanceOf(InvalidRepositoryException.class)
.hasMessageContaining(dir.toString())
);
} }
// When opening a repo in a dir that is a file then an exception is thrown // When opening a repo in a dir that is a file then an exception is thrown
@ -134,10 +150,15 @@ class GitDBTest implements WithAssertions {
void openRepo_whenDirIsFile_thenThrowException() throws IOException { void openRepo_whenDirIsFile_thenThrowException() throws IOException {
//given //given
final Path dir = fileExists(); final Path dir = fileExists();
//when
final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress);
//then //then
assertThatExceptionOfType(InvalidRepositoryException.class) gitDBResult.match(
.isThrownBy(() -> GitDB.openLocal(dir, userName, userEmailAddress)) success -> fail("Directory is a file"),
.withMessageContaining(dir.toString()); error -> assertThat(error)
.isInstanceOf(InvalidRepositoryException.class)
.hasMessageContaining(dir.toString())
);
} }
// When opening a repo in a dir that doesn't exist then an exception is thrown // When opening a repo in a dir that doesn't exist then an exception is thrown
@ -145,10 +166,15 @@ class GitDBTest implements WithAssertions {
void openRepo_whenDirNotExist_thenThrowException() throws IOException { void openRepo_whenDirNotExist_thenThrowException() throws IOException {
//given //given
final Path dir = dirDoesNotExist(); final Path dir = dirDoesNotExist();
//when
final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress);
//then //then
assertThatExceptionOfType(InvalidRepositoryException.class) gitDBResult.match(
.isThrownBy(() -> GitDB.openLocal(dir, userName, userEmailAddress)) success -> fail("Directory does not exist"),
.withMessageContaining(dir.toString()); error -> assertThat(error)
.isInstanceOf(InvalidRepositoryException.class)
.hasMessageContaining(dir.toString())
);
} }
// When opening a repo in a dir that is not a bare repo then an exception is thrown // When opening a repo in a dir that is not a bare repo then an exception is thrown
@ -156,12 +182,17 @@ class GitDBTest implements WithAssertions {
void openRepo_whenRepoNotBare_thenThrowException() throws IOException, GitAPIException { void openRepo_whenRepoNotBare_thenThrowException() throws IOException, GitAPIException {
//given //given
final Path dir = nonBareRepo(); final Path dir = nonBareRepo();
//when
final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress);
//then //then
assertThatExceptionOfType(InvalidRepositoryException.class) gitDBResult.match(
.isThrownBy(() -> GitDB.openLocal(dir, userName, userEmailAddress)) success -> fail("Not a bare repo"),
.withMessageContaining("Invalid GitDB repo") error -> assertThat(error)
.withMessageContaining("Not a bare repo") .isInstanceOf(InvalidRepositoryException.class)
.withMessageContaining(dir.toString()); .hasMessageContaining("Invalid GitDB repo")
.hasMessageContaining("Not a bare repo")
.hasMessageContaining(dir.toString())
);
} }
private Path nonBareRepo() throws IOException, GitAPIException { private Path nonBareRepo() throws IOException, GitAPIException {
@ -176,9 +207,13 @@ class GitDBTest implements WithAssertions {
//given //given
final Path dir = gitDBRepoPath(); final Path dir = gitDBRepoPath();
//when //when
final GitDB gitDB = GitDB.openLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDB = GitDB.openLocal(dir, userName, userEmailAddress);
//then //then
assertThat(gitDB).isNotNull(); assertThat(gitDB.isOkay()).isTrue();
gitDB.match(
success -> assertThat(success).isNotNull(),
error -> fail("did not open local repo")
);
} }
private Path gitDBRepoPath() throws IOException { private Path gitDBRepoPath() throws IOException {
@ -189,45 +224,38 @@ class GitDBTest implements WithAssertions {
// Given a valid GitDb handle // Given a valid GitDb handle
private GitDB gitDB(final Path dbDir) throws IOException {
return GitDB.initLocal(dbDir, userName, userEmailAddress);
}
// When select a branch that doesn't exist then an empty Optional is returned // When select a branch that doesn't exist then an empty Optional is returned
@Test @Test
void selectBranch_whenBranchNotExist_thenEmptyOptional() throws IOException { void selectBranch_whenBranchNotExist_thenEmptyOptional() throws Throwable {
//given //given
final GitDB gitDb = gitDB(dirDoesNotExist()); final Result<GitDB> gitDb = gitDB(dirDoesNotExist());
//when //when
final Optional<GitDBBranch> branch = gitDb.branch("unknown"); final Result<Maybe<GitDBBranch>> branch = gitDb.flatMap(db -> db.branch("unknown"));
//then //then
assertThat(branch).isEmpty(); assertThat(branch.orElseThrow().toOptional()).isEmpty();
}
private Result<GitDB> gitDB(final Path dbDir) {
return GitDB.initLocal(dbDir, userName, userEmailAddress);
} }
// 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 IOException { void selectBranch_branchExists_thenReturnBranch() throws Throwable {
//given //given
final Path dbDir = dirDoesNotExist(); final Path dbDir = dirDoesNotExist();
final GitDB gitDb = gitDB(dbDir); final Result<GitDB> gitDb = gitDB(dbDir);
//when //when
final Optional<GitDBBranch> branch = gitDb.branch("master"); final Result<Maybe<GitDBBranch>> branch = gitDb.flatMap(db -> db.branch("master"));
//then //then
assertThat(branch).as("Branch master exists").isNotEmpty(); assertThat(branch.orElseThrow().toOptional()).as("Branch master exists").isNotEmpty();
} }
// Given a valid GitDbBranch handle // Given a valid GitDbBranch handle
private GitDBBranch gitDBBranch() throws IOException {
final GitDB gitDB = gitDB(dirDoesNotExist());
final Optional<GitDBBranch> branchOptional = gitDB.branch("master");
assumeThat(branchOptional).isNotEmpty();
return branchOptional.get();
}
// 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, ClassNotFoundException { void getKey_whenKeyNotExist_thenReturnEmptyOptional() throws IOException {
//given //given
final GitDBBranch branch = gitDBBranch(); final GitDBBranch branch = gitDBBranch();
//when //when
@ -236,6 +264,17 @@ class GitDBTest implements WithAssertions {
assertThat(value).isEmpty(); assertThat(value).isEmpty();
} }
private GitDBBranch gitDBBranch() {
try {
return gitDB(dirDoesNotExist())
.flatMap(db -> db.branch("master"))
.orElseThrow()
.orElse(null);
} catch (Throwable throwable) {
throw new RuntimeException("Couldn't create master branch");
}
}
// When getting the format version it matches expected // When getting the format version it matches expected
@Test @Test
void getVersionFormat_thenFormatIsSet() throws IOException { void getVersionFormat_thenFormatIsSet() throws IOException {
@ -263,7 +302,7 @@ class GitDBTest implements WithAssertions {
// 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
@Test @Test
void getKey_whenExists_thenReturnValueInOptional() throws IOException, ClassNotFoundException { void getKey_whenExists_thenReturnValueInOptional() throws IOException {
//given //given
final String key = stringSupplier.get(); final String key = stringSupplier.get();
final String value = stringSupplier.get(); final String value = stringSupplier.get();