* 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;
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;
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;
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;
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;
}
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();
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
@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;
}
};
@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;
}
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
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
@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;
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();
}
}
- 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");
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()));
}
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);