Share test model SchemaContext 51/84951/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 26 Aug 2019 10:14:28 +0000 (12:14 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 7 Oct 2019 08:12:51 +0000 (10:12 +0200)
There is no point really to construct this context multiple times,
as it is an invariant. This shaves off some time from test execution.

Change-Id: I4335181bb6992ce5fe1820a452b7a90e0ea14f3a
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit bb56949ce4ba4d10089f4b58aa42c29181871d8e)

netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/AbstractTestModelTest.java [new file with mode: 0644]
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDeviceTest.java
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateRunningTxTest.java
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateTxTest.java
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteRunningTxTest.java
netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfBaseOpsTest.java

diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/AbstractTestModelTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/AbstractTestModelTest.java
new file mode 100644 (file)
index 0000000..03977c7
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2019 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.sal.connect.netconf;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+public abstract class AbstractTestModelTest {
+    protected static SchemaContext SCHEMA_CONTEXT;
+
+    @BeforeClass
+    public static void beforeClass() {
+        SCHEMA_CONTEXT = YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        SCHEMA_CONTEXT = null;
+    }
+}
index 00a0c9b4cea7f9caba9bb58b5b44fbdb126caf31..59f3ec6023f331577861c1ff65832e1e4508bd85 100644 (file)
@@ -76,11 +76,10 @@ import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.PotentialSchemaSource;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistration;
 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 import org.xml.sax.SAXException;
 
 @SuppressWarnings("checkstyle:IllegalCatch")
