Bug 2594 - PUT method returns wrong status for create resource
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / JSONRestconfServiceImplTest.java
index 26a8632e72f1cb1296c7c33e0dc947305343b408..b472f1781f52e14dc082961b05d92975753d496a 100644 (file)
@@ -21,6 +21,7 @@ import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 import com.google.common.base.Optional;
 import com.google.common.io.Resources;
 import com.google.common.util.concurrent.Futures;
@@ -29,6 +30,7 @@ import java.io.IOException;
 import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Map;
+import javax.ws.rs.core.Response.Status;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
@@ -44,6 +46,7 @@ import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceImpl;
+import org.opendaylight.netconf.sal.restconf.impl.PutResult;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
 import org.opendaylight.yangtools.yang.common.OperationFailedException;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -119,9 +122,12 @@ public class JSONRestconfServiceImplTest {
     @SuppressWarnings("rawtypes")
     @Test
     public void testPut() throws Exception {
-        doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPut(
-                notNull(SchemaContext.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class));
-
+        final PutResult result = mock(PutResult.class);
+        when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
+                notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class))).thenReturn(result);
+        when(result.getFutureOfPutData())
+                .thenReturn(Futures.immediateCheckedFuture(null));
+        when(result.getStatus()).thenReturn(Status.OK);
         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
         final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
 
@@ -149,10 +155,11 @@ public class JSONRestconfServiceImplTest {
     @Test
     public void testPutBehindMountPoint() throws Exception {
         final DOMMountPoint mockMountPoint = setupTestMountPoint();
-
-        doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPut(
-                notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class));
-
+        final PutResult result = mock(PutResult.class);
+        when(brokerFacade.commitMountPointDataPut(notNull(DOMMountPoint.class),
+                notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class))).thenReturn(result);
+        when(result.getFutureOfPutData()).thenReturn(Futures.immediateCheckedFuture(null));
+        when(result.getStatus()).thenReturn(Status.OK);
         final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont/cont1";
         final String payload = loadData("/full-versions/testCont1Data.json");
 
@@ -160,7 +167,7 @@ public class JSONRestconfServiceImplTest {
 
         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
-        verify(brokerFacade).commitConfigurationDataPut(same(mockMountPoint), capturedPath.capture(),
+        verify(brokerFacade).commitMountPointDataPut(same(mockMountPoint), capturedPath.capture(),
                 capturedNode.capture());
 
         verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
@@ -172,21 +179,19 @@ public class JSONRestconfServiceImplTest {
         verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
     }
 
-    @Test(expected=TransactionCommitFailedException.class)
+    @Test(expected = OperationFailedException.class)
     public void testPutFailure() throws Throwable {
-        doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")))
-                .when(brokerFacade).commitConfigurationDataPut(notNull(SchemaContext.class),
-                        notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class));
+        final PutResult result = mock(PutResult.class);
+        when(result.getFutureOfPutData())
+                .thenReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")));
+        when(result.getStatus()).thenReturn(Status.OK);
+        when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
+                notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class))).thenReturn(result);
 
         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
         final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
 
-        try {
-            this.service.put(uriPath, payload);
-        } catch (final OperationFailedException e) {
-            assertNotNull(e.getCause());
-            throw e.getCause();
-        }
+        this.service.put(uriPath, payload);
     }
 
     @SuppressWarnings("rawtypes")
@@ -231,7 +236,6 @@ public class JSONRestconfServiceImplTest {
     @Test
     public void testPostBehindMountPoint() throws Exception {
         final DOMMountPoint mockMountPoint = setupTestMountPoint();
-
         doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
                 notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class));
 
@@ -254,27 +258,22 @@ public class JSONRestconfServiceImplTest {
         verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
     }
 
-    @Test(expected=TransactionCommitFailedException.class)
+    @Test
     public void testPostFailure() throws Throwable {
-        doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")))
-                .when(brokerFacade).commitConfigurationDataPost(any(SchemaContext.class),
-                        any(YangInstanceIdentifier.class), any(NormalizedNode.class));
+        doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock"))).when(brokerFacade)
+                .commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
+                        any(NormalizedNode.class));
 
         final String uriPath = null;
         final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
 
-        try {
-            this.service.post(uriPath, payload);
-        } catch (final OperationFailedException e) {
-            assertNotNull(e.getCause());
-            throw e.getCause();
-        }
+        this.service.post(uriPath, payload);
     }
 
     @Test
     public void testDelete() throws Exception {
-        doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataDelete(
-                notNull(YangInstanceIdentifier.class));
+        doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade)
+                .commitConfigurationDataDelete(notNull(YangInstanceIdentifier.class));
 
         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
 
@@ -305,14 +304,10 @@ public class JSONRestconfServiceImplTest {
     }
 
     @Test
-    public void testGetWithNoData() throws Exception {
+    public void testGetWithNoData() throws OperationFailedException {
         doReturn(null).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class));
-
         final String uriPath = "ietf-interfaces:interfaces";
-
-        final Optional<String> optionalResp = this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
-
-        assertEquals("Response present", false, optionalResp.isPresent());
+        this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
     }
 
     @Test(expected=OperationFailedException.class)