import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
+import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.verify;
import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture;
import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
-import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
-import java.io.UnsupportedEncodingException;
+import com.google.common.util.concurrent.Futures;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
-import org.mockito.Mockito;
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.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.common.errors.RestconfDocumentedException;
import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
-import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
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;
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public class PostDataTransactionUtilTest {
-
private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
- @Mock
- private DOMTransactionChain transactionChain;
@Mock
private DOMDataTreeReadWriteTransaction readWrite;
@Mock
- private DOMDataTreeReadTransaction read;
- @Mock
private UriInfo uriInfo;
@Mock
private DOMDataBroker mockDataBroker;
@Mock
private NetconfDataTreeService netconfService;
- private TransactionChainHandler transactionChainHandler;
private ContainerNode buildBaseCont;
private EffectiveModelContext schema;
private YangInstanceIdentifier iid2;
.build();
doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/16/")).when(this.uriInfo).getBaseUriBuilder();
- doReturn(this.readWrite).when(this.transactionChain).newReadWriteTransaction();
+ doReturn(this.readWrite).when(this.mockDataBroker).newReadWriteTransaction();
- Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
- transactionChainHandler = new TransactionChainHandler(mockDataBroker);
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).lock();
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).unlock();
}
@Test
doReturn(immediateFalseFluentFuture()).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION,
this.iid2);
- doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(this.iid2);
final NodeIdentifier identifier =
- ((ContainerNode) ((Collection<?>) payload.getData().getValue()).iterator().next()).getIdentifier();
+ ((ContainerNode) ((Collection<?>) payload.getData().body()).iterator().next()).getIdentifier();
final YangInstanceIdentifier node =
payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node.getParent(), payload.getData());
doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
- doReturn(CommitInfo.emptyFluentFuture()).when(this.netconfService).commit(Mockito.any());
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).commit();
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService)
+ .create(LogicalDatastoreType.CONFIGURATION, payload.getInstanceIdentifierContext().getInstanceIdentifier(),
+ payload.getData(), Optional.empty());
Response response = PostDataTransactionUtil.postData(this.uriInfo, payload,
- new MdsalRestconfStrategy(transactionChainHandler), this.schema, null, null);
+ new MdsalRestconfStrategy(mockDataBroker), this.schema, null, null);
assertEquals(201, response.getStatus());
verify(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iid2);
verify(this.readWrite).put(LogicalDatastoreType.CONFIGURATION,
response = PostDataTransactionUtil.postData(this.uriInfo, payload,
new NetconfRestconfStrategy(netconfService), this.schema, null, null);
assertEquals(201, response.getStatus());
- verify(this.netconfService).getConfig(this.iid2);
verify(this.netconfService).create(LogicalDatastoreType.CONFIGURATION,
payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData(), Optional.empty());
}
@Test
- public void testPostListData() throws UnsupportedEncodingException {
+ public void testPostListData() {
final InstanceIdentifierContext<? extends SchemaNode> iidContext =
new InstanceIdentifierContext<>(this.iidList, null, null, this.schema);
final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildList);
final MapNode data = (MapNode) payload.getData();
- final MapEntryNode entryNode = data.getValue().iterator().next();
+ final MapEntryNode entryNode = data.body().iterator().next();
final NodeIdentifierWithPredicates identifier = entryNode.getIdentifier();
final YangInstanceIdentifier node =
payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
doReturn(immediateFalseFluentFuture()).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
- doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(node);
doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
- doReturn(CommitInfo.emptyFluentFuture()).when(this.netconfService).commit(Mockito.any());
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService)
+ .merge(any(), any(), any(), any());
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).commit();
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).create(
+ LogicalDatastoreType.CONFIGURATION, node, entryNode, Optional.empty());
Response response = PostDataTransactionUtil.postData(this.uriInfo, payload,
- new MdsalRestconfStrategy(transactionChainHandler), this.schema, null, null);
+ new MdsalRestconfStrategy(mockDataBroker), this.schema, null, null);
assertEquals(201, response.getStatus());
assertThat(URLDecoder.decode(response.getLocation().toString(), StandardCharsets.UTF_8),
containsString(identifier.getValue(identifier.keySet().iterator().next()).toString()));
assertEquals(201, response.getStatus());
assertThat(URLDecoder.decode(response.getLocation().toString(), StandardCharsets.UTF_8),
containsString(identifier.getValue(identifier.keySet().iterator().next()).toString()));
- verify(this.netconfService).getConfig(node);
verify(this.netconfService).create(LogicalDatastoreType.CONFIGURATION, node, entryNode,
Optional.empty());
}
doReturn(immediateFalseFluentFuture()).when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION,
this.iid2);
- doReturn(immediateFluentFuture(Optional.empty())).when(this.netconfService).getConfig(this.iid2);
final NodeIdentifier identifier =
- ((ContainerNode) ((Collection<?>) payload.getData().getValue()).iterator().next()).getIdentifier();
+ ((ContainerNode) ((Collection<?>) payload.getData().body()).iterator().next()).getIdentifier();
final YangInstanceIdentifier node =
payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node.getParent(), payload.getData());
final DOMException domException = new DOMException((short) 414, "Post request failed");
doReturn(immediateFailedFluentFuture(domException)).when(this.readWrite).commit();
- doReturn(immediateFailedFluentFuture(domException)).when(this.netconfService).commit(Mockito.any());
+ doReturn(immediateFailedFluentFuture(domException)).when(this.netconfService)
+ .create(any(), any(), any(), any());
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).discardChanges();
+ doReturn(Futures.immediateFuture(new DefaultDOMRpcResult())).when(this.netconfService).unlock();
try {
PostDataTransactionUtil.postData(this.uriInfo, payload,
- new MdsalRestconfStrategy(transactionChainHandler), this.schema, null, null);
+ new MdsalRestconfStrategy(mockDataBroker), this.schema, null, null);
fail("Expected RestconfDocumentedException");
} catch (final RestconfDocumentedException e) {
assertEquals(1, e.getErrors().size());
assertTrue(e.getErrors().get(0).getErrorInfo().contains(domException.getMessage()));
}
- verify(this.netconfService).getConfig(this.iid2);
verify(this.netconfService).create(LogicalDatastoreType.CONFIGURATION,
payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData(), Optional.empty());
}