Add JSONRestconfService impl for the restconf Draft18 impl 17/59217/9
authorTom Pantelis <tompantelis@gmail.com>
Wed, 28 Jun 2017 17:15:40 +0000 (13:15 -0400)
committerTom Pantelis <tompantelis@gmail.com>
Wed, 5 Jul 2017 18:25:45 +0000 (18:25 +0000)
Added a JSONRestconfServiceDraft18 implementation class that uses the
restconf Draft18 impl and the blueprint wiring to instantiate and advertise
it. The original Draft02 JSONRestconfServiceImpl remains.

Change-Id: Ieab9c606ff40659bfe9d1a62ced7bf80891aa01a
Signed-off-by: Tom Pantelis <tompantelis@gmail.com>
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceDraft18.java [new file with mode: 0644]
restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceImpl.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/RestConnectorProvider.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/jersey/providers/JsonNormalizedNodeBodyReader.java
restconf/sal-rest-connector/src/main/java/org/opendaylight/restconf/restful/utils/FutureCallbackTx.java
restconf/sal-rest-connector/src/main/resources/org/opendaylight/blueprint/restconf-config.xml
restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/JSONRestconfServiceDraft18Test.java [new file with mode: 0644]
restconf/sal-rest-connector/src/test/java/org/opendaylight/restconf/restful/utils/PostDataTransactionUtilTest.java

