GitDBBranch returns Result<T> values

This commit is contained in:
Paul Campbell 2018-06-25 20:03:29 +01:00
parent 193a0f380b
commit f5483f419b
6 changed files with 205 additions and 146 deletions

View file

@ -22,9 +22,8 @@
package net.kemitix.gitdb; package net.kemitix.gitdb;
import com.github.zafarkhaja.semver.Version; import com.github.zafarkhaja.semver.Version;
import net.kemitix.mon.maybe.Maybe;
import java.io.IOException; import net.kemitix.mon.result.Result;
import java.util.Optional;
/** /**
* API for interacting with a branch in a GirDB. * API for interacting with a branch in a GirDB.
@ -38,9 +37,8 @@ public interface GitDBBranch {
* *
* @param key the key to lookup * @param key the key to lookup
* @return an Optional containing the value, if it exists, or empty if not * @return an Optional containing the value, if it exists, or empty if not
* @throws IOException if there was an error reading the value
*/ */
Optional<String> get(String key) throws IOException; Result<Maybe<String>> get(String key);
/** /**
* Put a value into the store for the key. * Put a value into the store for the key.
@ -48,18 +46,16 @@ public interface GitDBBranch {
* @param key the key to place the value under * @param key the key to place the value under
* @param value the value (must be Serializable) * @param value the value (must be Serializable)
* @return an updated branch containing the new key/value * @return an updated branch containing the new key/value
* @throws IOException if there was an error writing the key/value
*/ */
GitDBBranch put(String key, String value) throws IOException; Result<GitDBBranch> put(String key, String value);
/** /**
* Removes a key and its value from the store. * Removes a key and its value from the store.
* *
* @param key the key to remove * @param key the key to remove
* @return an updated branch without the key, or the original if the key was not found * @return an updated branch without the key, or the original if the key was not found
* @throws IOException if there was an error removing the key/value
*/ */
GitDBBranch remove(String key) throws IOException; Result<GitDBBranch> remove(String key);
/** /**
* Returns the GitDB format for the current branch. * Returns the GitDB format for the current branch.
@ -67,8 +63,7 @@ public interface GitDBBranch {
* <p>Different branches can have different versions.</p> * <p>Different branches can have different versions.</p>
* *
* @return the format as per semantic versioning, i.e. "x.y.z" within an Optional * @return the format as per semantic versioning, i.e. "x.y.z" within an Optional
* @throws IOException error reading version
*/ */
Optional<Version> getFormatVersion() throws IOException; Result<Maybe<Version>> getFormatVersion();
} }

View file

