Skip to content

Commit f6c357a

Browse files
committed
Added tests
1 parent 5137e84 commit f6c357a

File tree

5 files changed

+69
-7
lines changed

5 files changed

+69
-7
lines changed

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/storage/GitHubJsonStore.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import java.util.stream.Collectors;
2525
import org.kohsuke.github.GHBlob;
2626
import org.kohsuke.github.GHCommit;
27-
import org.kohsuke.github.GHContent;
2827
import org.kohsuke.github.GHMyself;
2928
import org.kohsuke.github.GHRepository;
3029
import org.slf4j.Logger;
@@ -142,8 +141,8 @@ public Set<Map.Entry<String, JsonElement>> parseStoreToImagesMap(DockerfileGitHu
142141
String login = myself.getLogin();
143142
GHRepository store = dockerfileGitHubUtil.getRepo(Paths.get(login, storeName).toString());
144143

145-
GHBlob storeContent = dockerfileGitHubUtil.tryRetrievingBlob(store, Constants.STORE_JSON_FILE,
146-
store.getDefaultBranch());
144+
GHBlob storeContent = dockerfileGitHubUtil.tryRetrievingBlob(store,
145+
Constants.STORE_JSON_FILE, store.getDefaultBranch());
147146

148147
if (storeContent == null) {
149148
return Collections.emptySet();
@@ -163,7 +162,8 @@ public Set<Map.Entry<String, JsonElement>> parseStoreToImagesMap(DockerfileGitHu
163162
return imagesJson.getAsJsonObject().entrySet();
164163
}
165164

166-
public List<ImageTagStoreContent> getStoreContent(DockerfileGitHubUtil dockerfileGitHubUtil, String storeName) throws IOException {
165+
public List<ImageTagStoreContent> getStoreContent(DockerfileGitHubUtil dockerfileGitHubUtil, String storeName)
166+
throws IOException {
167167
Set<Map.Entry<String, JsonElement>> imageToTagStore = parseStoreToImagesMap(dockerfileGitHubUtil, storeName);
168168
return imageToTagStore.stream()
169169
.map(entry -> new ImageTagStoreContent(entry.getKey(), entry.getValue().getAsString()))

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,8 @@ public GHContent tryRetrievingContent(GHRepository repo, String path, String bra
257257
return gitHubUtil.tryRetrievingContent(repo, path, branch);
258258
}
259259

260-
public GHBlob tryRetrievingBlob(GHRepository repo, String path, String branch) throws IOException {
260+
public GHBlob tryRetrievingBlob(GHRepository repo, String path, String branch)
261+
throws IOException {
261262
return gitHubUtil.tryRetrievingBlob(repo, path, branch);
262263
}
263264

dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/GitHubUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ public GHContent tryRetrievingContent(GHRepository repo, String path, String bra
196196
return content;
197197
}
198198

199-
public GHBlob tryRetrievingBlob(GHRepository repo, String path, String branch) throws IOException {
199+
public GHBlob tryRetrievingBlob(GHRepository repo, String path, String branch)
200+
throws IOException {
200201
return repo.getCommit(branch).getTree().getEntry(path).asBlob();
201202
}
202203

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/DockerfileGitHubUtilTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,26 @@ public void testTryRetrievingContent() throws Exception {
406406
assertEquals(dockerfileGitHubUtil.tryRetrievingContent(new GHRepository(), "path", "branch"), content);
407407
}
408408

409+
@Test
410+
public void testTryRetrievingBlob() throws IOException {
411+
gitHubUtil = mock(GitHubUtil.class);
412+
GHBlob content = mock(GHBlob.class);
413+
when(gitHubUtil.tryRetrievingBlob(any(), anyString(), anyString())).thenReturn(content);
414+
dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil);
415+
assertEquals(dockerfileGitHubUtil.tryRetrievingBlob(new GHRepository(), "path", "branch"), content);
416+
}
417+
418+
@Test(
419+
expectedExceptions = IOException.class,
420+
expectedExceptionsMessageRegExp = "error while reading blob")
421+
public void testTryRetrievingBlobException() throws IOException {
422+
gitHubUtil = mock(GitHubUtil.class);
423+
GHBlob content = mock(GHBlob.class);
424+
when(gitHubUtil.tryRetrievingBlob(any(), anyString(), anyString())).thenThrow(new IOException("error while reading blob"));
425+
dockerfileGitHubUtil = new DockerfileGitHubUtil(gitHubUtil);
426+
assertNotEquals(dockerfileGitHubUtil.tryRetrievingBlob(new GHRepository(), "path", "branch"), content);
427+
}
428+
409429

410430
@DataProvider
411431
public Object[][] inputBranchesImagesAndTags() {

dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/GitHubUtilTest.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,46 @@ public void testTryRetrievingContent() throws Exception {
153153
verify(repo, times(1)).getFileContent(eq("path"), eq("branch"));
154154
}
155155

156+
@Test
157+
public void testTryRetrievingBlobSuccess() throws Exception {
158+
159+
GitHub github = mock(GitHub.class);
160+
GHRepository repo = mock(GHRepository.class);
161+
GHCommit commit = mock(GHCommit.class);
162+
GHTree ghTree = mock(GHTree.class);
163+
GHTreeEntry ghTreeEntry = mock(GHTreeEntry.class);
164+
GHBlob ghBlob = mock(GHBlob.class);
165+
when(repo.getCommit(anyString())).thenReturn(commit);
166+
when(commit.getTree()).thenReturn(ghTree);
167+
when(ghTree.getEntry(anyString())).thenReturn(ghTreeEntry);
168+
when(ghTreeEntry.asBlob()).thenReturn(ghBlob);
169+
170+
GitHubUtil gitHubUtil = new GitHubUtil(github);
171+
assertEquals(gitHubUtil.tryRetrievingBlob(repo, "path", "branch"), ghBlob);
172+
173+
verify(commit, times(1)).getTree();
174+
verify(ghTreeEntry, times(1)).asBlob();
175+
}
176+
177+
@Test(
178+
expectedExceptions = IOException.class,
179+
expectedExceptionsMessageRegExp = "error while reading commit")
180+
public void testTryRetrievingBlobException() throws Exception {
181+
182+
GitHub github = mock(GitHub.class);
183+
GHRepository repo = mock(GHRepository.class);
184+
GHCommit commit = mock(GHCommit.class);
185+
GHTreeEntry ghTreeEntry = mock(GHTreeEntry.class);
186+
GHBlob ghBlob = mock(GHBlob.class);
187+
when(repo.getCommit(anyString())).thenThrow(new IOException("error while reading commit"));
188+
189+
GitHubUtil gitHubUtil = new GitHubUtil(github);
190+
assertEquals(gitHubUtil.tryRetrievingBlob(repo, "path", "branch"), ghBlob);
191+
192+
verify(commit, times(0)).getTree();
193+
verify(ghTreeEntry, times(0)).asBlob();
194+
}
195+
156196
/* There is a timeout because if this part of the code is broken, it might enter 60 seconds of sleep. */
157197
@Test(timeOut = 1000)
158198
public void testGetGHRepositories() throws Exception {
@@ -335,4 +375,4 @@ public void testRepoHasBranchFalseForGHFileNotFoundException() throws IOExceptio
335375
when(repo.getBranch(branchName)).thenThrow(new GHFileNotFoundException());
336376
assertFalse(gitHubUtil.repoHasBranch(repo, branchName));
337377
}
338-
}
378+
}

0 commit comments

Comments
 (0)