diff --git a/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceDraft18.java b/restconf/sal-rest-connector/src/main/java/org/opendaylight/netconf/sal/restconf/impl/JSONRestconfServiceDraft18.java
new file mode 100644 (file)
index 0000000..b2940e6
--- /dev/null
@@ -0,0 +1,360 @@
+/*
+ * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.netconf.sal.restconf.impl;
+
+import com.google.common.base.Optional;
+import com.google.common.base.Preconditions;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.lang.annotation.Annotation;
+import java.net.URI;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.List;
+import javax.annotation.Nullable;
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.MultivaluedHashMap;
+import javax.ws.rs.core.MultivaluedMap;
+import javax.ws.rs.core.PathSegment;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriBuilder;
+import javax.ws.rs.core.UriInfo;
+import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
+import org.opendaylight.netconf.sal.restconf.api.JSONRestconfService;
+import org.opendaylight.netconf.sal.restconf.impl.RestconfError.ErrorTag;
+import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
+import org.opendaylight.restconf.jersey.providers.JsonNormalizedNodeBodyReader;
+import org.opendaylight.restconf.jersey.providers.NormalizedNodeJsonBodyWriter;
+import org.opendaylight.restconf.restful.services.api.TransactionServicesWrapper;
+import org.opendaylight.restconf.restful.utils.RestconfDataServiceConstant;
+import org.opendaylight.restconf.utils.parser.ParserIdentifier;
+import org.opendaylight.yangtools.yang.common.OperationFailedException;
+import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Implementation of the JSONRestconfService interface using the restconf Draft18 implementation.
+ *
+ * @author Thomas Pantelis
+ */
+public class JSONRestconfServiceDraft18 implements JSONRestconfService, AutoCloseable {
+    private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceDraft18.class);
+
+    private static final Annotation[] EMPTY_ANNOTATIONS = new Annotation[0];
+
+    private final TransactionServicesWrapper services;
+    private final DOMMountPointServiceHandler mountPointServiceHandler;
+
+    public JSONRestconfServiceDraft18(final TransactionServicesWrapper services,
+            final DOMMountPointServiceHandler mountPointServiceHandler) {
+        this.services = services;
+        this.mountPointServiceHandler = mountPointServiceHandler;
+    }
+
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    @Override
+    public void put(final String uriPath, final String payload) throws OperationFailedException {
+        Preconditions.checkNotNull(payload, "payload can't be null");
+
+        LOG.debug("put: uriPath: {}, payload: {}", uriPath, payload);
+
+        final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, false);
+
+        LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
+        LOG.debug("Parsed NormalizedNode: {}", context.getData());
+
+        try {
+            services.putData(uriPath, context, new SimpleUriInfo(uriPath));
+        } catch (final Exception e) {
+            propagateExceptionAs(uriPath, e, "PUT");
+        }
+    }
+
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    @Override
+    public void post(final String uriPath, final String payload)
+            throws OperationFailedException {
+        Preconditions.checkNotNull(payload, "payload can't be null");
+
+        LOG.debug("post: uriPath: {}, payload: {}", uriPath, payload);
+
+        final NormalizedNodeContext context = toNormalizedNodeContext(uriPath, payload, true);
+
+        LOG.debug("Parsed YangInstanceIdentifier: {}", context.getInstanceIdentifierContext().getInstanceIdentifier());
+        LOG.debug("Parsed NormalizedNode: {}", context.getData());
+
+        try {
+            services.postData(uriPath, context, new SimpleUriInfo(uriPath));
+        } catch (final Exception e) {
+            propagateExceptionAs(uriPath, e, "POST");
+        }
+    }
+
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    @Override
+    public void delete(final String uriPath) throws OperationFailedException {
+        LOG.debug("delete: uriPath: {}", uriPath);
+
+        try {
+            services.deleteData(uriPath);
+        } catch (final Exception e) {
+            propagateExceptionAs(uriPath, e, "DELETE");
+        }
+    }
+
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    @Override
+    public Optional<String> get(final String uriPath, final LogicalDatastoreType datastoreType)
+            throws OperationFailedException {
+        LOG.debug("get: uriPath: {}", uriPath);
+
+        try {
+            final MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();
+            queryParams.putSingle(RestconfDataServiceConstant.ReadData.CONTENT,
+                    datastoreType == LogicalDatastoreType.CONFIGURATION ? RestconfDataServiceConstant.ReadData.CONFIG :
+                        RestconfDataServiceConstant.ReadData.NONCONFIG);
+
+            final Response response = services.readData(uriPath, new SimpleUriInfo(uriPath, queryParams));
+            NormalizedNodeContext readData = (NormalizedNodeContext) response.getEntity();
+
+            final Optional<String> result = Optional.of(toJson(readData));
+
+            LOG.debug("get returning: {}", result.get());
+
+            return result;
+        } catch (final Exception e) {
+            if (!isDataMissing(e)) {
+                propagateExceptionAs(uriPath, e, "GET");
+            }
+
+            LOG.debug("Data missing - returning absent");
+            return Optional.absent();
+        }
+    }
+
+    @SuppressWarnings("checkstyle:IllegalCatch")
+    @Override
+    public Optional<String> invokeRpc(final String uriPath, final Optional<String> input)
+            throws OperationFailedException {
+        Preconditions.checkNotNull(uriPath, "uriPath can't be null");
+
+        final String actualInput = input.isPresent() ? input.get() : null;
+
+        LOG.debug("invokeRpc: uriPath: {}, input: {}", uriPath, actualInput);
+
+        String output = null;
+        try {
+            final NormalizedNodeContext inputContext = toNormalizedNodeContext(uriPath, actualInput, true);
+
+            LOG.debug("Parsed YangInstanceIdentifier: {}", inputContext.getInstanceIdentifierContext()
+                    .getInstanceIdentifier());
+            LOG.debug("Parsed NormalizedNode: {}", inputContext.getData());
+
+            NormalizedNodeContext outputContext = services.invokeRpc(uriPath, inputContext, new SimpleUriInfo(uriPath));
+
+            if (outputContext.getData() != null) {
+                output = toJson(outputContext);
+            }
+        } catch (final Exception e) {
+            propagateExceptionAs(uriPath, e, "RPC");
+        }
+
+        return Optional.fromNullable(output);
+    }
+
+    @Override
+    public void close() {
+    }
+
+    private NormalizedNodeContext toNormalizedNodeContext(final String uriPath, @Nullable final String payload,
+            final boolean isPost) throws OperationFailedException {
+        final InstanceIdentifierContext<?> instanceIdentifierContext = ParserIdentifier.toInstanceIdentifier(
+                uriPath, ControllerContext.getInstance().getGlobalSchema(),
+                Optional.of(mountPointServiceHandler.get()));
+
+        if (payload == null) {
+            return new NormalizedNodeContext(instanceIdentifierContext, null);
+        }
+
+        final InputStream entityStream = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
+        try {
+            return JsonNormalizedNodeBodyReader.readFrom(instanceIdentifierContext, entityStream, isPost);
+        } catch (IOException e) {
+            propagateExceptionAs(uriPath, e, "GET");
+            return null;
+        }
+    }
+
+    private static String toJson(final NormalizedNodeContext readData) throws IOException {
+        final NormalizedNodeJsonBodyWriter writer = new NormalizedNodeJsonBodyWriter();
+        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        writer.writeTo(readData, NormalizedNodeContext.class, null, EMPTY_ANNOTATIONS,
+                MediaType.APPLICATION_JSON_TYPE, null, outputStream);
+        return outputStream.toString(StandardCharsets.UTF_8.name());
+    }
+
+    private static boolean isDataMissing(final Exception exception) {
+        if (exception instanceof RestconfDocumentedException) {
+            final RestconfDocumentedException rde = (RestconfDocumentedException)exception;
+            return !rde.getErrors().isEmpty() && rde.getErrors().get(0).getErrorTag() == ErrorTag.DATA_MISSING;
+        }
+
+        return false;
+    }
+
+    private static void propagateExceptionAs(final String uriPath, final Exception exception, final String operation)
+            throws OperationFailedException {
+        LOG.debug("Error for uriPath: {}", uriPath, exception);
+
+        if (exception instanceof RestconfDocumentedException) {
+            throw new OperationFailedException(String.format(
+                    "%s failed for URI %s", operation, uriPath), exception.getCause(),
+                    toRpcErrors(((RestconfDocumentedException)exception).getErrors()));
+        }
+
+        throw new OperationFailedException(String.format("%s failed for URI %s", operation, uriPath), exception);
+    }
+
+    private static RpcError[] toRpcErrors(final List<RestconfError> from) {
+        final RpcError[] to = new RpcError[from.size()];
+        int index = 0;
+        for (final RestconfError e: from) {
+            to[index++] = RpcResultBuilder.newError(toRpcErrorType(e.getErrorType()), e.getErrorTag().getTagValue(),
+                    e.getErrorMessage());
+        }
+
+        return to;
+    }
+
+    private static ErrorType toRpcErrorType(final RestconfError.ErrorType errorType) {
+        switch (errorType) {
+            case TRANSPORT:
+                return ErrorType.TRANSPORT;
+            case RPC:
+                return ErrorType.RPC;
+            case PROTOCOL:
+                return ErrorType.PROTOCOL;
+            default:
+                return ErrorType.APPLICATION;
+        }
+    }
+
+    private static class SimpleUriInfo implements UriInfo {
+        private final String path;
+        private final MultivaluedMap<String, String> queryParams;
+
+        SimpleUriInfo(String path) {
+            this(path, new MultivaluedHashMap<>());
+        }
+
+        SimpleUriInfo(String path, MultivaluedMap<String, String> queryParams) {
+            this.path = path;
+            this.queryParams = queryParams;
+        }
+
+        @Override
+        public String getPath() {
+            return path;
+        }
+
+        @Override
+        public String getPath(boolean decode) {
+            return path;
+        }
+
+        @Override
+        public List<PathSegment> getPathSegments() {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public List<PathSegment> getPathSegments(boolean decode) {
+            throw new UnsupportedOperationException();
+        }
+
+        @Override
+        public URI getRequestUri() {
+            return URI.create(path);
+        }
+
+        @Override
+        public UriBuilder getRequestUriBuilder() {
+            return UriBuilder.fromUri(getRequestUri());
+        }
+
+        @Override
+        public URI getAbsolutePath() {
+            return getRequestUri();
+        }
+
+        @Override
+        public UriBuilder getAbsolutePathBuilder() {
+            return UriBuilder.fromUri(getAbsolutePath());
+        }
+
+        @Override
+        public URI getBaseUri() {
+            return URI.create("");
+        }
+
+        @Override
+        public UriBuilder getBaseUriBuilder() {
+            return UriBuilder.fromUri(getBaseUri());
+        }
+
+        @Override
+        public MultivaluedMap<String, String> getPathParameters() {
+            return new MultivaluedHashMap<>();
+        }
+
+        @Override
+        public MultivaluedMap<String, String> getPathParameters(boolean decode) {
+            return getPathParameters();
+        }
+
+        @Override
+        public MultivaluedMap<String, String> getQueryParameters() {
+            return queryParams;
+        }
+
+        @Override
+        public MultivaluedMap<String, String> getQueryParameters(boolean decode) {
+            return getQueryParameters();
+        }
+
+        @Override
+        public List<String> getMatchedURIs() {
+            return Collections.emptyList();
+        }
+
+        @Override
+        public List<String> getMatchedURIs(boolean decode) {
+            return getMatchedURIs();
+        }
+
+        @Override
+        public List<Object> getMatchedResources() {
+            return Collections.emptyList();
+        }
+
+        @Override
+        public URI resolve(URI uri) {
+            return uri;
+        }
+
+        @Override
+        public URI relativize(URI uri) {
+            return uri;
+        }
+    }
+}
index f6359a68b85c5014cf41aa0dde5bd6e789af366a..2f9d3e49cfd35a82b2fc3308e1f8895bc86206b8 100644 (file)
@@ -37,10 +37,12 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 /**
- * Implementation of the JSONRestconfService interface.
+ * Implementation of the JSONRestconfService interface using the restconf Draft02 implementation.
  *
  * @author Thomas Pantelis
+ * @deprecated Replaced by {@link JSONRestconfServiceDraft18}
  */
