Deprecated Rpcs and RpcErrors helper classes
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfErrorTest.java
index 70ad7683a4cb6b5bd1a965c2d924de97b6ffd4af..18311104a41f9761dd5bede15965df9625ea061b 100644 (file)
@@ -1,10 +1,10 @@
 /*
-* Copyright (c) 2014 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
-*/
+ * Copyright (c) 2014 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.equalTo;
@@ -14,20 +14,15 @@ import static org.junit.Assert.assertThat;
 
 import java.util.HashMap;
 import java.util.Map;
-
-import javax.ws.rs.core.Response.Status;
-
 import org.hamcrest.BaseMatcher;
 import org.hamcrest.Description;
 import org.hamcrest.Matcher;
 import org.junit.Test;
-
-import static org.opendaylight.controller.sal.common.util.RpcErrors.getRpcError;
-
 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
 import org.opendaylight.yangtools.yang.common.RpcError;
+import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 
 /**
  * Unit tests for RestconfError.
@@ -42,195 +37,166 @@ public class RestconfErrorTest {
 
         private final String text;
 
-        public Contains( String text ) {
+        public Contains(String text) {
             this.text = text;
         }
 
         @Override
-        public void describeTo( Description desc ) {
-            desc.appendText( "contains " ).appendValue( text );
+        public void describeTo(Description desc) {
+            desc.appendText("contains ").appendValue(text);
         }
 
         @Override
-        public boolean matches( Object arg ) {
-            return arg != null && arg.toString().contains( text );
+        public boolean matches(Object arg) {
+            return arg != null && arg.toString().contains(text);
         }
     }
 
     @Test
-    public void testErrorTagValueOf()
-    {
-        assertEquals( ErrorTag.IN_USE,
-                ErrorTag.valueOfCaseInsensitive( ErrorTag.IN_USE.getTagValue() ) );
+    public void testErrorTagValueOf() {
+        assertEquals(ErrorTag.IN_USE, ErrorTag.valueOfCaseInsensitive(ErrorTag.IN_USE.getTagValue()));
     }
 
     @Test
-    public void testErrorTagValueOfIsLowercase()
-    {
-        assertEquals( "in-use",
-                ErrorTag.IN_USE.getTagValue() );
+    public void testErrorTagValueOfIsLowercase() {
+        assertEquals("in-use", ErrorTag.IN_USE.getTagValue());
     }
 
     @Test
-    public void testErrorTypeGetErrorTypeTagIsLowerCase()
-    {
-       assertEquals( ErrorType.APPLICATION.name().toLowerCase(),
-               ErrorType.APPLICATION.getErrorTypeTag() );
+    public void testErrorTypeGetErrorTypeTagIsLowerCase() {
+        assertEquals(ErrorType.APPLICATION.name().toLowerCase(), ErrorType.APPLICATION.getErrorTypeTag());
     }
 
     @Test
-    public void testErrorTypeValueOf()
-    {
-       assertEquals( ErrorType.APPLICATION,
-                     ErrorType.valueOfCaseInsensitive( ErrorType.APPLICATION.getErrorTypeTag() ) );
+    public void testErrorTypeValueOf() {
+        assertEquals(ErrorType.APPLICATION, ErrorType.valueOfCaseInsensitive(ErrorType.APPLICATION.getErrorTypeTag()));
     }
 
     @Test
-    public void testErrorTagStatusCodes()
-    {
-        Map<String,Status> lookUpMap = new HashMap<String,Status>();
-
-        lookUpMap.put( "in-use", Status.fromStatusCode(409));
-        lookUpMap.put( "invalid-value", Status.fromStatusCode(400));
-        lookUpMap.put( "too-big", Status.fromStatusCode(413));
-        lookUpMap.put( "missing-attribute", Status.fromStatusCode(400));
-        lookUpMap.put( "bad-attribute", Status.fromStatusCode(400));
-        lookUpMap.put( "unknown-attribute", Status.fromStatusCode(400));
-        lookUpMap.put( "bad-element", Status.fromStatusCode(400));
-        lookUpMap.put( "unknown-element", Status.fromStatusCode(400));
-        lookUpMap.put( "unknown-namespace", Status.fromStatusCode(400));
-        lookUpMap.put( "access-denied", Status.fromStatusCode(403));
-        lookUpMap.put( "lock-denied", Status.fromStatusCode(409));
-        lookUpMap.put( "resource-denied", Status.fromStatusCode(409));
-        lookUpMap.put( "rollback-failed", Status.fromStatusCode(500));
-        lookUpMap.put( "data-exists", Status.fromStatusCode(409));
-        lookUpMap.put( "data-missing", Status.fromStatusCode(409));
-        lookUpMap.put( "operation-not-supported", Status.fromStatusCode(501));
-        lookUpMap.put( "operation-failed", Status.fromStatusCode(500));
-        lookUpMap.put( "partial-operation", Status.fromStatusCode(500));
-        lookUpMap.put( "malformed-message", Status.fromStatusCode(400));
-
-        for( ErrorTag tag : ErrorTag.values() )
-        {
-            Status expectedStatusCode = lookUpMap.get( tag.getTagValue() );
-            assertNotNull( "Failed to find " + tag.getTagValue(), expectedStatusCode );
-            assertEquals( "Status Code does not match", expectedStatusCode, tag.getStatusCode() );
+    public void testErrorTagStatusCodes() {
+        Map<String, Integer> lookUpMap = new HashMap<String, Integer>();
+
+        lookUpMap.put("in-use", 409);
+        lookUpMap.put("invalid-value", 400);
+        lookUpMap.put("too-big", 413);
+        lookUpMap.put("missing-attribute", 400);
+        lookUpMap.put("bad-attribute", 400);
+        lookUpMap.put("unknown-attribute", 400);
+        lookUpMap.put("bad-element", 400);
+        lookUpMap.put("unknown-element", 400);
+        lookUpMap.put("unknown-namespace", 400);
+        lookUpMap.put("access-denied", 403);
+        lookUpMap.put("lock-denied", 409);
+        lookUpMap.put("resource-denied", 409);
+        lookUpMap.put("rollback-failed", 500);
+        lookUpMap.put("data-exists", 409);
+        lookUpMap.put("data-missing", 409);
+        lookUpMap.put("operation-not-supported", 501);
+        lookUpMap.put("operation-failed", 500);
+        lookUpMap.put("partial-operation", 500);
+        lookUpMap.put("malformed-message", 400);
+
+        for (ErrorTag tag : ErrorTag.values()) {
+            Integer expectedStatusCode = lookUpMap.get(tag.getTagValue());
+            assertNotNull("Failed to find " + tag.getTagValue(), expectedStatusCode);
+            assertEquals("Status Code does not match", expectedStatusCode, Integer.valueOf(tag.getStatusCode()));
         }
     }
 
     @Test
-    public void testRestConfDocumentedException_NoCause()
-    {
+    public void testRestConfDocumentedException_NoCause() {
         String expectedMessage = "Message";
         ErrorType expectedErrorType = ErrorType.RPC;
         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
-        RestconfError e =
-                new RestconfError( expectedErrorType,
-                                                 expectedErrorTag, expectedMessage );
+        RestconfError e = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage);
 
-        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag,
-                              null, (String)null, e);
+        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, null, (String) null, e);
     }
 
     @Test
-    public void testRestConfDocumentedException_WithAppTag()
-    {
+    public void testRestConfDocumentedException_WithAppTag() {
         String expectedMessage = "Message";
         ErrorType expectedErrorType = ErrorType.RPC;
         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
         String expectedErrorAppTag = "application.tag";
 
-        RestconfError e =
-                new RestconfError( expectedErrorType,
-                                                 expectedErrorTag, expectedMessage, expectedErrorAppTag );
+        RestconfError e = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag);
 
-        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag,
-                              expectedErrorAppTag, (String)null, e);
+        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, (String) null,
+                e);
     }
 
     @Test
-    public void testRestConfDocumentedException_WithAppTagErrorInfo()
-    {
+    public void testRestConfDocumentedException_WithAppTagErrorInfo() {
         String expectedMessage = "Message";
         ErrorType expectedErrorType = ErrorType.RPC;
         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
         String expectedErrorAppTag = "application.tag";
         String errorInfo = "<extra><sessionid>session.id</sessionid></extra>";
 
-        RestconfError e = new RestconfError( expectedErrorType,
-                                             expectedErrorTag,
-                                             expectedMessage,
-                                             expectedErrorAppTag,
-                                             errorInfo );
+        RestconfError e = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag,
+                errorInfo);
 
-        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag,
-                expectedErrorAppTag, errorInfo, e);
+        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, errorInfo, e);
     }
 
     @Test
     public void testRestConfErrorWithRpcError() {
 
         // All fields set
-        RpcError rpcError = getRpcError( "mock app-tag", ErrorTag.BAD_ATTRIBUTE.getTagValue(),
-                                         "mock error-info", RpcError.ErrorSeverity.ERROR,
-                                         "mock error-message", RpcError.ErrorType.PROTOCOL,
-                                         new Exception( "mock cause" ) );
+        RpcError rpcError = RpcResultBuilder.newError(
+                RpcError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE.getTagValue(), "mock error-message",
+                "mock app-tag", "mock error-info", new Exception( "mock cause" ) );
 
-        validateRestConfError( "mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE,
-                               "mock app-tag", "mock error-info",
-                               new RestconfError( rpcError ) );
+        validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
+                "mock error-info", new RestconfError(rpcError));
 
         // All fields set except 'info' - expect error-info set to 'cause'
-        rpcError = getRpcError( "mock app-tag", ErrorTag.BAD_ATTRIBUTE.getTagValue(),
-                                null, RpcError.ErrorSeverity.ERROR,
-                                "mock error-message", RpcError.ErrorType.PROTOCOL,
-                                new Exception( "mock cause" ) );
+        rpcError = RpcResultBuilder.newError(
+                RpcError.ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE.getTagValue(), "mock error-message",
+                "mock app-tag", null, new Exception( "mock cause" ) );
 
-        validateRestConfError( "mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE,
-                               "mock app-tag", new Contains( "mock cause" ),
-                               new RestconfError( rpcError ) );
+        validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
+                new Contains("mock cause"), new RestconfError(rpcError));
 
         // Some fields set - expect error-info set to ErrorSeverity
-        rpcError = getRpcError( null, ErrorTag.ACCESS_DENIED.getTagValue(),
-                                null, RpcError.ErrorSeverity.ERROR,
-                                null, RpcError.ErrorType.RPC, null );
+        rpcError = RpcResultBuilder.newError(
+                RpcError.ErrorType.RPC, ErrorTag.ACCESS_DENIED.getTagValue(), null, null, null, null );
 
-        validateRestConfError( null, ErrorType.RPC, ErrorTag.ACCESS_DENIED,
-                               null, "<severity>error</severity>",
-                               new RestconfError( rpcError ) );
+        validateRestConfError(null, ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, "<severity>error</severity>",
+                new RestconfError(rpcError));
 
-        // 'tag' field not mapped to ErrorTag - expect error-tag set to OPERATION_FAILED
-        rpcError = getRpcError( null, "not mapped",
-                                null, RpcError.ErrorSeverity.WARNING,
-                                null, RpcError.ErrorType.TRANSPORT, null );
+        // 'tag' field not mapped to ErrorTag - expect error-tag set to
+        // OPERATION_FAILED
+        rpcError = RpcResultBuilder.newWarning(
+                RpcError.ErrorType.TRANSPORT, "not mapped", null, null, null, null );
 
-        validateRestConfError( null, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED,
-                               null, "<severity>warning</severity>",
-                               new RestconfError( rpcError ) );
+        validateRestConfError(null, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED, null,
+                "<severity>warning</severity>", new RestconfError(rpcError));
 
         // No fields set - edge case
-        rpcError = getRpcError( null, null, null, null, null, null, null );
+        rpcError = RpcResultBuilder.newError( null, null, null, null, null, null );
 
         validateRestConfError( null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
-                               null, (String)null, new RestconfError( rpcError ) );
+                               null, "<severity>error</severity>", new RestconfError( rpcError ) );
     }
 
-    private void validateRestConfError(String expectedMessage, ErrorType expectedErrorType,
-            ErrorTag expectedErrorTag, String expectedErrorAppTag, String errorInfo, RestconfError e) {
+    private void validateRestConfError(String expectedMessage, ErrorType expectedErrorType, ErrorTag expectedErrorTag,
+            String expectedErrorAppTag, String errorInfo, RestconfError e) {
 
-        validateRestConfError( expectedMessage, expectedErrorType, expectedErrorTag,
-                               expectedErrorAppTag, equalTo( errorInfo ), e );
+        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag,
+                equalTo(errorInfo), e);
     }
 
-    private void validateRestConfError(String expectedMessage, ErrorType expectedErrorType,
-            ErrorTag expectedErrorTag, String expectedErrorAppTag,
-            Matcher<String> errorInfoMatcher, RestconfError e) {
+    private void validateRestConfError(String expectedMessage, ErrorType expectedErrorType, ErrorTag expectedErrorTag,
+            String expectedErrorAppTag, Matcher<String> errorInfoMatcher, RestconfError e) {
 
-        assertEquals( "getErrorMessage", expectedMessage, e.getErrorMessage() );
-        assertEquals( "getErrorType", expectedErrorType, e.getErrorType() );
-        assertEquals( "getErrorTag", expectedErrorTag, e.getErrorTag() );
-        assertEquals( "getErrorAppTag", expectedErrorAppTag, e.getErrorAppTag() );
-        assertThat( "getErrorInfo", e.getErrorInfo(), errorInfoMatcher );
-        e.toString(); // really just checking for NPE etc. Don't care about contents.
+        assertEquals("getErrorMessage", expectedMessage, e.getErrorMessage());
+        assertEquals("getErrorType", expectedErrorType, e.getErrorType());
+        assertEquals("getErrorTag", expectedErrorTag, e.getErrorTag());
+        assertEquals("getErrorAppTag", expectedErrorAppTag, e.getErrorAppTag());
+        assertThat("getErrorInfo", e.getErrorInfo(), errorInfoMatcher);
+        e.toString(); // really just checking for NPE etc. Don't care about
+                      // contents.
     }
 }