Create NetconfDataTreeService with base and additional operations for netconf
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / utils / DeleteDataTransactionUtilTest.java
index b0fce39c4853728dc5d666f7a6c514814aef3d57..bc60219e4d09fe01003f3a20e9ad288e5cda9088 100644 (file)
@@ -9,9 +9,12 @@ package org.opendaylight.restconf.nb.rfc8040.rests.utils;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
+import static org.mockito.Mockito.mock;
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFalseFluentFuture;
+import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFluentFuture;
 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
 
+import java.util.Optional;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status;
 import org.junit.Before;
@@ -24,13 +27,17 @@ import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
+import org.opendaylight.netconf.dom.api.NetconfDataTreeService;
 import org.opendaylight.restconf.common.context.InstanceIdentifierContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.restconf.common.errors.RestconfError.ErrorType;
 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
-import org.opendaylight.restconf.nb.rfc8040.rests.transactions.TransactionVarsWrapper;
+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.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 
 public class DeleteDataTransactionUtilTest {
     @Mock
@@ -41,14 +48,17 @@ public class DeleteDataTransactionUtilTest {
     private DOMDataTreeReadWriteTransaction readWrite;
     @Mock
     private DOMDataBroker mockDataBroker;
+    @Mock
+    private NetconfDataTreeService netconfService;
 
     private TransactionChainHandler transactionChainHandler;
 
     @Before
-    public void init() throws Exception {
+    public void init() {
         MockitoAnnotations.initMocks(this);
         Mockito.when(this.transactionChain.newReadWriteTransaction()).thenReturn(this.readWrite);
         Mockito.doReturn(CommitInfo.emptyFluentFuture()).when(this.readWrite).commit();
+        Mockito.doReturn(CommitInfo.emptyFluentFuture()).when(this.netconfService).commit(Mockito.any());
         Mockito.when(this.context.getInstanceIdentifier()).thenReturn(YangInstanceIdentifier.empty());
 
         Mockito.doReturn(transactionChain).when(mockDataBroker).createTransactionChain(Mockito.any());
@@ -59,32 +69,41 @@ public class DeleteDataTransactionUtilTest {
      * Test of successful DELETE operation.
      */
     @Test
-    public void deleteData() throws Exception {
+    public void deleteData() {
         // assert that data to delete exists
         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
                 YangInstanceIdentifier.empty())).thenReturn(immediateTrueFluentFuture());
-
+        Mockito.when(this.netconfService.getConfig(YangInstanceIdentifier.empty()))
+                .thenReturn(immediateFluentFuture(Optional.of(mock(NormalizedNode.class))));
         // test
-        final Response response = DeleteDataTransactionUtil.deleteData(
-                new TransactionVarsWrapper(this.context, null, transactionChainHandler));
-
-        // assert success
-        assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
+        delete(new MdsalRestconfStrategy(this.context, transactionChainHandler));
+        delete(new NetconfRestconfStrategy(netconfService, this.context));
     }
 
     /**
      * Negative test for DELETE operation when data to delete does not exist. Error DATA_MISSING is expected.
      */
     @Test
-    public void deleteDataNegativeTest() throws Exception {
+    public void deleteDataNegativeTest() {
         // assert that data to delete does NOT exist
         Mockito.when(this.transactionChain.newReadWriteTransaction().exists(LogicalDatastoreType.CONFIGURATION,
                 YangInstanceIdentifier.empty())).thenReturn(immediateFalseFluentFuture());
-
+        Mockito.when(this.netconfService.getConfig(YangInstanceIdentifier.empty()))
+                .thenReturn(immediateFluentFuture(Optional.empty()));
         // test and assert error
+        deleteFail(new MdsalRestconfStrategy(this.context, transactionChainHandler));
+        deleteFail(new NetconfRestconfStrategy(netconfService, this.context));
+    }
+
+    private void delete(final RestconfStrategy strategy) {
+        final Response response = DeleteDataTransactionUtil.deleteData(strategy);
+        // assert success
+        assertEquals("Not expected response received", Status.NO_CONTENT.getStatusCode(), response.getStatus());
+    }
+
+    private void deleteFail(final RestconfStrategy strategy) {
         try {
-            DeleteDataTransactionUtil.deleteData(new TransactionVarsWrapper(this.context, null,
-                    transactionChainHandler));
+            DeleteDataTransactionUtil.deleteData(strategy);
             fail("Delete operation should fail due to missing data");
         } catch (final RestconfDocumentedException e) {
             assertEquals(ErrorType.PROTOCOL, e.getErrors().get(0).getErrorType());