From 841d191a2175eb26b9bb5e1125097a58b09639fd Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Tue, 1 Aug 2023 22:42:15 +0200 Subject: [PATCH] Eliminate PlainPatchDataTransactionUtilTest Move the tests to AbstractRestconfStrategyTest and add proper specializations. Also adds a few asserts to actually verify things. JIRA: NETCONF-1107 Change-Id: I64dbfcd10db082d219a4f5e1a25e22319731e8b1 Signed-off-by: Robert Varga --- .../AbstractRestconfStrategyTest.java | 44 ++++++ .../MdsalRestconfStrategyTest.java | 21 +++ .../NetconfRestconfStrategyTest.java | 23 ++++ .../PlainPatchDataTransactionUtilTest.java | 128 ------------------ 4 files changed, 88 insertions(+), 128 deletions(-) delete mode 100644 restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/PlainPatchDataTransactionUtilTest.java diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/AbstractRestconfStrategyTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/AbstractRestconfStrategyTest.java index 191e575d88..d791b44189 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/AbstractRestconfStrategyTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/AbstractRestconfStrategyTest.java @@ -25,6 +25,7 @@ import org.opendaylight.restconf.common.errors.RestconfDocumentedException; import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest; import org.opendaylight.restconf.nb.rfc8040.WriteDataParams; import org.opendaylight.restconf.nb.rfc8040.rests.utils.DeleteDataTransactionUtil; +import org.opendaylight.restconf.nb.rfc8040.rests.utils.PlainPatchDataTransactionUtil; import org.opendaylight.restconf.nb.rfc8040.rests.utils.PostDataTransactionUtil; import org.opendaylight.yangtools.yang.common.ErrorTag; import org.opendaylight.yangtools.yang.common.ErrorType; @@ -51,6 +52,22 @@ abstract class AbstractRestconfStrategyTest extends AbstractJukeboxTest { .build()) .build()) .build(); + static final ContainerNode JUKEBOX_WITH_PLAYLIST = Builders.containerBuilder() + .withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME)) + .withChild(Builders.mapBuilder() + .withNodeIdentifier(new NodeIdentifier(PLAYLIST_QNAME)) + .withChild(Builders.mapEntryBuilder() + .withNodeIdentifier(NodeIdentifierWithPredicates.of(PLAYLIST_QNAME, NAME_QNAME, "MyFavoriteBand-A")) + .withChild(ImmutableNodes.leafNode(NAME_QNAME, "MyFavoriteBand-A")) + .withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, "band description A")) + .build()) + .withChild(Builders.mapEntryBuilder() + .withNodeIdentifier(NodeIdentifierWithPredicates.of(PLAYLIST_QNAME, NAME_QNAME, "MyFavoriteBand-B")) + .withChild(ImmutableNodes.leafNode(NAME_QNAME, "MyFavoriteBand-B")) + .withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, "band description B")) + .build()) + .build()) + .build(); static final MapNode PLAYLIST = Builders.mapBuilder() .withNodeIdentifier(new NodeIdentifier(PLAYLIST_QNAME)) .withChild(Builders.mapEntryBuilder() @@ -133,4 +150,31 @@ abstract class AbstractRestconfStrategyTest extends AbstractJukeboxTest { } abstract @NonNull RestconfStrategy testPostDataFailStrategy(DOMException domException); + + @Test + public final void testPatchContainerData() { + final var response = PlainPatchDataTransactionUtil.patchData(JUKEBOX_IID, EMPTY_JUKEBOX, + testPatchContainerDataStrategy(), JUKEBOX_SCHEMA); + assertEquals(200, response.getStatus()); + } + + abstract @NonNull RestconfStrategy testPatchContainerDataStrategy(); + + @Test + public final void testPatchLeafData() { + final var response = PlainPatchDataTransactionUtil.patchData(GAP_IID, GAP_LEAF, + testPatchLeafDataStrategy(), JUKEBOX_SCHEMA); + assertEquals(200, response.getStatus()); + } + + abstract @NonNull RestconfStrategy testPatchLeafDataStrategy(); + + @Test + public final void testPatchListData() { + final var response = PlainPatchDataTransactionUtil.patchData(JUKEBOX_IID, JUKEBOX_WITH_PLAYLIST, + testPatchListDataStrategy(), JUKEBOX_SCHEMA); + assertEquals(200, response.getStatus()); + } + + abstract @NonNull RestconfStrategy testPatchListDataStrategy(); } diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfStrategyTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfStrategyTest.java index a3b29d4c2e..faa61be9a8 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfStrategyTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/MdsalRestconfStrategyTest.java @@ -128,4 +128,25 @@ public final class MdsalRestconfStrategyTest extends AbstractRestconfStrategyTes doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit(); return new MdsalRestconfStrategy(mockDataBroker); } + + @Override + RestconfStrategy testPatchContainerDataStrategy() { + doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction(); + doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit(); + return new MdsalRestconfStrategy(mockDataBroker); + } + + @Override + RestconfStrategy testPatchLeafDataStrategy() { + doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction(); + doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit(); + return new MdsalRestconfStrategy(mockDataBroker); + } + + @Override + RestconfStrategy testPatchListDataStrategy() { + doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction(); + doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit(); + return new MdsalRestconfStrategy(mockDataBroker); + } } diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/NetconfRestconfStrategyTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/NetconfRestconfStrategyTest.java index c1741ad7c3..073f6a629a 100644 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/NetconfRestconfStrategyTest.java +++ b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/transactions/NetconfRestconfStrategyTest.java @@ -91,6 +91,29 @@ public final class NetconfRestconfStrategyTest extends AbstractRestconfStrategyT return new NetconfRestconfStrategy(netconfService); } + @Override + RestconfStrategy testPatchContainerDataStrategy() { + doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).merge(any(), any(),any(), + any()); + return new NetconfRestconfStrategy(netconfService); + } + + @Override + RestconfStrategy testPatchLeafDataStrategy() { + doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService) + .merge(any(), any(), any(), any()); + doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).commit(); + return new NetconfRestconfStrategy(netconfService); + } + + @Override + RestconfStrategy testPatchListDataStrategy() { + doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).commit(); + doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService) + .merge(any(), any(),any(),any()); + return new NetconfRestconfStrategy(netconfService); + } + @Test public void testPutCreateContainerData() { doReturn(immediateFluentFuture(Optional.empty())).when(netconfService).getConfig(JUKEBOX_IID); diff --git a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/PlainPatchDataTransactionUtilTest.java b/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/PlainPatchDataTransactionUtilTest.java deleted file mode 100644 index 058f55d87e..0000000000 --- a/restconf/restconf-nb/src/test/java/org/opendaylight/restconf/nb/rfc8040/rests/utils/PlainPatchDataTransactionUtilTest.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright (c) 2020 Lumina Networks, Inc. and others. All rights reserved. - * Copyright (c) 2016 Cisco Systems, Inc. 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.restconf.nb.rfc8040.rests.utils; - -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.verify; - -import com.google.common.util.concurrent.Futures; -import java.util.Optional; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.Mock; -import org.mockito.junit.MockitoJUnitRunner; -import org.opendaylight.mdsal.common.api.CommitInfo; -import org.opendaylight.mdsal.common.api.LogicalDatastoreType; -import org.opendaylight.mdsal.dom.api.DOMDataBroker; -import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction; -import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction; -import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction; -import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult; -import org.opendaylight.netconf.dom.api.NetconfDataTreeService; -import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest; -import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy; -import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates; -import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; -import org.opendaylight.yangtools.yang.data.impl.schema.Builders; -import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; - -@RunWith(MockitoJUnitRunner.StrictStubs.class) -public class PlainPatchDataTransactionUtilTest extends AbstractJukeboxTest { - private static final ContainerNode JUKEBOX_WITH_PLAYLIST = Builders.containerBuilder() - .withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME)) - .withChild(Builders.mapBuilder() - .withNodeIdentifier(new NodeIdentifier(PLAYLIST_QNAME)) - .withChild(Builders.mapEntryBuilder() - .withNodeIdentifier(NodeIdentifierWithPredicates.of(PLAYLIST_QNAME, NAME_QNAME, "MyFavoriteBand-A")) - .withChild(ImmutableNodes.leafNode(NAME_QNAME, "MyFavoriteBand-A")) - .withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, "band description A")) - .build()) - .withChild(Builders.mapEntryBuilder() - .withNodeIdentifier(NodeIdentifierWithPredicates.of(PLAYLIST_QNAME, NAME_QNAME, "MyFavoriteBand-B")) - .withChild(ImmutableNodes.leafNode(NAME_QNAME, "MyFavoriteBand-B")) - .withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, "band description B")) - .build()) - .build()) - .build(); - - @Mock - private DOMDataTreeReadWriteTransaction readWrite; - @Mock - private DOMDataTreeReadTransaction read; - @Mock - private DOMDataTreeWriteTransaction write; - @Mock - private DOMDataBroker mockDataBroker; - @Mock - private NetconfDataTreeService netconfService; - - @Before - public void before() { - doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).lock(); - doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).unlock(); - } - - @Test - public void testPatchContainerData() { - doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction(); - doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit(); - doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).commit(); - doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).merge(any(), any(),any(), - any()); - - PlainPatchDataTransactionUtil.patchData(JUKEBOX_IID, EMPTY_JUKEBOX, - new MdsalRestconfStrategy(mockDataBroker), JUKEBOX_SCHEMA); - verify(readWrite).merge(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, EMPTY_JUKEBOX); - - PlainPatchDataTransactionUtil.patchData(JUKEBOX_IID, EMPTY_JUKEBOX, - new NetconfRestconfStrategy(netconfService), JUKEBOX_SCHEMA); - verify(netconfService).merge(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, EMPTY_JUKEBOX, - Optional.empty()); - } - - @Test - public void testPatchLeafData() { - doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction(); - doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit(); - doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService) - .merge(any(), any(), any(), any()); - doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).commit(); - - PlainPatchDataTransactionUtil.patchData(GAP_IID, GAP_LEAF, new MdsalRestconfStrategy(mockDataBroker), - JUKEBOX_SCHEMA); - verify(readWrite).merge(LogicalDatastoreType.CONFIGURATION, GAP_IID, GAP_LEAF); - - PlainPatchDataTransactionUtil.patchData(GAP_IID, GAP_LEAF, new NetconfRestconfStrategy(netconfService), - JUKEBOX_SCHEMA); - verify(netconfService).lock(); - verify(netconfService).merge(LogicalDatastoreType.CONFIGURATION, GAP_IID, GAP_LEAF, Optional.empty()); - } - - @Test - public void testPatchListData() { - doReturn(readWrite).when(mockDataBroker).newReadWriteTransaction(); - doReturn(CommitInfo.emptyFluentFuture()).when(readWrite).commit(); - doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).commit(); - doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService) - .merge(any(), any(),any(),any()); - - PlainPatchDataTransactionUtil.patchData(JUKEBOX_IID, JUKEBOX_WITH_PLAYLIST, - new MdsalRestconfStrategy(mockDataBroker), JUKEBOX_SCHEMA); - verify(readWrite).merge(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, JUKEBOX_WITH_PLAYLIST); - - PlainPatchDataTransactionUtil.patchData(JUKEBOX_IID, JUKEBOX_WITH_PLAYLIST, - new NetconfRestconfStrategy(netconfService), JUKEBOX_SCHEMA); - verify(netconfService).merge(LogicalDatastoreType.CONFIGURATION, JUKEBOX_IID, JUKEBOX_WITH_PLAYLIST, - Optional.empty()); - } -} -- 2.36.6