Fix error reporting for PUT/POST
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / JSONRestconfServiceImplTest.java
index b472f1781f52e14dc082961b05d92975753d496a..fa788c61a01afd4708311277f5770f3dff0d1a26 100644 (file)
@@ -22,19 +22,25 @@ 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;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.nio.charset.StandardCharsets;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response.Status;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
 import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
+import org.mockito.Mockito;
 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
@@ -71,6 +77,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
  *
  * @author Thomas Pantelis
  */
+@Deprecated
 public class JSONRestconfServiceImplTest {
     static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
     static final String IETF_INTERFACES_VERSION = "2013-07-04";
@@ -124,19 +131,23 @@ public class JSONRestconfServiceImplTest {
     public void testPut() throws Exception {
         final PutResult result = mock(PutResult.class);
         when(brokerFacade.commitConfigurationDataPut(notNull(SchemaContext.class),
-                notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class))).thenReturn(result);
+                notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
+                Mockito.anyString())).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");
-
-        this.service.put(uriPath, payload);
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
+        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
+        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
+        this.service.put(uriPath, payload, uriInfo);
 
         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
         verify(brokerFacade).commitConfigurationDataPut(notNull(SchemaContext.class), capturedPath.capture(),
-                capturedNode.capture());
+                capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
 
         verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
                 new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
@@ -157,18 +168,23 @@ public class JSONRestconfServiceImplTest {
         final DOMMountPoint mockMountPoint = setupTestMountPoint();
         final PutResult result = mock(PutResult.class);
         when(brokerFacade.commitMountPointDataPut(notNull(DOMMountPoint.class),
-                notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class))).thenReturn(result);
+                notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
+                Mockito.anyString())).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");
 
-        this.service.put(uriPath, payload);
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
+        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
+        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
+        this.service.put(uriPath, payload, uriInfo);
 
         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
         verify(brokerFacade).commitMountPointDataPut(same(mockMountPoint), capturedPath.capture(),
-                capturedNode.capture());
+                capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
 
         verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
 
@@ -182,33 +198,46 @@ public class JSONRestconfServiceImplTest {
     @Test(expected = OperationFailedException.class)
     public void testPutFailure() throws Throwable {
         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);
+                notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class), Mockito.anyString(),
+                Mockito.anyString())).thenReturn(result);
 
         final String uriPath = "ietf-interfaces:interfaces/interface/eth0";
         final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
 
-        this.service.put(uriPath, payload);
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
+        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
+        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
+        this.service.put(uriPath, payload, uriInfo);
     }
 
     @SuppressWarnings("rawtypes")
     @Test
     public void testPost() throws Exception {
         doReturn(Futures.immediateCheckedFuture(null)).when(brokerFacade).commitConfigurationDataPost(
-                any(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class));
+                any(SchemaContext.class), any(YangInstanceIdentifier.class), any(NormalizedNode.class),
+                Mockito.anyString(), Mockito.anyString());
 
         final String uriPath = null;
         final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
 
-        this.service.post(uriPath, payload);
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
+        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
+        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
+        final UriBuilder uriBuilder = UriBuilder.fromPath("");
+        Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
+        this.service.post(uriPath, payload, uriInfo);
 
         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
         verify(brokerFacade).commitConfigurationDataPost(notNull(SchemaContext.class), capturedPath.capture(),
-                capturedNode.capture());
+                capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
 
         verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
 
@@ -237,17 +266,24 @@ public class JSONRestconfServiceImplTest {
     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));
+                notNull(DOMMountPoint.class), notNull(YangInstanceIdentifier.class), notNull(NormalizedNode.class),
+                Mockito.anyString(), Mockito.anyString());
 
         final String uriPath = "ietf-interfaces:interfaces/yang-ext:mount/test-module:cont";
         final String payload = loadData("/full-versions/testCont1Data.json");
 