-public class NetconfDeviceTest {
+public class NetconfDeviceTest extends AbstractTestModelTest {
 
     private static final NetconfMessage NOTIFICATION;
 
@@ -118,13 +117,13 @@ public class NetconfDeviceTest {
     private static final NetconfDeviceSchemasResolver STATE_SCHEMAS_RESOLVER =
         (deviceRpc, remoteSessionCapabilities, id, schemaContext) -> NetconfStateSchemas.EMPTY;
 
+
     @Test
     public void testNetconfDeviceFlawedModelFailedResolution() throws Exception {
         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
         final NetconfDeviceCommunicator listener = getListener();
 
         final SchemaContextFactory schemaFactory = getSchemaFactory();
-        final SchemaContext schema = getSchema();
         final SchemaRepository schemaRepository = getSchemaRepository();
 
         final SchemaResolutionException schemaResolutionException =
@@ -133,13 +132,13 @@ public class NetconfDeviceTest {
             if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
                 return Futures.immediateFailedFuture(schemaResolutionException);
             } else {
-                return Futures.immediateFuture(schema);
+                return Futures.immediateFuture(SCHEMA_CONTEXT);
             }
         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
 
         final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id,
                 schemaContext) -> {
-            final Module first = Iterables.getFirst(schema.getModules(), null);
+            final Module first = Iterables.getFirst(SCHEMA_CONTEXT.getModules(), null);
             final QName qName = QName.create(first.getQNameModule(), first.getName());
             final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
             final NetconfStateSchemas.RemoteYangSchema source2 =
@@ -207,7 +206,6 @@ public class NetconfDeviceTest {
     public void testNetconfDeviceMissingSource() throws Exception {
         final RemoteDeviceHandler<NetconfSessionPreferences> facade = getFacade();
         final NetconfDeviceCommunicator listener = getListener();
-        final SchemaContext schema = getSchema();
 
         final SchemaContextFactory schemaFactory = getSchemaFactory();
         final SchemaRepository schemaRepository = getSchemaRepository();
@@ -221,13 +219,13 @@ public class NetconfDeviceTest {
             if (((Collection<?>) invocation.getArguments()[0]).size() == 2) {
                 return Futures.immediateFailedFuture(schemaResolutionException);
             } else {
-                return Futures.immediateFuture(schema);
+                return Futures.immediateFuture(SCHEMA_CONTEXT);
             }
         }).when(schemaFactory).createSchemaContext(anyCollectionOf(SourceIdentifier.class));
 
         final NetconfDeviceSchemasResolver stateSchemasResolver = (deviceRpc, remoteSessionCapabilities, id,
             schemaContext) -> {
-            final Module first = Iterables.getFirst(schema.getModules(), null);
+            final Module first = Iterables.getFirst(SCHEMA_CONTEXT.getModules(), null);
             final QName qName = QName.create(first.getQNameModule(), first.getName());
             final NetconfStateSchemas.RemoteYangSchema source1 = new NetconfStateSchemas.RemoteYangSchema(qName);
             final NetconfStateSchemas.RemoteYangSchema source2 =
@@ -368,7 +366,7 @@ public class NetconfDeviceTest {
         device.onRemoteSessionDown();
         verify(facade, timeout(5000)).onDeviceDisconnected();
         //complete schema setup
-        schemaFuture.set(getSchema());
+        schemaFuture.set(SCHEMA_CONTEXT);
         //facade.onDeviceDisconnected() was called, so facade.onDeviceConnected() shouldn't be called anymore
         verify(facade, after(1000).never()).onDeviceConnected(any(), any(), any(), any(DOMActionService.class));
     }
@@ -414,15 +412,11 @@ public class NetconfDeviceTest {
 
     private static SchemaContextFactory getSchemaFactory() throws Exception {
         final SchemaContextFactory schemaFactory = mockClass(SchemaContextFactory.class);
-        doReturn(Futures.immediateFuture(getSchema()))
+        doReturn(Futures.immediateFuture(SCHEMA_CONTEXT))
                 .when(schemaFactory).createSchemaContext(any(Collection.class));
         return schemaFactory;
     }
 
-    public static SchemaContext getSchema() {
-        return YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
-    }
-
     private static RemoteDeviceHandler<NetconfSessionPreferences> getFacade() throws Exception {
         final RemoteDeviceHandler<NetconfSessionPreferences> remoteDeviceHandler =
                 mockCloseableClass(RemoteDeviceHandler.class);
index fdc2085e7d2db79ee162b95a9d0dd48b2c52bbdb..4974ce9ccf148933414f961bb06e9a3185cbd7f1 100644 (file)
@@ -25,6 +25,7 @@ import org.mockito.MockitoAnnotations;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.sal.connect.netconf.AbstractTestModelTest;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
@@ -36,11 +37,9 @@ import org.opendaylight.yangtools.yang.data.api.schema.ChoiceNode;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
-public class WriteCandidateRunningTxTest {
+public class WriteCandidateRunningTxTest extends AbstractTestModelTest {
     @Mock
     private DOMRpcService rpc;
     private NetconfBaseOps netconfOps;
@@ -49,10 +48,8 @@ public class WriteCandidateRunningTxTest {
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        final SchemaContext schemaContext =
-                YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
         doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult())).when(rpc).invokeRpc(any(), any());
-        netconfOps = new NetconfBaseOps(rpc, schemaContext);
+        netconfOps = new NetconfBaseOps(rpc, SCHEMA_CONTEXT);
         id = new RemoteDeviceId("device1", InetSocketAddress.createUnresolved("0.0.0.0", 17830));
     }
 
index 436782f8095f360d51a3fa6ced1c45908d091462..59e140b67ae93b1680d1f4172c58dac63066fe22 100644 (file)
@@ -21,16 +21,14 @@ import org.mockito.MockitoAnnotations;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.sal.connect.netconf.AbstractTestModelTest;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-
-public class WriteCandidateTxTest {
 
+public class WriteCandidateTxTest extends AbstractTestModelTest {
     @Mock
     private DOMRpcService rpc;
     private NetconfBaseOps netconfOps;
@@ -39,10 +37,8 @@ public class WriteCandidateTxTest {
     @Before
     public void setUp() throws Exception {
         MockitoAnnotations.initMocks(this);
-        final SchemaContext schemaContext =
-                YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
         doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult())).when(rpc).invokeRpc(any(), any());
-        netconfOps = new NetconfBaseOps(rpc, schemaContext);
+        netconfOps = new NetconfBaseOps(rpc, SCHEMA_CONTEXT);
         id = new RemoteDeviceId("device1", InetSocketAddress.createUnresolved("0.0.0.0", 17830));
     }
 
index 6424df9fe0373eba1b8786d853b1064ebdc25651..cd672a773e50841dcb5cdf4a77ef05b8bd844c97 100644 (file)
@@ -22,28 +22,24 @@ import org.mockito.MockitoAnnotations;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMRpcService;
 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.sal.connect.netconf.AbstractTestModelTest;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfBaseOps;
 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
-
-public class WriteRunningTxTest {
 
+public class WriteRunningTxTest extends AbstractTestModelTest {
     @Mock
     private DOMRpcService rpc;
     private NetconfBaseOps netconfOps;
     private RemoteDeviceId id;
 
     @Before
-    public void setUp() throws Exception {
+    public void setUp() {
         MockitoAnnotations.initMocks(this);
-        final SchemaContext schemaContext =
-                YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
         doReturn(FluentFutures.immediateFluentFuture(new DefaultDOMRpcResult())).when(rpc).invokeRpc(any(), any());
-        netconfOps = new NetconfBaseOps(rpc, schemaContext);
+        netconfOps = new NetconfBaseOps(rpc, SCHEMA_CONTEXT);
         id = new RemoteDeviceId("device1", InetSocketAddress.createUnresolved("0.0.0.0", 17830));
     }
 
index 5c9739dbd0d5df2326e462bd1b4e3c260d64450a..18fde694865cbf6a84ca0cd0e722d4f16292f581 100644 (file)
@@ -34,6 +34,7 @@ import org.opendaylight.netconf.api.NetconfMessage;
 import org.opendaylight.netconf.api.xml.XmlUtil;
 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
+import org.opendaylight.netconf.sal.connect.netconf.AbstractTestModelTest;
 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfDeviceRpc;
 import org.opendaylight.netconf.sal.connect.netconf.schema.mapping.NetconfMessageTransformer;
 import org.opendaylight.netconf.sal.connect.util.RemoteDeviceId;
@@ -45,22 +46,19 @@ import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.NamedNodeMap;
 import org.xml.sax.SAXException;
 
-public class NetconfBaseOpsTest {
+public class NetconfBaseOpsTest extends AbstractTestModelTest {
+    private static final QName CONTAINER_Q_NAME = QName.create("test:namespace", "2013-07-22", "c");
 
     static {
         XMLUnit.setIgnoreWhitespace(true);
         XMLUnit.setIgnoreComments(true);
     }
 
-    private static final QName CONTAINER_Q_NAME = QName.create("test:namespace", "2013-07-22", "c");
-
     @Mock
     private RemoteDeviceCommunicator<NetconfMessage> listener;
     private NetconfRpcFutureCallback callback;
@@ -91,14 +89,12 @@ public class NetconfBaseOpsTest {
                 .thenReturn(FluentFuture.from(RpcResultBuilder.success(ok).buildFuture()));
         when(listener.sendRequest(any(), eq(NetconfMessageTransformUtil.NETCONF_COMMIT_QNAME)))
                 .thenReturn(FluentFuture.from(RpcResultBuilder.success(ok).buildFuture()));
-        final SchemaContext schemaContext =
-                YangParserTestUtils.parseYangResource("/schemas/test-module.yang");
-        final MessageTransformer<NetconfMessage> transformer = new NetconfMessageTransformer(schemaContext, true);
-        final DOMRpcService rpc = new NetconfDeviceRpc(schemaContext, listener, transformer);
+        final MessageTransformer<NetconfMessage> transformer = new NetconfMessageTransformer(SCHEMA_CONTEXT, true);
+        final DOMRpcService rpc = new NetconfDeviceRpc(SCHEMA_CONTEXT, listener, transformer);
         final RemoteDeviceId id =
                 new RemoteDeviceId("device-1", InetSocketAddress.createUnresolved("localhost", 17830));
         callback = new NetconfRpcFutureCallback("prefix", id);
-        baseOps = new NetconfBaseOps(rpc, schemaContext);
+        baseOps = new NetconfBaseOps(rpc, SCHEMA_CONTEXT);
     }
 
     @Test