@ -25,12 +25,13 @@ import com.github.zafarkhaja.semver.Version;
import lombok.AccessLevel; import lombok.AccessLevel;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.kemitix.gitdb.GitDBBranch; import net.kemitix.gitdb.GitDBBranch;
import net.kemitix.mon.maybe.Maybe;
import net.kemitix.mon.result.Result;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import java.io.IOException; import java.io.IOException;
import java.util.Optional;
import java.util.function.Function; import java.util.function.Function;
/** /**
@ -48,15 +49,6 @@ class GitDBBranchImpl implements GitDBBranch {
private final String userEmailAddress; private final String userEmailAddress;
private final String name; private final String name;
private static GitDBBranch select(
final Ref branchRef,
final GitDBRepo gitDBRepo,
final String userName,
final String userEmailAddress
) {
return new GitDBBranchImpl(branchRef, gitDBRepo, userName, userEmailAddress, branchRef.getName());
}
/** /**
* Initialise the creation of new GitDBBranch instances. * Initialise the creation of new GitDBBranch instances.
* *
@ -65,7 +57,7 @@ class GitDBBranchImpl implements GitDBBranch {
* @param userEmailAddress the user's email address to record against changes * @param userEmailAddress the user's email address to record against changes
* @return a Function for creating a GitDBBranch when supplied with a Ref for a branch * @return a Function for creating a GitDBBranch when supplied with a Ref for a branch
*/ */
static Function<Ref, GitDBBranch> init( static Function<Ref, Result<GitDBBranch>> init(
final Repository repository, final Repository repository,
final String userName, final String userName,
final String userEmailAddress final String userEmailAddress
@ -73,38 +65,56 @@ class GitDBBranchImpl implements GitDBBranch {
return ref -> select(ref, new GitDBRepo(repository), userName, userEmailAddress); return ref -> select(ref, new GitDBRepo(repository), userName, userEmailAddress);
} }
private static Result<GitDBBranch> select(
final Ref branchRef,
final GitDBRepo gitDBRepo,
final String userName,
final String userEmailAddress
) {
return Result.ok(new GitDBBranchImpl(branchRef, gitDBRepo, userName, userEmailAddress, branchRef.getName()));
}
@Override @Override
public Optional<String> get(final String key) throws IOException { public Result<Maybe<String>> get(final String key) {
return gitDBRepo.readValue(branchRef, KEY_PREFIX + key); return gitDBRepo.readValue(branchRef, KEY_PREFIX + key);
} }
@Override @Override
public GitDBBranch put(final String key, final String value) throws IOException { public Result<GitDBBranch> put(final String key, final String value) {
try {
final ObjectId newTree = gitDBRepo.writeValue(branchRef, KEY_PREFIX + key, value); final ObjectId newTree = gitDBRepo.writeValue(branchRef, KEY_PREFIX + key, value);
final String message = String.format("Add key [%s] = [%s]", key, value); final String message = String.format("Add key [%s] = [%s]", key, value);
final Ref newBranch = gitDBRepo.writeCommit(branchRef, newTree, message, userName, userEmailAddress); final Result<Ref> newBranch = gitDBRepo.writeCommit(branchRef, newTree, message, userName, userEmailAddress);
return select(newBranch, gitDBRepo, userName, userEmailAddress); return newBranch.flatMap(b -> select(b, gitDBRepo, userName, userEmailAddress));
} catch (IOException e) {
return Result.error(e);
}
} }
@Override @Override
public GitDBBranch remove(final String key) throws IOException { public Result<GitDBBranch> remove(final String key) {
final Optional<ObjectId> newTree = gitDBRepo.removeKey(branchRef, KEY_PREFIX + key); return gitDBRepo.removeKey(branchRef, KEY_PREFIX + key).flatMap(treeId ->
if (newTree.isPresent()) { writeRemoveKeyCommit(key, treeId)
final Ref newBranch = .map(selectUpdatedBranch())
gitDBRepo.writeCommit( .orElse(Result.ok(this)));
branchRef, newTree.get(),
String.format("Remove Key [%s]", key),
userName,
userEmailAddress);
return select(newBranch, gitDBRepo, userName, userEmailAddress);
} }
return this;
private Maybe<Result<Ref>> writeRemoveKeyCommit(final String key, final Maybe<ObjectId> idMaybe) {
return idMaybe.map(objectId -> {
final String message = String.format("Remove Key [%s]", key);
return gitDBRepo.writeCommit(branchRef, objectId, message, userName, userEmailAddress);
});
}
private Function<Result<Ref>, Result<GitDBBranch>> selectUpdatedBranch() {
return refResult -> refResult.flatMap(ref ->
select(ref, gitDBRepo, userName, userEmailAddress));
} }
@Override @Override
public Optional<Version> getFormatVersion() throws IOException { public Result<Maybe<Version>> getFormatVersion() {
return gitDBRepo.readValue(branchRef, "GitDB.Version") return gitDBRepo.readValue(branchRef, "GitDB.Version")
.map(Version::valueOf); .map(version -> version.map(Version::valueOf));
} }
} }

View file

