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