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