30a04495af5f9de6d02a8e7c1b4c45e41cae96d0
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfErrorTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
10 import static org.hamcrest.CoreMatchers.equalTo;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13
14 import org.hamcrest.BaseMatcher;
15 import org.hamcrest.Description;
16 import org.hamcrest.Matcher;
17 import org.junit.Test;
18 import org.opendaylight.restconf.common.errors.RestconfError;
19 import org.opendaylight.yangtools.yang.common.ErrorTag;
20 import org.opendaylight.yangtools.yang.common.ErrorType;
21 import org.opendaylight.yangtools.yang.common.RpcError;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
23
24 /**
25  * Unit tests for RestconfError.
26  *
27  * @author Devin Avery
28  * @author Thomas Pantelis
29  *
30  */
31 public class RestconfErrorTest {
32
33     static class Contains extends BaseMatcher<String> {
34
35         private final String text;
36
37         Contains(final String text) {
38             this.text = text;
39         }
40
41         @Override
42         public void describeTo(final Description desc) {
43             desc.appendText("contains ").appendValue(text);
44         }
45
46         @Override
47         public boolean matches(final Object arg) {
48             return arg != null && arg.toString().contains(text);
49         }
50     }
51
52     @Test
53     public void testRestConfDocumentedException_NoCause() {
54         String expectedMessage = "Message";
55         ErrorType expectedErrorType = ErrorType.RPC;
56         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
57         RestconfError error = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage);
58
59         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, null, (String) null, error);
60     }
61
62     @Test
63     public void testRestConfDocumentedException_WithAppTag() {
64         String expectedMessage = "Message";
65         ErrorType expectedErrorType = ErrorType.RPC;
66         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
67         String expectedErrorAppTag = "application.tag";
68
69         RestconfError error =
70                 new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag);
71
72         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, (String) null,
73                 error);
74     }
75
76     @Test
77     public void testRestConfDocumentedException_WithAppTagErrorInfo() {
78         String expectedMessage = "Message";
79         ErrorType expectedErrorType = ErrorType.RPC;
80         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
81         String expectedErrorAppTag = "application.tag";
82         String errorInfo = "<extra><sessionid>session.id</sessionid></extra>";
83
84         RestconfError error =
85                 new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag, errorInfo);
86
87         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, errorInfo,
88                 error);
89     }
90
91     @Test
92     public void testRestConfErrorWithRpcError() {
93
94         // All fields set
95         RpcError rpcError = RpcResultBuilder.newError(ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock error-message",
96                 "mock app-tag", "mock error-info", new Exception("mock cause"));
97
98         validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
99                 "mock error-info", new RestconfError(rpcError));
100
101         // All fields set except 'info' - expect error-info set to 'cause'
102         rpcError = RpcResultBuilder.newError(ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock error-message",
103                 "mock app-tag", null, new Exception("mock cause"));
104
105         validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
106                 new Contains("mock cause"), new RestconfError(rpcError));
107
108         // Some fields set - expect error-info set to ErrorSeverity
109         rpcError = RpcResultBuilder.newError(ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, null, null, null);
110
111         validateRestConfError(null, ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, "<severity>error</severity>",
112                 new RestconfError(rpcError));
113
114         // 'tag' field not mapped to ErrorTag - expect error-tag set to OPERATION_FAILED
115         rpcError = RpcResultBuilder.newWarning(ErrorType.TRANSPORT, new ErrorTag("not mapped"), null, null, null, null);
116
117         validateRestConfError(null, ErrorType.TRANSPORT, new ErrorTag("not mapped"), null,
118                 "<severity>warning</severity>", new RestconfError(rpcError));
119
120         // No fields set - edge case
121         rpcError = RpcResultBuilder.newError(ErrorType.APPLICATION, null, null, null, null, null);
122
123         validateRestConfError(null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
124                 null, "<severity>error</severity>", new RestconfError(rpcError));
125     }
126
127     private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
128             final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final String errorInfo,
129             final RestconfError error) {
130
131         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag,
132                 equalTo(errorInfo), error);
133     }
134
135     private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
136             final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final Matcher<String> errorInfoMatcher,
137             final RestconfError error) {
138
139         assertEquals("getErrorMessage", expectedMessage, error.getErrorMessage());
140         assertEquals("getErrorType", expectedErrorType, error.getErrorType());
141         assertEquals("getErrorTag", expectedErrorTag, error.getErrorTag());
142         assertEquals("getErrorAppTag", expectedErrorAppTag, error.getErrorAppTag());
143         assertThat("getErrorInfo", error.getErrorInfo(), errorInfoMatcher);
144         error.toString(); // really just checking for NPE etc. Don't care about
145                       // contents.
146     }
147 }