+@Deprecated
 public class JSONRestconfServiceImpl implements JSONRestconfService, AutoCloseable {
     private static final Logger LOG = LoggerFactory.getLogger(JSONRestconfServiceImpl.class);
 
index 9e1e8cd7c205b3a7adda6a5f32dbb450de9ee799..2aab6df3a243926742c4ca9499bfb30b6c7d878a 100644 (file)
@@ -99,6 +99,10 @@ public class RestConnectorProvider implements RestConnector, AutoCloseable {
                 notificationServiceHandler);
     }
 
+    public DOMMountPointServiceHandler getMountPointServiceHandler() {
+        return mountPointServiceHandler;
+    }
+
     /**
      * After {@link TransactionChain} failed, this updates {@link TransactionChainHandler} with new transaction chain.
      *
index a14cec0d0479c2d3e2340b2b6ec7848872c99be0..6896e5c3cbcb81897fdd6b5def829f8df350f6fc 100644 (file)
@@ -62,7 +62,7 @@ public class JsonNormalizedNodeBodyReader extends AbstractNormalizedNodeBodyRead
         }
     }
 
-    private static NormalizedNodeContext readFrom(
+    public static NormalizedNodeContext readFrom(
             final InstanceIdentifierContext<?> path, final InputStream entityStream, final boolean isPost)
             throws IOException {
         final NormalizedNodeResult resultHolder = new NormalizedNodeResult();
index 496d1c859b5174d80cf2e72964fb0d347661ac0d..8f7e6583128f6d3945717fb59ca5d23f96311d93 100644 (file)
@@ -8,12 +8,8 @@
 package org.opendaylight.restconf.restful.utils;
 
 import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.FutureCallback;
-import com.google.common.util.concurrent.Futures;
 import java.util.ArrayList;
 import java.util.List;
-import java.util.concurrent.CountDownLatch;
-import javax.annotation.Nullable;
 import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
@@ -43,68 +39,29 @@ final class FutureCallbackTx {
      *             type of operation (READ, POST, PUT, DELETE)
      * @param dataFactory
      *             factory setting result
+     * @throws RestconfDocumentedException
+     *             if the Future throws an exception
      */
     @SuppressWarnings("checkstyle:IllegalCatch")
     static <T, X extends Exception> void addCallback(final CheckedFuture<T, X> listenableFuture, final String txType,
-            final FutureDataFactory<T> dataFactory) {
-        final CountDownLatch responseWaiter = new CountDownLatch(1);
-        Futures.addCallback(listenableFuture, new FutureCallback<T>() {
+            final FutureDataFactory<T> dataFactory) throws RestconfDocumentedException {
 
-            @Override
-            public void onFailure(final Throwable throwable) {
-                responseWaiter.countDown();
-                handlingLoggerAndValues(throwable, txType, null, dataFactory);
-            }
-
-            @Override
-            public void onSuccess(final T result) {
-                handlingLoggerAndValues(null, txType, result, dataFactory);
-                responseWaiter.countDown();
-            }
-
-        });
         try {
-            responseWaiter.await();
-        } catch (final Exception e) {
-            final String msg = "Problem while waiting for response";
-            LOG.warn(msg);
-            throw new RestconfDocumentedException(msg, e);
-        }
-    }
-
-    /**
-     * Handling logger and result of callback - on success or on failure.
-     * <ul>
-     * <li>on success - set result to the factory
-     * <li>on failure - throw exception
-     * </ul>
-     *
-     * @param throwable
-     *             exception - if callback is onFailure
-     * @param txType
-     *             type of operation (READ, POST, PUT, DELETE)
-     * @param result
-     *             result of future - if callback is on Success
-     * @param dataFactory
-     *             setter for result - in callback is onSuccess
-     */
-    protected static <T> void handlingLoggerAndValues(@Nullable final Throwable throwable, final String txType,
-            final T result, final FutureDataFactory<T> dataFactory) {
-        if (throwable != null) {
+            final T result = listenableFuture.checkedGet();
+            dataFactory.setResult(result);
+            LOG.trace("Transaction({}) SUCCESSFUL", txType);
+        } catch (Exception e) {
             dataFactory.setFailureStatus();
-            LOG.warn("Transaction({}) FAILED!", txType, throwable);
-            if (throwable instanceof DOMRpcException) {
+            LOG.warn("Transaction({}) FAILED!", txType, e);
+            if (e instanceof DOMRpcException) {
                 final List<RpcError> rpcErrorList = new ArrayList<>();
                 rpcErrorList.add(
-                        RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", throwable.getMessage()));
+                        RpcResultBuilder.newError(RpcError.ErrorType.RPC, "operation-failed", e.getMessage()));
                 dataFactory.setResult((T) new DefaultDOMRpcResult(rpcErrorList));
             } else {
                 throw new RestconfDocumentedException(
-                        "Transaction(" + txType + ") not committed correctly", throwable);
+                        "Transaction(" + txType + ") not committed correctly", e);
             }
-        } else {
-            LOG.trace("Transaction({}) SUCCESSFUL!", txType);
-            dataFactory.setResult(result);
         }
     }
 }
