Merge "Bug 1125: Added regression test"
[controller.git] / opendaylight / md-sal / sal-rest-connector / 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.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertThat;
14 import static org.opendaylight.controller.sal.common.util.RpcErrors.getRpcError;
15
16 import java.util.HashMap;
17 import java.util.Map;
18 import org.hamcrest.BaseMatcher;
19 import org.hamcrest.Description;
20 import org.hamcrest.Matcher;
21 import org.junit.Test;
22 import org.opendaylight.controller.sal.restconf.impl.RestconfError;
23 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorTag;
24 import org.opendaylight.controller.sal.restconf.impl.RestconfError.ErrorType;
25 import org.opendaylight.yangtools.yang.common.RpcError;
26
27 /**
28  * Unit tests for RestconfError.
29  *
30  * @author Devin Avery
31  * @author Thomas Pantelis
32  *
33  */
34 public class RestconfErrorTest {
35
36     static class Contains extends BaseMatcher<String> {
37
38         private final String text;
39
40         public Contains(String text) {
41             this.text = text;
42         }
43
44         @Override
45         public void describeTo(Description desc) {
46             desc.appendText("contains ").appendValue(text);
47         }
48
49         @Override
50         public boolean matches(Object arg) {
51             return arg != null && arg.toString().contains(text);
52         }
53     }
54
55     @Test
56     public void testErrorTagValueOf() {
57         assertEquals(ErrorTag.IN_USE, ErrorTag.valueOfCaseInsensitive(ErrorTag.IN_USE.getTagValue()));
58     }
59
60     @Test
61     public void testErrorTagValueOfIsLowercase() {
62         assertEquals("in-use", ErrorTag.IN_USE.getTagValue());
63     }
64
65     @Test
66     public void testErrorTypeGetErrorTypeTagIsLowerCase() {
67         assertEquals(ErrorType.APPLICATION.name().toLowerCase(), ErrorType.APPLICATION.getErrorTypeTag());
68     }
69
70     @Test
71     public void testErrorTypeValueOf() {
72         assertEquals(ErrorType.APPLICATION, ErrorType.valueOfCaseInsensitive(ErrorType.APPLICATION.getErrorTypeTag()));
73     }
74
75     @Test
76     public void testErrorTagStatusCodes() {
77         Map<String, Integer> lookUpMap = new HashMap<String, Integer>();
78
79         lookUpMap.put("in-use", 409);
80         lookUpMap.put("invalid-value", 400);
81         lookUpMap.put("too-big", 413);
82         lookUpMap.put("missing-attribute", 400);
83         lookUpMap.put("bad-attribute", 400);
84         lookUpMap.put("unknown-attribute", 400);
85         lookUpMap.put("bad-element", 400);
86         lookUpMap.put("unknown-element", 400);
87         lookUpMap.put("unknown-namespace", 400);
88         lookUpMap.put("access-denied", 403);
89         lookUpMap.put("lock-denied", 409);
90         lookUpMap.put("resource-denied", 409);
91         lookUpMap.put("rollback-failed", 500);
92         lookUpMap.put("data-exists", 409);
93         lookUpMap.put("data-missing", 409);
94         lookUpMap.put("operation-not-supported", 501);
95         lookUpMap.put("operation-failed", 500);
96         lookUpMap.put("partial-operation", 500);
97         lookUpMap.put("malformed-message", 400);
98
99         for (ErrorTag tag : ErrorTag.values()) {
100             Integer expectedStatusCode = lookUpMap.get(tag.getTagValue());
101             assertNotNull("Failed to find " + tag.getTagValue(), expectedStatusCode);
102             assertEquals("Status Code does not match", expectedStatusCode, Integer.valueOf(tag.getStatusCode()));
103         }
104     }
105
106     @Test
107     public void testRestConfDocumentedException_NoCause() {
108         String expectedMessage = "Message";
109         ErrorType expectedErrorType = ErrorType.RPC;
110         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
111         RestconfError e = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage);
112
113         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, null, (String) null, e);
114     }
115
116     @Test
117     public void testRestConfDocumentedException_WithAppTag() {
118         String expectedMessage = "Message";
119         ErrorType expectedErrorType = ErrorType.RPC;
120         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
121         String expectedErrorAppTag = "application.tag";
122
123         RestconfError e = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag);
124
125         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, (String) null,
126                 e);
127     }
128
129     @Test
130     public void testRestConfDocumentedException_WithAppTagErrorInfo() {
131         String expectedMessage = "Message";
132         ErrorType expectedErrorType = ErrorType.RPC;
133         ErrorTag expectedErrorTag = ErrorTag.IN_USE;
134         String expectedErrorAppTag = "application.tag";
135         String errorInfo = "<extra><sessionid>session.id</sessionid></extra>";
136
137         RestconfError e = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag,
138                 errorInfo);
139
140         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, errorInfo, e);
141     }
142
143     @Test
144     public void testRestConfErrorWithRpcError() {
145
146         // All fields set
147         RpcError rpcError = getRpcError("mock app-tag", ErrorTag.BAD_ATTRIBUTE.getTagValue(), "mock error-info",
148                 RpcError.ErrorSeverity.ERROR, "mock error-message", RpcError.ErrorType.PROTOCOL, new Exception(
149                         "mock cause"));
150
151         validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
152                 "mock error-info", new RestconfError(rpcError));
153
154         // All fields set except 'info' - expect error-info set to 'cause'
155         rpcError = getRpcError("mock app-tag", ErrorTag.BAD_ATTRIBUTE.getTagValue(), null,
156                 RpcError.ErrorSeverity.ERROR, "mock error-message", RpcError.ErrorType.PROTOCOL, new Exception(
157                         "mock cause"));
158
159         validateRestConfError("mock error-message", ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "mock app-tag",
160                 new Contains("mock cause"), new RestconfError(rpcError));
161
162         // Some fields set - expect error-info set to ErrorSeverity
163         rpcError = getRpcError(null, ErrorTag.ACCESS_DENIED.getTagValue(), null, RpcError.ErrorSeverity.ERROR, null,
164                 RpcError.ErrorType.RPC, null);
165
166         validateRestConfError(null, ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, "<severity>error</severity>",
167                 new RestconfError(rpcError));
168
169         // 'tag' field not mapped to ErrorTag - expect error-tag set to
170         // OPERATION_FAILED
171         rpcError = getRpcError(null, "not mapped", null, RpcError.ErrorSeverity.WARNING, null,
172                 RpcError.ErrorType.TRANSPORT, null);
173
174         validateRestConfError(null, ErrorType.TRANSPORT, ErrorTag.OPERATION_FAILED, null,
175                 "<severity>warning</severity>", new RestconfError(rpcError));
176
177         // No fields set - edge case
178         rpcError = getRpcError(null, null, null, null, null, null, null);
179
180         validateRestConfError(null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED, null, (String) null,
181                 new RestconfError(rpcError));
182     }
183
184     private void validateRestConfError(String expectedMessage, ErrorType expectedErrorType, ErrorTag expectedErrorTag,
185             String expectedErrorAppTag, String errorInfo, RestconfError e) {
186
187         validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag,
188                 equalTo(errorInfo), e);
189     }
190
191     private void validateRestConfError(String expectedMessage, ErrorType expectedErrorType, ErrorTag expectedErrorTag,
192             String expectedErrorAppTag, Matcher<String> errorInfoMatcher, RestconfError e) {
193
194         assertEquals("getErrorMessage", expectedMessage, e.getErrorMessage());
195         assertEquals("getErrorType", expectedErrorType, e.getErrorType());
196         assertEquals("getErrorTag", expectedErrorTag, e.getErrorTag());
197         assertEquals("getErrorAppTag", expectedErrorAppTag, e.getErrorAppTag());
198         assertThat("getErrorInfo", e.getErrorInfo(), errorInfoMatcher);
199         e.toString(); // really just checking for NPE etc. Don't care about
200                       // contents.
201     }
202 }