From 53a890ab589980dad044de545fab7a105816d4e7 Mon Sep 17 00:00:00 2001 From: Avimanyu Mukhopadhyay Date: Tue, 13 Feb 2024 10:04:58 -0800 Subject: [PATCH] Catch json exception --- .../utils/PullRequests.java | 3 +++ .../utils/PullRequestsTest.java | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) 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 67086dfd..6d4f3f39 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 @@ -6,6 +6,7 @@ import net.sourceforge.argparse4j.inf.*; import org.json.JSONObject; import org.json.JSONTokener; +import org.json.JSONException; import org.kohsuke.github.*; import org.slf4j.*; @@ -67,6 +68,8 @@ protected boolean isRenovateEnabled(List filePaths, GitHubContentToProce log.debug("The file with name {} not found in the repository. Exception: {}", filePath, e.getMessage()); } catch (IOException e) { log.debug("Exception while trying to close a resource. Exception: {}", e.getMessage()); + } catch (JSONException e) { + log.debug("Exception while trying to read the renovate configuration file. Exception: {}", e.getMessage()); } } return false; 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 4bc9aead..d1aaf477 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 @@ -4,6 +4,7 @@ import com.salesforce.dockerfileimageupdate.model.*; import com.salesforce.dockerfileimageupdate.process.*; import net.sourceforge.argparse4j.inf.*; +import org.json.JSONException; import org.kohsuke.github.*; import org.mockito.*; import org.testng.*; @@ -199,7 +200,7 @@ public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundButEnabledK } @Test - public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndResourcesThrowAnException() throws IOException { + public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileFoundAndResourcesThrowAnException() throws IOException { PullRequests pullRequests = new PullRequests(); List filePaths = Collections.singletonList("renovate.json"); GitHubContentToProcess gitHubContentToProcess = mock(GitHubContentToProcess.class); @@ -211,6 +212,19 @@ public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndResource Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess)); } + @Test + public void testisRenovateEnabledReturnsFalseIfRenovateConfigFileFoundAndJSONParsingThrowsAnException() 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 JSONException("")); + Assert.assertFalse(pullRequests.isRenovateEnabled(filePaths, gitHubContentToProcess)); + } + @Test public void testisRenovateEnabledReturnsTrueIfRenovateConfigFileFoundAndEnabledKeySetToTrue() throws IOException { PullRequests pullRequests = new PullRequests();