From d7943a0551aaa6c427b17892e61bee802b53b193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Pfeuffer?= Date: Wed, 13 Jul 2022 09:28:56 +0200 Subject: [PATCH] Fix plugin wizard for foreign languages (#2086) This fixes the plugin wizard, that throws a NullPointerException for other languages then Englisch or German. --- gradle/changelog/plugin_wizard.yaml | 2 + .../api/v2/resources/PluginSetDtoMapper.java | 3 ++ .../v2/resources/PluginSetDtoMapperTest.java | 46 +++++++++++++------ 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 gradle/changelog/plugin_wizard.yaml diff --git a/gradle/changelog/plugin_wizard.yaml b/gradle/changelog/plugin_wizard.yaml new file mode 100644 index 0000000000..b98f51f6d1 --- /dev/null +++ b/gradle/changelog/plugin_wizard.yaml @@ -0,0 +1,2 @@ +- type: fixed + description: Plugin wizard for foreign languages ([#2086](https://github.com/scm-manager/scm-manager/pull/2086)) diff --git a/scm-webapp/src/main/java/sonia/scm/api/v2/resources/PluginSetDtoMapper.java b/scm-webapp/src/main/java/sonia/scm/api/v2/resources/PluginSetDtoMapper.java index 38327bb012..d3f2d35b4d 100644 --- a/scm-webapp/src/main/java/sonia/scm/api/v2/resources/PluginSetDtoMapper.java +++ b/scm-webapp/src/main/java/sonia/scm/api/v2/resources/PluginSetDtoMapper.java @@ -59,6 +59,9 @@ public class PluginSetDtoMapper { .collect(Collectors.toList()); PluginSet.Description description = pluginSet.getDescriptions().get(locale.getLanguage()); + if (description == null) { + description = pluginSet.getDescriptions().get(Locale.ENGLISH.getLanguage()); + } return new PluginSetDto(pluginSet.getId(), pluginSet.getSequence(), pluginDtos, description.getName(), description.getFeatures(), pluginSet.getImages()); } diff --git a/scm-webapp/src/test/java/sonia/scm/api/v2/resources/PluginSetDtoMapperTest.java b/scm-webapp/src/test/java/sonia/scm/api/v2/resources/PluginSetDtoMapperTest.java index 365c257982..31cb1bc30c 100644 --- a/scm-webapp/src/test/java/sonia/scm/api/v2/resources/PluginSetDtoMapperTest.java +++ b/scm-webapp/src/test/java/sonia/scm/api/v2/resources/PluginSetDtoMapperTest.java @@ -52,6 +52,35 @@ class PluginSetDtoMapperTest { @Test void shouldMap() { + List availablePlugins = createAvailablePlugins(); + ImmutableSet pluginSets = createPluginSets(); + + List dtos = mapper.map(pluginSets, availablePlugins, Locale.ENGLISH); + + assertThat(dtos).hasSize(2); + PluginSetDto first = dtos.get(0); + assertThat(first.getSequence()).isZero(); + assertThat(first.getName()).isEqualTo("My Plugin Set 2"); + assertThat(first.getFeatures()).contains("this is also awesome!"); + assertThat(first.getImages()).isNotEmpty(); + assertThat(first.getPlugins()).hasSize(2); + + assertThat(dtos.get(1).getSequence()).isEqualTo(1); + } + + @Test + void shouldMapWithOtherLanguage() { + List availablePlugins = createAvailablePlugins(); + ImmutableSet pluginSets = createPluginSets(); + + List dtos = mapper.map(pluginSets, availablePlugins, Locale.FRENCH); + + assertThat(dtos).hasSize(2); + PluginSetDto first = dtos.get(0); + assertThat(first.getName()).isEqualTo("My Plugin Set 2"); + } + + private List createAvailablePlugins() { AvailablePlugin git = createAvailable("scm-git-plugin"); AvailablePlugin svn = createAvailable("scm-svn-plugin"); AvailablePlugin hg = createAvailable("scm-hg-plugin"); @@ -66,8 +95,10 @@ class PluginSetDtoMapperTest { when(pluginDtoMapper.mapAvailable(svn)).thenReturn(svnDto); when(pluginDtoMapper.mapAvailable(hg)).thenReturn(hgDto); - List availablePlugins = List.of(git, svn, hg); + return List.of(git, svn, hg); + } + private ImmutableSet createPluginSets() { PluginSet pluginSet = new PluginSet( "my-plugin-set", 1, @@ -83,17 +114,6 @@ class PluginSetDtoMapperTest { ImmutableMap.of("en", new PluginSet.Description("My Plugin Set 2", List.of("this is also awesome!"))), ImmutableMap.of("standard", "base64image") ); - ImmutableSet pluginSets = ImmutableSet.of(pluginSet, pluginSet2); - - List dtos = mapper.map(pluginSets, availablePlugins, Locale.ENGLISH); - assertThat(dtos).hasSize(2); - PluginSetDto first = dtos.get(0); - assertThat(first.getSequence()).isZero(); - assertThat(first.getName()).isEqualTo("My Plugin Set 2"); - assertThat(first.getFeatures()).contains("this is also awesome!"); - assertThat(first.getImages()).isNotEmpty(); - assertThat(first.getPlugins()).hasSize(2); - - assertThat(dtos.get(1).getSequence()).isEqualTo(1); + return ImmutableSet.of(pluginSet, pluginSet2); } }