index e09adb66f7a5e673b9d012e36d0958bcdae32d31..268dfe2274767a448bfe29fb8eda80f585192e8f 100644 (file)
            xmlns:ext="http://aries.apache.org/blueprint/xmlns/blueprint-ext/v1.4.0"
            odl:use-default-for-reference-types="true">
 
-  <!-- JSONRestconfService -->
-
-  <bean id="jsonRestconfService"
-      class="org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceImpl"
-      destroy-method="close"/>
-
-  <service ref="jsonRestconfService"
-      interface="org.opendaylight.netconf.sal.restconf.api.JSONRestconfService" />
-
   <!-- Restconf providers -->
 
   <cm:property-placeholder persistent-id="org.opendaylight.restconf" update-strategy="reload">
   <service ref="restconfProviderDraft18"
       interface="org.opendaylight.netconf.sal.rest.api.RestConnector" />
 
+  <!-- JSONRestconfService -->
+
+  <bean id="jsonRestconfServiceDraft02"
+      class="org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceImpl"
+      destroy-method="close"/>
+
+  <service ref="jsonRestconfServiceDraft02" odl:type="default"
+      interface="org.opendaylight.netconf.sal.restconf.api.JSONRestconfService" />
+
+  <bean id="jsonRestconfServiceDraft18" depends-on="restconfProviderDraft18"
+      class="org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceDraft18"
+      destroy-method="close">
+    <argument>
+      <bean class="org.opendaylight.restconf.common.wrapper.services.ServicesWrapperImpl"
+          factory-method="getInstance" />
+    </argument>
+    <argument>
+      <bean factory-ref="restconfProviderDraft18" factory-method="getMountPointServiceHandler" />
+    </argument>
+  </bean>
+
+  <service ref="jsonRestconfServiceDraft18" odl:type="draft18"
+      interface="org.opendaylight.netconf.sal.restconf.api.JSONRestconfService" />
+
 </blueprint>
