*/
package org.opendaylight.restconf.nb.rfc8040.rests.utils;
-import static org.mockito.Mockito.doNothing;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
-import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
+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.Mockito;
-import org.mockito.MockitoAnnotations;
+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.api.DOMTransactionChain;
+import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
-import org.opendaylight.restconf.common.context.NormalizedNodeContext;
import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
-import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
-import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
+import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
+import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
+import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
import org.opendaylight.yangtools.yang.common.QName;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class PlainPatchDataTransactionUtilTest {
-
private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
-
- @Mock
- private DOMTransactionChain transactionChain;
@Mock
private DOMDataTreeReadWriteTransaction readWrite;
@Mock
private DOMDataTreeWriteTransaction write;
@Mock
private DOMDataBroker mockDataBroker;
+ @Mock
+ private NetconfDataTreeService netconfService;
- private TransactionChainHandler transactionChainHandler;
- private LeafNode leafGap;
+ private LeafNode<?> leafGap;
private ContainerNode jukeboxContainerWithPlayer;
private ContainerNode jukeboxContainerWithPlaylist;
private EffectiveModelContext schema;
@Before
public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this);
- this.schema = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
+ schema = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
final QName qnJukebox = QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
final QName qnPlayer = QName.create(qnJukebox, "player");
final NodeIdentifierWithPredicates nidBandB =
NodeIdentifierWithPredicates.of(qnPlaylist, qnPlaylistKey, "MyFavoriteBand-B");
- this.iidGap = YangInstanceIdentifier.builder()
+ iidGap = YangInstanceIdentifier.builder()
.node(qnJukebox)
.node(qnPlayer)
.node(qnGap)
.build();
- this.schemaNodeForGap = DataSchemaContextTree.from(this.schema).getChild(this.iidGap).getDataSchemaNode();
+ schemaNodeForGap = DataSchemaContextTree.from(schema).findChild(iidGap).orElseThrow()
+ .getDataSchemaNode();
- this.iidJukebox = YangInstanceIdentifier.builder()
+ iidJukebox = YangInstanceIdentifier.builder()
.node(qnJukebox)
.build();
- this.schemaNodeForJukebox = DataSchemaContextTree.from(this.schema)
- .getChild(this.iidJukebox).getDataSchemaNode();
+ schemaNodeForJukebox = DataSchemaContextTree.from(schema)
+ .findChild(iidJukebox).orElseThrow().getDataSchemaNode();
- this.leafGap = Builders.leafBuilder()
+ leafGap = Builders.leafBuilder()
.withNodeIdentifier(new NodeIdentifier(qnGap))
.withValue(0.2)
.build();
final ContainerNode playerContainer = Builders.containerBuilder()
.withNodeIdentifier(new NodeIdentifier(qnPlayer))
- .withChild(this.leafGap)
+ .withChild(leafGap)
.build();
- this.jukeboxContainerWithPlayer = Builders.containerBuilder()
+ jukeboxContainerWithPlayer = Builders.containerBuilder()
.withNodeIdentifier(new NodeIdentifier(qnJukebox))
.withChild(playerContainer)
.build();
.withChild(entryBandA)
.withChild(entryBandB)
.build();
- this.jukeboxContainerWithPlaylist = Builders.containerBuilder()
+ jukeboxContainerWithPlaylist = Builders.containerBuilder()
.withNodeIdentifier(new NodeIdentifier(qnJukebox))
.withChild(listBands)
.build();
- Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
- transactionChainHandler = new TransactionChainHandler(mockDataBroker);
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).lock();
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(netconfService).unlock();
}
@Test
public void testPatchContainerData() {
- final InstanceIdentifierContext<DataSchemaNode> iidContext =
- new InstanceIdentifierContext<>(this.iidJukebox, this.schemaNodeForJukebox, null, this.schema);
- final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.jukeboxContainerWithPlayer);
-
- doReturn(this.readWrite).when(this.transactionChain).newReadWriteTransaction();
- doReturn(this.read).when(this.transactionChain).newReadOnlyTransaction();
- doReturn(this.write).when(this.transactionChain).newWriteOnlyTransaction();
- doReturn(immediateFalseFluentFuture())
- .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidJukebox);
- doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION,
+ final InstanceIdentifierContext iidContext =
+ new InstanceIdentifierContext(iidJukebox, schemaNodeForJukebox, null, schema);
+ final NormalizedNodePayload payload = NormalizedNodePayload.of(iidContext, jukeboxContainerWithPlayer);
+
+ 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(payload, new MdsalRestconfStrategy(mockDataBroker), schema);
+ verify(readWrite).merge(LogicalDatastoreType.CONFIGURATION,
payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData());
- doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
-
- PlainPatchDataTransactionUtil.patchData(payload,
- new TransactionVarsWrapper(payload.getInstanceIdentifierContext(), null, transactionChainHandler),
- this.schema);
- verify(this.readWrite).merge(LogicalDatastoreType.CONFIGURATION,
- payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData());
+ PlainPatchDataTransactionUtil.patchData(payload, new NetconfRestconfStrategy(netconfService),
+ schema);
+ verify(netconfService).merge(LogicalDatastoreType.CONFIGURATION,
+ payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData(), Optional.empty());
}
@Test
public void testPatchLeafData() {
- final InstanceIdentifierContext<DataSchemaNode> iidContext =
- new InstanceIdentifierContext<>(this.iidGap, this.schemaNodeForGap, null, this.schema);
- final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.leafGap);
-
- doReturn(this.readWrite).when(this.transactionChain).newReadWriteTransaction();
- doReturn(this.read).when(this.transactionChain).newReadOnlyTransaction();
- doReturn(this.write).when(this.transactionChain).newWriteOnlyTransaction();
- doReturn(immediateFalseFluentFuture())
- .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidGap);
- doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION,
+ final InstanceIdentifierContext iidContext =
+ new InstanceIdentifierContext(iidGap, schemaNodeForGap, null, schema);
+ final NormalizedNodePayload payload = NormalizedNodePayload.of(iidContext, leafGap);
+
+ 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(payload, new MdsalRestconfStrategy(mockDataBroker), schema);
+ verify(readWrite).merge(LogicalDatastoreType.CONFIGURATION,
payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData());
- doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
- PlainPatchDataTransactionUtil.patchData(payload,
- new TransactionVarsWrapper(payload.getInstanceIdentifierContext(), null, transactionChainHandler),
- this.schema);
-
- verify(this.readWrite).merge(LogicalDatastoreType.CONFIGURATION,
- payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData());
+ PlainPatchDataTransactionUtil.patchData(payload, new NetconfRestconfStrategy(netconfService),
+ schema);
+ verify(netconfService).lock();
+ verify(netconfService).merge(LogicalDatastoreType.CONFIGURATION,
+ payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData(), Optional.empty());
}
@Test
public void testPatchListData() {
- final InstanceIdentifierContext<DataSchemaNode> iidContext =
- new InstanceIdentifierContext<>(this.iidJukebox, this.schemaNodeForJukebox, null, this.schema);
- final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.jukeboxContainerWithPlaylist);
-
- doReturn(this.readWrite).when(this.transactionChain).newReadWriteTransaction();
- doReturn(this.read).when(this.transactionChain).newReadOnlyTransaction();
- doReturn(this.write).when(this.transactionChain).newWriteOnlyTransaction();
- doReturn(immediateFalseFluentFuture())
- .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidJukebox);
- doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION,
- payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData());
- doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
-
- PlainPatchDataTransactionUtil.patchData(payload,
- new TransactionVarsWrapper(payload.getInstanceIdentifierContext(), null, transactionChainHandler),
- this.schema);
-
- verify(this.readWrite).merge(LogicalDatastoreType.CONFIGURATION, this.iidJukebox, payload.getData());
+ final InstanceIdentifierContext iidContext =
+ new InstanceIdentifierContext(iidJukebox, schemaNodeForJukebox, null, schema);
+ final NormalizedNodePayload payload = NormalizedNodePayload.of(iidContext, jukeboxContainerWithPlaylist);
+
+ 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(payload, new MdsalRestconfStrategy(mockDataBroker), schema);
+ verify(readWrite).merge(LogicalDatastoreType.CONFIGURATION, iidJukebox, payload.getData());
+
+ PlainPatchDataTransactionUtil.patchData(payload, new NetconfRestconfStrategy(netconfService),
+ schema);
+ verify(netconfService).merge(LogicalDatastoreType.CONFIGURATION, iidJukebox, payload.getData(),
+ Optional.empty());
}
}