-        this.service.post(uriPath, payload);
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
+        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
+        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
+        final UriBuilder uriBuilder = UriBuilder.fromPath("");
+        Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
+        this.service.post(uriPath, payload, uriInfo);
 
         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
         final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
         verify(brokerFacade).commitConfigurationDataPost(same(mockMountPoint), capturedPath.capture(),
-                capturedNode.capture());
+                capturedNode.capture(), Mockito.anyString(), Mockito.anyString());
 
         verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
 
@@ -258,16 +294,28 @@ public class JSONRestconfServiceImplTest {
         verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
     }
 
-    @Test
+    @Test(expected = TransactionCommitFailedException.class)
     public void testPostFailure() throws Throwable {
         doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock"))).when(brokerFacade)
                 .commitConfigurationDataPost(any(SchemaContext.class), any(YangInstanceIdentifier.class),
-                        any(NormalizedNode.class));
+                        any(NormalizedNode.class), Mockito.anyString(), Mockito.anyString());
 
         final String uriPath = null;
         final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
 
-        this.service.post(uriPath, payload);
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
+        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
+        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
+        final UriBuilder uriBuilder = UriBuilder.fromPath("");
+        Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(uriBuilder);
+
+        try {
+            this.service.post(uriPath, payload, uriInfo);
+        } catch (final OperationFailedException e) {
+            assertNotNull(e.getCause());
+            throw e.getCause();
+        }
     }
 
     @Test
@@ -305,16 +353,24 @@ public class JSONRestconfServiceImplTest {
 
     @Test
     public void testGetWithNoData() throws OperationFailedException {
-        doReturn(null).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class));
+        doReturn(null).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
+                Mockito.anyString());
         final String uriPath = "ietf-interfaces:interfaces";
-        this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
+        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
+        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
+        this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
     }
 
     @Test(expected=OperationFailedException.class)
     public void testGetFailure() throws Exception {
         final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
-
-        this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION);
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
+        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
+        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
+        this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION, uriInfo);
     }
 
     @SuppressWarnings("rawtypes")
@@ -402,14 +458,21 @@ public class JSONRestconfServiceImplTest {
                 .build();
 
         if(datastoreType == LogicalDatastoreType.CONFIGURATION) {
-            doReturn(entryNode).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class));
+            doReturn(entryNode).when(brokerFacade).readConfigurationData(notNull(YangInstanceIdentifier.class),
+                    Mockito.anyString());
         } else {
             doReturn(entryNode).when(brokerFacade).readOperationalData(notNull(YangInstanceIdentifier.class));
         }
 
         final String uriPath = "/ietf-interfaces:interfaces/interface/eth0";
-
-        final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
+        final UriInfo uriInfo = Mockito.mock(UriInfo.class);
+        final MultivaluedMap<String, String> value = Mockito.mock(MultivaluedMap.class);
+        Mockito.when(value.entrySet()).thenReturn(new HashSet<>());
+        Mockito.when(uriInfo.getQueryParameters()).thenReturn(value);
+        Mockito.when(uriInfo.getQueryParameters(false)).thenReturn(value);
+        Mockito.when(value.getFirst("depth")).thenReturn("");
+
+        final Optional<String> optionalResp = this.service.get(uriPath, datastoreType, uriInfo);
         assertEquals("Response present", true, optionalResp.isPresent());
         final String jsonResp = optionalResp.get();
 
@@ -421,7 +484,7 @@ public class JSONRestconfServiceImplTest {
 
         final ArgumentCaptor<YangInstanceIdentifier> capturedPath = ArgumentCaptor.forClass(YangInstanceIdentifier.class);
         if (datastoreType == LogicalDatastoreType.CONFIGURATION) {
-            verify(brokerFacade).readConfigurationData(capturedPath.capture());
+            verify(brokerFacade).readConfigurationData(capturedPath.capture(), Mockito.anyString());
         } else {
             verify(brokerFacade).readOperationalData(capturedPath.capture());
         }