blob: c9a1d04e08385de098c2bbbfefe137b6c1ca6bf7 [file] [log] [blame]
Sean Condon13b16812018-01-25 10:31:49 +00001/*
2 * Copyright 2017-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.restconf.api;
17
18import com.fasterxml.jackson.databind.node.ObjectNode;
19import org.junit.Test;
20
21import javax.ws.rs.core.Response;
22
23import java.util.Arrays;
24import java.util.Optional;
25
26import static org.junit.Assert.assertEquals;
27
28/**
29 * Test the conversion of RestconfErrors in to JSON.
30 */
31public class RestconfExceptionTest {
32
33 private RestconfError error1 = RestconfError
34 .builder(RestconfError.ErrorType.TRANSPORT, RestconfError.ErrorTag.ACCESS_DENIED)
35 .build();
36
37 private RestconfError error2 = RestconfError
38 .builder(RestconfError.ErrorType.TRANSPORT, RestconfError.ErrorTag.BAD_ATTRIBUTE)
39 .build();
40
41 private RestconfError error3 = RestconfError
42 .builder(RestconfError.ErrorType.RPC, RestconfError.ErrorTag.BAD_ELEMENT)
43 .errorAppTag("my-app-tag")
44 .errorMessage("a message about the error")
45 .errorPath("/a/b/c")
46 .errorInfo("info about the error")
47 .build();
48
49 /**
50 * Test a Restconf Exception with many RestconfErrors converted to Json.
51 */
52 @Test
53 public void testToRestconfErrorJson() {
54 IllegalArgumentException ie = new IllegalArgumentException("This is a test");
55 RestconfException e = new RestconfException("Error in system", ie,
56 RestconfError.ErrorTag.DATA_EXISTS, Response.Status.BAD_REQUEST,
57 Optional.of("/some/path"));
58 e.addToErrors(error1);
59 e.addToErrors(error2);
60 e.addToErrors(error3);
61
62 assertEquals("{\"ietf-restconf:errors\":[" +
63 "{\"error\":{" +
64 "\"error-type\":\"application\"," +
65 "\"error-tag\":\"data-exists\"," +
66 "\"error-path\":\"/some/path\"," +
67 "\"error-message\":\"Error in system\"," +
68 "\"error-info\":\"This is a test\"}}," +
69 "{\"error\":{" +
70 "\"error-type\":\"transport\"," +
71 "\"error-tag\":\"access-denied\"}}," +
72 "{\"error\":{" +
73 "\"error-type\":\"transport\"," +
74 "\"error-tag\":\"bad-attribute\"}}," +
75 "{\"error\":{" +
76 "\"error-type\":\"rpc\"," +
77 "\"error-tag\":\"bad-element\"," +
78 "\"error-app-tag\":\"my-app-tag\"," +
79 "\"error-path\":\"/a/b/c\"," +
80 "\"error-message\":\"a message about the error\"," +
81 "\"error-info\":\"info about the error\"}}]}",
82 e.toRestconfErrorJson().toString());
83 }
84
85 @Test
86 public void testWrappingErrorsNotInException() {
87 RestconfError error4 = RestconfError
88 .builder(RestconfError.ErrorType.TRANSPORT, RestconfError.ErrorTag.UNKNOWN_ELEMENT)
89 .build();
90
91 ObjectNode json = RestconfError.wrapErrorAsJson(Arrays.asList(error4, error1, error2, error3));
92
93 assertEquals("{\"ietf-restconf:errors\":[" +
94 "{\"error\":{" +
95 "\"error-type\":\"transport\"," +
96 "\"error-tag\":\"unknown-element\"}}," +
97 "{\"error\":{" +
98 "\"error-type\":\"transport\"," +
99 "\"error-tag\":\"access-denied\"}}," +
100 "{\"error\":{" +
101 "\"error-type\":\"transport\"," +
102 "\"error-tag\":\"bad-attribute\"}}," +
103 "{\"error\":{" +
104 "\"error-type\":\"rpc\"," +
105 "\"error-tag\":\"bad-element\"," +
106 "\"error-app-tag\":\"my-app-tag\"," +
107 "\"error-path\":\"/a/b/c\"," +
108 "\"error-message\":\"a message about the error\"," +
109 "\"error-info\":\"info about the error\"}}]}",
110 json.toString());
111
112 }
113}