diff --git a/CHANGELOG.md b/CHANGELOG.md index f885d5469..25c39a98a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - Replace `SdmxWebConnection#ping()` with `SdmxConnection#testConnection()` - Refactor data query API [#218](https://github.com/nbbrd/sdmx-dl/issues/218) - Replace `SdmxConnection#isDetailSupported()` with a more general solution [#89](https://github.com/nbbrd/sdmx-dl/issues/89) +- Simplify class hierarchy [#222](https://github.com/nbbrd/sdmx-dl/issues/222) ### Fixed diff --git a/sdmx-dl-api/src/main/java/internal/sdmxdl/FunctionalListener.java b/sdmx-dl-api/src/main/java/internal/sdmxdl/FunctionalListener.java deleted file mode 100644 index 94cafce54..000000000 --- a/sdmx-dl-api/src/main/java/internal/sdmxdl/FunctionalListener.java +++ /dev/null @@ -1,45 +0,0 @@ -package internal.sdmxdl; - -import org.checkerframework.checker.nullness.qual.NonNull; -import sdmxdl.file.SdmxFileListener; -import sdmxdl.file.SdmxFileSource; -import sdmxdl.web.SdmxWebListener; -import sdmxdl.web.SdmxWebSource; - -import java.util.Objects; -import java.util.function.BiConsumer; - -@lombok.Builder -public final class FunctionalListener implements SdmxWebListener, SdmxFileListener { - - @lombok.NonNull - @lombok.Builder.Default - private final BiConsumer onWeb = FunctionalListener::doNothing; - - @lombok.NonNull - @lombok.Builder.Default - private final BiConsumer onFile = FunctionalListener::doNothing; - - @Override - public boolean isEnabled() { - return true; - } - - @Override - public void onWebSourceEvent(@NonNull SdmxWebSource source, @NonNull String message) { - Objects.requireNonNull(source); - Objects.requireNonNull(message); - onWeb.accept(source, message); - } - - @Override - public void onFileSourceEvent(@NonNull SdmxFileSource source, @NonNull String message) { - Objects.requireNonNull(source); - Objects.requireNonNull(message); - onFile.accept(source, message); - } - - @SuppressWarnings("EmptyMethod") - private static void doNothing(Object source, Object message) { - } -} diff --git a/sdmx-dl-api/src/main/java/internal/sdmxdl/LoggingListener.java b/sdmx-dl-api/src/main/java/internal/sdmxdl/LoggingListener.java deleted file mode 100644 index c6e85b076..000000000 --- a/sdmx-dl-api/src/main/java/internal/sdmxdl/LoggingListener.java +++ /dev/null @@ -1,35 +0,0 @@ -package internal.sdmxdl; - -import org.checkerframework.checker.nullness.qual.NonNull; -import sdmxdl.file.SdmxFileListener; -import sdmxdl.file.SdmxFileSource; -import sdmxdl.web.SdmxWebListener; -import sdmxdl.web.SdmxWebSource; - -import java.util.Objects; -import java.util.logging.Level; - -@lombok.extern.java.Log -public enum LoggingListener implements SdmxWebListener, SdmxFileListener { - - INSTANCE; - - @Override - public boolean isEnabled() { - return log.isLoggable(Level.INFO); - } - - @Override - public void onWebSourceEvent(SdmxWebSource source, String message) { - Objects.requireNonNull(source); - Objects.requireNonNull(message); - log.log(Level.INFO, message); - } - - @Override - public void onFileSourceEvent(@NonNull SdmxFileSource source, @NonNull String message) { - Objects.requireNonNull(source); - Objects.requireNonNull(message); - log.log(Level.INFO, message); - } -} diff --git a/sdmx-dl-api/src/main/java/internal/sdmxdl/NoOpListener.java b/sdmx-dl-api/src/main/java/internal/sdmxdl/NoOpListener.java deleted file mode 100644 index 893572f4c..000000000 --- a/sdmx-dl-api/src/main/java/internal/sdmxdl/NoOpListener.java +++ /dev/null @@ -1,32 +0,0 @@ -package internal.sdmxdl; - -import org.checkerframework.checker.nullness.qual.NonNull; -import sdmxdl.file.SdmxFileListener; -import sdmxdl.file.SdmxFileSource; -import sdmxdl.web.SdmxWebListener; -import sdmxdl.web.SdmxWebSource; - -import java.util.Objects; - -@lombok.extern.java.Log -public enum NoOpListener implements SdmxWebListener, SdmxFileListener { - - INSTANCE; - - @Override - public boolean isEnabled() { - return false; - } - - @Override - public void onWebSourceEvent(@NonNull SdmxWebSource source, @NonNull String message) { - Objects.requireNonNull(source); - Objects.requireNonNull(message); - } - - @Override - public void onFileSourceEvent(@NonNull SdmxFileSource source, @NonNull String message) { - Objects.requireNonNull(source); - Objects.requireNonNull(message); - } -} diff --git a/sdmx-dl-api/src/main/java/internal/sdmxdl/web/spi/FailsafeWebConnection.java b/sdmx-dl-api/src/main/java/internal/sdmxdl/web/spi/FailsafeConnection.java similarity index 87% rename from sdmx-dl-api/src/main/java/internal/sdmxdl/web/spi/FailsafeWebConnection.java rename to sdmx-dl-api/src/main/java/internal/sdmxdl/web/spi/FailsafeConnection.java index d913311d3..5f7b186ac 100644 --- a/sdmx-dl-api/src/main/java/internal/sdmxdl/web/spi/FailsafeWebConnection.java +++ b/sdmx-dl-api/src/main/java/internal/sdmxdl/web/spi/FailsafeConnection.java @@ -19,7 +19,6 @@ import lombok.AccessLevel; import org.checkerframework.checker.nullness.qual.NonNull; import sdmxdl.*; -import sdmxdl.web.SdmxWebConnection; import java.io.IOException; import java.util.Collection; @@ -34,16 +33,16 @@ */ @SuppressWarnings("ConstantConditions") @lombok.AllArgsConstructor(access = AccessLevel.PACKAGE) -final class FailsafeWebConnection implements SdmxWebConnection { +final class FailsafeConnection implements SdmxConnection { - static SdmxWebConnection wrap(SdmxWebConnection obj) { - if (obj instanceof FailsafeWebConnection) return obj; + static SdmxConnection wrap(SdmxConnection obj) { + if (obj instanceof FailsafeConnection) return obj; FailsafeLogging logging = FailsafeLogging.of(FailsafeWebDriver.class); - return new FailsafeWebConnection(obj, logging::logUnexpectedError, logging::logUnexpectedNull); + return new FailsafeConnection(obj, logging::logUnexpectedError, logging::logUnexpectedNull); } @lombok.NonNull - private final SdmxWebConnection delegate; + private final SdmxConnection delegate; @lombok.NonNull private final BiConsumer onUnexpectedError; @@ -60,23 +59,6 @@ public void testConnection() throws IOException { } } - @Override - public String getDriver() throws IOException { - String result; - - try { - result = delegate.getDriver(); - } catch (RuntimeException ex) { - throw unexpectedError(ex, "while getting driver"); - } - - if (result == null) { - throw unexpectedNull("driver"); - } - - return result; - } - @Override public Collection getFlows() throws IOException { Collection result; diff --git a/sdmx-dl-api/src/main/java/internal/sdmxdl/web/spi/FailsafeWebDriver.java b/sdmx-dl-api/src/main/java/internal/sdmxdl/web/spi/FailsafeWebDriver.java index 656325605..9a5cb11fb 100644 --- a/sdmx-dl-api/src/main/java/internal/sdmxdl/web/spi/FailsafeWebDriver.java +++ b/sdmx-dl-api/src/main/java/internal/sdmxdl/web/spi/FailsafeWebDriver.java @@ -17,7 +17,7 @@ package internal.sdmxdl.web.spi; import lombok.AccessLevel; -import sdmxdl.web.SdmxWebConnection; +import sdmxdl.SdmxConnection; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; import sdmxdl.web.spi.SdmxWebDriver; @@ -90,11 +90,11 @@ public boolean isAvailable() { } @Override - public SdmxWebConnection connect(SdmxWebSource source, SdmxWebContext context) throws IOException, IllegalArgumentException { + public SdmxConnection connect(SdmxWebSource source, SdmxWebContext context) throws IOException, IllegalArgumentException { Objects.requireNonNull(source); Objects.requireNonNull(context); - SdmxWebConnection result; + SdmxConnection result; try { result = delegate.connect(source, context); @@ -108,7 +108,7 @@ public SdmxWebConnection connect(SdmxWebSource source, SdmxWebContext context) t throw newUnexpectedNull("null connection"); } - return FailsafeWebConnection.wrap(result); + return FailsafeConnection.wrap(result); } @Override diff --git a/sdmx-dl-api/src/main/java/sdmxdl/SdmxManager.java b/sdmx-dl-api/src/main/java/sdmxdl/SdmxManager.java index 54144b833..5dcd7e6e3 100644 --- a/sdmx-dl-api/src/main/java/sdmxdl/SdmxManager.java +++ b/sdmx-dl-api/src/main/java/sdmxdl/SdmxManager.java @@ -26,6 +26,7 @@ import java.io.IOException; import java.util.List; +import java.util.function.BiConsumer; /** * @author Philippe Charles @@ -35,7 +36,7 @@ SdmxWebManager.class }) @ThreadSafe -public abstract class SdmxManager { +public abstract class SdmxManager { public abstract @NonNull SdmxConnection getConnection(@NonNull SOURCE source) throws IOException; @@ -43,5 +44,12 @@ public abstract class SdmxManager { public abstract @NonNull SdmxCache getCache(); + public abstract @NonNull BiConsumer getEventListener(); + public abstract @NonNull List getDialects(); + + public static final BiConsumer NO_OP_EVENT_LISTENER = SdmxManager::doNothing; + + private static void doNothing(SdmxSource source, String message) { + } } diff --git a/sdmx-dl-api/src/main/java/sdmxdl/SdmxSource.java b/sdmx-dl-api/src/main/java/sdmxdl/SdmxSource.java new file mode 100644 index 000000000..47a4cc0d1 --- /dev/null +++ b/sdmx-dl-api/src/main/java/sdmxdl/SdmxSource.java @@ -0,0 +1,12 @@ +package sdmxdl; + +import nbbrd.design.SealedType; +import sdmxdl.file.SdmxFileSource; +import sdmxdl.web.SdmxWebSource; + +@SealedType({ + SdmxFileSource.class, + SdmxWebSource.class +}) +public abstract class SdmxSource { +} diff --git a/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileConnection.java b/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileConnection.java deleted file mode 100644 index 9aee323b4..000000000 --- a/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileConnection.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2017 National Bank of Belgium - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved - * by the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * - * http://ec.europa.eu/idabc/eupl - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - */ -package sdmxdl.file; - -import nbbrd.design.NotThreadSafe; -import org.checkerframework.checker.nullness.qual.NonNull; -import sdmxdl.DataStructure; -import sdmxdl.Dataflow; -import sdmxdl.DataflowRef; -import sdmxdl.SdmxConnection; -import sdmxdl.ext.SdmxException; - -import java.io.IOException; - -/** - * @author Philippe Charles - */ -@NotThreadSafe -public interface SdmxFileConnection extends SdmxConnection { - - @NonNull - DataflowRef getDataflowRef() throws IOException; - - @NonNull - default Dataflow getFlow() throws IOException { - DataflowRef ref = getDataflowRef(); - return getFlows() - .stream() - .filter(o -> o.getRef().equals(ref)) - .findFirst() - .orElseThrow(() -> SdmxException.missingFlow("", ref)); - } - - @NonNull - default DataStructure getStructure() throws IOException { - return getStructure(getDataflowRef()); - } -} diff --git a/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileListener.java b/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileListener.java deleted file mode 100644 index 6638df903..000000000 --- a/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileListener.java +++ /dev/null @@ -1,33 +0,0 @@ -package sdmxdl.file; - -import internal.sdmxdl.FunctionalListener; -import internal.sdmxdl.LoggingListener; -import internal.sdmxdl.NoOpListener; -import nbbrd.design.StaticFactoryMethod; -import nbbrd.design.ThreadSafe; -import org.checkerframework.checker.nullness.qual.NonNull; - -import java.util.function.BiConsumer; - -@ThreadSafe -public interface SdmxFileListener { - - boolean isEnabled(); - - void onFileSourceEvent(@NonNull SdmxFileSource source, @NonNull String message); - - @StaticFactoryMethod - static @NonNull SdmxFileListener getDefault() { - return LoggingListener.INSTANCE; - } - - @StaticFactoryMethod - static @NonNull SdmxFileListener noOp() { - return NoOpListener.INSTANCE; - } - - @StaticFactoryMethod - static @NonNull SdmxFileListener of(@NonNull BiConsumer listener) { - return FunctionalListener.builder().onFile(listener).build(); - } -} diff --git a/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileManager.java b/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileManager.java index 14b5c41f0..ff9d51e55 100644 --- a/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileManager.java +++ b/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileManager.java @@ -19,6 +19,7 @@ import lombok.AccessLevel; import org.checkerframework.checker.nullness.qual.NonNull; import sdmxdl.LanguagePriorityList; +import sdmxdl.SdmxConnection; import sdmxdl.SdmxManager; import sdmxdl.ext.SdmxCache; import sdmxdl.ext.spi.SdmxDialect; @@ -31,6 +32,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; +import java.util.function.BiConsumer; /** * @author Philippe Charles @@ -58,7 +60,7 @@ public static SdmxFileManager ofServiceLoader() { @lombok.NonNull @lombok.Builder.Default - SdmxFileListener eventListener = SdmxFileListener.getDefault(); + BiConsumer eventListener = NO_OP_EVENT_LISTENER; @lombok.NonNull @lombok.Singular @@ -73,7 +75,7 @@ public static SdmxFileManager ofServiceLoader() { SdmxFileContext context = initContext(); @Override - public @NonNull SdmxFileConnection getConnection(@NonNull SdmxFileSource source) throws IOException { + public @NonNull SdmxConnection getConnection(@NonNull SdmxFileSource source) throws IOException { Objects.requireNonNull(source); SdmxFileReader reader = lookupReader(source) diff --git a/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileSource.java b/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileSource.java index 3788df979..ac47230ac 100644 --- a/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileSource.java +++ b/sdmx-dl-api/src/main/java/sdmxdl/file/SdmxFileSource.java @@ -19,6 +19,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import sdmxdl.DataflowRef; +import sdmxdl.SdmxSource; import java.io.File; @@ -27,7 +28,8 @@ */ @lombok.Value @lombok.Builder(toBuilder = true) -public class SdmxFileSource { +@lombok.EqualsAndHashCode(callSuper = false) +public class SdmxFileSource extends SdmxSource { @lombok.NonNull File data; diff --git a/sdmx-dl-api/src/main/java/sdmxdl/file/spi/SdmxFileContext.java b/sdmx-dl-api/src/main/java/sdmxdl/file/spi/SdmxFileContext.java index d80777433..e5bfa659f 100644 --- a/sdmx-dl-api/src/main/java/sdmxdl/file/spi/SdmxFileContext.java +++ b/sdmx-dl-api/src/main/java/sdmxdl/file/spi/SdmxFileContext.java @@ -1,11 +1,13 @@ package sdmxdl.file.spi; import sdmxdl.LanguagePriorityList; +import sdmxdl.SdmxManager; import sdmxdl.ext.SdmxCache; import sdmxdl.ext.spi.SdmxDialect; -import sdmxdl.file.SdmxFileListener; +import sdmxdl.file.SdmxFileSource; import java.util.List; +import java.util.function.BiConsumer; @lombok.Value @lombok.Builder(toBuilder = true) @@ -25,5 +27,5 @@ public class SdmxFileContext { @lombok.NonNull @lombok.Builder.Default - SdmxFileListener eventListener = SdmxFileListener.getDefault(); + BiConsumer eventListener = SdmxManager.NO_OP_EVENT_LISTENER; } diff --git a/sdmx-dl-api/src/main/java/sdmxdl/file/spi/SdmxFileReader.java b/sdmx-dl-api/src/main/java/sdmxdl/file/spi/SdmxFileReader.java index 726653249..25f770dad 100644 --- a/sdmx-dl-api/src/main/java/sdmxdl/file/spi/SdmxFileReader.java +++ b/sdmx-dl-api/src/main/java/sdmxdl/file/spi/SdmxFileReader.java @@ -20,7 +20,7 @@ import nbbrd.service.Quantifier; import nbbrd.service.ServiceDefinition; import org.checkerframework.checker.nullness.qual.NonNull; -import sdmxdl.file.SdmxFileConnection; +import sdmxdl.SdmxConnection; import sdmxdl.file.SdmxFileSource; import java.io.IOException; @@ -37,7 +37,7 @@ public interface SdmxFileReader { boolean canRead(@NonNull SdmxFileSource source); @NonNull - SdmxFileConnection read( + SdmxConnection read( @NonNull SdmxFileSource source, @NonNull SdmxFileContext context ) throws IOException, IllegalArgumentException; diff --git a/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebConnection.java b/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebConnection.java deleted file mode 100644 index 532595493..000000000 --- a/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebConnection.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2017 National Bank of Belgium - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved - * by the European Commission - subsequent versions of the EUPL (the "Licence"); - * You may not use this work except in compliance with the Licence. - * You may obtain a copy of the Licence at: - * - * http://ec.europa.eu/idabc/eupl - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the Licence is distributed on an "AS IS" basis, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and - * limitations under the Licence. - */ -package sdmxdl.web; - -import nbbrd.design.NotThreadSafe; -import org.checkerframework.checker.nullness.qual.NonNull; -import sdmxdl.SdmxConnection; - -import java.io.IOException; - -/** - * @author Philippe Charles - */ -@NotThreadSafe -public interface SdmxWebConnection extends SdmxConnection { - - @NonNull - String getDriver() throws IOException; -} diff --git a/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebListener.java b/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebListener.java deleted file mode 100644 index 3e47b5a26..000000000 --- a/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebListener.java +++ /dev/null @@ -1,33 +0,0 @@ -package sdmxdl.web; - -import internal.sdmxdl.FunctionalListener; -import internal.sdmxdl.LoggingListener; -import internal.sdmxdl.NoOpListener; -import nbbrd.design.StaticFactoryMethod; -import nbbrd.design.ThreadSafe; -import org.checkerframework.checker.nullness.qual.NonNull; - -import java.util.function.BiConsumer; - -@ThreadSafe -public interface SdmxWebListener { - - boolean isEnabled(); - - void onWebSourceEvent(@NonNull SdmxWebSource source, @NonNull String message); - - @StaticFactoryMethod - static @NonNull SdmxWebListener getDefault() { - return LoggingListener.INSTANCE; - } - - @StaticFactoryMethod - static @NonNull SdmxWebListener noOp() { - return NoOpListener.INSTANCE; - } - - @StaticFactoryMethod - static @NonNull SdmxWebListener of(@NonNull BiConsumer listener) { - return FunctionalListener.builder().onWeb(listener).build(); - } -} diff --git a/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebManager.java b/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebManager.java index 998b9c323..6d92a525c 100644 --- a/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebManager.java +++ b/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebManager.java @@ -22,6 +22,7 @@ import lombok.AccessLevel; import org.checkerframework.checker.nullness.qual.NonNull; import sdmxdl.LanguagePriorityList; +import sdmxdl.SdmxConnection; import sdmxdl.SdmxManager; import sdmxdl.ext.NetworkFactory; import sdmxdl.ext.SdmxCache; @@ -37,6 +38,7 @@ import java.net.URI; import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.BiConsumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collector; @@ -88,7 +90,7 @@ public static SdmxWebManager ofServiceLoader() { @lombok.NonNull @lombok.Builder.Default - SdmxWebListener eventListener = SdmxWebListener.getDefault(); + BiConsumer eventListener = NO_OP_EVENT_LISTENER; @lombok.NonNull @lombok.Singular @@ -110,7 +112,7 @@ public static SdmxWebManager ofServiceLoader() { @lombok.Getter(lazy = true, value = AccessLevel.PRIVATE) SdmxWebContext context = initContext(); - public @NonNull SdmxWebConnection getConnection(@NonNull String name) throws IOException { + public @NonNull SdmxConnection getConnection(@NonNull String name) throws IOException { Objects.requireNonNull(name); SdmxWebSource source = lookupSource(name) @@ -120,7 +122,7 @@ public static SdmxWebManager ofServiceLoader() { } @Override - public @NonNull SdmxWebConnection getConnection(@NonNull SdmxWebSource source) throws IOException { + public @NonNull SdmxConnection getConnection(@NonNull SdmxWebSource source) throws IOException { Objects.requireNonNull(source); SdmxWebDriver driver = lookupDriver(source.getDriver()) @@ -158,12 +160,12 @@ public SdmxWebMonitorReport getMonitorReport(@NonNull SdmxWebSource source) thro } private void checkSourceProperties(SdmxWebSource source, SdmxWebDriver driver) { - if (eventListener.isEnabled()) { + if (eventListener != NO_OP_EVENT_LISTENER) { Collection expected = driver.getSupportedProperties(); Collection found = source.getProperties().keySet(); String diff = found.stream().filter(item -> !expected.contains(item)).sorted().collect(Collectors.joining(",")); if (!diff.isEmpty()) { - eventListener.onWebSourceEvent(source, "Unexpected properties [" + diff + "]"); + eventListener.accept(source, "Unexpected properties [" + diff + "]"); } } } diff --git a/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebSource.java b/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebSource.java index 0cc5b8ff2..c8120f177 100644 --- a/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebSource.java +++ b/sdmx-dl-api/src/main/java/sdmxdl/web/SdmxWebSource.java @@ -18,6 +18,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; +import sdmxdl.SdmxSource; import java.net.MalformedURLException; import java.net.URI; @@ -32,7 +33,8 @@ */ @lombok.Value @lombok.Builder(toBuilder = true) -public class SdmxWebSource { +@lombok.EqualsAndHashCode(callSuper = false) +public class SdmxWebSource extends SdmxSource { @lombok.NonNull String name; diff --git a/sdmx-dl-api/src/main/java/sdmxdl/web/spi/SdmxWebContext.java b/sdmx-dl-api/src/main/java/sdmxdl/web/spi/SdmxWebContext.java index 7ff9d9942..5601bd4c1 100644 --- a/sdmx-dl-api/src/main/java/sdmxdl/web/spi/SdmxWebContext.java +++ b/sdmx-dl-api/src/main/java/sdmxdl/web/spi/SdmxWebContext.java @@ -17,12 +17,14 @@ package sdmxdl.web.spi; import sdmxdl.LanguagePriorityList; +import sdmxdl.SdmxManager; import sdmxdl.ext.NetworkFactory; import sdmxdl.ext.SdmxCache; import sdmxdl.ext.spi.SdmxDialect; -import sdmxdl.web.SdmxWebListener; +import sdmxdl.web.SdmxWebSource; import java.util.List; +import java.util.function.BiConsumer; /** * @author Philippe Charles @@ -45,7 +47,7 @@ public class SdmxWebContext { @lombok.NonNull @lombok.Builder.Default - SdmxWebListener eventListener = SdmxWebListener.getDefault(); + BiConsumer eventListener = SdmxManager.NO_OP_EVENT_LISTENER; @lombok.NonNull @lombok.Singular diff --git a/sdmx-dl-api/src/main/java/sdmxdl/web/spi/SdmxWebDriver.java b/sdmx-dl-api/src/main/java/sdmxdl/web/spi/SdmxWebDriver.java index e96462eb0..ec8238326 100644 --- a/sdmx-dl-api/src/main/java/sdmxdl/web/spi/SdmxWebDriver.java +++ b/sdmx-dl-api/src/main/java/sdmxdl/web/spi/SdmxWebDriver.java @@ -1,17 +1,17 @@ /* * Copyright 2015 National Bank of Belgium - * - * Licensed under the EUPL, Version 1.1 or - as soon they will be approved + * + * Licensed under the EUPL, Version 1.1 or - as soon they will be approved * by the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: - * + * * http://ec.europa.eu/idabc/eupl - * - * Unless required by applicable law or agreed to in writing, software + * + * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the Licence for the specific language governing permissions and + * See the Licence for the specific language governing permissions and * limitations under the Licence. */ package sdmxdl.web.spi; @@ -23,14 +23,13 @@ import nbbrd.service.ServiceFilter; import nbbrd.service.ServiceSorter; import org.checkerframework.checker.nullness.qual.NonNull; -import sdmxdl.web.SdmxWebConnection; +import sdmxdl.SdmxConnection; import sdmxdl.web.SdmxWebSource; import java.io.IOException; import java.util.Collection; /** - * * @author Philippe Charles */ @ServiceDefinition( @@ -51,7 +50,7 @@ public interface SdmxWebDriver { boolean isAvailable(); @NonNull - SdmxWebConnection connect( + SdmxConnection connect( @NonNull SdmxWebSource source, @NonNull SdmxWebContext context ) throws IOException, IllegalArgumentException; diff --git a/sdmx-dl-api/src/test/java/_test/sdmxdl/TestConnection.java b/sdmx-dl-api/src/test/java/_test/sdmxdl/TestConnection.java index 6a7821b9d..fdbe5e661 100644 --- a/sdmx-dl-api/src/test/java/_test/sdmxdl/TestConnection.java +++ b/sdmx-dl-api/src/test/java/_test/sdmxdl/TestConnection.java @@ -18,7 +18,6 @@ import org.checkerframework.checker.nullness.qual.NonNull; import sdmxdl.*; -import sdmxdl.web.SdmxWebConnection; import tests.sdmxdl.api.RepoSamples; import java.util.Collection; @@ -29,17 +28,12 @@ /** * @author Philippe Charles */ -public enum TestConnection implements SdmxWebConnection { +public enum TestConnection implements SdmxConnection { VALID { @Override public void testConnection() { } - @Override - public String getDriver() { - return DRIVER; - } - @Override public Collection getFlows() { return RepoSamples.REPO.getFlows(); @@ -80,11 +74,6 @@ public void testConnection() { throw new CustomException(); } - @Override - public String getDriver() { - throw new CustomException(); - } - @Override public Collection getFlows() { throw new CustomException(); @@ -125,11 +114,6 @@ public void close() { public void testConnection() { } - @Override - public String getDriver() { - return null; - } - @Override public Collection getFlows() { return null; @@ -165,6 +149,4 @@ public void close() { throw new UnsupportedOperationException(); } }; - - public static final String DRIVER = "validDriver"; } diff --git a/sdmx-dl-api/src/test/java/_test/sdmxdl/TestDriver.java b/sdmx-dl-api/src/test/java/_test/sdmxdl/TestDriver.java index 12e5244ee..2d692ec79 100644 --- a/sdmx-dl-api/src/test/java/_test/sdmxdl/TestDriver.java +++ b/sdmx-dl-api/src/test/java/_test/sdmxdl/TestDriver.java @@ -16,7 +16,7 @@ */ package _test.sdmxdl; -import sdmxdl.web.SdmxWebConnection; +import sdmxdl.SdmxConnection; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; import sdmxdl.web.spi.SdmxWebDriver; @@ -45,7 +45,7 @@ public boolean isAvailable() { } @Override - public SdmxWebConnection connect(SdmxWebSource source, SdmxWebContext context) throws IllegalArgumentException { + public SdmxConnection connect(SdmxWebSource source, SdmxWebContext context) throws IllegalArgumentException { return TestConnection.VALID; } @@ -75,7 +75,7 @@ public boolean isAvailable() { } @Override - public SdmxWebConnection connect(SdmxWebSource source, SdmxWebContext context) throws IllegalArgumentException { + public SdmxConnection connect(SdmxWebSource source, SdmxWebContext context) throws IllegalArgumentException { throw new CustomException(); } @@ -105,7 +105,7 @@ public boolean isAvailable() { } @Override - public SdmxWebConnection connect(SdmxWebSource source, SdmxWebContext context) throws IllegalArgumentException { + public SdmxConnection connect(SdmxWebSource source, SdmxWebContext context) throws IllegalArgumentException { return null; } diff --git a/sdmx-dl-api/src/test/java/internal/sdmxdl/web/spi/FailsafeWebConnectionTest.java b/sdmx-dl-api/src/test/java/internal/sdmxdl/web/spi/FailsafeConnectionTest.java similarity index 85% rename from sdmx-dl-api/src/test/java/internal/sdmxdl/web/spi/FailsafeWebConnectionTest.java rename to sdmx-dl-api/src/test/java/internal/sdmxdl/web/spi/FailsafeConnectionTest.java index 4dcd17e39..cc2baba1c 100644 --- a/sdmx-dl-api/src/test/java/internal/sdmxdl/web/spi/FailsafeWebConnectionTest.java +++ b/sdmx-dl-api/src/test/java/internal/sdmxdl/web/spi/FailsafeConnectionTest.java @@ -23,7 +23,6 @@ import sdmxdl.DataQuery; import tests.sdmxdl.api.RepoSamples; import tests.sdmxdl.api.SdmxConnectionAssert; -import tests.sdmxdl.web.MockedWebConnection; import static org.assertj.core.api.Assertions.assertThatIOException; import static org.assertj.core.api.Assertions.assertThatNoException; @@ -32,12 +31,12 @@ * @author Philippe Charles */ @lombok.extern.java.Log -public class FailsafeWebConnectionTest { +public class FailsafeConnectionTest { @Test public void testCompliance() { SdmxConnectionAssert.assertCompliance( - () -> FailsafeWebConnection.wrap(new MockedWebConnection("driver", RepoSamples.REPO.asConnection())), + () -> FailsafeConnection.wrap(RepoSamples.REPO.asConnection()), SdmxConnectionAssert.Sample .builder() .validFlow(RepoSamples.FLOW_REF) @@ -62,26 +61,6 @@ public void testTestConnection() { failsafe.assertUnexpectedError("unexpected CustomException", CustomException.class); } - @Test - public void testGetDriver() { - failsafe.reset(); - assertThatNoException() - .isThrownBy(valid::getDriver); - failsafe.assertEmpty(); - - failsafe.reset(); - assertThatIOException() - .isThrownBy(failing::getDriver) - .withCauseInstanceOf(CustomException.class); - failsafe.assertUnexpectedError("unexpected CustomException", CustomException.class); - - failsafe.reset(); - assertThatIOException() - .isThrownBy(nul::getDriver) - .withNoCause(); - failsafe.assertUnexpectedNull("unexpected null"); - } - @Test public void testGetFlows() { failsafe.reset(); @@ -218,7 +197,7 @@ public void testClose() { private final FailsafeHandler failsafe = new FailsafeHandler(); - private final FailsafeWebConnection valid = new FailsafeWebConnection(TestConnection.VALID, failsafe, failsafe); - private final FailsafeWebConnection failing = new FailsafeWebConnection(TestConnection.FAILING, failsafe, failsafe); - private final FailsafeWebConnection nul = new FailsafeWebConnection(TestConnection.NULL, failsafe, failsafe); + private final FailsafeConnection valid = new FailsafeConnection(TestConnection.VALID, failsafe, failsafe); + private final FailsafeConnection failing = new FailsafeConnection(TestConnection.FAILING, failsafe, failsafe); + private final FailsafeConnection nul = new FailsafeConnection(TestConnection.NULL, failsafe, failsafe); } diff --git a/sdmx-dl-api/src/test/java/internal/sdmxdl/web/spi/FailsafeWebDriverTest.java b/sdmx-dl-api/src/test/java/internal/sdmxdl/web/spi/FailsafeWebDriverTest.java index df31b302e..8242d3b44 100644 --- a/sdmx-dl-api/src/test/java/internal/sdmxdl/web/spi/FailsafeWebDriverTest.java +++ b/sdmx-dl-api/src/test/java/internal/sdmxdl/web/spi/FailsafeWebDriverTest.java @@ -85,7 +85,7 @@ public void testConnect() throws IOException { failsafe.reset(); assertThat(valid.connect(TestDriver.SOURCE, context)) .isNotNull() - .isInstanceOf(FailsafeWebConnection.class); + .isInstanceOf(FailsafeConnection.class); failsafe.assertEmpty(); failsafe.reset(); diff --git a/sdmx-dl-api/src/test/java/sdmxdl/file/SdmxFileListenerTest.java b/sdmx-dl-api/src/test/java/sdmxdl/file/SdmxFileListenerTest.java deleted file mode 100644 index 4f1bd115e..000000000 --- a/sdmx-dl-api/src/test/java/sdmxdl/file/SdmxFileListenerTest.java +++ /dev/null @@ -1,39 +0,0 @@ -package sdmxdl.file; - -import nl.altindag.log.LogCaptor; -import nl.altindag.log.model.LogEvent; -import org.junit.jupiter.api.Test; -import tests.sdmxdl.file.SdmxFileListenerAssert; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.InstanceOfAssertFactories.STRING; - -public class SdmxFileListenerTest { - - @Test - public void testDefault() { - LogCaptor logCaptor = LogCaptor.forName("internal.sdmxdl.LoggingListener"); - logCaptor.setLogLevelToInfo(); - - SdmxFileListenerAssert.assertCompliance(SdmxFileListener.getDefault()); - - assertThat(logCaptor.getLogEvents()) - .hasSize(1) - .map(LogEvent::getMessage) - .element(0, STRING) - .isEqualTo("hello"); - } - - @Test - public void testNoOp() { - SdmxFileListenerAssert.assertCompliance(SdmxFileListener.noOp()); - } - - @Test - public void testFunctional() { - SdmxFileListenerAssert.assertCompliance(SdmxFileListener.of(this::doNothing)); - } - - private void doNothing(Object source, Object message) { - } -} diff --git a/sdmx-dl-api/src/test/java/sdmxdl/web/SdmxWebListenerTest.java b/sdmx-dl-api/src/test/java/sdmxdl/web/SdmxWebListenerTest.java deleted file mode 100644 index 5645d2c4e..000000000 --- a/sdmx-dl-api/src/test/java/sdmxdl/web/SdmxWebListenerTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package sdmxdl.web; - -import nl.altindag.log.LogCaptor; -import nl.altindag.log.model.LogEvent; -import org.junit.jupiter.api.Test; -import tests.sdmxdl.web.SdmxWebListenerAssert; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatNullPointerException; -import static org.assertj.core.api.InstanceOfAssertFactories.STRING; - -public class SdmxWebListenerTest { - - @Test - public void testFactories() { - assertThatNullPointerException().isThrownBy(() -> SdmxWebListener.of(null)); - } - - @Test - public void testDefault() { - LogCaptor logCaptor = LogCaptor.forName("internal.sdmxdl.LoggingListener"); - logCaptor.setLogLevelToInfo(); - - SdmxWebListenerAssert.assertCompliance(SdmxWebListener.getDefault()); - - assertThat(logCaptor.getLogEvents()) - .hasSize(1) - .map(LogEvent::getMessage) - .element(0, STRING) - .isEqualTo("hello"); - } - - @Test - public void testNoOp() { - SdmxWebListenerAssert.assertCompliance(SdmxWebListener.noOp()); - } - - @Test - public void testFunctional() { - SdmxWebListenerAssert.assertCompliance(SdmxWebListener.of(this::doNothing)); - } - - private void doNothing(Object source, Object message) { - } -} diff --git a/sdmx-dl-api/src/test/java/sdmxdl/web/SdmxWebManagerTest.java b/sdmx-dl-api/src/test/java/sdmxdl/web/SdmxWebManagerTest.java index 03b415170..6244885fb 100644 --- a/sdmx-dl-api/src/test/java/sdmxdl/web/SdmxWebManagerTest.java +++ b/sdmx-dl-api/src/test/java/sdmxdl/web/SdmxWebManagerTest.java @@ -18,6 +18,8 @@ import org.junit.jupiter.api.Test; import sdmxdl.LanguagePriorityList; +import sdmxdl.SdmxConnection; +import sdmxdl.SdmxManager; import sdmxdl.ext.NetworkFactory; import sdmxdl.ext.SdmxCache; import sdmxdl.ext.spi.SdmxDialect; @@ -28,7 +30,6 @@ import tests.sdmxdl.web.MockedWebDriver; import java.io.IOException; -import java.net.URI; import java.util.AbstractMap; import java.util.ArrayList; import java.util.List; @@ -65,7 +66,7 @@ public void testFactories() { assertThat(o.getLanguages()).isEqualTo(LanguagePriorityList.ANY); assertThat(o.getNetwork()).isEqualTo(NetworkFactory.getDefault()); assertThat(o.getCache()).isEqualTo(SdmxCache.noOp()); - assertThat(o.getEventListener()).isEqualTo(SdmxWebListener.getDefault()); + assertThat(o.getEventListener()).isEqualTo(SdmxManager.NO_OP_EVENT_LISTENER); assertThat(o.getAuthenticators()).isEmpty(); assertThat(o.getCustomSources()).isEmpty(); assertThat(o.getDefaultSources()).isEmpty(); @@ -79,7 +80,7 @@ public void testFactories() { assertThat(o.getLanguages()).isEqualTo(LanguagePriorityList.ANY); assertThat(o.getNetwork()).isEqualTo(NetworkFactory.getDefault()); assertThat(o.getCache()).isEqualTo(SdmxCache.noOp()); - assertThat(o.getEventListener()).isEqualTo(SdmxWebListener.getDefault()); + assertThat(o.getEventListener()).isEqualTo(SdmxManager.NO_OP_EVENT_LISTENER); assertThat(o.getAuthenticators()).isEmpty(); assertThat(o.getCustomSources()).isEmpty(); assertThat(o.getDefaultSources()).isEmpty(); @@ -92,7 +93,7 @@ public void testFactories() { assertThat(o.getLanguages()).isEqualTo(LanguagePriorityList.ANY); assertThat(o.getNetwork()).isEqualTo(NetworkFactory.getDefault()); assertThat(o.getCache()).isEqualTo(SdmxCache.noOp()); - assertThat(o.getEventListener()).isEqualTo(SdmxWebListener.getDefault()); + assertThat(o.getEventListener()).isEqualTo(SdmxManager.NO_OP_EVENT_LISTENER); assertThat(o.getAuthenticators()).isEmpty(); assertThat(o.getCustomSources()).isEmpty(); assertThat(o.getDefaultSources()).containsAll(sampleDriver.getDefaultSources()); @@ -114,8 +115,8 @@ public void testGetSources() { .name("sdmx21") .rank(WRAPPED_RANK) .available(true) - .source(nbb) - .source(ecb) + .defaultSource(nbb) + .defaultSource(ecb) .build(); assertThat( @@ -179,11 +180,11 @@ public void testGetSources() { public void testGetDefaultSources() { SdmxWebSource source1a = SdmxWebSource.builder().name("s1").driver("dX").endpointOf("http://abc").build(); SdmxWebSource source2 = SdmxWebSource.builder().name("s2").driver("dX").endpointOf("http://abc").build(); - SdmxWebDriver driverX = MockedWebDriver.builder().name("dX").rank(WRAPPED_RANK).available(true).source(source1a).source(source2).build(); + SdmxWebDriver driverX = MockedWebDriver.builder().name("dX").rank(WRAPPED_RANK).available(true).defaultSource(source1a).defaultSource(source2).build(); SdmxWebSource source1b = SdmxWebSource.builder().name("s1").driver("dY").endpointOf("http://xyz").build(); SdmxWebSource source3 = SdmxWebSource.builder().name("s3").driver("dY").endpointOf("http://xyz").build(); - SdmxWebDriver driverY = MockedWebDriver.builder().name("dY").rank(NATIVE_RANK).available(true).source(source1b).source(source3).build(); + SdmxWebDriver driverY = MockedWebDriver.builder().name("dY").rank(NATIVE_RANK).available(true).defaultSource(source1b).defaultSource(source3).build(); assertThat(SdmxWebManager.builder().driver(driverX).driver(driverY).build().getDefaultSources()) .containsExactly(source1a, source2, source3); @@ -210,8 +211,8 @@ public void testGetConnection() throws IOException { .name("d1") .rank(WRAPPED_RANK) .available(true) - .repo(asURI("http://abc"), sample) - .source(SdmxWebSource.builder().name("source").driver("d1").dialect("azerty").endpointOf("http://abc").build()) + .repo(sample) + .defaultSource(SdmxWebSource.builder().name("source").driver("d1").dialect("azerty").endpointOf(sample.getName()).build()) .build(); SdmxWebDriver driver2 = MockedWebDriver @@ -219,12 +220,13 @@ public void testGetConnection() throws IOException { .name("d2") .rank(NATIVE_RANK) .available(true) - .repo(asURI("http://xyz"), sample) - .source(SdmxWebSource.builder().name("source").driver("d2").dialect("azerty").endpointOf("http://xyz").build()) + .repo(sample) + .defaultSource(SdmxWebSource.builder().name("source").driver("d2").dialect("azerty").endpointOf(sample.getName()).build()) .build(); - try (SdmxWebConnection c = SdmxWebManager.builder().driver(driver2).driver(driver1).dialect(sampleDialect).build().getConnection("source")) { - assertThat(c.getDriver()).isEqualTo(driver2.getName()); + try (SdmxConnection c = SdmxWebManager.builder().driver(driver2).driver(driver1).dialect(sampleDialect).build().getConnection("source")) { + // TODO: create code that verifies that driver2 is selected +// assertThat(c.getDriver()).isEqualTo(driver2.getName()); } } @@ -256,21 +258,21 @@ public void testInvalidSourceProperties() throws IOException { .builder() .driver(sampleDriver) .dialect(sampleDialect) - .eventListener(SdmxWebListener.of((source, event) -> events.add(source.getName() + ":" + event))) + .eventListener((source, event) -> events.add(source.getName() + ":" + event)) .build(); SdmxWebSource noProp = sampleSource.toBuilder().name("noProp").clearProperties().build(); - try (SdmxWebConnection ignored = manager.getConnection(noProp)) { + try (SdmxConnection ignored = manager.getConnection(noProp)) { } assertThat(events).isEmpty(); SdmxWebSource validProp = sampleSource.toBuilder().name("validProp").build(); - try (SdmxWebConnection ignored = manager.getConnection(validProp)) { + try (SdmxConnection ignored = manager.getConnection(validProp)) { } assertThat(events).isEmpty(); SdmxWebSource invalidProp = sampleSource.toBuilder().name("invalidProp").property("boom", "123").build(); - try (SdmxWebConnection ignored = manager.getConnection(invalidProp)) { + try (SdmxConnection ignored = manager.getConnection(invalidProp)) { } assertThat(events).hasSize(1).element(0, STRING) .contains(invalidProp.getName()) @@ -283,7 +285,7 @@ public void testInvalidSourceProperties() throws IOException { .name("repoSource") .driver("repoDriver") .dialect("azerty") - .endpoint(asURI(sample)) + .endpointOf(sample.getName()) .property("someproperty", "somevalue") .build(); private final SdmxWebDriver sampleDriver = MockedWebDriver @@ -291,20 +293,12 @@ public void testInvalidSourceProperties() throws IOException { .name("repoDriver") .rank(0) .available(true) - .repo(asURI(sample), sample) + .repo(sample) .supportedProperty("someproperty") - .source(sampleSource) + .defaultSource(sampleSource) .build(); private final SdmxDialect sampleDialect = new MockedDialect("azerty"); - private static URI asURI(SdmxRepository o) { - return URI.create("http://" + o.getName()); - } - - private static URI asURI(String o) { - return URI.create(o); - } - private static AbstractMap.SimpleEntry entryOf(K name, V source) { return new AbstractMap.SimpleEntry<>(name, source); } diff --git a/sdmx-dl-api/src/test/java/tests/sdmxdl/api/SdmxManagerAssert.java b/sdmx-dl-api/src/test/java/tests/sdmxdl/api/SdmxManagerAssert.java index d91055a0f..ab54ca9d8 100644 --- a/sdmx-dl-api/src/test/java/tests/sdmxdl/api/SdmxManagerAssert.java +++ b/sdmx-dl-api/src/test/java/tests/sdmxdl/api/SdmxManagerAssert.java @@ -19,6 +19,7 @@ import org.assertj.core.api.SoftAssertions; import sdmxdl.SdmxConnection; import sdmxdl.SdmxManager; +import sdmxdl.SdmxSource; import java.io.IOException; @@ -33,25 +34,25 @@ public class SdmxManagerAssert { @lombok.Value @lombok.Builder(toBuilder = true) - public static class Sample { - SOURCE validSource; - SOURCE invalidSource; + public static class Sample { + S validSource; + S invalidSource; } - public void assertCompliance(SdmxManager manager, Sample sample) { + public void assertCompliance(SdmxManager manager, Sample sample) { TckUtil.run(s -> assertCompliance(s, manager, sample)); } - public void assertCompliance(SoftAssertions s, SdmxManager manager, Sample sample) { + public void assertCompliance(SoftAssertions s, SdmxManager manager, Sample sample) { checkGetLanguages(s, manager); checkGetConnection(s, manager, sample); } - private void checkGetLanguages(SoftAssertions s, SdmxManager manager) { + private void checkGetLanguages(SoftAssertions s, SdmxManager manager) { s.assertThat(manager.getLanguages()).isNotNull(); } - private void checkGetConnection(SoftAssertions s, SdmxManager manager, Sample sample) { + private void checkGetConnection(SoftAssertions s, SdmxManager manager, Sample sample) { s.assertThatThrownBy(() -> manager.getConnection(null)) .as(nullDescriptionOf("getConnection(SOURCE)", "source")) .isInstanceOf(NullPointerException.class); diff --git a/sdmx-dl-api/src/test/java/tests/sdmxdl/file/SdmxFileConnectionAssert.java b/sdmx-dl-api/src/test/java/tests/sdmxdl/file/SdmxFileConnectionAssert.java deleted file mode 100644 index 738ee783d..000000000 --- a/sdmx-dl-api/src/test/java/tests/sdmxdl/file/SdmxFileConnectionAssert.java +++ /dev/null @@ -1,51 +0,0 @@ -package tests.sdmxdl.file; - -import org.assertj.core.api.SoftAssertions; -import sdmxdl.file.SdmxFileConnection; -import tests.sdmxdl.api.SdmxConnectionAssert; -import tests.sdmxdl.api.TckUtil; - -import java.io.IOException; - -@lombok.experimental.UtilityClass -public class SdmxFileConnectionAssert { - - @lombok.Value - @lombok.Builder(toBuilder = true) - public static class Sample { - SdmxConnectionAssert.Sample connection; - } - - @FunctionalInterface - public interface SdmxFileConnectionSupplier { - SdmxFileConnection getWithIO() throws IOException; - } - - public void assertCompliance(SdmxFileConnectionSupplier supplier, Sample sample) { - TckUtil.run(s -> assertCompliance(s, supplier, sample)); - } - - public void assertCompliance(SoftAssertions s, SdmxFileConnectionSupplier supplier, Sample sample) { - checkGetDataflowRef(s, supplier, sample); - checkGetFlow(s, supplier, sample); - SdmxConnectionAssert.assertCompliance(s, supplier::getWithIO, sample.connection); - } - - private static void checkGetDataflowRef(SoftAssertions s, SdmxFileConnectionSupplier supplier, Sample sample) { - try (SdmxFileConnection conn = supplier.getWithIO()) { - s.assertThat(conn.getDataflowRef()) - .isEqualTo(sample.connection.getValidFlow()); - } catch (IOException ex) { - s.fail("Not expected to raise exception", ex); - } - } - - private static void checkGetFlow(SoftAssertions s, SdmxFileConnectionSupplier supplier, Sample sample) { - try (SdmxFileConnection conn = supplier.getWithIO()) { - s.assertThat(conn.getFlow().getRef()) - .isEqualTo(sample.connection.getValidFlow()); - } catch (IOException ex) { - s.fail("Not expected to raise exception", ex); - } - } -} diff --git a/sdmx-dl-api/src/test/java/tests/sdmxdl/file/SdmxFileListenerAssert.java b/sdmx-dl-api/src/test/java/tests/sdmxdl/file/SdmxFileListenerAssert.java deleted file mode 100644 index 7924fc037..000000000 --- a/sdmx-dl-api/src/test/java/tests/sdmxdl/file/SdmxFileListenerAssert.java +++ /dev/null @@ -1,38 +0,0 @@ -package tests.sdmxdl.file; - -import org.assertj.core.api.SoftAssertions; -import sdmxdl.file.SdmxFileListener; -import sdmxdl.file.SdmxFileSource; -import tests.sdmxdl.api.TckUtil; - -import java.io.File; - -@lombok.experimental.UtilityClass -public class SdmxFileListenerAssert { - - public void assertCompliance(SdmxFileListener actual) { - TckUtil.run(s -> assertCompliance(s, actual)); - } - - public void assertCompliance(SoftAssertions s, SdmxFileListener actual) { - checkIsEnabled(s, actual); - checkOnSourceEvent(s, actual); - } - - private void checkOnSourceEvent(SoftAssertions s, SdmxFileListener actual) { - SdmxFileSource source = SdmxFileSource.builder().data(new File("")).build(); - s.assertThatCode(() -> actual.onFileSourceEvent(source, "hello")) - .doesNotThrowAnyException(); - - s.assertThatThrownBy(() -> actual.onFileSourceEvent(null, "hello")) - .isInstanceOf(NullPointerException.class); - - s.assertThatThrownBy(() -> actual.onFileSourceEvent(source, null)) - .isInstanceOf(NullPointerException.class); - } - - private void checkIsEnabled(SoftAssertions s, SdmxFileListener actual) { - s.assertThatCode(actual::isEnabled) - .doesNotThrowAnyException(); - } -} diff --git a/sdmx-dl-api/src/test/java/tests/sdmxdl/file/SdmxFileReaderAssert.java b/sdmx-dl-api/src/test/java/tests/sdmxdl/file/SdmxFileReaderAssert.java index dc9edd044..23548959c 100644 --- a/sdmx-dl-api/src/test/java/tests/sdmxdl/file/SdmxFileReaderAssert.java +++ b/sdmx-dl-api/src/test/java/tests/sdmxdl/file/SdmxFileReaderAssert.java @@ -1,7 +1,7 @@ package tests.sdmxdl.file; import org.assertj.core.api.SoftAssertions; -import sdmxdl.file.SdmxFileConnection; +import sdmxdl.SdmxConnection; import sdmxdl.file.SdmxFileSource; import sdmxdl.file.spi.SdmxFileContext; import sdmxdl.file.spi.SdmxFileReader; @@ -39,7 +39,7 @@ private static void checkRead(SoftAssertions s, SdmxFileReader reader, Sample sa s.assertThatThrownBy(() -> reader.read(sample.invalidSource, sample.context)) .isInstanceOf(IllegalArgumentException.class); - try (SdmxFileConnection conn = reader.read(sample.validSource, sample.context)) { + try (SdmxConnection conn = reader.read(sample.validSource, sample.context)) { s.assertThat(conn).isNotNull(); } catch (Exception ex) { s.fail("Not expected to raise exception", ex); diff --git a/sdmx-dl-api/src/test/java/tests/sdmxdl/web/MockedWebConnection.java b/sdmx-dl-api/src/test/java/tests/sdmxdl/web/MockedWebConnection.java deleted file mode 100644 index df9a239a3..000000000 --- a/sdmx-dl-api/src/test/java/tests/sdmxdl/web/MockedWebConnection.java +++ /dev/null @@ -1,14 +0,0 @@ -package tests.sdmxdl.web; - -import sdmxdl.SdmxConnection; -import sdmxdl.web.SdmxWebConnection; - -@lombok.RequiredArgsConstructor -public final class MockedWebConnection implements SdmxWebConnection { - - @lombok.Getter - private final String driver; - - @lombok.experimental.Delegate - private final SdmxConnection delegate; -} diff --git a/sdmx-dl-api/src/test/java/tests/sdmxdl/web/MockedWebDriver.java b/sdmx-dl-api/src/test/java/tests/sdmxdl/web/MockedWebDriver.java index 8a0156c0a..6b7ef174f 100644 --- a/sdmx-dl-api/src/test/java/tests/sdmxdl/web/MockedWebDriver.java +++ b/sdmx-dl-api/src/test/java/tests/sdmxdl/web/MockedWebDriver.java @@ -1,19 +1,17 @@ package tests.sdmxdl.web; +import sdmxdl.SdmxConnection; +import sdmxdl.ext.SdmxException; import sdmxdl.repo.SdmxRepository; -import sdmxdl.web.SdmxWebConnection; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; import sdmxdl.web.spi.SdmxWebDriver; import java.io.IOException; -import java.net.URI; import java.util.Collection; import java.util.List; -import java.util.Map; import java.util.Objects; -@lombok.RequiredArgsConstructor @lombok.Builder(toBuilder = true) public final class MockedWebDriver implements SdmxWebDriver { @@ -30,30 +28,27 @@ public final class MockedWebDriver implements SdmxWebDriver { private final boolean available = false; @lombok.Singular - private final Map repos; + private final List repos; + @lombok.Getter @lombok.Singular - private final List sources; + private final List defaultSources; + @lombok.Getter @lombok.Singular - private final List supportedProperties; + private final Collection supportedProperties; @Override - public SdmxWebConnection connect(SdmxWebSource source, SdmxWebContext context) throws IOException { + public SdmxConnection connect(SdmxWebSource source, SdmxWebContext context) throws IOException { Objects.requireNonNull(source); Objects.requireNonNull(context); checkSource(source); - return connect(source.getEndpoint()); - } - - @Override - public Collection getDefaultSources() { - return sources; - } - @Override - public Collection getSupportedProperties() { - return supportedProperties; + return repos.stream() + .filter(repo -> repo.getName().equals(source.getEndpoint().toString())) + .map(SdmxRepository::asConnection) + .findFirst() + .orElseThrow(() -> SdmxException.missingSource(source.toString(), SdmxWebSource.class)); } private void checkSource(SdmxWebSource source) throws IllegalArgumentException { @@ -62,11 +57,17 @@ private void checkSource(SdmxWebSource source) throws IllegalArgumentException { } } - private SdmxWebConnection connect(URI endpoint) throws IOException { - SdmxRepository result = repos.get(endpoint); - if (result != null) { - return new MockedWebConnection(name, result.asConnection()); + public static final class Builder { + + public Builder generateSources(String driver, SdmxRepository repo) { + return defaultSource( + SdmxWebSource + .builder() + .name(repo.getName()) + .driver(driver) + .endpointOf(repo.getName()) + .build() + ); } - throw new IOException(endpoint.toString()); } } diff --git a/sdmx-dl-api/src/test/java/tests/sdmxdl/web/SdmxWebConnectionAssert.java b/sdmx-dl-api/src/test/java/tests/sdmxdl/web/SdmxWebConnectionAssert.java deleted file mode 100644 index 25739f5c6..000000000 --- a/sdmx-dl-api/src/test/java/tests/sdmxdl/web/SdmxWebConnectionAssert.java +++ /dev/null @@ -1,5 +0,0 @@ -package tests.sdmxdl.web; - -@lombok.experimental.UtilityClass -public class SdmxWebConnectionAssert { -} diff --git a/sdmx-dl-api/src/test/java/tests/sdmxdl/web/SdmxWebListenerAssert.java b/sdmx-dl-api/src/test/java/tests/sdmxdl/web/SdmxWebListenerAssert.java deleted file mode 100644 index 4e6404457..000000000 --- a/sdmx-dl-api/src/test/java/tests/sdmxdl/web/SdmxWebListenerAssert.java +++ /dev/null @@ -1,36 +0,0 @@ -package tests.sdmxdl.web; - -import org.assertj.core.api.SoftAssertions; -import sdmxdl.web.SdmxWebListener; -import sdmxdl.web.SdmxWebSource; -import tests.sdmxdl.api.TckUtil; - -@lombok.experimental.UtilityClass -public class SdmxWebListenerAssert { - - public void assertCompliance(SdmxWebListener actual) { - TckUtil.run(s -> assertCompliance(s, actual)); - } - - public void assertCompliance(SoftAssertions s, SdmxWebListener actual) { - checkIsEnabled(s, actual); - checkOnSourceEvent(s, actual); - } - - private void checkOnSourceEvent(SoftAssertions s, SdmxWebListener actual) { - SdmxWebSource source = SdmxWebSource.builder().name("localhost").driver("").endpointOf("http://localhost").build(); - s.assertThatCode(() -> actual.onWebSourceEvent(source, "hello")) - .doesNotThrowAnyException(); - - s.assertThatThrownBy(() -> actual.onWebSourceEvent(null, "hello")) - .isInstanceOf(NullPointerException.class); - - s.assertThatThrownBy(() -> actual.onWebSourceEvent(source, null)) - .isInstanceOf(NullPointerException.class); - } - - private void checkIsEnabled(SoftAssertions s, SdmxWebListener actual) { - s.assertThatCode(actual::isEnabled) - .doesNotThrowAnyException(); - } -} diff --git a/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebFlowOptions.java b/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebFlowOptions.java index a4a4eb9ff..36a9fc2d5 100644 --- a/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebFlowOptions.java +++ b/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebFlowOptions.java @@ -18,7 +18,6 @@ import picocli.CommandLine; import sdmxdl.*; -import sdmxdl.web.SdmxWebConnection; import sdmxdl.web.SdmxWebManager; import java.io.IOException; @@ -40,13 +39,13 @@ public class WebFlowOptions extends WebSourceOptions { private DataflowRef flow; public DataStructure loadStructure(SdmxWebManager manager) throws IOException { - try (SdmxWebConnection conn = open(manager)) { + try (SdmxConnection conn = open(manager)) { return conn.getStructure(getFlow()); } } public DataSet loadSeries(SdmxWebManager manager, Key key, DataDetail detail) throws IOException { - try (SdmxWebConnection conn = open(manager)) { + try (SdmxConnection conn = open(manager)) { return conn.getData(getFlow(), DataQuery.of(key, detail)); } } diff --git a/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebOptions.java b/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebOptions.java index fe9442e91..8186159ed 100644 --- a/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebOptions.java +++ b/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebOptions.java @@ -19,7 +19,7 @@ import internal.sdmxdl.cli.ext.VerboseOptions; import picocli.CommandLine; import sdmxdl.LanguagePriorityList; -import sdmxdl.web.SdmxWebListener; +import sdmxdl.SdmxManager; import sdmxdl.web.SdmxWebManager; import sdmxdl.web.SdmxWebSource; import sdmxdl.xml.XmlWebSource; @@ -28,6 +28,8 @@ import java.io.IOException; import java.util.Collections; import java.util.List; +import java.util.function.BiConsumer; +import java.util.logging.Level; /** * @author Philippe Charles @@ -78,9 +80,9 @@ public SdmxWebManager loadManager() throws IOException { .build(); } - private SdmxWebListener getEventListener() { - SdmxWebListener original = isNoLog() ? SdmxWebListener.noOp() : SdmxWebListener.getDefault(); - return new VerboseWebListener(original, verboseOptions); + private BiConsumer getEventListener() { + BiConsumer original = isNoLog() ? SdmxManager.NO_OP_EVENT_LISTENER : new LoggingListener()::onSourceEvent; + return new VerboseListener(original, verboseOptions)::onSourceEvent; } private List parseCustomSources() throws IOException { @@ -93,24 +95,28 @@ private List parseCustomSources() throws IOException { return Collections.emptyList(); } + @lombok.extern.java.Log + private static class LoggingListener { + + public void onSourceEvent(SdmxWebSource source, String message) { + if (log.isLoggable(Level.INFO)) { + log.info(message); + } + } + } + @lombok.AllArgsConstructor - private static class VerboseWebListener implements SdmxWebListener { + private static class VerboseListener { @lombok.NonNull - private final SdmxWebListener main; + private final BiConsumer main; @lombok.NonNull private final VerboseOptions verboseOptions; - @Override - public boolean isEnabled() { - return true; - } - - @Override - public void onWebSourceEvent(SdmxWebSource source, String message) { - if (main.isEnabled()) { - main.onWebSourceEvent(source, message); + public void onSourceEvent(SdmxWebSource source, String message) { + if (main != SdmxManager.NO_OP_EVENT_LISTENER) { + main.accept(source, message); } if (verboseOptions.isVerbose()) { verboseOptions.reportToErrorStream("WEB", source.getName() + ": " + message); diff --git a/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebSourceOptions.java b/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebSourceOptions.java index 3b7292078..b2188942c 100644 --- a/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebSourceOptions.java +++ b/sdmx-dl-cli/src/main/java/internal/sdmxdl/cli/WebSourceOptions.java @@ -19,7 +19,7 @@ import picocli.CommandLine; import sdmxdl.Dataflow; import sdmxdl.Feature; -import sdmxdl.web.SdmxWebConnection; +import sdmxdl.SdmxConnection; import sdmxdl.web.SdmxWebManager; import java.io.IOException; @@ -41,18 +41,18 @@ public class WebSourceOptions extends WebNetOptions { ) private String source; - public SdmxWebConnection open(SdmxWebManager manager) throws IOException { + public SdmxConnection open(SdmxWebManager manager) throws IOException { return manager.getConnection(getSource()); } public Set loadFeatures(SdmxWebManager manager) throws IOException { - try (SdmxWebConnection conn = open(manager)) { + try (SdmxConnection conn = open(manager)) { return conn.getSupportedFeatures(); } } public Collection loadFlows(SdmxWebManager manager) throws IOException { - try (SdmxWebConnection conn = open(manager)) { + try (SdmxConnection conn = open(manager)) { return conn.getFlows(); } } diff --git a/sdmx-dl-cli/src/main/java/sdmxdl/cli/CheckAccessCommand.java b/sdmx-dl-cli/src/main/java/sdmxdl/cli/CheckAccessCommand.java index f5393d30d..b3a28d4d0 100644 --- a/sdmx-dl-cli/src/main/java/sdmxdl/cli/CheckAccessCommand.java +++ b/sdmx-dl-cli/src/main/java/sdmxdl/cli/CheckAccessCommand.java @@ -25,7 +25,7 @@ import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; import picocli.CommandLine; -import sdmxdl.web.SdmxWebConnection; +import sdmxdl.SdmxConnection; import sdmxdl.web.SdmxWebManager; import java.io.IOException; @@ -85,7 +85,7 @@ private static String formatDuration(Duration o) { private static class Access { static @NonNull Access of(@NonNull SdmxWebManager manager, @NonNull String source) { - try (final SdmxWebConnection conn = manager.getConnection(source)) { + try (SdmxConnection conn = manager.getConnection(source)) { Clock clock = Clock.systemDefaultZone(); Instant start = clock.instant(); conn.testConnection(); diff --git a/sdmx-dl-cli/src/main/java/sdmxdl/cli/FetchDataCommand.java b/sdmx-dl-cli/src/main/java/sdmxdl/cli/FetchDataCommand.java index 9a9ea4b49..8ee8b48f1 100644 --- a/sdmx-dl-cli/src/main/java/sdmxdl/cli/FetchDataCommand.java +++ b/sdmx-dl-cli/src/main/java/sdmxdl/cli/FetchDataCommand.java @@ -28,7 +28,6 @@ import sdmxdl.*; import sdmxdl.csv.SdmxCsvFieldWriter; import sdmxdl.csv.SdmxPicocsvFormatter; -import sdmxdl.web.SdmxWebConnection; import java.io.IOException; import java.time.LocalDateTime; @@ -71,7 +70,7 @@ private void writeHead(Csv.Writer w) throws IOException { } private void writeBody(Csv.Writer w) throws IOException { - try (SdmxWebConnection conn = web.loadManager().getConnection(web.getSource())) { + try (SdmxConnection conn = web.loadManager().getConnection(web.getSource())) { DataStructure dsd = conn.getStructure(web.getFlow()); getBodyFormatter(dsd, format).formatCsv(getSortedSeries(conn, web), w); } @@ -97,7 +96,7 @@ private static Formatter getPeriodFormat(ObsFormat format) { return Formatter.onDateTimeFormatter(format.newDateTimeFormatter(true)); } - private static DataSet getSortedSeries(SdmxWebConnection conn, WebKeyOptions web) throws IOException { + private static DataSet getSortedSeries(SdmxConnection conn, WebKeyOptions web) throws IOException { DataQuery query = DataQuery.of(web.getKey(), getDetail()); try (Stream stream = conn.getDataStream(web.getFlow(), query)) { return stream diff --git a/sdmx-dl-cli/src/main/java/sdmxdl/cli/FetchExtraCommand.java b/sdmx-dl-cli/src/main/java/sdmxdl/cli/FetchExtraCommand.java index 1bdd2de40..e69d1d71d 100644 --- a/sdmx-dl-cli/src/main/java/sdmxdl/cli/FetchExtraCommand.java +++ b/sdmx-dl-cli/src/main/java/sdmxdl/cli/FetchExtraCommand.java @@ -23,7 +23,6 @@ import nbbrd.io.text.Formatter; import picocli.CommandLine; import sdmxdl.*; -import sdmxdl.web.SdmxWebConnection; import java.io.IOException; import java.util.Collection; @@ -70,7 +69,7 @@ private CsvTable getTable() { } private Stream getRows() throws IOException { - try (SdmxWebConnection conn = web.open(web.loadManager())) { + try (SdmxConnection conn = web.open(web.loadManager())) { DataStructure dsd = conn.getStructure(web.getFlow()); Function toValueUnit = getValueUnit(dsd); diff --git a/sdmx-dl-provider-connectors/src/main/java/internal/sdmxdl/connectors/ConnectorRestClient.java b/sdmx-dl-provider-connectors/src/main/java/internal/sdmxdl/connectors/ConnectorRestClient.java index 6c1b20abe..ee5f03f58 100644 --- a/sdmx-dl-provider-connectors/src/main/java/internal/sdmxdl/connectors/ConnectorRestClient.java +++ b/sdmx-dl-provider-connectors/src/main/java/internal/sdmxdl/connectors/ConnectorRestClient.java @@ -34,7 +34,6 @@ import sdmxdl.util.web.SdmxRestClient; import sdmxdl.util.web.SdmxRestClientSupplier; import sdmxdl.util.web.SdmxWebEvents; -import sdmxdl.web.SdmxWebListener; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; @@ -43,6 +42,7 @@ import java.net.URISyntaxException; import java.util.List; import java.util.Map; +import java.util.function.BiConsumer; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -220,17 +220,17 @@ private static final class DefaultRestSdmxEventListener implements RestSdmxEvent private final SdmxWebSource source; @lombok.NonNull - private final SdmxWebListener listener; + private final BiConsumer listener; @Override public void onSdmxEvent(RestSdmxEvent event) { - if (listener.isEnabled()) { + if (listener != SdmxManager.NO_OP_EVENT_LISTENER) { if (event instanceof RedirectionEvent) { RedirectionEvent redirectionEvent = (RedirectionEvent) event; - listener.onWebSourceEvent(source, SdmxWebEvents.onRedirection(redirectionEvent.getUrl(), redirectionEvent.getRedirection())); + listener.accept(source, SdmxWebEvents.onRedirection(redirectionEvent.getUrl(), redirectionEvent.getRedirection())); } else if (event instanceof OpenEvent) { OpenEvent openEvent = (OpenEvent) event; - listener.onWebSourceEvent(source, SdmxWebEvents.onQuery(openEvent.getUrl(), openEvent.getProxy())); + listener.accept(source, SdmxWebEvents.onQuery(openEvent.getUrl(), openEvent.getProxy())); } } } diff --git a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/XmlDecoder.java b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/XmlDecoder.java index 277206126..d19b784e3 100644 --- a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/XmlDecoder.java +++ b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/XmlDecoder.java @@ -19,8 +19,8 @@ import nbbrd.io.xml.Xml; import sdmxdl.DataStructure; import sdmxdl.LanguagePriorityList; +import sdmxdl.SdmxManager; import sdmxdl.ext.SdmxMediaType; -import sdmxdl.file.SdmxFileListener; import sdmxdl.file.SdmxFileSource; import sdmxdl.util.file.SdmxFileInfo; import sdmxdl.xml.SdmxmlDataTypeProbe; @@ -29,6 +29,7 @@ import java.io.IOException; import java.util.List; +import java.util.function.BiConsumer; /** * @author Philippe Charles @@ -37,7 +38,7 @@ public final class XmlDecoder implements SdmxDecoder { @lombok.NonNull - private final SdmxFileListener eventListener; + private final BiConsumer eventListener; @Override public SdmxFileInfo decode(SdmxFileSource source, LanguagePriorityList langs) throws IOException { @@ -46,8 +47,8 @@ public SdmxFileInfo decode(SdmxFileSource source, LanguagePriorityList langs) th } private String probeDataType(SdmxFileSource source) throws IOException { - if (eventListener.isEnabled()) { - eventListener.onFileSourceEvent(source, "Probing data type from '" + source.getData() + "'"); + if (eventListener != SdmxManager.NO_OP_EVENT_LISTENER) { + eventListener.accept(source, "Probing data type from '" + source.getData() + "'"); } return SdmxmlDataTypeProbe.of() .parseFile(source.getData()); @@ -60,8 +61,8 @@ private DataStructure loadStructure(SdmxFileSource source, LanguagePriorityList } private DataStructure parseStruct(String dataType, LanguagePriorityList langs, SdmxFileSource source) throws IOException { - if (eventListener.isEnabled()) { - eventListener.onFileSourceEvent(source, "Parsing structure from '" + source.getStructure() + "' with data type '" + dataType + "'"); + if (eventListener != SdmxManager.NO_OP_EVENT_LISTENER) { + eventListener.accept(source, "Parsing structure from '" + source.getStructure() + "' with data type '" + dataType + "'"); } return getStructParser(dataType, langs) .parseFile(source.getStructure()) @@ -84,8 +85,8 @@ private Xml.Parser> getStructParser(String dataType, Languag } private DataStructure decodeStruct(String dataType, SdmxFileSource source) throws IOException { - if (eventListener.isEnabled()) { - eventListener.onFileSourceEvent(source, "Decoding structure from '" + source.getData() + "' with data type '" + dataType + "'"); + if (eventListener != SdmxManager.NO_OP_EVENT_LISTENER) { + eventListener.accept(source, "Decoding structure from '" + source.getData() + "' with data type '" + dataType + "'"); } return getStructDecoder(dataType) .parseFile(source.getData()); diff --git a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/XmlFileClient.java b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/XmlFileClient.java index 540512879..4fad0c768 100644 --- a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/XmlFileClient.java +++ b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/XmlFileClient.java @@ -20,10 +20,10 @@ import org.checkerframework.checker.nullness.qual.Nullable; import sdmxdl.DataStructure; import sdmxdl.LanguagePriorityList; +import sdmxdl.SdmxManager; import sdmxdl.Series; import sdmxdl.ext.ObsFactory; import sdmxdl.ext.SdmxMediaType; -import sdmxdl.file.SdmxFileListener; import sdmxdl.file.SdmxFileSource; import sdmxdl.util.DataRef; import sdmxdl.util.file.SdmxFileClient; @@ -34,6 +34,7 @@ import java.io.FileNotFoundException; import java.io.IOException; +import java.util.function.BiConsumer; import java.util.stream.Stream; /** @@ -55,7 +56,7 @@ public class XmlFileClient implements SdmxFileClient { private final ObsFactory obsFactory; @lombok.NonNull - SdmxFileListener eventListener; + BiConsumer eventListener; @Override public void testClient() throws IOException { @@ -74,8 +75,8 @@ public SdmxFileInfo decode() throws IOException { @Override public Stream loadData(SdmxFileInfo info, DataRef dataRef) throws IOException { - if (eventListener.isEnabled()) { - eventListener.onFileSourceEvent(source, "Loading data from file '" + source.getData() + "'"); + if (eventListener != SdmxManager.NO_OP_EVENT_LISTENER) { + eventListener.accept(source, "Loading data from file '" + source.getData() + "'"); } return dataRef.getQuery().execute( getDataSupplier(info.getDataType(), info.getStructure()) diff --git a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/readers/XmlFileReader.java b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/readers/XmlFileReader.java index dc16c4e6d..2a90050ff 100644 --- a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/readers/XmlFileReader.java +++ b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/file/readers/XmlFileReader.java @@ -5,7 +5,7 @@ import nbbrd.service.ServiceProvider; import sdmxdl.DataStructureRef; import sdmxdl.Dataflow; -import sdmxdl.file.SdmxFileConnection; +import sdmxdl.SdmxConnection; import sdmxdl.file.SdmxFileSource; import sdmxdl.file.spi.SdmxFileContext; import sdmxdl.file.spi.SdmxFileReader; @@ -28,7 +28,7 @@ public boolean canRead(SdmxFileSource source) { } @Override - public SdmxFileConnection read(SdmxFileSource source, SdmxFileContext context) throws IOException, IllegalArgumentException { + public SdmxConnection read(SdmxFileSource source, SdmxFileContext context) throws IOException, IllegalArgumentException { Objects.requireNonNull(source); Objects.requireNonNull(context); if (!canRead(source)) { diff --git a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/RiHttpUtils.java b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/RiHttpUtils.java index 2d4b2a155..e265e4420 100644 --- a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/RiHttpUtils.java +++ b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/RiHttpUtils.java @@ -27,9 +27,9 @@ import org.checkerframework.checker.nullness.qual.Nullable; import sdmxdl.About; import sdmxdl.LanguagePriorityList; +import sdmxdl.SdmxManager; import sdmxdl.ext.SdmxMediaType; import sdmxdl.util.web.SdmxWebEvents; -import sdmxdl.web.SdmxWebListener; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebAuthenticator; import sdmxdl.web.spi.SdmxWebContext; @@ -41,6 +41,7 @@ import java.net.URL; import java.util.List; import java.util.Objects; +import java.util.function.BiConsumer; import static sdmxdl.util.web.SdmxWebProperty.*; @@ -120,27 +121,27 @@ private static final class RiHttpEventListener implements HttpEventListener { private final SdmxWebSource source; @lombok.NonNull - private final SdmxWebListener listener; + private final BiConsumer listener; @Override public void onOpen(HttpRequest request, Proxy proxy, HttpAuthScheme scheme) { Objects.requireNonNull(request); Objects.requireNonNull(proxy); Objects.requireNonNull(scheme); - if (listener.isEnabled()) { + if (listener != SdmxManager.NO_OP_EVENT_LISTENER) { String message = SdmxWebEvents.onQuery(request.getQuery(), proxy); if (!HttpAuthScheme.NONE.equals(scheme)) { message += " with auth '" + scheme.name() + "'"; } - listener.onWebSourceEvent(source, message); + listener.accept(source, message); } } @Override public void onSuccess(@NonNull MediaType mediaType) { Objects.requireNonNull(mediaType); - if (listener.isEnabled()) { - listener.onWebSourceEvent(source, String.format("Parsing '%s'", mediaType)); + if (listener != SdmxManager.NO_OP_EVENT_LISTENER) { + listener.accept(source, String.format("Parsing '%s'", mediaType)); } } @@ -148,8 +149,8 @@ public void onSuccess(@NonNull MediaType mediaType) { public void onRedirection(URL oldUrl, URL newUrl) { Objects.requireNonNull(oldUrl); Objects.requireNonNull(newUrl); - if (listener.isEnabled()) { - listener.onWebSourceEvent(source, SdmxWebEvents.onRedirection(oldUrl, newUrl)); + if (listener != SdmxManager.NO_OP_EVENT_LISTENER) { + listener.accept(source, SdmxWebEvents.onRedirection(oldUrl, newUrl)); } } @@ -158,16 +159,16 @@ public void onUnauthorized(URL url, HttpAuthScheme oldScheme, HttpAuthScheme new Objects.requireNonNull(url); Objects.requireNonNull(oldScheme); Objects.requireNonNull(newScheme); - if (listener.isEnabled()) { - listener.onWebSourceEvent(source, String.format("Authenticating %s with '%s'", url, newScheme.name())); + if (listener != SdmxManager.NO_OP_EVENT_LISTENER) { + listener.accept(source, String.format("Authenticating %s with '%s'", url, newScheme.name())); } } @Override public void onEvent(@NonNull String message) { Objects.requireNonNull(message); - if (listener.isEnabled()) { - listener.onWebSourceEvent(source, message); + if (listener != SdmxManager.NO_OP_EVENT_LISTENER) { + listener.accept(source, message); } } } @@ -182,7 +183,7 @@ private static final class RiHttpAuthenticator implements HttpAuthenticator { private final List authenticators; @lombok.NonNull - private final SdmxWebListener listener; + private final BiConsumer listener; @Override public @Nullable PasswordAuthentication getPasswordAuthentication(URL url) { @@ -213,7 +214,7 @@ private PasswordAuthentication getPasswordAuthentication(SdmxWebAuthenticator au try { return authenticator.getPasswordAuthentication(source); } catch (IOException ex) { - listener.onWebSourceEvent(source, "Failed to get password authentication: " + ex.getMessage()); + listener.accept(source, "Failed to get password authentication: " + ex.getMessage()); return null; } } @@ -222,7 +223,7 @@ private void invalidate(SdmxWebAuthenticator authenticator) { try { authenticator.invalidate(source); } catch (IOException ex) { - listener.onWebSourceEvent(source, "Failed to invalidate password authentication: " + ex.getMessage()); + listener.accept(source, "Failed to invalidate password authentication: " + ex.getMessage()); } } } diff --git a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/FileDriver.java b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/FileDriver.java index 32124cce5..f24f9cdbd 100644 --- a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/FileDriver.java +++ b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/FileDriver.java @@ -8,14 +8,10 @@ import nbbrd.service.ServiceProvider; import org.checkerframework.checker.nullness.qual.NonNull; import sdmxdl.SdmxConnection; -import sdmxdl.file.SdmxFileConnection; -import sdmxdl.file.SdmxFileListener; import sdmxdl.file.SdmxFileManager; import sdmxdl.file.SdmxFileSource; import sdmxdl.util.web.SdmxValidators; import sdmxdl.util.web.Validator; -import sdmxdl.web.SdmxWebConnection; -import sdmxdl.web.SdmxWebListener; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; import sdmxdl.web.spi.SdmxWebDriver; @@ -55,12 +51,19 @@ public boolean isAvailable() { } @Override - public @NonNull SdmxWebConnection connect(@NonNull SdmxWebSource source, @NonNull SdmxWebContext context) throws IOException, IllegalArgumentException { + public @NonNull SdmxConnection connect(@NonNull SdmxWebSource source, @NonNull SdmxWebContext context) throws IOException, IllegalArgumentException { Objects.requireNonNull(source, "source"); Objects.requireNonNull(context, "context"); sourceValidator.checkValidity(source); - return new WebOverFileConnection(open(source, context), getName()); + return fileManager + .toBuilder() + .languages(context.getLanguages()) + .eventListener((fileSource, message) -> context.getEventListener().accept(source, message)) + .cache(context.getCache()) + .dialects(context.getDialects()) + .build() + .getConnection(toFileSource(source)); } @Override @@ -73,17 +76,6 @@ public boolean isAvailable() { return Collections.singletonList(STRUCTURE_PROPERTY.getKey()); } - private SdmxFileConnection open(SdmxWebSource source, SdmxWebContext context) throws IOException { - return fileManager - .toBuilder() - .languages(context.getLanguages()) - .eventListener(new FileOverWebListener(source, context.getEventListener())) - .cache(context.getCache()) - .dialects(context.getDialects()) - .build() - .getConnection(toFileSource(source)); - } - private static SdmxFileSource toFileSource(SdmxWebSource source) throws IOException { return SdmxFileSource .builder() @@ -107,41 +99,4 @@ static File toFile(URI endpoint) throws IOException { private static final Property STRUCTURE_PROPERTY = Property.of("structureURL", null, Parser.of(text -> URI.create(text.toString())), Formatter.of(Objects::toString)); - - @VisibleForTesting - @lombok.RequiredArgsConstructor - static final class WebOverFileConnection implements SdmxWebConnection { - - @lombok.experimental.Delegate(types = SdmxConnection.class) - @lombok.NonNull - private final SdmxFileConnection delegate; - - @lombok.NonNull - private final String driver; - - @Override - public @NonNull String getDriver() { - return driver; - } - } - - @lombok.RequiredArgsConstructor - static final class FileOverWebListener implements SdmxFileListener { - - @lombok.NonNull - private final SdmxWebSource webSource; - - @lombok.NonNull - private final SdmxWebListener webListener; - - @Override - public boolean isEnabled() { - return webListener.isEnabled(); - } - - @Override - public void onFileSourceEvent(@NonNull SdmxFileSource source, @NonNull String message) { - webListener.onWebSourceEvent(webSource, message); - } - } } diff --git a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/RngDriver.java b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/RngDriver.java index cbedac956..4680f202a 100644 --- a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/RngDriver.java +++ b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/RngDriver.java @@ -10,7 +10,6 @@ import sdmxdl.ext.SdmxException; import sdmxdl.util.web.SdmxValidators; import sdmxdl.util.web.Validator; -import sdmxdl.web.SdmxWebConnection; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; import sdmxdl.web.spi.SdmxWebDriver; @@ -58,7 +57,7 @@ public boolean isAvailable() { } @Override - public @NonNull SdmxWebConnection connect(@NonNull SdmxWebSource source, @NonNull SdmxWebContext context) throws IOException, IllegalArgumentException { + public @NonNull SdmxConnection connect(@NonNull SdmxWebSource source, @NonNull SdmxWebContext context) throws IOException, IllegalArgumentException { Objects.requireNonNull(source); Objects.requireNonNull(context); sourceValidator.checkValidity(source); @@ -121,7 +120,7 @@ public static RngDriverId parse(URI endpoint) throws IllegalArgumentException { } @lombok.AllArgsConstructor - private static class RngWebConnection implements SdmxWebConnection { + private static class RngWebConnection implements SdmxConnection { private static final String FREQ = "FREQ"; private static final String INDEX = "INDEX"; @@ -133,11 +132,6 @@ private static class RngWebConnection implements SdmxWebConnection { public void testConnection() { } - @Override - public @NonNull String getDriver() { - return RI_RNG; - } - @Override public @NonNull Collection getFlows() { return singleton(Dataflow.of(DataflowRef.parse("RNG"), DataStructureRef.parse("STRUCT_RNG"), "RNG")); diff --git a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/StatCanDriver.java b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/StatCanDriver.java index 3ade2ab20..4b892f589 100644 --- a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/StatCanDriver.java +++ b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/drivers/StatCanDriver.java @@ -19,7 +19,6 @@ import sdmxdl.util.parser.ObsFactories; import sdmxdl.util.web.SdmxValidators; import sdmxdl.util.web.Validator; -import sdmxdl.web.SdmxWebConnection; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; import sdmxdl.web.spi.SdmxWebDriver; @@ -74,7 +73,7 @@ public boolean isAvailable() { } @Override - public @NonNull SdmxWebConnection connect(@NonNull SdmxWebSource source, @NonNull SdmxWebContext context) throws IOException, IllegalArgumentException { + public @NonNull SdmxConnection connect(@NonNull SdmxWebSource source, @NonNull SdmxWebContext context) throws IOException, IllegalArgumentException { Objects.requireNonNull(source); Objects.requireNonNull(context); sourceValidator.checkValidity(source); @@ -119,7 +118,7 @@ public boolean isAvailable() { } @lombok.AllArgsConstructor - private static final class StatCanConnection implements SdmxWebConnection { + private static final class StatCanConnection implements SdmxConnection { @lombok.NonNull private final String source; @@ -180,11 +179,6 @@ public void testConnection() throws IOException { client.ping(); } - @Override - public @NonNull String getDriver() { - return RI_STATCAN; - } - @Override public void close() throws IOException { } diff --git a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/monitors/UptimeRobot.java b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/monitors/UptimeRobot.java index 7001b49e8..86b6e5679 100644 --- a/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/monitors/UptimeRobot.java +++ b/sdmx-dl-provider-ri/src/main/java/internal/sdmxdl/ri/web/monitors/UptimeRobot.java @@ -6,6 +6,7 @@ import nbbrd.io.xml.Stax; import nbbrd.io.xml.Xml; import nbbrd.service.ServiceProvider; +import sdmxdl.SdmxManager; import sdmxdl.util.web.SdmxWebEvents; import sdmxdl.util.web.SdmxWebMonitors; import sdmxdl.web.SdmxWebMonitorReport; @@ -94,8 +95,8 @@ private static T post(URL url, String query, IOFunction factory, Proxy proxy = context.getNetwork().getProxySelector().select(toURI(url)).stream().findFirst().orElse(Proxy.NO_PROXY); - if (context.getEventListener().isEnabled()) { - context.getEventListener().onWebSourceEvent(source, SdmxWebEvents.onQuery(url, proxy)); + if (context.getEventListener() != SdmxManager.NO_OP_EVENT_LISTENER) { + context.getEventListener().accept(source, SdmxWebEvents.onQuery(url, proxy)); } HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy); diff --git a/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/file/XmlFileClientTest.java b/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/file/XmlFileClientTest.java index e86db6bd8..35ce58e68 100644 --- a/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/file/XmlFileClientTest.java +++ b/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/file/XmlFileClientTest.java @@ -20,14 +20,13 @@ import org.junit.jupiter.api.io.TempDir; import sdmxdl.*; import sdmxdl.ext.SdmxMediaType; -import sdmxdl.file.SdmxFileListener; import sdmxdl.file.SdmxFileSource; -import tests.sdmxdl.api.RepoSamples; -import tests.sdmxdl.api.SdmxConnectionAssert; import sdmxdl.util.DataRef; import sdmxdl.util.file.SdmxFileClient; import sdmxdl.util.file.SdmxFileConnectionImpl; import sdmxdl.util.file.SdmxFileInfo; +import tests.sdmxdl.api.RepoSamples; +import tests.sdmxdl.api.SdmxConnectionAssert; import tests.sdmxdl.xml.SdmxXmlSources; import java.io.File; @@ -49,7 +48,7 @@ public void testCompactData21(@TempDir Path temp) throws IOException { SdmxXmlSources.OTHER_COMPACT21.copyTo(compact21); SdmxFileSource source = sourceOf(compact21); - SdmxFileClient x = new XmlFileClient(source, ANY, DECODER, null, SdmxFileListener.noOp()); + SdmxFileClient x = new XmlFileClient(source, ANY, DECODER, null, SdmxManager.NO_OP_EVENT_LISTENER); SdmxFileInfo info = x.decode(); @@ -97,6 +96,6 @@ public static SdmxFileSource sourceOf(File compact21) { return SdmxFileSource.builder().data(compact21).build(); } - public static final SdmxDecoder DECODER = new XmlDecoder(SdmxFileListener.noOp()); + public static final SdmxDecoder DECODER = new XmlDecoder(SdmxManager.NO_OP_EVENT_LISTENER); public static final Dataflow DATAFLOW = Dataflow.of(DataflowRef.parse("data"), DataStructureRef.parse("xyz"), "label"); } diff --git a/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/RiHttpUtilsTest.java b/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/RiHttpUtilsTest.java index 08f41892e..4d5a7b225 100644 --- a/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/RiHttpUtilsTest.java +++ b/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/RiHttpUtilsTest.java @@ -5,7 +5,6 @@ import internal.util.http.MediaType; import org.checkerframework.checker.nullness.qual.NonNull; import org.junit.jupiter.api.Test; -import sdmxdl.web.SdmxWebListener; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; @@ -60,7 +59,7 @@ public void testListener() throws MalformedURLException { SdmxWebContext webContext = SdmxWebContext .builder() - .eventListener(events) + .eventListener(events::onSourceEvent) .build(); HttpEventListener x = RiHttpUtils.newContext(source, webContext).getListener(); @@ -118,7 +117,7 @@ private static class Event { String message; } - private static class MockedSdmxWebListener implements SdmxWebListener { + private static class MockedSdmxWebListener { private List events = new ArrayList<>(); @@ -128,13 +127,7 @@ public List pop() { return result; } - @Override - public boolean isEnabled() { - return true; - } - - @Override - public void onWebSourceEvent(@NonNull SdmxWebSource source, @NonNull String message) { + public void onSourceEvent(@NonNull SdmxWebSource source, @NonNull String message) { events.add(new Event(source, message)); } } diff --git a/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/drivers/RngDriverTest.java b/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/drivers/RngDriverTest.java index 840655a69..69cbecad6 100644 --- a/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/drivers/RngDriverTest.java +++ b/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/drivers/RngDriverTest.java @@ -3,8 +3,8 @@ import org.junit.jupiter.api.Test; import sdmxdl.DataQuery; import sdmxdl.Dataflow; +import sdmxdl.SdmxConnection; import tests.sdmxdl.web.SdmxWebDriverAssert; -import sdmxdl.web.SdmxWebConnection; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; @@ -23,7 +23,7 @@ public static void main(String[] args) throws IOException { for (SdmxWebSource source : x.getDefaultSources()) { System.out.println(source); - try (SdmxWebConnection conn = x.connect(source, context)) { + try (SdmxConnection conn = x.connect(source, context)) { for (Dataflow dataflow : conn.getFlows()) { System.out.println(dataflow); System.out.println(conn.getStructure(dataflow.getRef())); diff --git a/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/drivers/StatCanDriverTest.java b/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/drivers/StatCanDriverTest.java index d32f8e42f..7186afb30 100644 --- a/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/drivers/StatCanDriverTest.java +++ b/sdmx-dl-provider-ri/src/test/java/internal/sdmxdl/ri/web/drivers/StatCanDriverTest.java @@ -4,12 +4,8 @@ import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; -import sdmxdl.DataQuery; -import sdmxdl.DataStructureRef; -import sdmxdl.DataflowRef; -import sdmxdl.LanguagePriorityList; +import sdmxdl.*; import tests.sdmxdl.web.SdmxWebDriverAssert; -import sdmxdl.web.SdmxWebConnection; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; @@ -34,7 +30,7 @@ public void testCompliance() { public void testConnectionArgs() throws IOException { StatCanDriver driver = new StatCanDriver(); SdmxWebSource source = driver.getDefaultSources().iterator().next(); - try (SdmxWebConnection connection = driver.connect(source, SdmxWebContext.builder().build())) { + try (SdmxConnection connection = driver.connect(source, SdmxWebContext.builder().build())) { DataflowRef badDataflowRef = DataflowRef.parse("F_10100001"); String msg = "Expecting DataflowRef id 'F_10100001' to match pattern 'DF_\\d+'"; diff --git a/sdmx-dl-provider-util/src/main/java/sdmxdl/util/file/SdmxFileConnectionImpl.java b/sdmx-dl-provider-util/src/main/java/sdmxdl/util/file/SdmxFileConnectionImpl.java index 9f52f354f..a256c728b 100644 --- a/sdmx-dl-provider-util/src/main/java/sdmxdl/util/file/SdmxFileConnectionImpl.java +++ b/sdmx-dl-provider-util/src/main/java/sdmxdl/util/file/SdmxFileConnectionImpl.java @@ -19,7 +19,6 @@ import org.checkerframework.checker.nullness.qual.NonNull; import sdmxdl.*; import sdmxdl.ext.SdmxException; -import sdmxdl.file.SdmxFileConnection; import sdmxdl.util.DataRef; import sdmxdl.util.web.SdmxValidators; @@ -33,7 +32,7 @@ * @author Philippe Charles */ @lombok.RequiredArgsConstructor -public final class SdmxFileConnectionImpl implements SdmxFileConnection { +public final class SdmxFileConnectionImpl implements SdmxConnection { @lombok.NonNull private final SdmxFileClient client; @@ -49,24 +48,12 @@ public void testConnection() throws IOException { client.testClient(); } - @Override - public DataflowRef getDataflowRef() throws IOException { - checkState(); - return dataflow.getRef(); - } - @Override public Collection getFlows() throws IOException { checkState(); return Collections.singleton(dataflow); } - @Override - public Dataflow getFlow() throws IOException { - checkState(); - return dataflow; - } - @Override public Dataflow getFlow(DataflowRef flowRef) throws IOException, IllegalArgumentException { checkState(); @@ -74,12 +61,6 @@ public Dataflow getFlow(DataflowRef flowRef) throws IOException, IllegalArgument return dataflow; } - @Override - public DataStructure getStructure() throws IOException { - checkState(); - return client.decode().getStructure(); - } - @Override public DataStructure getStructure(DataflowRef flowRef) throws IOException, IllegalArgumentException { checkState(); diff --git a/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRepoConnection.java b/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRepoConnection.java deleted file mode 100644 index d3d1a9b89..000000000 --- a/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRepoConnection.java +++ /dev/null @@ -1,14 +0,0 @@ -package sdmxdl.util.web; - -import sdmxdl.SdmxConnection; -import sdmxdl.web.SdmxWebConnection; - -@lombok.RequiredArgsConstructor -final class SdmxRepoConnection implements SdmxWebConnection { - - @lombok.Getter - private final String driver; - - @lombok.experimental.Delegate - private final SdmxConnection delegate; -} diff --git a/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRepoDriverSupport.java b/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRepoDriverSupport.java deleted file mode 100644 index a4f5d490d..000000000 --- a/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRepoDriverSupport.java +++ /dev/null @@ -1,66 +0,0 @@ -package sdmxdl.util.web; - -import lombok.AccessLevel; -import org.checkerframework.checker.nullness.qual.NonNull; -import sdmxdl.ext.SdmxException; -import sdmxdl.repo.SdmxRepository; -import sdmxdl.web.SdmxWebConnection; -import sdmxdl.web.SdmxWebSource; -import sdmxdl.web.spi.SdmxWebContext; -import sdmxdl.web.spi.SdmxWebDriver; - -import java.io.IOException; -import java.net.URI; -import java.util.Collection; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; - -@lombok.Builder(toBuilder = true) -public final class SdmxRepoDriverSupport implements SdmxWebDriver { - - @lombok.Getter - @lombok.NonNull - private final String name; - - @lombok.Getter - @lombok.Builder.Default - private final int rank = SdmxWebDriver.UNKNOWN; - - @lombok.Getter - @lombok.Builder.Default - private final boolean available = true; - - @lombok.Singular - private final List repos; - - @lombok.Getter(value = AccessLevel.PRIVATE, lazy = true) - private final Validator sourceValidator = SdmxValidators.onDriverName(name); - - @Override - public @NonNull SdmxWebConnection connect(@NonNull SdmxWebSource source, @NonNull SdmxWebContext context) throws IOException { - Objects.requireNonNull(source); - Objects.requireNonNull(context); - getSourceValidator().checkValidity(source); - - return repos.stream() - .filter(repo -> repo.getName().equals(source.getName())) - .map(repo -> new SdmxRepoConnection(name, repo.asConnection())) - .findFirst() - .orElseThrow(() -> SdmxException.missingSource(source.toString(), SdmxWebSource.class)); - } - - @Override - public @NonNull Collection getDefaultSources() { - return repos - .stream() - .map(repo -> SdmxWebSource.builder().name(repo.getName()).driver(name).endpoint(URI.create(repo.getName())).build()) - .collect(Collectors.toSet()); - } - - @Override - public @NonNull Collection getSupportedProperties() { - return Collections.emptyList(); - } -} diff --git a/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRestConnection.java b/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRestConnection.java index 22bf5f370..f4f612aaf 100644 --- a/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRestConnection.java +++ b/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRestConnection.java @@ -20,7 +20,6 @@ import sdmxdl.*; import sdmxdl.ext.SdmxException; import sdmxdl.util.DataRef; -import sdmxdl.web.SdmxWebConnection; import java.io.IOException; import java.util.Collection; @@ -34,14 +33,11 @@ * @author Philippe Charles */ @lombok.RequiredArgsConstructor(staticName = "of") -final class SdmxRestConnection implements SdmxWebConnection { +final class SdmxRestConnection implements SdmxConnection { @lombok.NonNull private final SdmxRestClient client; - @lombok.NonNull - private final String driver; - @lombok.NonNull private final Validator dataflowRefValidator; @@ -123,12 +119,6 @@ public void testConnection() throws IOException { client.testClient(); } - @Override - public String getDriver() throws IOException { - checkState(); - return driver; - } - @Override public void close() { closed = true; diff --git a/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRestDriverSupport.java b/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRestDriverSupport.java index 89a47fd62..c6c6f481a 100644 --- a/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRestDriverSupport.java +++ b/sdmx-dl-provider-util/src/main/java/sdmxdl/util/web/SdmxRestDriverSupport.java @@ -19,7 +19,7 @@ import lombok.AccessLevel; import org.checkerframework.checker.nullness.qual.NonNull; import sdmxdl.DataflowRef; -import sdmxdl.web.SdmxWebConnection; +import sdmxdl.SdmxConnection; import sdmxdl.web.SdmxWebSource; import sdmxdl.web.spi.SdmxWebContext; import sdmxdl.web.spi.SdmxWebDriver; @@ -66,12 +66,12 @@ public boolean isAvailable() { } @Override - public SdmxWebConnection connect(SdmxWebSource source, SdmxWebContext context) throws IOException { + public SdmxConnection connect(SdmxWebSource source, SdmxWebContext context) throws IOException { Objects.requireNonNull(source); Objects.requireNonNull(context); getSourceValidator().checkValidity(source); - return SdmxRestConnection.of(getClient(source, context), name, dataflowRefValidator); + return SdmxRestConnection.of(getClient(source, context), dataflowRefValidator); } @Override diff --git a/sdmx-dl-provider-util/src/test/java/sdmxdl/util/file/SdmxFileConnectionImplTest.java b/sdmx-dl-provider-util/src/test/java/sdmxdl/util/file/SdmxFileConnectionImplTest.java index 192576b8d..e31b13d7d 100644 --- a/sdmx-dl-provider-util/src/test/java/sdmxdl/util/file/SdmxFileConnectionImplTest.java +++ b/sdmx-dl-provider-util/src/test/java/sdmxdl/util/file/SdmxFileConnectionImplTest.java @@ -18,16 +18,10 @@ import _test.sdmxdl.util.XRepoFileClient; import org.junit.jupiter.api.Test; -import sdmxdl.DataQuery; -import sdmxdl.Series; import tests.sdmxdl.api.RepoSamples; import tests.sdmxdl.api.SdmxConnectionAssert; -import tests.sdmxdl.file.SdmxFileConnectionAssert; import java.io.IOException; -import java.util.stream.Stream; - -import static org.assertj.core.api.Assertions.assertThat; /** * @author Philippe Charles @@ -36,34 +30,15 @@ public class SdmxFileConnectionImplTest { @Test public void testCompliance() throws IOException { - SdmxFileConnectionAssert.assertCompliance( + SdmxConnectionAssert.assertCompliance( () -> new SdmxFileConnectionImpl(new XRepoFileClient(RepoSamples.REPO), RepoSamples.FLOW), - SdmxFileConnectionAssert.Sample + SdmxConnectionAssert.Sample .builder() - .connection(SdmxConnectionAssert.Sample - .builder() - .validFlow(RepoSamples.FLOW_REF) - .invalidFlow(RepoSamples.BAD_FLOW_REF) - .validKey(RepoSamples.K1) - .invalidKey(RepoSamples.INVALID_KEY) - .build()) + .validFlow(RepoSamples.FLOW_REF) + .invalidFlow(RepoSamples.BAD_FLOW_REF) + .validKey(RepoSamples.K1) + .invalidKey(RepoSamples.INVALID_KEY) .build() ); } - - @Test - @SuppressWarnings("null") - public void testFile() throws IOException { - SdmxFileClient r = new XRepoFileClient(RepoSamples.REPO); - - SdmxFileConnectionImpl conn = new SdmxFileConnectionImpl(r, RepoSamples.FLOW); - - assertThat(conn.getDataflowRef()).isEqualTo(RepoSamples.FLOW_REF); - assertThat(conn.getFlow()).isEqualTo(conn.getFlow(RepoSamples.FLOW_REF)); - assertThat(conn.getStructure()).isEqualTo(conn.getStructure(RepoSamples.FLOW_REF)); - - try (Stream stream = conn.getDataStream(conn.getDataflowRef(), DataQuery.ALL)) { - assertThat(stream).containsExactly(conn.getDataStream(conn.getDataflowRef(), DataQuery.ALL).toArray(Series[]::new)); - } - } } diff --git a/sdmx-dl-provider-util/src/test/java/sdmxdl/util/web/SdmxRepoDriverSupportTest.java b/sdmx-dl-provider-util/src/test/java/sdmxdl/util/web/SdmxRepoDriverSupportTest.java deleted file mode 100644 index a78d0a461..000000000 --- a/sdmx-dl-provider-util/src/test/java/sdmxdl/util/web/SdmxRepoDriverSupportTest.java +++ /dev/null @@ -1,44 +0,0 @@ -package sdmxdl.util.web; - -import org.junit.jupiter.api.Test; -import tests.sdmxdl.web.SdmxWebDriverAssert; -import sdmxdl.web.SdmxWebSource; -import sdmxdl.web.spi.SdmxWebContext; - -import static org.assertj.core.api.Assertions.*; -import static tests.sdmxdl.api.RepoSamples.REPO; - -public class SdmxRepoDriverSupportTest { - - @Test - public void testCompliance() { - SdmxWebDriverAssert.assertCompliance( - SdmxRepoDriverSupport - .builder() - .name("REPO") - .repo(REPO) - .build() - ); - } - - @Test - public void testGetDefaultSources() { - assertThat(SdmxRepoDriverSupport.builder().name("REPO").build().getDefaultSources()) - .isEmpty(); - - assertThat(SdmxRepoDriverSupport.builder().name("REPO").repo(REPO).build().getDefaultSources()) - .containsExactly(SdmxWebSource.builder().name(REPO.getName()).driver("REPO").endpointOf(REPO.getName()).build()); - } - - @Test - public void testConnect() { - SdmxWebSource source = SdmxWebSource.builder().name(REPO.getName()).driver("REPO").endpointOf(REPO.getName()).build(); - SdmxWebContext context = SdmxWebContext.builder().build(); - - assertThatIOException() - .isThrownBy(() -> SdmxRepoDriverSupport.builder().name("REPO").build().connect(source, context)); - - assertThatNoException() - .isThrownBy(() -> SdmxRepoDriverSupport.builder().name("REPO").repo(REPO).build().connect(source, context)); - } -} diff --git a/sdmx-dl-provider-util/src/test/java/sdmxdl/util/web/SdmxRestConnectionTest.java b/sdmx-dl-provider-util/src/test/java/sdmxdl/util/web/SdmxRestConnectionTest.java index c5e714967..d46d1c01c 100644 --- a/sdmx-dl-provider-util/src/test/java/sdmxdl/util/web/SdmxRestConnectionTest.java +++ b/sdmx-dl-provider-util/src/test/java/sdmxdl/util/web/SdmxRestConnectionTest.java @@ -31,7 +31,7 @@ public class SdmxRestConnectionTest { public void testCompliance() { SdmxRepository repo = RepoSamples.REPO; SdmxConnectionAssert.assertCompliance( - () -> SdmxRestConnection.of(XRepoRestClient.of(repo), "", Validator.noOp()), + () -> SdmxRestConnection.of(XRepoRestClient.of(repo), Validator.noOp()), SdmxConnectionAssert.Sample .builder() .validFlow(RepoSamples.FLOW_REF) diff --git a/sdmx-dl-testing/src/main/java/sdmxdl/testing/WebResponse.java b/sdmx-dl-testing/src/main/java/sdmxdl/testing/WebResponse.java index 3dfbec0c3..6b4101ce3 100644 --- a/sdmx-dl-testing/src/main/java/sdmxdl/testing/WebResponse.java +++ b/sdmx-dl-testing/src/main/java/sdmxdl/testing/WebResponse.java @@ -20,8 +20,8 @@ import org.checkerframework.checker.nullness.qual.Nullable; import sdmxdl.DataStructure; import sdmxdl.Dataflow; +import sdmxdl.SdmxConnection; import sdmxdl.Series; -import sdmxdl.web.SdmxWebConnection; import sdmxdl.web.SdmxWebManager; import sdmxdl.web.SdmxWebSource; @@ -65,7 +65,7 @@ public static WebResponse of(@NonNull WebRequest request, @NonNull SdmxWebManage .request(request) .source(manager.getSources().get(request.getSource())); - try (SdmxWebConnection conn = manager.getConnection(request.getSource())) { + try (SdmxConnection conn = manager.getConnection(request.getSource())) { result .flows(conn.getFlows()) .flow(conn.getFlow(request.getFlowRef()))