summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/test/RestconfErrorTest.java
blob: 30a04495af5f9de6d02a8e7c1b4c45e41cae96d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
 * 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;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertEquals;

import org.hamcrest.BaseMatcher;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.junit.Test;
import org.opendaylight.restconf.common.errors.RestconfError;
import org.opendaylight.yangtools.yang.common.ErrorTag;
import org.opendaylight.yangtools.yang.common.ErrorType;
import org.opendaylight.yangtools.yang.common.RpcError;
import org.opendaylight.yangtools.yang.common.RpcResultBuilder;

/**
 * Unit tests for RestconfError.
 *
 * @author Devin Avery
 * @author Thomas Pantelis
 *
 */
public class RestconfErrorTest {

    static class Contains extends BaseMatcher<String> {

        private final String text;

        Contains(final String text) {
            this.text = text;
        }

        @Override
        public void describeTo(final Description desc) {
            desc.appendText("contains ").appendValue(text);
        }

        @Override
        public boolean matches(final Object arg) {
            return arg != null && arg.toString().contains(text);
        }
    }

    @Test
    public void testRestConfDocumentedException_NoCause() {
        String expectedMessage = "Message";
        ErrorType expectedErrorType = ErrorType.RPC;
        ErrorTag expectedErrorTag = ErrorTag.IN_USE;
        RestconfError error = new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage);

        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, null, (String) null, error);
    }

    @Test
    public void testRestConfDocumentedException_WithAppTag() {
        String expectedMessage = "Message";
        ErrorType expectedErrorType = ErrorType.RPC;
        ErrorTag expectedErrorTag = ErrorTag.IN_USE;
        String expectedErrorAppTag = "application.tag";

        RestconfError error =
                new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag);

        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, (String) null,
                error);
    }

    @Test
    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 error =
                new RestconfError(expectedErrorType, expectedErrorTag, expectedMessage, expectedErrorAppTag, errorInfo);

        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag, errorInfo,
                error);
    }

    @Test
    public void testRestConfErrorWithRpcError() {

        // All fields set
        RpcError rpcError = RpcResultBuilder.newError(ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "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));

        // All fields set except 'info' - expect error-info set to 'cause'
        rpcError = RpcResultBuilder.newError(ErrorType.PROTOCOL, ErrorTag.BAD_ATTRIBUTE, "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));

        // Some fields set - expect error-info set to ErrorSeverity
        rpcError = RpcResultBuilder.newError(ErrorType.RPC, ErrorTag.ACCESS_DENIED, null, null, null, null);

        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 = RpcResultBuilder.newWarning(ErrorType.TRANSPORT, new ErrorTag("not mapped"), null, null, null, null);

        validateRestConfError(null, ErrorType.TRANSPORT, new ErrorTag("not mapped"), null,
                "<severity>warning</severity>", new RestconfError(rpcError));

        // No fields set - edge case
        rpcError = RpcResultBuilder.newError(ErrorType.APPLICATION, null, null, null, null, null);

        validateRestConfError(null, ErrorType.APPLICATION, ErrorTag.OPERATION_FAILED,
                null, "<severity>error</severity>", new RestconfError(rpcError));
    }

    private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
            final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final String errorInfo,
            final RestconfError error) {

        validateRestConfError(expectedMessage, expectedErrorType, expectedErrorTag, expectedErrorAppTag,
                equalTo(errorInfo), error);
    }

    private static void validateRestConfError(final String expectedMessage, final ErrorType expectedErrorType,
            final ErrorTag expectedErrorTag, final String expectedErrorAppTag, final Matcher<String> errorInfoMatcher,
            final RestconfError error) {

        assertEquals("getErrorMessage", expectedMessage, error.getErrorMessage());
        assertEquals("getErrorType", expectedErrorType, error.getErrorType());
        assertEquals("getErrorTag", expectedErrorTag, error.getErrorTag());
        assertEquals("getErrorAppTag", expectedErrorAppTag, error.getErrorAppTag());
        assertThat("getErrorInfo", error.getErrorInfo(), errorInfoMatcher);
        error.toString(); // really just checking for NPE etc. Don't care about
                      // contents.
    }
}