Bug 8977 - Failed on binary key type
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfImplTest.java
index 7c4b350c6cf622a3259dfe43b0d5c8d051991591..c08b3349989469d993b880f86fc458d0e98ebef6 100644 (file)
@@ -24,9 +24,15 @@ import com.google.common.util.concurrent.Futures;
 import java.io.FileNotFoundException;
 import java.net.URI;
 import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
 import java.util.Set;
 import javax.ws.rs.core.MultivaluedHashMap;
-import javax.ws.rs.core.Response;
+import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
 import org.junit.Before;
@@ -40,14 +46,20 @@ import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
 import org.opendaylight.netconf.sal.restconf.impl.InstanceIdentifierContext;
 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfError;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorType;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.model.api.Module;
 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
@@ -57,8 +69,7 @@ import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
 
 /**
- * @See {@link InvokeRpcMethodTest}
- *
+ * See {@link InvokeRpcMethodTest}.
  */
 public class RestconfImplTest {
 
@@ -82,12 +93,49 @@ public class RestconfImplTest {
         this.restconfImpl.setControllerContext(controllerContext);
     }
 
+    @Test
+    public void binaryKeyTest() {
+        final List<Byte> al = new ArrayList<>();
+        al.add(new Byte((byte) 1));
+        binaryKeyTest(al, al);
+    }
+
+    private void binaryKeyTest(final List<Byte> al, final List<Byte> al2) {
+
+        final QName keyDef = QName.create("test:key:binary", "2017-14-08", "b1");
+
+        final Map<QName, Object> uriKeyValues = new HashMap<>();
+        uriKeyValues.put(keyDef, al.toArray());
+
+        final MapEntryNode payload = mock(MapEntryNode.class);
+        final NodeIdentifierWithPredicates nodeIdWithPred =
+                new NodeIdentifierWithPredicates(keyDef, keyDef, al2.toArray());
+        when(payload.getIdentifier()).thenReturn(nodeIdWithPred);
+
+        final List<QName> keyDefinitions = new ArrayList<>();
+        keyDefinitions.add(keyDef);
+        RestconfImpl.isEqualUriAndPayloadKeyValues(uriKeyValues, payload, keyDefinitions);
+    }
+
+    @Test
+    public void binaryKeyFailTest() {
+        final List<Byte> al = new ArrayList<>();
+        al.add(new Byte((byte) 1));
+        final List<Byte> al2 = new ArrayList<>();
+        try {
+            binaryKeyTest(al, al2);
+        } catch (final RestconfDocumentedException e) {
+            final RestconfError err = e.getErrors().iterator().next();
+            assertEquals(ErrorType.PROTOCOL, err.getErrorType());
+            assertEquals(ErrorTag.INVALID_VALUE, err.getErrorTag());
+        }
+    }
+
     @SuppressWarnings("unchecked")
     @Test
     public void testExample() throws FileNotFoundException, ParseException {
         @SuppressWarnings("rawtypes")
-        final
-        NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
+        final NormalizedNode normalizedNodeData = TestUtils.prepareNormalizedNodeWithIetfInterfacesInterfacesData();
         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
         when(brokerFacade.readOperationalData(any(YangInstanceIdentifier.class))).thenReturn(normalizedNodeData);
         assertEquals(normalizedNodeData,
@@ -111,14 +159,15 @@ public class RestconfImplTest {
         doReturn(mount).when(iiCtx).getMountPoint();
         final DOMRpcService rpcService = mock(DOMRpcService.class);
         doReturn(Optional.of(rpcService)).when(mount).getService(DOMRpcService.class);
-        doReturn(Futures.immediateCheckedFuture(mock(DOMRpcResult.class))).when(rpcService).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
+        doReturn(Futures.immediateCheckedFuture(mock(DOMRpcResult.class))).when(rpcService)
+                .invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
         this.restconfImpl.invokeRpc("randomId", ctx, uriInfo);
         this.restconfImpl.invokeRpc("ietf-netconf", ctx, uriInfo);
         verify(rpcService, times(2)).invokeRpc(any(SchemaPath.class), any(NormalizedNode.class));
     }
 
     /**
-     * Create notification stream for toaster module
+     * Create notification stream for toaster module.
      */
     @Test
     public void createNotificationStreamTest() {
@@ -156,7 +205,7 @@ public class RestconfImplTest {
     }
 
     /**
-     * Subscribe for notification stream of toaster module
+     * Subscribe for notification stream of toaster module.
      */
     @Test
     public void subscribeToNotificationStreamTest() throws Exception {
@@ -174,13 +223,16 @@ public class RestconfImplTest {
         when(uriBuilder.build()).thenReturn(new URI(""));
         when(uriBuilder.scheme("ws")).thenReturn(uriBuilder);
         when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
+        final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
+        final Set<Entry<String, List<String>>> set = new HashSet<>();
+        when(map.entrySet()).thenReturn(set);
+        when(uriInfo.getQueryParameters()).thenReturn(map);
 
         final BrokerFacade brokerFacade = mock(BrokerFacade.class);
         this.restconfImpl.setBroker(brokerFacade);
 
         // subscribe to stream and verify response
-        final Response response = this.restconfImpl.subscribeToStream(identifier, uriInfo);
-        assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
+        final NormalizedNodeContext response = this.restconfImpl.subscribeToStream(identifier, uriInfo);
 
         // remove test notification stream
         Notificator.removeAllListeners();