@ -22,13 +22,14 @@
package net.kemitix.gitdb.impl; package net.kemitix.gitdb.impl;
import lombok.val; import lombok.val;
import net.kemitix.mon.maybe.Maybe;
import net.kemitix.mon.result.Result;
import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.Repository;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Optional;
/** /**
* Wrapper for interacting with the GitDB Repository. * Wrapper for interacting with the GitDB Repository.
@ -73,47 +74,29 @@ class GitDBRepo {
return keyWriter.writeFirst(key, valueId); return keyWriter.writeFirst(key, valueId);
} }
/**
* Insert a commit into the store, returning its unique id.
*
* @param treeId id of the tree
* @param branchRef the branch to commit to
* @param message the message
* @param userName the user name
* @param userEmailAddress the user email address
* @return the id of the commit
* @throws IOException the commit could not be stored
*/
ObjectId insertCommit(
final ObjectId treeId,
final String message,
final String userName,
final String userEmailAddress,
final Ref branchRef
) throws IOException {
return commitWriter.write(treeId, branchRef, message, userName, userEmailAddress);
}
/** /**
* Reads a value from the branch with the given key. * Reads a value from the branch with the given key.
* *
* @param branchRef the branch to select from * @param branchRef the branch to select from
* @param key the key to get the value for * @param key the key to get the value for
* @return an Optional containing the value if found, or empty * @return an Optional containing the value if found, or empty
* @throws IOException if there was an error reading the value
*/ */
Optional<String> readValue( Result<Maybe<String>> readValue(
final Ref branchRef, final Ref branchRef,
final String key final String key
) throws IOException { ) {
try {
val blob = new GitTreeReader(repository) val blob = new GitTreeReader(repository)
.treeFilter(key) .treeFilter(key)
.stream(branchRef) .stream(branchRef)
.findFirst(); .findFirst();
if (blob.isPresent()) { if (blob.isPresent()) {
return Optional.of(blob.get().blobAsString()); return Result.ok(Maybe.just(blob.get().blobAsString()));
}
return Result.ok(Maybe.nothing());
} catch (IOException e) {
return Result.error(e);
} }
return Optional.empty();
} }
/** /**
@ -143,15 +126,40 @@ class GitDBRepo {
* @return the Ref of the updated branch * @return the Ref of the updated branch
* @throws IOException if there was an error writing the branch * @throws IOException if there was an error writing the branch
*/ */
Ref writeCommit( Result<Ref> writeCommit(
final Ref branchRef, final Ref branchRef,
final ObjectId tree, final ObjectId tree,
final String message, final String message,
final String userName, final String userName,
final String userEmailAddress final String userEmailAddress
) throws IOException { ) {
try {
final ObjectId commitId = insertCommit(tree, message, userName, userEmailAddress, branchRef); final ObjectId commitId = insertCommit(tree, message, userName, userEmailAddress, branchRef);
return headWriter.write(branchRef.getName(), commitId); return Result.ok(headWriter.write(branchRef.getName(), commitId));
} catch (IOException e) {
return Result.error(e);
}
}
/**
* Insert a commit into the store, returning its unique id.
*
* @param treeId id of the tree
* @param branchRef the branch to commit to
* @param message the message
* @param userName the user name
* @param userEmailAddress the user email address
* @return the id of the commit
* @throws IOException the commit could not be stored
*/
ObjectId insertCommit(
final ObjectId treeId,
final String message,
final String userName,
final String userEmailAddress,
final Ref branchRef
) throws IOException {
return commitWriter.write(treeId, branchRef, message, userName, userEmailAddress);
} }
/** /**
@ -184,7 +192,7 @@ class GitDBRepo {
* empty Optional if there key was not found, the there was no changes made * empty Optional if there key was not found, the there was no changes made
* @throws IOException if there was an error writing the value * @throws IOException if there was an error writing the value
*/ */
Optional<ObjectId> removeKey(final Ref branchRef, final String key) throws IOException { Result<Maybe<ObjectId>> removeKey(final Ref branchRef, final String key) {
return keyRemover.remove(branchRef, key); return keyRemover.remove(branchRef, key);
} }

View file

@ -22,10 +22,11 @@
package net.kemitix.gitdb.impl; package net.kemitix.gitdb.impl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import net.kemitix.mon.maybe.Maybe;
import net.kemitix.mon.result.Result;
import org.eclipse.jgit.lib.*; import org.eclipse.jgit.lib.*;
import java.io.IOException; import java.io.IOException;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -40,6 +41,28 @@ class KeyRemover {
private final Repository repository; private final Repository repository;
/**
* Remove a key from the repository.
*
* @param branchRef the branch to update
* @param key the key to remove
* @return the id of the updated tree
*/
Result<Maybe<ObjectId>> remove(final Ref branchRef, final String key) {
final TreeFormatter treeFormatter = new TreeFormatter();
final AtomicBoolean removed = new AtomicBoolean(false);
try {
new GitTreeReader(repository)
.stream(branchRef)
.peek(flagIfFound(key, removed))
.filter(isNotKey(key))
.forEach(addToTree(treeFormatter));
return insertTree(treeFormatter).maybe(oi -> removed.get());
} catch (IOException e) {
return Result.error(e);
}
}
/** /**
* Sets the boolean to true if the key matches a NamedRevBlob's name. * Sets the boolean to true if the key matches a NamedRevBlob's name.
* *
@ -75,38 +98,17 @@ class KeyRemover {
return item -> treeFormatter.append(item.getName(), item.getRevBlob()); return item -> treeFormatter.append(item.getName(), item.getRevBlob());
} }
/**
* Remove a key from the repository.
*
* @param branchRef the branch to update
* @param key the key to remove
* @return the id of the updated tree
* @throws IOException if there is an error writing the value
*/
Optional<ObjectId> remove(final Ref branchRef, final String key) throws IOException {
final TreeFormatter treeFormatter = new TreeFormatter();
final AtomicBoolean removed = new AtomicBoolean(false);
new GitTreeReader(repository)
.stream(branchRef)
.peek(flagIfFound(key, removed))
.filter(isNotKey(key))
.forEach(addToTree(treeFormatter));
if (removed.get()) {
return Optional.of(insertTree(treeFormatter));
}
return Optional.empty();
}
/** /**
* Insert a tree into the repo, returning its id. * Insert a tree into the repo, returning its id.
* *
* @param treeFormatter the formatter containing the proposed tree's data. * @param treeFormatter the formatter containing the proposed tree's data.
* @return the name of the tree object. * @return the name of the tree object.
* @throws IOException the object could not be stored.
*/ */
private ObjectId insertTree(final TreeFormatter treeFormatter) throws IOException { private Result<ObjectId> insertTree(final TreeFormatter treeFormatter) {
try (ObjectInserter inserter = repository.getObjectDatabase().newInserter()) { try (ObjectInserter inserter = repository.getObjectDatabase().newInserter()) {
return inserter.insert(treeFormatter); return Result.ok(inserter.insert(treeFormatter));
} catch (IOException e) {
return Result.error(e);
} }
} }
} }

