diff --git a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/PullRequests.java b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/PullRequests.java index 39ac8a20..9e957ffe 100644 --- a/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/PullRequests.java +++ b/dockerfile-image-update/src/main/java/com/salesforce/dockerfileimageupdate/utils/PullRequests.java @@ -58,7 +58,7 @@ public void prepareToCreate(final Namespace ns, * @param fork A GitHubContentToProcess object that contains the fork repository that is under process * @return true if the file is found in the path specified and is not disabled, false otherwise */ - private boolean isRenovateEnabled(List filePaths, GitHubContentToProcess fork) throws IOException { + public boolean isRenovateEnabled(List filePaths, GitHubContentToProcess fork) throws IOException { for (String filePath : filePaths) { try { GHContent fileContent = fork.getParent().getFileContent(filePath); diff --git a/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/PullRequestsTest.java b/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/PullRequestsTest.java index 99da117f..4bc9aead 100644 --- a/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/PullRequestsTest.java +++ b/dockerfile-image-update/src/test/java/com/salesforce/dockerfileimageupdate/utils/PullRequestsTest.java @@ -6,6 +6,7 @@ import net.sourceforge.argparse4j.inf.*; import org.kohsuke.github.*; import org.mockito.*; +import org.testng.*; import org.testng.annotations.*; import java.io.*; @@ -157,4 +158,70 @@ public void testPullRequestsPrepareSkipsSendingPRIfRepoOnboardedToRenovate() thr eq(gitHubContentToProcess), anyList(), eq(gitForkBranch), eq(rateLimiter)); } + + @Test + public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileNotFound() throws IOException { + PullRequests pullRequests = new PullRequests(); + List filePaths = Collections.singletonList("renovate.json"); + GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class); + GHRepository ghRepository = mock(GHRepository.class); + when(gitHubContentToProcess.getParent()).thenReturn(ghRepository); + when(ghRepository.getFileContent(anyString())).thenThrow(new FileNotFoundException()); + Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess)); + } + + @Test + public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileFoundButIsDisabled() throws IOException { + PullRequests pullRequests = new PullRequests(); + List filePaths = Collections.singletonList("renovate.json"); + GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class); + GHRepository ghRepository = mock(GHRepository.class); + GHContent content = mock(GHContent.class); + InputStream inputStream = new ByteArrayInputStream("{enabled:false}".getBytes()); + when(gitHubContentToProcess.getParent()).thenReturn(ghRepository); + when(ghRepository.getFileContent(anyString())).thenReturn(content); + when(content.read()).thenReturn(inputStream); + Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess)); + } + + @Test + public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundButEnabledKeyNotFound() throws IOException { + PullRequests pullRequests = new PullRequests(); + List filePaths = Collections.singletonList("renovate.json"); + GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class); + GHRepository ghRepository = mock(GHRepository.class); + GHContent content = mock(GHContent.class); + InputStream inputStream = new ByteArrayInputStream("{someKey:someValue}".getBytes()); + when(gitHubContentToProcess.getParent()).thenReturn(ghRepository); + when(ghRepository.getFileContent(anyString())).thenReturn(content); + when(content.read()).thenReturn(inputStream); + Assert.assertTrue(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess)); + } + + @Test + public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndResourcesThrowAnException() throws IOException { + PullRequests pullRequests = new PullRequests(); + List filePaths = Collections.singletonList("renovate.json"); + GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class); + GHRepository ghRepository = mock(GHRepository.class); + GHContent content = mock(GHContent.class); + when(gitHubContentToProcess.getParent()).thenReturn(ghRepository); + when(ghRepository.getFileContent(anyString())).thenReturn(content); + when(content.read()).thenThrow(new IOException()); + Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess)); + } + + @Test + public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndEnabledKeySetToTrue() throws IOException { + PullRequests pullRequests = new PullRequests(); + List filePaths = Collections.singletonList("renovate.json"); + GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class); + GHRepository ghRepository = mock(GHRepository.class); + GHContent content = mock(GHContent.class); + InputStream inputStream = new ByteArrayInputStream("{enabled:true}".getBytes()); + when(gitHubContentToProcess.getParent()).thenReturn(ghRepository); + when(ghRepository.getFileContent(anyString())).thenReturn(content); + when(content.read()).thenReturn(inputStream); + Assert.assertTrue(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess)); + } }