From bfc3db49d73512fab27b90e7e45b8a586f3a954a Mon Sep 17 00:00:00 2001 From: Vincent Massol <vincent@massol.net> Date: Thu, 16 Feb 2023 14:44:21 +0100 Subject: [PATCH] XWIKI-20533: Allow users to see What's New in XWiki * Change NewsSourceFactory#create() to accept a descriptor, after Thomas's suggestion --- .../main/java/org/xwiki/whatsnew/NewsSourceFactory.java | 6 ++---- .../internal/configured/ConfiguredNewsSourceFactory.java | 9 ++++----- .../internal/xwikiblog/XWikiBlogNewsSourceFactory.java | 7 +++---- .../configured/ConfiguredNewsSourceFactoryTest.java | 4 +--- .../xwikiblog/XWikiBlogNewsSourceFactoryTest.java | 7 +++++-- 5 files changed, 15 insertions(+), 18 deletions(-) diff --git a/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/NewsSourceFactory.java b/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/NewsSourceFactory.java index bb3f599422c..79bab89b038 100644 --- a/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/NewsSourceFactory.java +++ b/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/NewsSourceFactory.java @@ -19,8 +19,6 @@ */ package org.xwiki.whatsnew; -import java.util.Map; - import org.xwiki.component.annotation.Role; import org.xwiki.stability.Unstable; @@ -42,10 +40,10 @@ public interface NewsSourceFactory { /** - * @param parameters the source-dependent list of parameters to configure the source + * @param descriptor the definition of a news source to be instantiated * @return the News source instance * @throws NewsException when there's a problem creating the news source (e.g. not specific RSS URL for XWiki Blog * source type) */ - NewsSource create(Map<String, String> parameters) throws NewsException; + NewsSource create(NewsSourceDescriptor descriptor) throws NewsException; } diff --git a/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/internal/configured/ConfiguredNewsSourceFactory.java b/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/internal/configured/ConfiguredNewsSourceFactory.java index 916667313c9..3f22efdaf54 100644 --- a/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/internal/configured/ConfiguredNewsSourceFactory.java +++ b/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/internal/configured/ConfiguredNewsSourceFactory.java @@ -21,7 +21,6 @@ import java.util.ArrayList; import java.util.List; -import java.util.Map; import javax.inject.Inject; import javax.inject.Named; @@ -56,8 +55,8 @@ public class ConfiguredNewsSourceFactory implements NewsSourceFactory, Initializ private ComponentManager componentManager; /** - * Cached news source so that calling several times {@link #create(Map)} will be performant and return the same - * News source. + * Cached news source so that calling several times {@link #create(NewsSourceDescriptor)} will be performant and + * return the same News source. */ private NewsSource source; @@ -73,7 +72,7 @@ public void initialize() throws InitializationException } @Override - public NewsSource create(Map<String, String> parameters) + public NewsSource create(NewsSourceDescriptor descriptor) { return this.source; } @@ -84,7 +83,7 @@ private NewsSource create() throws NewsException for (NewsSourceDescriptor descriptor : this.configuration.getNewsSourceDescriptors()) { NewsSourceFactory factory = getFactory(descriptor.getSourceTypeHint()); if (factory != null) { - sources.add(factory.create(descriptor.getParameters())); + sources.add(factory.create(descriptor)); } } return new CompositeNewsSource(sources); diff --git a/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/internal/xwikiblog/XWikiBlogNewsSourceFactory.java b/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/internal/xwikiblog/XWikiBlogNewsSourceFactory.java index da642461a2b..219033328d5 100644 --- a/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/internal/xwikiblog/XWikiBlogNewsSourceFactory.java +++ b/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/main/java/org/xwiki/whatsnew/internal/xwikiblog/XWikiBlogNewsSourceFactory.java @@ -19,14 +19,13 @@ */ package org.xwiki.whatsnew.internal.xwikiblog; -import java.util.Map; - import javax.inject.Named; import javax.inject.Singleton; import org.xwiki.component.annotation.Component; import org.xwiki.whatsnew.NewsException; import org.xwiki.whatsnew.NewsSource; +import org.xwiki.whatsnew.NewsSourceDescriptor; import org.xwiki.whatsnew.NewsSourceFactory; /** @@ -41,9 +40,9 @@ public class XWikiBlogNewsSourceFactory implements NewsSourceFactory { @Override - public NewsSource create(Map<String, String> parameters) throws NewsException + public NewsSource create(NewsSourceDescriptor descriptor) throws NewsException { - String rssURL = parameters.get("rssURL"); + String rssURL = descriptor.getParameters().get("rssURL"); if (rssURL == null) { throw new NewsException("Failed to create a XWiki Blog news source. A 'rssURL' parameter must be passed"); } diff --git a/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/test/java/org/xwiki/whatsnew/internal/configured/ConfiguredNewsSourceFactoryTest.java b/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/test/java/org/xwiki/whatsnew/internal/configured/ConfiguredNewsSourceFactoryTest.java index f70dd17c2ac..07a7b85d248 100644 --- a/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/test/java/org/xwiki/whatsnew/internal/configured/ConfiguredNewsSourceFactoryTest.java +++ b/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/test/java/org/xwiki/whatsnew/internal/configured/ConfiguredNewsSourceFactoryTest.java @@ -19,8 +19,6 @@ */ package org.xwiki.whatsnew.internal.configured; -import java.util.Collections; - import org.junit.jupiter.api.Test; import org.xwiki.test.junit5.mockito.ComponentTest; import org.xwiki.test.junit5.mockito.InjectMockComponents; @@ -37,7 +35,7 @@ class ConfiguredNewsSourceFactoryTest @Test void createWhenEmptyConfiguration() { - NewsSource source = this.factory.create(Collections.emptyMap()); + NewsSource source = this.factory.create(null); assertNotNull(source); } diff --git a/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/test/java/org/xwiki/whatsnew/internal/xwikiblog/XWikiBlogNewsSourceFactoryTest.java b/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/test/java/org/xwiki/whatsnew/internal/xwikiblog/XWikiBlogNewsSourceFactoryTest.java index ae0247cd21f..03b6f291732 100644 --- a/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/test/java/org/xwiki/whatsnew/internal/xwikiblog/XWikiBlogNewsSourceFactoryTest.java +++ b/xwiki-platform-core/xwiki-platform-whatsnew/xwiki-platform-whatsnew-api/src/test/java/org/xwiki/whatsnew/internal/xwikiblog/XWikiBlogNewsSourceFactoryTest.java @@ -26,6 +26,7 @@ import org.xwiki.test.junit5.mockito.InjectMockComponents; import org.xwiki.whatsnew.NewsException; import org.xwiki.whatsnew.NewsSource; +import org.xwiki.whatsnew.NewsSourceDescriptor; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -46,14 +47,16 @@ class XWikiBlogNewsSourceFactoryTest @Test void create() throws Exception { - NewsSource source = this.factory.create(Collections.singletonMap("rssURL", "some url")); + NewsSource source = + this.factory.create(new NewsSourceDescriptor("id", "hint", Collections.singletonMap("rssURL", "some url"))); assertNotNull(source); } @Test void createWhenNoRSSURLParameter() { - Throwable exception = assertThrows(NewsException.class, () -> this.factory.create(Collections.emptyMap())); + Throwable exception = assertThrows(NewsException.class, () -> this.factory.create(new NewsSourceDescriptor( + "id", "hint", Collections.emptyMap()))); assertEquals("Failed to create a XWiki Blog news source. A 'rssURL' parameter must be passed", exception.getMessage()); } -- GitLab