View file

@ -46,7 +46,7 @@ final class LocalGitDBImpl implements GitDB, LocalGitDB {
private final Repository repository; private final Repository repository;
private final Function<Ref, GitDBBranch> branchInit; private final Function<Ref, Result<GitDBBranch>> branchInit;
private LocalGitDBImpl( private LocalGitDBImpl(
final Repository repository, final Repository repository,
@ -113,7 +113,9 @@ final class LocalGitDBImpl implements GitDB, LocalGitDB {
@Override @Override
public Result<Maybe<GitDBBranch>> branch(final String name) { public Result<Maybe<GitDBBranch>> branch(final String name) {
try { try {
return Result.ok(Maybe.maybe(repository.findRef(name)).map(branchInit)); return Result.invert(Maybe.maybe(
repository.findRef(name))
.map(branchInit::apply));
} catch (IOException e) { } catch (IOException e) {
return Result.error(e); return Result.error(e);
} }

View file

@ -24,8 +24,8 @@ 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.Optional;
import java.util.UUID; import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Supplier; import java.util.function.Supplier;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
@ -80,7 +80,7 @@ class GitDBTest implements WithAssertions {
final Result<GitDB> gitDBResult = GitDB.initLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDBResult = GitDB.initLocal(dir, userName, userEmailAddress);
//then //then
gitDBResult.match( gitDBResult.match(
success -> fail("Is a file not a directory"), failOnSuccess("Is a file not a directory"),
error -> assertThat(error) error -> assertThat(error)
.isInstanceOf(NotDirectoryException.class) .isInstanceOf(NotDirectoryException.class)
.hasMessageContaining(dir.toString()) .hasMessageContaining(dir.toString())
@ -91,6 +91,10 @@ class GitDBTest implements WithAssertions {
return Files.createTempFile("gitdb", "file"); return Files.createTempFile("gitdb", "file");
} }
private <T> Consumer<T> failOnSuccess(String message) {
return success -> fail(message);
}
// When initialising a repo in a non-empty dir then an exception is thrown // When initialising a repo in a non-empty dir then an exception is thrown
@Test @Test
void initRepo_whenNotEmptyDir_thenThrowException() throws IOException { void initRepo_whenNotEmptyDir_thenThrowException() throws IOException {
@ -101,7 +105,7 @@ class GitDBTest implements WithAssertions {
final Result<GitDB> gitDBResult = GitDB.initLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDBResult = GitDB.initLocal(dir, userName, userEmailAddress);
//then //then
gitDBResult.match( gitDBResult.match(
success -> fail("Directory is not empty"), failOnSuccess("Directory is not empty"),
error -> assertThat(error) error -> assertThat(error)
.isInstanceOf(DirectoryNotEmptyException.class) .isInstanceOf(DirectoryNotEmptyException.class)
.hasMessageContaining(dir.toString()) .hasMessageContaining(dir.toString())
@ -137,7 +141,7 @@ class GitDBTest implements WithAssertions {
final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress);
//then //then
gitDBResult.match( gitDBResult.match(
success -> fail("Not a bare repo"), failOnSuccess("Not a bare repo"),
error -> assertThat(error) error -> assertThat(error)
.isInstanceOf(InvalidRepositoryException.class) .isInstanceOf(InvalidRepositoryException.class)
.hasMessageContaining(dir.toString()) .hasMessageContaining(dir.toString())
@ -154,7 +158,7 @@ class GitDBTest implements WithAssertions {
final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress);
//then //then
gitDBResult.match( gitDBResult.match(
success -> fail("Directory is a file"), failOnSuccess("Directory is a file"),
error -> assertThat(error) error -> assertThat(error)
.isInstanceOf(InvalidRepositoryException.class) .isInstanceOf(InvalidRepositoryException.class)
.hasMessageContaining(dir.toString()) .hasMessageContaining(dir.toString())
@ -170,7 +174,7 @@ class GitDBTest implements WithAssertions {
final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress);
//then //then
gitDBResult.match( gitDBResult.match(
success -> fail("Directory does not exist"), failOnSuccess("Directory does not exist"),
error -> assertThat(error) error -> assertThat(error)
.isInstanceOf(InvalidRepositoryException.class) .isInstanceOf(InvalidRepositoryException.class)
.hasMessageContaining(dir.toString()) .hasMessageContaining(dir.toString())
@ -186,7 +190,7 @@ class GitDBTest implements WithAssertions {
final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress); final Result<GitDB> gitDBResult = GitDB.openLocal(dir, userName, userEmailAddress);
//then //then
gitDBResult.match( gitDBResult.match(
success -> fail("Not a bare repo"), failOnSuccess("Not a bare repo"),
error -> assertThat(error) error -> assertThat(error)
.isInstanceOf(InvalidRepositoryException.class) .isInstanceOf(InvalidRepositoryException.class)
.hasMessageContaining("Invalid GitDB repo") .hasMessageContaining("Invalid GitDB repo")
@ -259,33 +263,50 @@ class GitDBTest implements WithAssertions {
//given //given
final GitDBBranch branch = gitDBBranch(); final GitDBBranch branch = gitDBBranch();
//when //when
final Optional<String> value = branch.get("unknown"); final Result<Maybe<String>> value = branch.get("unknown");
//then //then
assertThat(value).isEmpty(); value.match(
success -> assertThat(success.toOptional()).isEmpty(),
failOnError()
);
} }
private GitDBBranch gitDBBranch() { private GitDBBranch gitDBBranch() {
try { try {
return gitDB(dirDoesNotExist()) final Result<GitDB> gitDBResult = gitDB(dirDoesNotExist());
.flatMap(db -> db.branch("master")) System.out.println("gitDBResult = " + gitDBResult);
.orElseThrow() final Result<Maybe<GitDBBranch>> master = gitDBResult.flatMap(db -> {
.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"); throw new RuntimeException("Couldn't create master branch", throwable);
} }
} }
private Consumer<Throwable> failOnError() {
return error -> fail("Not an error");
}
// 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 {
//given //given
final GitDBBranch gitDBBranch = gitDBBranch(); final GitDBBranch gitDBBranch = gitDBBranch();
//when
final Optional<Version> formatVersion = gitDBBranch.getFormatVersion();
//then
final Version version = new FormatVersion().getVersion(); final Version version = new FormatVersion().getVersion();
assertThat(formatVersion).contains(version); //when
assertThat(formatVersion.get()).isNotSameAs(version); final Result<Maybe<Version>> formatVersion = gitDBBranch.getFormatVersion();
//then
formatVersion.match(
success -> success.peek(v -> assertThat(v).isEqualTo(version).isNotSameAs(version)),
failOnError()
);
} }
// When putting a key/value pair then a GitDbBranch is returned // When putting a key/value pair then a GitDbBranch is returned
@ -294,10 +315,14 @@ class GitDBTest implements WithAssertions {
//given //given
final GitDBBranch originalBranch = gitDBBranch(); final GitDBBranch originalBranch = gitDBBranch();
//when //when
final GitDBBranch updatedBranch = originalBranch.put("key-name", "value"); final Result<GitDBBranch> updatedBranch = originalBranch.put("key-name", "value");
//then //then
assertThat(updatedBranch).isNotNull(); updatedBranch.match(
assertThat(updatedBranch).isNotSameAs(originalBranch); success -> assertThat(success).isNotNull().isNotSameAs(originalBranch),
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
@ -307,11 +332,14 @@ class GitDBTest implements WithAssertions {
final String key = stringSupplier.get(); final String key = stringSupplier.get();
final String value = stringSupplier.get(); final String value = stringSupplier.get();
final GitDBBranch originalBranch = gitDBBranch(); final GitDBBranch originalBranch = gitDBBranch();
final GitDBBranch updatedBranch = originalBranch.put(key, value); final Result<GitDBBranch> updatedBranch = originalBranch.put(key, value);
//when //when
final Optional<String> result = updatedBranch.get(key); final Result<Maybe<String>> result = updatedBranch.flatMap(b -> b.get(key));
//then //then
assertThat(result).contains(value); result.match(
success -> success.map(v -> assertThat(v).contains(value)),
failOnError()
);
} }
// When removing a key that does not exist then the GitDbBranch is returned // When removing a key that does not exist then the GitDbBranch is returned
@ -320,9 +348,12 @@ class GitDBTest implements WithAssertions {
//given //given
final GitDBBranch gitDBBranch = gitDBBranch(); final GitDBBranch gitDBBranch = gitDBBranch();
//when //when
final GitDBBranch result = gitDBBranch.remove("unknown"); final Result<GitDBBranch> result = gitDBBranch.remove("unknown");
//then //then
assertThat(result).isSameAs(gitDBBranch); result.match(
success -> assertThat(success).isSameAs(gitDBBranch),
failOnError()
);
} }
// When removing a key that does exist then a GitDbBranch is returned // When removing a key that does exist then a GitDbBranch is returned
@ -331,14 +362,17 @@ class GitDBTest implements WithAssertions {
//given //given
final String key = stringSupplier.get(); final String key = stringSupplier.get();
final String value = stringSupplier.get(); final String value = stringSupplier.get();
final GitDBBranch originalBranch = gitDBBranchWithKeyValue(key, value); final Result<GitDBBranch> originalBranch = gitDBBranchWithKeyValue(key, value);
//when //when
final GitDBBranch updatedBranch = originalBranch.remove(key); final Result<GitDBBranch> updatedBranch = originalBranch.flatMap(b -> b.remove(key));
//then //then
assertThat(updatedBranch).isNotSameAs(originalBranch); updatedBranch.match(
success -> assertThat(success).isNotSameAs(originalBranch),
failOnError()
);
} }
private GitDBBranch gitDBBranchWithKeyValue(final String key, final String value) throws IOException { private Result<GitDBBranch> gitDBBranchWithKeyValue(final String key, final String value) throws IOException {
return gitDBBranch().put(key, value); return gitDBBranch().put(key, value);
} }
@ -348,11 +382,15 @@ class GitDBTest implements WithAssertions {
//given //given
final String key = stringSupplier.get(); final String key = stringSupplier.get();
final String value = stringSupplier.get(); final String value = stringSupplier.get();
final GitDBBranch originalBranch = gitDBBranchWithKeyValue(key, value); final Result<GitDBBranch> originalBranch = gitDBBranchWithKeyValue(key, value);
//when //when
final GitDBBranch updatedBranch = originalBranch.remove(key); final Result<GitDBBranch> updatedBranch = originalBranch.flatMap(b -> b.remove(key));
//then //then
assertThat(originalBranch.get(key)).contains(value); originalBranch.flatMap(b -> b.get(key))
.match(
success -> assertThat(success.toOptional()).contains(value),
failOnError()
);
} }
// When removing a key that does exist then the updated GitDbBranch can't find it // When removing a key that does exist then the updated GitDbBranch can't find it
@ -361,11 +399,15 @@ class GitDBTest implements WithAssertions {
//given //given
final String key = stringSupplier.get(); final String key = stringSupplier.get();
final String value = stringSupplier.get(); final String value = stringSupplier.get();
final GitDBBranch originalBranch = gitDBBranchWithKeyValue(key, value); final Result<GitDBBranch> originalBranch = gitDBBranchWithKeyValue(key, value);
//when //when
final GitDBBranch updatedBranch = originalBranch.remove(key); final Result<GitDBBranch> updatedBranch = originalBranch.flatMap(b -> b.remove(key));
//then //then
assertThat(updatedBranch.get(key)).isEmpty(); updatedBranch.flatMap(b -> b.get(key))
.match(
success -> assertThat(success.toOptional()).isEmpty(),
failOnError()
);
} }
} }