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