diff --git a/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/JSONRestconfServiceDraft18Test.java b/restconf/sal-rest-connector/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/JSONRestconfServiceDraft18Test.java
new file mode 100644 (file)
index 0000000..04e23de
--- /dev/null
@@ -0,0 +1,539 @@
+/*
+ * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.controller.sal.restconf.impl.test;
+
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Matchers.isNull;
+import static org.mockito.Matchers.notNull;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+
+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.List;
+import java.util.Map;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+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.DOMDataBroker;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadOnlyTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
+import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
+import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
+import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcException;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcService;
+import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
+import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
+import org.opendaylight.netconf.sal.restconf.impl.JSONRestconfServiceDraft18;
+import org.opendaylight.restconf.common.wrapper.services.ServicesWrapperImpl;
+import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
+import org.opendaylight.restconf.handlers.DOMMountPointServiceHandler;
+import org.opendaylight.restconf.handlers.NotificationServiceHandler;
+import org.opendaylight.restconf.handlers.RpcServiceHandler;
+import org.opendaylight.restconf.handlers.SchemaContextHandler;
+import org.opendaylight.restconf.handlers.TransactionChainHandler;
+import org.opendaylight.yangtools.yang.common.OperationFailedException;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
+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.DataContainerNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
+import org.opendaylight.yangtools.yang.data.api.schema.MapNode;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
+import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
+
+/**
+ * Unit tests for JSONRestconfServiceDraft18.
+ *
+ * @author Thomas Pantelis
+ */
+public class JSONRestconfServiceDraft18Test {
+    static final String IETF_INTERFACES_NS = "urn:ietf:params:xml:ns:yang:ietf-interfaces";
+    static final String IETF_INTERFACES_VERSION = "2013-07-04";
+    static final QName INTERFACES_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interfaces");
+    static final QName INTERFACE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "interface");
+    static final QName NAME_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "name");
+    static final QName TYPE_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "type");
+    static final QName ENABLED_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "enabled");
+    static final QName DESC_QNAME = QName.create(IETF_INTERFACES_NS, IETF_INTERFACES_VERSION, "description");
+
+    static final String TEST_MODULE_NS = "test:module";
+    static final String TEST_MODULE_VERSION = "2014-01-09";
+    static final QName TEST_CONT_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont");
+    static final QName TEST_CONT1_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "cont1");
+    static final QName TEST_LF11_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf11");
+    static final QName TEST_LF12_QNAME = QName.create(TEST_MODULE_NS, TEST_MODULE_VERSION, "lf12");
+
+    static final String TOASTER_MODULE_NS = "http://netconfcentral.org/ns/toaster";
+    static final String TOASTER_MODULE_VERSION = "2009-11-20";
+    static final QName TOASTER_DONENESS_QNAME =
+            QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterDoneness");
+    static final QName TOASTER_TYPE_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "toasterToastType");
+    static final QName WHEAT_BREAD_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "wheat-bread");
+    static final QName MAKE_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "make-toast");
+    static final QName CANCEL_TOAST_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "cancel-toast");
+    static final QName TEST_OUTPUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "testOutput");
+    static final QName TEXT_OUT_QNAME = QName.create(TOASTER_MODULE_NS, TOASTER_MODULE_VERSION, "textOut");
+
+    private static SchemaContext schemaContext;
+
+    @Mock
+    private DOMTransactionChain mockTxChain;
+
+    @Mock
+    private DOMDataReadWriteTransaction mockReadWriteTx;
+
+    @Mock
+    private DOMDataReadOnlyTransaction mockReadOnlyTx;
+
+    @Mock
+    private DOMDataWriteTransaction mockWriteTx;
+
+    @Mock
+    private DOMMountPointService mockMountPointService;
+
+    @Mock
+    private SchemaContextHandler mockSchemaContextHandler;
+
+    @Mock
+    private DOMDataBroker mockDOMDataBroker;
+
+    @Mock
+    private DOMRpcService mockRpcService;
+
+    private JSONRestconfServiceDraft18 service;
+
+    @BeforeClass
+    public static void init() throws IOException, ReactorException {
+        schemaContext = TestUtils.loadSchemaContext("/full-versions/yangs");
+        ControllerContext.getInstance().setSchemas(schemaContext);
+    }
+
+    @Before
+    public void setup() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(mockReadOnlyTx).read(
+                eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
+
+        doNothing().when(mockWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
+                any(NormalizedNode.class));
+        doNothing().when(mockWriteTx).merge(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
+                any(NormalizedNode.class));
+        doNothing().when(mockWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
+        doReturn(Futures.immediateCheckedFuture(null)).when(mockWriteTx).submit();
+
+        doNothing().when(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class),
+                any(NormalizedNode.class));
+        doReturn(Futures.immediateCheckedFuture(null)).when(mockReadWriteTx).submit();
+        doReturn(Futures.immediateCheckedFuture(Optional.absent())).when(mockReadWriteTx).read(
+                eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
+        doReturn(Futures.immediateCheckedFuture(Boolean.FALSE)).when(mockReadWriteTx).exists(
+                eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
+
+        doReturn(mockReadOnlyTx).when(mockTxChain).newReadOnlyTransaction();
+        doReturn(mockReadWriteTx).when(mockTxChain).newReadWriteTransaction();
+        doReturn(mockWriteTx).when(mockTxChain).newWriteOnlyTransaction();
+
+        doReturn(mockTxChain).when(mockDOMDataBroker).createTransactionChain(any());
+
+        doReturn(schemaContext).when(mockSchemaContextHandler).get();
+
+        TransactionChainHandler txChainHandler = new TransactionChainHandler(mockTxChain);
+
+        final DOMMountPointServiceHandler mountPointServiceHandler =
+                new DOMMountPointServiceHandler(mockMountPointService);
+
+        ServicesWrapperImpl.getInstance().setHandlers(mockSchemaContextHandler, mountPointServiceHandler,
+                txChainHandler, new DOMDataBrokerHandler(mockDOMDataBroker),
+                new RpcServiceHandler(mockRpcService),
+                new NotificationServiceHandler(mock(DOMNotificationService.class)));
+
+        service = new JSONRestconfServiceDraft18(ServicesWrapperImpl.getInstance(), mountPointServiceHandler);
+    }
+
+    private static String loadData(final String path) throws IOException {
+        return Resources.asCharSource(JSONRestconfServiceDraft18Test.class.getResource(path),
+                StandardCharsets.UTF_8).read();
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Test
+    public void testPut() throws Exception {
+        final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
+        final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
+
+        this.service.put(uriPath, payload);
+
+        final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
+                ArgumentCaptor.forClass(YangInstanceIdentifier.class);
+        final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
+
+        verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
+                capturedNode.capture());
+
+        verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
+                new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
+
+        assertTrue("Expected MapEntryNode. Actual " + capturedNode.getValue().getClass(),
+                capturedNode.getValue() instanceof MapEntryNode);
+        final MapEntryNode actualNode = (MapEntryNode) capturedNode.getValue();
+        assertEquals("MapEntryNode node type", INTERFACE_QNAME, actualNode.getNodeType());
+        verifyLeafNode(actualNode, NAME_QNAME, "eth0");
+        verifyLeafNode(actualNode, TYPE_QNAME, "ethernetCsmacd");
+        verifyLeafNode(actualNode, ENABLED_QNAME, Boolean.FALSE);
+        verifyLeafNode(actualNode, DESC_QNAME, "some interface");
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Test
+    public void testPutBehindMountPoint() throws Exception {
+        setupTestMountPoint();
+
+        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 ArgumentCaptor<YangInstanceIdentifier> capturedPath =
+                ArgumentCaptor.forClass(YangInstanceIdentifier.class);
+        final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
+
+        verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
+                capturedNode.capture());
+
+        verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
+
+        assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
+        final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
+        assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
+        verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
+        verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
+    }
+
+    @Test(expected = OperationFailedException.class)
+    @SuppressWarnings("checkstyle:IllegalThrows")
+    public void testPutFailure() throws Throwable {
+        doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")))
+                .when(mockReadWriteTx).submit();
+
+        final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
+        final String payload = loadData("/parts/ietf-interfaces_interfaces.json");
+
+        this.service.put(uriPath, payload);
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Test
+    public void testPost() throws Exception {
+        final String uriPath = null;
+        final String payload = loadData("/parts/ietf-interfaces_interfaces_absolute_path.json");
+
+        this.service.post(uriPath, payload);
+
+        final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
+                ArgumentCaptor.forClass(YangInstanceIdentifier.class);
+        final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
+
+        verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
+                capturedNode.capture());
+
+        verifyPath(capturedPath.getValue(), INTERFACES_QNAME);
+
+        assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
+        final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
+        assertEquals("ContainerNode node type", INTERFACES_QNAME, actualNode.getNodeType());
+
+        final Optional<DataContainerChild<?, ?>> mapChild = actualNode.getChild(new NodeIdentifier(INTERFACE_QNAME));
+        assertEquals(INTERFACE_QNAME.toString() + " present", true, mapChild.isPresent());
+        assertTrue("Expected MapNode. Actual " + mapChild.get().getClass(), mapChild.get() instanceof MapNode);
+        final MapNode mapNode = (MapNode)mapChild.get();
+
+        final NodeIdentifierWithPredicates entryNodeID = new NodeIdentifierWithPredicates(
+                INTERFACE_QNAME, NAME_QNAME, "eth0");
+        final Optional<MapEntryNode> entryChild = mapNode.getChild(entryNodeID);
+        assertEquals(entryNodeID.toString() + " present", true, entryChild.isPresent());
+        final MapEntryNode entryNode = entryChild.get();
+        verifyLeafNode(entryNode, NAME_QNAME, "eth0");
+        verifyLeafNode(entryNode, TYPE_QNAME, "ethernetCsmacd");
+        verifyLeafNode(entryNode, ENABLED_QNAME, Boolean.FALSE);
+        verifyLeafNode(entryNode, DESC_QNAME, "some interface");
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Test
+    public void testPostBehindMountPoint() throws Exception {
+        setupTestMountPoint();
+
+        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 ArgumentCaptor<YangInstanceIdentifier> capturedPath =
+                ArgumentCaptor.forClass(YangInstanceIdentifier.class);
+        final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
+
+        verify(mockReadWriteTx).put(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture(),
+                capturedNode.capture());
+
+        verifyPath(capturedPath.getValue(), TEST_CONT_QNAME, TEST_CONT1_QNAME);
+
+        assertTrue("Expected ContainerNode", capturedNode.getValue() instanceof ContainerNode);
+        final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
+        assertEquals("ContainerNode node type", TEST_CONT1_QNAME, actualNode.getNodeType());
+        verifyLeafNode(actualNode, TEST_LF11_QNAME, "lf11 data");
+        verifyLeafNode(actualNode, TEST_LF12_QNAME, "lf12 data");
+    }
+
+    @Test(expected = TransactionCommitFailedException.class)
+    @SuppressWarnings("checkstyle:IllegalThrows")
+    public void testPostFailure() throws Throwable {
+        doReturn(Futures.immediateFailedCheckedFuture(new TransactionCommitFailedException("mock")))
+                .when(mockReadWriteTx).submit();
+
+        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();
+        }
+    }
+
+    @Test
+    public void testDelete() throws Exception {
+        doReturn(Futures.immediateCheckedFuture(Boolean.TRUE)).when(mockReadWriteTx).exists(
+                eq(LogicalDatastoreType.CONFIGURATION), any(YangInstanceIdentifier.class));
+
+        final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
+
+        this.service.delete(uriPath);
+
+        final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
+                ArgumentCaptor.forClass(YangInstanceIdentifier.class);
+
+        verify(mockReadWriteTx).delete(eq(LogicalDatastoreType.CONFIGURATION), capturedPath.capture());
+
+        verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
+                new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
+    }
+
+    @Test(expected = OperationFailedException.class)
+    public void testDeleteFailure() throws Exception {
+        final String invalidUriPath = "ietf-interfaces:interfaces/invalid";
+
+        this.service.delete(invalidUriPath);
+    }
+
+    @Test
+    public void testGetConfig() throws Exception {
+        testGet(LogicalDatastoreType.CONFIGURATION);
+    }
+
+    @Test
+    public void testGetOperational() throws Exception {
+        testGet(LogicalDatastoreType.OPERATIONAL);
+    }
+
+    @Test
+    public void testGetWithNoData() throws OperationFailedException {
+        final String uriPath = "ietf-interfaces:interfaces";
+        this.service.get(uriPath, LogicalDatastoreType.CONFIGURATION);
+    }
+
+    @Test(expected = OperationFailedException.class)
+    public void testGetFailure() throws Exception {
+        final String invalidUriPath = "/ietf-interfaces:interfaces/invalid";
+        this.service.get(invalidUriPath, LogicalDatastoreType.CONFIGURATION);
+    }
+
+    @SuppressWarnings("rawtypes")
+    @Test
+    public void testInvokeRpcWithInput() throws Exception {
+        final SchemaPath path = SchemaPath.create(true, MAKE_TOAST_QNAME);
+
+        final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
+        doReturn(Futures.immediateCheckedFuture(expResult)).when(mockRpcService).invokeRpc(eq(path),
+                any(NormalizedNode.class));
+
+        final String uriPath = "toaster:make-toast";
+        final String input = loadData("/full-versions/make-toast-rpc-input.json");
+
+        final Optional<String> output = this.service.invokeRpc(uriPath, Optional.of(input));
+
+        assertEquals("Output present", false, output.isPresent());
+
+        final ArgumentCaptor<NormalizedNode> capturedNode = ArgumentCaptor.forClass(NormalizedNode.class);
+        verify(mockRpcService).invokeRpc(eq(path), capturedNode.capture());
+
+        assertTrue("Expected ContainerNode. Actual " + capturedNode.getValue().getClass(),
+                capturedNode.getValue() instanceof ContainerNode);
+        final ContainerNode actualNode = (ContainerNode) capturedNode.getValue();
+        verifyLeafNode(actualNode, TOASTER_DONENESS_QNAME, Long.valueOf(10));
+        verifyLeafNode(actualNode, TOASTER_TYPE_QNAME, WHEAT_BREAD_QNAME);
+    }
+
+    @Test
+    public void testInvokeRpcWithNoInput() throws Exception {
+        final SchemaPath path = SchemaPath.create(true, CANCEL_TOAST_QNAME);
+
+        final DOMRpcResult expResult = new DefaultDOMRpcResult((NormalizedNode<?, ?>)null);
+        doReturn(Futures.immediateCheckedFuture(expResult)).when(mockRpcService).invokeRpc(eq(path),
+                any(NormalizedNode.class));
+
+        final String uriPath = "toaster:cancel-toast";
+
+        final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
+
+        assertEquals("Output present", false, output.isPresent());
+
+        verify(mockRpcService).invokeRpc(eq(path), isNull(NormalizedNode.class));
+    }
+
+    @Test
+    public void testInvokeRpcWithOutput() throws Exception {
+        final SchemaPath path = SchemaPath.create(true, TEST_OUTPUT_QNAME);
+
+        final NormalizedNode<?, ?> outputNode = ImmutableContainerNodeBuilder.create()
+                .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(TEST_OUTPUT_QNAME))
+                .withChild(ImmutableNodes.leafNode(TEXT_OUT_QNAME, "foo")).build();
+        final DOMRpcResult expResult = new DefaultDOMRpcResult(outputNode);
+        doReturn(Futures.immediateCheckedFuture(expResult)).when(mockRpcService).invokeRpc(eq(path),
+                any(NormalizedNode.class));
+
+        final String uriPath = "toaster:testOutput";
+
+        final Optional<String> output = this.service.invokeRpc(uriPath, Optional.<String>absent());
+
+        assertEquals("Output present", true, output.isPresent());
+        assertNotNull("Returned null response", output.get());
+        assertThat("Missing \"textOut\"", output.get(), containsString("\"textOut\":\"foo\""));
+
+        verify(mockRpcService).invokeRpc(eq(path), isNull(NormalizedNode.class));
+    }
+
+    @Test(expected = OperationFailedException.class)
+    public void testInvokeRpcFailure() throws Exception {
+        final DOMRpcException exception = new DOMRpcImplementationNotAvailableException("testExeption");
+        doReturn(Futures.immediateFailedCheckedFuture(exception)).when(mockRpcService).invokeRpc(any(SchemaPath.class),
+                any(NormalizedNode.class));
+
+        final String uriPath = "toaster:cancel-toast";
+
+        this.service.invokeRpc(uriPath, Optional.<String>absent());
+    }
+
+    void testGet(final LogicalDatastoreType datastoreType) throws OperationFailedException {
+        final MapEntryNode entryNode = ImmutableNodes.mapEntryBuilder(INTERFACE_QNAME, NAME_QNAME, "eth0")
+                .withChild(ImmutableNodes.leafNode(NAME_QNAME, "eth0"))
+                .withChild(ImmutableNodes.leafNode(TYPE_QNAME, "ethernetCsmacd"))
+                .withChild(ImmutableNodes.leafNode(ENABLED_QNAME, Boolean.TRUE))
+                .withChild(ImmutableNodes.leafNode(DESC_QNAME, "eth interface"))
+                .build();
+
+        doReturn(Futures.immediateCheckedFuture(Optional.of(entryNode))).when(mockReadOnlyTx).read(
+                eq(datastoreType), any(YangInstanceIdentifier.class));
+
+        final String uriPath = "ietf-interfaces:interfaces/interface=eth0";
+
+        final Optional<String> optionalResp = this.service.get(uriPath, datastoreType);
+        assertEquals("Response present", true, optionalResp.isPresent());
+        final String jsonResp = optionalResp.get();
+
+        assertNotNull("Returned null response", jsonResp);
+        assertThat("Missing \"name\"", jsonResp, containsString("\"name\":\"eth0\""));
+        assertThat("Missing \"type\"", jsonResp, containsString("\"type\":\"ethernetCsmacd\""));
+        assertThat("Missing \"enabled\"", jsonResp, containsString("\"enabled\":true"));
+        assertThat("Missing \"description\"", jsonResp, containsString("\"description\":\"eth interface\""));
+
+        final ArgumentCaptor<YangInstanceIdentifier> capturedPath =
+                ArgumentCaptor.forClass(YangInstanceIdentifier.class);
+        verify(mockReadOnlyTx).read(eq(datastoreType), capturedPath.capture());
+
+        verifyPath(capturedPath.getValue(), INTERFACES_QNAME, INTERFACE_QNAME,
+                new Object[]{INTERFACE_QNAME, NAME_QNAME, "eth0"});
+    }
+
+    DOMMountPoint setupTestMountPoint() throws FileNotFoundException, ReactorException {
+        final SchemaContext schemaContextTestModule = TestUtils.loadSchemaContext("/full-versions/test-module");
+        final DOMMountPoint mockMountPoint = mock(DOMMountPoint.class);
+        doReturn(schemaContextTestModule).when(mockMountPoint).getSchemaContext();
+
+        doReturn(Optional.of(mockDOMDataBroker)).when(mockMountPoint).getService(DOMDataBroker.class);
+
+        doReturn(Optional.of(mockMountPoint))
+                .when(mockMountPointService).getMountPoint(notNull(YangInstanceIdentifier.class));
+
+        return mockMountPoint;
+    }
+
+    void verifyLeafNode(final DataContainerNode<?> parent, final QName leafType, final Object leafValue) {
+        final Optional<DataContainerChild<?, ?>> leafChild = parent.getChild(new NodeIdentifier(leafType));
+        assertEquals(leafType.toString() + " present", true, leafChild.isPresent());
+        assertEquals(leafType.toString() + " value", leafValue, leafChild.get().getValue());
+    }
+
+    void verifyPath(final YangInstanceIdentifier path, final Object... expArgs) {
+        final List<PathArgument> pathArgs = path.getPathArguments();
+        assertEquals("Arg count for actual path " + path, expArgs.length, pathArgs.size());
+        int index = 0;
+        for (final PathArgument actual: pathArgs) {
+            QName expNodeType;
+            if (expArgs[index] instanceof Object[]) {
+                final Object[] listEntry = (Object[]) expArgs[index];
+                expNodeType = (QName) listEntry[0];
+
+                assertTrue(actual instanceof NodeIdentifierWithPredicates);
+                final Map<QName, Object> keyValues = ((NodeIdentifierWithPredicates)actual).getKeyValues();
+                assertEquals(String.format("Path arg %d keyValues size", index + 1), 1, keyValues.size());
+                final QName expKey = (QName) listEntry[1];
+                assertEquals(String.format("Path arg %d keyValue for %s", index + 1, expKey), listEntry[2],
+                        keyValues.get(expKey));
+            } else {
+                expNodeType = (QName) expArgs[index];
+            }
+
+            assertEquals(String.format("Path arg %d node type", index + 1), expNodeType, actual.getNodeType());
+            index++;
+        }
+
+    }
+}
index e796e03e1f7315597005349aaf1bbc3c5071072b..33d395ee49919ec75ec409eff114a6648043ab7b 100644 (file)
@@ -9,6 +9,8 @@
 package org.opendaylight.restconf.restful.utils;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 import static org.mockito.Mockito.doNothing;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.verify;
