61ece25aaa848ef728bc319914f7e88d8c23b60a
[netconf.git] /
1 /*
2  * Copyright (c) 2023 PANTHEON.tech s.r.o. 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.restconf.server.spi;
9
10 import java.io.IOException;
11 import java.util.List;
12 import org.junit.Test;
13 import org.opendaylight.restconf.nb.rfc8040.AbstractJukeboxTest;
14 import org.opendaylight.restconf.server.api.PatchStatusContext;
15 import org.opendaylight.restconf.server.api.PatchStatusEntity;
16 import org.opendaylight.restconf.server.api.ServerError;
17 import org.opendaylight.yangtools.yang.common.ErrorTag;
18 import org.opendaylight.yangtools.yang.common.ErrorType;
19
20 public class YangPatchStatusBodyTest extends AbstractJukeboxTest {
21     private final ServerError error = new ServerError(ErrorType.PROTOCOL, new ErrorTag("data-exists"),
22         "Data already exists");
23     private final PatchStatusEntity statusEntity = new PatchStatusEntity("patch1", true, null);
24     private final PatchStatusEntity statusEntityError = new PatchStatusEntity("patch1", false, List.of(error));
25
26     /**
27      * Test if per-operation status is omitted if global error is present.
28      */
29     @Test
30     public void testOutputWithGlobalError() throws IOException {
31         final var body = new YangPatchStatusBody(new PatchStatusContext("patch", List.of(statusEntity), false,
32             List.of(error)));
33
34         assertFormat("""
35             {
36               "ietf-yang-patch:yang-patch-status": {
37                 "patch-id": "patch",
38                 "errors": {
39                   "error": [
40                     {
41                       "error-type": "protocol",
42                       "error-tag": "data-exists",
43                       "error-message": "Data already exists"
44                     }
45                   ]
46                 }
47               }
48             }""", body::formatToJSON, true);
49         assertFormat("""
50             <yang-patch-status xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
51               <patch-id>patch</patch-id>
52               <errors>
53                 <error-type>protocol</error-type>
54                 <error-tag>data-exists</error-tag>
55                 <error-message>Data already exists</error-message>
56               </errors>
57             </yang-patch-status>""", body::formatToXML, true);
58     }
59
60     /**
61      * Test if per-operation status is present if there is no global error present.
62      */
63     @Test
64     public void testOutputWithoutGlobalError() throws IOException {
65         final var body = new YangPatchStatusBody(new PatchStatusContext("patch", List.of(statusEntityError), false,
66             null));
67
68         assertFormat("""
69             {
70               "ietf-yang-patch:yang-patch-status": {
71                 "patch-id": "patch",
72                 "edit-status": {
73                   "edit": [
74                     {
75                       "edit-id": "patch1",
76                       "errors": {
77                         "error": [
78                           {
79                             "error-type": "protocol",
80                             "error-tag": "data-exists",
81                             "error-message": "Data already exists"
82                           }
83                         ]
84                       }
85                     }
86                   ]
87                 }
88               }
89             }""", body::formatToJSON, true);
90         assertFormat("""
91             <yang-patch-status xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">
92               <patch-id>patch</patch-id>
93               <edit-status>
94                 <edit>
95                   <edit-id>patch1</edit-id>
96                   <errors>
97                     <error-type>protocol</error-type>
98                     <error-tag>data-exists</error-tag>
99                     <error-message>Data already exists</error-message>
100                   </errors>
101                 </edit>
102               </edit-status>
103             </yang-patch-status>""", body::formatToXML, true);
104     }
105 }