Reduce use of Status
[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),
37             false, List.of(error)));
38
39         assertFormat("""
40             {"ietf-yang-patch:yang-patch-status":{\
41             "patch-id":"patch",\
42             "errors":{"error":[{\
43             "error-type":"protocol",\
44             "error-tag":"data-exists",\
45             "error-message":"Data already exists"\
46             }]}}}""", body::formatToJSON);
47         assertFormat("""
48             <yang-patch-status xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">\
49             <patch-id>patch</patch-id>\
50             <errors>\
51             <error-type>protocol</error-type>\
52             <error-tag>data-exists</error-tag>\
53             <error-message>Data already exists</error-message>\
54             </errors></yang-patch-status>""", body::formatToXML);
55     }
56
57     /**
58      * Test if per-operation status is present if there is no global error present.
59      */
60     @Test
61     public void testOutputWithoutGlobalError() throws IOException {
62         final var body = new YangPatchStatusBody(new PatchStatusContext(databind,"patch", List.of(statusEntityError),
63             false, null));
64
65         assertFormat("""
66             {"ietf-yang-patch:yang-patch-status":{\
67             "patch-id":"patch",\
68             "edit-status":{"edit":[{\
69             "edit-id":"patch1",\
70             "errors":{"error":[{\
71             "error-type":"protocol",\
72             "error-tag":"data-exists",\
73             "error-message":"Data already exists"\
74             }]}}]}}}""", body::formatToJSON);
75         assertFormat("""
76             <yang-patch-status xmlns="urn:ietf:params:xml:ns:yang:ietf-yang-patch">\
77             <patch-id>patch</patch-id>\
78             <edit-status><edit>\
79             <edit-id>patch1</edit-id>\
80             <errors>\
81             <error-type>protocol</error-type>\
82             <error-tag>data-exists</error-tag>\
83             <error-message>Data already exists</error-message>\
84             </errors></edit></edit-status>\
85             </yang-patch-status>""", body::formatToXML);
86     }
87 }