From bb56949ce4ba4d10089f4b58aa42c29181871d8e Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 26 Aug 2019 12:14:28 +0200 Subject: [PATCH] Share test model SchemaContext 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 --- .../netconf/AbstractTestModelTest.java | 27 +++++++++++++++++++ .../connect/netconf/NetconfDeviceTest.java | 22 ++++++--------- .../sal/tx/WriteCandidateRunningTxTest.java | 9 +++---- .../netconf/sal/tx/WriteCandidateTxTest.java | 10 +++---- .../netconf/sal/tx/WriteRunningTxTest.java | 12 +++------ .../netconf/util/NetconfBaseOpsTest.java | 16 +++++------ 6 files changed, 51 insertions(+), 45 deletions(-) create mode 100644 netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/AbstractTestModelTest.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 index 0000000000..03977c7a95 --- /dev/null +++ b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/AbstractTestModelTest.java @@ -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; + } +} diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDeviceTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDeviceTest.java index 00a0c9b4ce..59f3ec6023 100644 --- a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDeviceTest.java +++ b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/NetconfDeviceTest.java @@ -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 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 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 getFacade() throws Exception { final RemoteDeviceHandler remoteDeviceHandler = mockCloseableClass(RemoteDeviceHandler.class); diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateRunningTxTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateRunningTxTest.java index fdc2085e7d..4974ce9ccf 100644 --- a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateRunningTxTest.java +++ b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateRunningTxTest.java @@ -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)); } diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateTxTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateTxTest.java index 436782f809..59e140b67a 100644 --- a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateTxTest.java +++ b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteCandidateTxTest.java @@ -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)); } diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteRunningTxTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteRunningTxTest.java index 6424df9fe0..cd672a773e 100644 --- a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteRunningTxTest.java +++ b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/sal/tx/WriteRunningTxTest.java @@ -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)); } diff --git a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfBaseOpsTest.java b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfBaseOpsTest.java index 5c9739dbd0..18fde69486 100644 --- a/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfBaseOpsTest.java +++ b/netconf/sal-netconf-connector/src/test/java/org/opendaylight/netconf/sal/connect/netconf/util/NetconfBaseOpsTest.java @@ -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 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 transformer = new NetconfMessageTransformer(schemaContext, true); - final DOMRpcService rpc = new NetconfDeviceRpc(schemaContext, listener, transformer); + final MessageTransformer 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 -- 2.36.6