@@ -29,6 +31,7 @@ import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 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.restconf.common.references.SchemaContextRef;
 import org.opendaylight.restconf.restful.transaction.TransactionVarsWrapper;
 import org.opendaylight.yangtools.util.SingletonSet;
@@ -187,13 +190,19 @@ public class PostDataTransactionUtilTest {
                 .when(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, node);
         doNothing().when(this.readWrite).put(LogicalDatastoreType.CONFIGURATION, node,
                 payload.getData());
-        doReturn(Futures.immediateFailedCheckedFuture(new DOMException((short) 414, "Post request failed")))
-                .when(this.readWrite).submit();
+        final DOMException domException = new DOMException((short) 414, "Post request failed");
+        doReturn(Futures.immediateFailedCheckedFuture(domException)).when(this.readWrite).submit();
         final TransactionVarsWrapper wrapper =
                 new TransactionVarsWrapper(payload.getInstanceIdentifierContext(), null, this.transactionChain);
-        final Response response =
-                PostDataTransactionUtil.postData(this.uriInfo, payload, wrapper, this.refSchemaCtx, null, null);
-        assertEquals(Response.Status.INTERNAL_SERVER_ERROR, response.getStatusInfo());
+
+        try {
+            PostDataTransactionUtil.postData(this.uriInfo, payload, wrapper, this.refSchemaCtx, null, null);
+            fail("Expected RestconfDocumentedException");
+        } catch (RestconfDocumentedException e) {
+            assertEquals(1, e.getErrors().size());
+            assertTrue(e.getErrors().get(0).getErrorInfo().contains(domException.getMessage()));
+        }
+
         verify(this.readWrite).exists(LogicalDatastoreType.CONFIGURATION, this.iid2);
         verify(this.readWrite).put(LogicalDatastoreType.CONFIGURATION,
                 payload.getInstanceIdentifierContext().getInstanceIdentifier(), payload.getData());