Skip to content

Commit

Permalink
Protects documentation service against potential null pointer excepti…
Browse files Browse the repository at this point in the history
…on (#383)
  • Loading branch information
alecharp authored Oct 12, 2023
1 parent b65ffa6 commit 639d92f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,15 @@ public PluginDocumentationService(ObjectMapper objectMapper, ApplicationConfigur

public Map<String, String> fetchPluginDocumentationUrl() {
try {
final Map<String, Link> foo = objectMapper.readValue(new URL(configuration.jenkins().documentationUrls()), new TypeReference<>() {});
return foo.entrySet().stream()
final Map<String, Link> documentationUrlsMap = objectMapper.readValue(
new URL(configuration.jenkins().documentationUrls()),
new TypeReference<>() {
}
);
return documentationUrlsMap.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
e -> e.getValue().url()
e -> e.getValue() == null || e.getValue().url() == null ? "" : e.getValue().url()
));
} catch (MalformedURLException e) {
LOGGER.error("URL to documentation link is incorrect.", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,24 @@ void shouldBeAbleToParseAvailableFile() {
.contains(entry("bar", "https://github.com/jenkinsci/bar-plugin"));
}

@Test
void shouldBeAbleToParseFileWithNullValue() {
final URL url = PluginDocumentationService.class.getResource("/documentation-urls/plugin-documentation-urls-with-nulls.json");
assertThat(url).isNotNull();

final ApplicationConfiguration config = new ApplicationConfiguration(
new ApplicationConfiguration.Jenkins("foo", url.toString()),
new ApplicationConfiguration.GitHub("foo", null, "bar")
);

final PluginDocumentationService service = new PluginDocumentationService(objectMapper, config);
final Map<String, String> map = service.fetchPluginDocumentationUrl();

assertThat(map)
.contains(entry("foo", "https://wiki.jenkins-ci.org/display/JENKINS/foo+plugin"))
.contains(entry("bar", "https://github.com/jenkinsci/bar-plugin"));
}

@Test
void shouldSurviveIncorrectlyConfiguredDocumentationURL() {
final ApplicationConfiguration config = new ApplicationConfiguration(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"foo": {
"url": "https://wiki.jenkins-ci.org/display/JENKINS/foo+plugin"
},
"bar": {
"url": "https://github.com/jenkinsci/bar-plugin"
},
"test": {
}
}

0 comments on commit 639d92f

Please sign in to comment.