Split transaction lifecycle
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfDataServiceImplTest.java
index c0dc6fada2e6680ee67b6079d52df3da203452ab..d68b9905330c4ec4d63837ba1b2c556dbe53d0f0 100644 (file)
@@ -13,7 +13,6 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doCallRealMethod;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.opendaylight.restconf.common.patch.PatchEditOperation.CREATE;
@@ -37,9 +36,10 @@ import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
 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;
@@ -51,6 +51,7 @@ import org.opendaylight.mdsal.dom.api.DOMMountPointService;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChainListener;
+import org.opendaylight.mdsal.dom.spi.FixedDOMSchemaService;
 import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
@@ -67,6 +68,7 @@ import org.opendaylight.restconf.nb.rfc8040.rests.services.api.RestconfStreamsSu
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.MdsalRestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.NetconfRestconfStrategy;
 import org.opendaylight.restconf.nb.rfc8040.rests.transactions.RestconfStrategy;
+import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
@@ -87,6 +89,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaContext;
 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
+@RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class RestconfDataServiceImplTest {
 
     private static final String PATH_FOR_NEW_SCHEMA_CONTEXT = "/jukebox";
@@ -130,11 +133,11 @@ public class RestconfDataServiceImplTest {
     private DOMTransactionChain mountTransactionChain;
     @Mock
     private RestconfStreamsSubscriptionService delegRestconfSubscrService;
+    @Mock
+    private Configuration configuration;
 
     @Before
     public void setUp() throws Exception {
-        MockitoAnnotations.initMocks(this);
-
         final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
         Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
         Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(value);
@@ -189,7 +192,8 @@ public class RestconfDataServiceImplTest {
 
         this.contextRef =
                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles(PATH_FOR_NEW_SCHEMA_CONTEXT));
-        this.schemaNode = DataSchemaContextTree.from(this.contextRef).getChild(this.iidBase).getDataSchemaNode();
+        this.schemaNode = DataSchemaContextTree.from(this.contextRef).findChild(this.iidBase).orElseThrow(
+            ).getDataSchemaNode();
 
         doReturn(CommitInfo.emptyFluentFuture()).when(this.write).commit();
         doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
@@ -209,12 +213,13 @@ public class RestconfDataServiceImplTest {
         schemaContextHandler.onModelContextUpdated(this.contextRef);
         this.dataService = new RestconfDataServiceImpl(schemaContextHandler, this.transactionChainHandler,
                 new DOMMountPointServiceHandler(mountPointService), this.delegRestconfSubscrService,
-                this.actionServiceHandler);
+                this.actionServiceHandler, configuration);
         doReturn(Optional.of(this.mountPoint)).when(this.mountPointService)
                 .getMountPoint(any(YangInstanceIdentifier.class));
-        doCallRealMethod().when(this.mountPoint).getSchemaContext();
-        doReturn(this.contextRef).when(this.mountPoint).getEffectiveModelContext();
+        doReturn(Optional.of(FixedDOMSchemaService.of(this.contextRef))).when(this.mountPoint)
+                .getService(DOMSchemaService.class);
         doReturn(Optional.of(this.mountDataBroker)).when(this.mountPoint).getService(DOMDataBroker.class);
+        doReturn(Optional.empty()).when(this.mountPoint).getService(NetconfDataTreeService.class);
         doReturn(this.mountTransactionChain).when(this.mountDataBroker)
                 .createTransactionChain(any(DOMTransactionChainListener.class));
         doReturn(this.read).when(this.mountTransactionChain).newReadOnlyTransaction();
@@ -311,8 +316,6 @@ public class RestconfDataServiceImplTest {
         doReturn(parameters).when(this.uriInfo).getQueryParameters();
         doReturn(immediateFluentFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
                 .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
-        doReturn(immediateFluentFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
-                .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
 
         final Response response = this.dataService.readData("example-jukebox:jukebox", this.uriInfo);
 
@@ -339,8 +342,6 @@ public class RestconfDataServiceImplTest {
         parameters.put("content", Collections.singletonList("nonconfig"));
 
         doReturn(parameters).when(this.uriInfo).getQueryParameters();
-        doReturn(immediateFluentFuture(Optional.of(this.buildBaseContConfig))).when(this.read)
-                .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
         doReturn(immediateFluentFuture(Optional.of(this.buildBaseContOperational))).when(this.read)
                 .read(LogicalDatastoreType.OPERATIONAL, this.iidBase);
 
@@ -366,7 +367,7 @@ public class RestconfDataServiceImplTest {
                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef);
         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
 
-        doReturn(immediateTrueFluentFuture()).when(this.readWrite)
+        doReturn(immediateTrueFluentFuture()).when(this.read)
                 .exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
         doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
         final Response response = this.dataService.putData(null, payload, this.uriInfo);
@@ -384,7 +385,7 @@ public class RestconfDataServiceImplTest {
                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, mountPoint, this.contextRef);
         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, this.buildBaseCont);
 
-        doReturn(immediateTrueFluentFuture()).when(this.readWrite)
+        doReturn(immediateTrueFluentFuture()).when(this.read)
                 .exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
         doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, payload.getData());
         final Response response = this.dataService.putData(null, payload, this.uriInfo);
@@ -420,16 +421,14 @@ public class RestconfDataServiceImplTest {
         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
                 new InstanceIdentifierContext<>(this.iidBase, null, null, this.contextRef);
         final NormalizedNodeContext payload = new NormalizedNodeContext(iidContext, buildList);
-        doReturn(immediateFluentFuture(Optional.empty()))
-                .when(this.read).read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
         final MapNode data = (MapNode) payload.getData();
-        final NodeIdentifierWithPredicates identifier =
-                data.getValue().iterator().next().getIdentifier();
+        final MapEntryNode entryNode = data.getValue().iterator().next();
+        final NodeIdentifierWithPredicates identifier = entryNode.getIdentifier();
         final YangInstanceIdentifier node =
                 payload.getInstanceIdentifierContext().getInstanceIdentifier().node(identifier);
         doReturn(immediateFalseFluentFuture())
-                .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
-        doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, payload.getData());
+                .when(this.read).exists(LogicalDatastoreType.CONFIGURATION, node);
+        doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node, entryNode);
         doReturn(UriBuilder.fromUri("http://localhost:8181/restconf/15/")).when(this.uriInfo).getBaseUriBuilder();
 
         final Response response = this.dataService.postData(null, payload, this.uriInfo);
@@ -461,7 +460,7 @@ public class RestconfDataServiceImplTest {
     }
 
     @Test
-    public void testPatchData() throws Exception {
+    public void testPatchData() {
         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef);
         final List<PatchEntity> entity = new ArrayList<>();
@@ -474,9 +473,6 @@ public class RestconfDataServiceImplTest {
         entity.add(new PatchEntity("delete data", DELETE, iidleaf));
         final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
 
-        doReturn(immediateFluentFuture(Optional.of(this.buildBaseCont))).when(this.read)
-                .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
-        doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
         doReturn(immediateFalseFluentFuture())
                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
@@ -502,9 +498,6 @@ public class RestconfDataServiceImplTest {
         entity.add(new PatchEntity("delete data", DELETE, iidleaf));
         final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
 
-        doReturn(immediateFluentFuture(Optional.of(this.buildBaseCont))).when(this.read)
-                .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
-        doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
         doReturn(immediateFalseFluentFuture())
                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
@@ -517,7 +510,7 @@ public class RestconfDataServiceImplTest {
     }
 
     @Test
-    public void testPatchDataDeleteNotExist() throws Exception {
+    public void testPatchDataDeleteNotExist() {
         final InstanceIdentifierContext<? extends SchemaNode> iidContext =
                 new InstanceIdentifierContext<>(this.iidBase, this.schemaNode, null, this.contextRef);
         final List<PatchEntity> entity = new ArrayList<>();
@@ -530,9 +523,6 @@ public class RestconfDataServiceImplTest {
         entity.add(new PatchEntity("delete data", DELETE, iidleaf));
         final PatchContext patch = new PatchContext(iidContext, entity, "test patch id");
 
-        doReturn(immediateFluentFuture(Optional.of(this.buildBaseCont))).when(this.read)
-                .read(LogicalDatastoreType.CONFIGURATION, this.iidBase);
-        doNothing().when(this.write).put(LogicalDatastoreType.CONFIGURATION, this.iidBase, this.buildBaseCont);
         doNothing().when(this.readWrite).delete(LogicalDatastoreType.CONFIGURATION, iidleaf);
         doReturn(immediateFalseFluentFuture())
                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iidBase);
@@ -556,11 +546,11 @@ public class RestconfDataServiceImplTest {
         final InstanceIdentifierContext<? extends SchemaNode> iidContext = new InstanceIdentifierContext<>(
                 this.iidBase, this.schemaNode, this.mountPoint, this.contextRef);
 
-        RestconfStrategy restconfStrategy = this.dataService.getRestconfStrategy(iidContext, this.mountPoint);
+        RestconfStrategy restconfStrategy = this.dataService.getRestconfStrategy(this.mountPoint);
         assertTrue(restconfStrategy instanceof MdsalRestconfStrategy);
 
         doReturn(Optional.of(this.netconfService)).when(this.mountPoint).getService(NetconfDataTreeService.class);
-        restconfStrategy = this.dataService.getRestconfStrategy(iidContext, this.mountPoint);
+        restconfStrategy = this.dataService.getRestconfStrategy(this.mountPoint);
         assertTrue(restconfStrategy instanceof NetconfRestconfStrategy);
     }
 }