Remove RestconfError.ErrorType
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfDocumentedExceptionMapperTest.java
index 8143eedbebe50cf004d32da3fcfc97c675be5d29..fffd64e0608ced9b3a5bb1d88df2c8a56b55dfcb 100644 (file)
@@ -5,7 +5,6 @@
  * 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.junit.Assert.assertEquals;
@@ -14,13 +13,12 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-import static org.mockito.Matchers.any;
+import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.reset;
 import static org.mockito.Mockito.when;
 
-import com.google.common.collect.Maps;
-import com.google.common.io.ByteStreams;
+import com.google.common.collect.Iterators;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonElement;
 import com.google.gson.JsonParser;
@@ -30,6 +28,7 @@ import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.util.Arrays;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
@@ -53,6 +52,7 @@ import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
+import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
 import org.opendaylight.netconf.sal.rest.api.Draft02;
 import org.opendaylight.netconf.sal.rest.api.RestconfService;
 import org.opendaylight.netconf.sal.rest.impl.JsonNormalizedNodeBodyReader;
@@ -61,12 +61,13 @@ import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeXmlBodyWriter;
 import org.opendaylight.netconf.sal.rest.impl.RestconfDocumentedExceptionMapper;
 import org.opendaylight.netconf.sal.rest.impl.XmlNormalizedNodeBodyReader;
 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
-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.restconf.common.context.NormalizedNodeContext;
+import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
+import org.opendaylight.restconf.common.errors.RestconfError;
+import org.opendaylight.restconf.common.errors.RestconfError.ErrorTag;
 import org.opendaylight.yangtools.util.xml.UntrustedXML;
+import org.opendaylight.yangtools.yang.common.ErrorType;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.w3c.dom.Document;
@@ -112,6 +113,7 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
     }
 
     private static final Logger LOG = LoggerFactory.getLogger(RestconfDocumentedExceptionMapperTest.class);
+    private static final String IETF_RESTCONF = "ietf-restconf";
     static RestconfService mockRestConf = mock(RestconfService.class);
 
     static XPath XPATH = XPathFactory.newInstance().newXPath();
@@ -122,14 +124,16 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
     static XPathExpression ERROR_APP_TAG;
     static XPathExpression ERROR_INFO;
 
+    private static EffectiveModelContext schemaContext;
+
     @BeforeClass
     public static void init() throws Exception {
-        ControllerContext.getInstance().setGlobalSchema(TestUtils.loadSchemaContext("/modules"));
+        schemaContext = TestUtils.loadSchemaContext("/modules");
 
         final NamespaceContext nsContext = new NamespaceContext() {
             @Override
-            public Iterator<?> getPrefixes(final String namespaceURI) {
-                return null;
+            public Iterator<String> getPrefixes(final String namespaceURI) {
+                return Iterators.singletonIterator(IETF_RESTCONF);
             }
 
             @Override
@@ -139,7 +143,7 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
 
             @Override
             public String getNamespaceURI(final String prefix) {
-                return "ietf-restconf".equals(prefix) ? Draft02.RestConfModule.NAMESPACE : null;
+                return IETF_RESTCONF.equals(prefix) ? Draft02.RestConfModule.NAMESPACE : null;
             }
         };
 
@@ -162,9 +166,11 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
     @Override
     protected Application configure() {
         ResourceConfig resourceConfig = new ResourceConfig();
-        resourceConfig = resourceConfig.registerInstances(mockRestConf, new XmlNormalizedNodeBodyReader(),
-            new JsonNormalizedNodeBodyReader(), new NormalizedNodeJsonBodyWriter(), new NormalizedNodeXmlBodyWriter());
-        resourceConfig.registerClasses(RestconfDocumentedExceptionMapper.class);
+        ControllerContext controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
+        resourceConfig = resourceConfig.registerInstances(mockRestConf,
+                new XmlNormalizedNodeBodyReader(controllerContext), new JsonNormalizedNodeBodyReader(controllerContext),
+                new NormalizedNodeJsonBodyWriter(), new NormalizedNodeXmlBodyWriter(),
+                new RestconfDocumentedExceptionMapper(controllerContext));
         return resourceConfig;
     }
 
@@ -326,7 +332,7 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
     public void testToJsonResponseWithDataMissingErrorTag() throws Exception {
 
         testJsonResponse(new RestconfDocumentedException("mock error", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING),
-                Status.NOT_FOUND, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, "mock error", null, null);
+                Status.CONFLICT, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, "mock error", null, null);
     }
 
     @Test
@@ -549,7 +555,7 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
     public void testToXMLResponseWithDataMissingErrorTag() throws Exception {
 
         testXMLResponse(new RestconfDocumentedException("mock error", ErrorType.PROTOCOL, ErrorTag.DATA_MISSING),
-                Status.NOT_FOUND, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, "mock error", null, null);
+                Status.CONFLICT, ErrorType.PROTOCOL, ErrorTag.DATA_MISSING, "mock error", null, null);
     }
 
     @Test
@@ -675,9 +681,9 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
     @SuppressWarnings("checkstyle:IllegalCatch")
     private static JsonArray parseJsonErrorArrayElement(final InputStream stream) throws IOException {
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        ByteStreams.copy(stream, bos);
+        stream.transferTo(bos);
 
-        LOG.info("JSON: " + bos.toString());
+        LOG.info("JSON: {}", bos);
 
         final JsonParser parser = new JsonParser();
         JsonElement rootElement;
@@ -723,7 +729,7 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
             final ErrorInfoVerifier errorInfoVerifier) {
 
         JsonElement errorInfoElement = null;
-        final Map<String, String> leafMap = Maps.newHashMap();
+        final Map<String, String> leafMap = new HashMap<>();
         for (final Entry<String, JsonElement> entry : errorEntryElement.getAsJsonObject().entrySet()) {
             final String leafName = entry.getKey();
             final JsonElement leafElement = entry.getValue();
@@ -739,7 +745,7 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
             }
         }
 
-        assertEquals("error-type", expErrorType.getErrorTypeTag(), leafMap.remove("error-type"));
+        assertEquals("error-type", expErrorType.elementBody(), leafMap.remove("error-type"));
         assertEquals("error-tag", expErrorTag.getTagValue(), leafMap.remove("error-tag"));
 
         verifyOptionalJsonLeaf(leafMap.remove("error-message"), expErrorMessage, "error-message");
@@ -777,9 +783,9 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
 
     private static Document parseXMLDocument(final InputStream stream) throws IOException, SAXException {
         final ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        ByteStreams.copy(stream, bos);
+        stream.transferTo(bos);
 
-        LOG.debug("XML: " + bos.toString());
+        LOG.debug("XML: {}", bos);
 
         return UntrustedXML.newDocumentBuilder().parse(new ByteArrayInputStream(bos.toByteArray()));
     }
@@ -789,7 +795,7 @@ public class RestconfDocumentedExceptionMapperTest extends JerseyTest {
             throws Exception {
 
         final String errorType = (String) ERROR_TYPE.evaluate(errorNode, XPathConstants.STRING);
-        assertEquals("error-type", expErrorType.getErrorTypeTag(), errorType);
+        assertEquals("error-type", expErrorType.elementBody(), errorType);
 
         final String errorTag = (String) ERROR_TAG.evaluate(errorNode, XPathConstants.STRING);
         assertEquals("error-tag", expErrorTag.getTagValue(), errorTag);