From 265bd61a11ee38ecbaf3b609ea629465ec99d7f4 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Thu, 7 Jun 2018 22:25:47 +0100 Subject: [PATCH] When getting a key that does not exist then return an empty Optional --- src/main/java/net/kemitix/gitdb/GitDBBranch.java | 14 ++++++++++++++ src/test/java/net/kemitix/gitdb/GitDBTest.java | 16 ++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/main/java/net/kemitix/gitdb/GitDBBranch.java b/src/main/java/net/kemitix/gitdb/GitDBBranch.java index bb7f1c9..6862eaf 100644 --- a/src/main/java/net/kemitix/gitdb/GitDBBranch.java +++ b/src/main/java/net/kemitix/gitdb/GitDBBranch.java @@ -25,6 +25,8 @@ import lombok.AccessLevel; import lombok.RequiredArgsConstructor; import org.eclipse.jgit.lib.Ref; +import java.util.Optional; + /** * API for interacting with a branch in a GirDB. * @@ -44,4 +46,16 @@ public class GitDBBranch { public static GitDBBranch withRef(final Ref ref) { return new GitDBBranch(ref); } + + /** + * Lookup a value for the key. + * + * @param key the key to lookup + * @param valueClass the expected class of the value + * @param the Class of the value + * @return an Optional containing the value, if it exists, or empty if not + */ + public Optional get(final String key, final Class valueClass) { + return Optional.empty(); + } } diff --git a/src/test/java/net/kemitix/gitdb/GitDBTest.java b/src/test/java/net/kemitix/gitdb/GitDBTest.java index 325769a..37707ac 100644 --- a/src/test/java/net/kemitix/gitdb/GitDBTest.java +++ b/src/test/java/net/kemitix/gitdb/GitDBTest.java @@ -1,5 +1,6 @@ package net.kemitix.gitdb; +import org.assertj.core.api.Assumptions; import org.assertj.core.api.WithAssertions; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; @@ -13,6 +14,8 @@ import java.io.*; import java.nio.file.*; import java.util.Optional; +import static org.assertj.core.api.Assumptions.assumeThat; + @ExtendWith(MockitoExtension.class) class GitDBTest implements WithAssertions { @@ -193,6 +196,19 @@ class GitDBTest implements WithAssertions { // Given a valid GitDbBranch handle // When getting a key that does not exist then return an empty Optional + @Test + void getKey_whenKeyNotExist_thenReturnEmptyOptional() throws IOException { + //given + final GitDB gitDB = newGitDBRepo(dirDoesNotExist()); + final Optional branchOptional = gitDB.branch("master"); + assumeThat(branchOptional).isNotEmpty(); + final GitDBBranch master = branchOptional.get(); + //when + final Optional value = master.get("unknown", String.class); + //then + assertThat(value).isEmpty(); + } + // When putting a key/value pair then a GitDbBranch is returned // When getting a key that does exist then the value is returned inside an Optional // When removing a key that does not exist then the GitDbBranch is returned