Clean up CachedYangTextSchemaSource 68/110068/6
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 28 Jan 2024 23:34:12 +0000 (00:34 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 31 Jan 2024 22:10:25 +0000 (22:10 +0000)
Rename to CachedYangTextSource and inherit from StringYangTextSource,
which does everything we need it to do. Also update the test to use
JUnit5.

Change-Id: Ibc0d19ebe4ca7582319da3f864dc7c4d5af2459c
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/CachedYangTextSchemaSource.java [deleted file]
plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/CachedYangTextSource.java [new file with mode: 0644]
plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/LibrarySchemaSourceProvider.java
plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/MonitoringSchemaSourceProvider.java
plugins/netconf-client-mdsal/src/test/java/org/opendaylight/netconf/client/mdsal/LibrarySchemaYangSourceProviderTest.java

diff --git a/plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/CachedYangTextSchemaSource.java b/plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/CachedYangTextSchemaSource.java
deleted file mode 100644 (file)
index 76b9c7e..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-/*
- * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.netconf.client.mdsal;
-
-import static java.util.Objects.requireNonNull;
-
-import com.google.common.base.MoreObjects;
-import java.io.Reader;
-import java.io.StringReader;
-import org.eclipse.jdt.annotation.NonNull;
-import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
-import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
-import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
-
-/**
- * A {@link YangTextSource} cached from a remote service.
- */
-// FIXME: subclass StringYangTextSource and cleanup addToStringAttributes()
-public final class CachedYangTextSchemaSource extends YangTextSource {
-    private final @NonNull SourceIdentifier sourceId;
-    private final @NonNull RemoteDeviceId id;
-    private final @NonNull String symbolicName;
-    private final @NonNull String schemaString;
-
-    public CachedYangTextSchemaSource(final RemoteDeviceId id, final SourceIdentifier sourceId,
-            final String symbolicName, final String schemaString) {
-        this.id = requireNonNull(id);
-        this.sourceId = requireNonNull(sourceId);
-        this.symbolicName = requireNonNull(symbolicName);
-        this.schemaString = requireNonNull(schemaString);
-    }
-
-    @Override
-    public Reader openStream() {
-        return new StringReader(schemaString);
-    }
-
-    @Override
-    public SourceIdentifier sourceId() {
-        return sourceId;
-    }
-
-    @Override
-    public String symbolicName() {
-        return symbolicName;
-    }
-
-    @Override
-    protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
-        return toStringHelper.add("device", id);
-    }
-}
\ No newline at end of file
diff --git a/plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/CachedYangTextSource.java b/plugins/netconf-client-mdsal/src/main/java/org/opendaylight/netconf/client/mdsal/CachedYangTextSource.java
new file mode 100644 (file)
index 0000000..4956713
--- /dev/null
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.netconf.client.mdsal;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.base.MoreObjects;
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
+import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
+import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
+import org.opendaylight.yangtools.yang.model.spi.source.StringYangTextSource;
+
+/**
+ * A {@link YangTextSource} cached from a remote service.
+ */
+@NonNullByDefault
+public final class CachedYangTextSource extends StringYangTextSource {
+    private final RemoteDeviceId deviceId;
+
+    public CachedYangTextSource(final RemoteDeviceId deviceId, final SourceIdentifier sourceId, final String content,
+            final @Nullable String symbolicName) {
+        super(sourceId, content, symbolicName);
+        this.deviceId = requireNonNull(deviceId);
+    }
+
+    public RemoteDeviceId deviceId() {
+        return deviceId;
+    }
+
+    @Override
+    protected MoreObjects.ToStringHelper addToStringAttributes(final MoreObjects.ToStringHelper toStringHelper) {
+        return super.addToStringAttributes(toStringHelper.add("deviceId", deviceId));
+    }
+}
index 34305f2f9e7be1f7245b3615ace98e54d0d40e25..85e3716e3ac10522beae329b14562ac50af72aab 100644 (file)
@@ -54,8 +54,8 @@ public final class LibrarySchemaSourceProvider implements SchemaSourceProvider<Y
                 "Unable to download remote schema for " + sourceIdentifier + " from " + url, e));
         }
 
-        final var yangSource = new CachedYangTextSchemaSource(id, sourceIdentifier, url.toString(),
-            new String(schemaContent, StandardCharsets.UTF_8));
+        final var yangSource = new CachedYangTextSource(id, sourceIdentifier,
+            new String(schemaContent, StandardCharsets.UTF_8), url.toString());
         LOG.debug("Source {} downloaded from a yang library's url {}", sourceIdentifier, url);
         return Futures.immediateFuture(yangSource);
     }
index 69340c5bbbccc84bae931f877c87a6458f24eb68..4c50943c451f290dc28228de407adcd1c3a98092 100644 (file)
@@ -103,7 +103,7 @@ public final class MonitoringSchemaSourceProvider implements SchemaSourceProvide
                             id + ": Unexpected response to get-schema, schema not present in message for: "
                                 + sourceIdentifier));
                     LOG.debug("{}: YANG Schema successfully retrieved for {}:{}", id, moduleName, revision);
-                    return new CachedYangTextSchemaSource(id, sourceIdentifier, moduleName, schemaString);
+                    return new CachedYangTextSource(id, sourceIdentifier, schemaString, null);
                 }
 
                 LOG.warn("{}: YANG schema was not successfully retrieved for {}. Errors: {}", id, sourceIdentifier,
index 8651fc062692182698ffe10cefa0544facfb92bf..4e380bb862279ebf50f01517d0a07c3d175d6197 100644 (file)
@@ -7,54 +7,46 @@
  */
 package org.opendaylight.netconf.client.mdsal;
 
-import static org.hamcrest.CoreMatchers.containsString;
-import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.CoreMatchers.startsWith;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
 import java.net.InetSocketAddress;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.util.Map;
 import java.util.concurrent.ExecutionException;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceId;
 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
 
 public class LibrarySchemaYangSourceProviderTest {
-    private SourceIdentifier workingSid;
-    private LibrarySchemaSourceProvider yangLibrarySchemaYangSourceProvider;
-
-    @Before
-    public void setUp() throws Exception {
-        workingSid = new SourceIdentifier("abc");
-        yangLibrarySchemaYangSourceProvider = new LibrarySchemaSourceProvider(
-            new RemoteDeviceId("id", new InetSocketAddress("localhost", 22)),
-            Map.of(workingSid, getClass().getResource("/schemas/config-test-rpc.yang")));
-    }
+    private final SourceIdentifier workingSid = new SourceIdentifier("abc");
+    private final LibrarySchemaSourceProvider yangLibrarySchemaYangSourceProvider = new LibrarySchemaSourceProvider(
+        new RemoteDeviceId("id", new InetSocketAddress("localhost", 22)),
+        Map.of(workingSid, LibrarySchemaYangSourceProviderTest.class.getResource("/schemas/config-test-rpc.yang")));
 
     @Test
-    public void testGetSource() throws Exception {
+    void testGetSource() throws Exception {
         var source = yangLibrarySchemaYangSourceProvider.getSource(workingSid);
-        final String x = source.get().read();
-        assertThat(x, containsString("module config-test-rpc"));
+        assertThat(source.get().read(), startsWith("module config-test-rpc"));
     }
 
     @Test
-    public void testGetSourceFailure() throws InterruptedException, MalformedURLException {
+    void testGetSourceFailure() throws InterruptedException, MalformedURLException {
         final var sourceIdentifierURLMap = Map.of(workingSid, new URL("http://non-existing-entity.yang"));
         final var failingYangLibrarySchemaYangSourceProvider = new LibrarySchemaSourceProvider(
             new RemoteDeviceId("id", new InetSocketAddress("localhost", 22)), sourceIdentifierURLMap);
 
         final var future = failingYangLibrarySchemaYangSourceProvider.getSource(workingSid);
         final var ex = assertThrows(ExecutionException.class, () -> future.get());
-        assertThat(ex.getCause(), instanceOf(SchemaSourceException.class));
+        assertInstanceOf(SchemaSourceException.class, ex.getCause());
     }
 
     @Test
-    public void testGetSourceNotAvailable() {
+    void testGetSourceNotAvailable() {
         assertThrows(IllegalArgumentException.class,
             () -> yangLibrarySchemaYangSourceProvider.getSource(new SourceIdentifier("aaaaa")));
     }