Reduce use of Status
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / JsonPatchStatusBodyWriterTest.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.nb.rfc8040.jersey.providers;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.mock;
12
13 import java.io.ByteArrayOutputStream;
14 import java.io.IOException;
15 import java.nio.charset.StandardCharsets;
16 import java.util.List;
17 import javax.ws.rs.core.MediaType;
18 import org.junit.Test;
19 import org.opendaylight.restconf.common.errors.RestconfError;
20 import org.opendaylight.restconf.server.api.DatabindContext;
21 import org.opendaylight.restconf.server.api.PatchStatusContext;
22 import org.opendaylight.restconf.server.api.PatchStatusEntity;
23 import org.opendaylight.yangtools.yang.common.ErrorTag;
24 import org.opendaylight.yangtools.yang.common.ErrorType;
25 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
26
27 public class JsonPatchStatusBodyWriterTest {
28     private final RestconfError error = new RestconfError(ErrorType.PROTOCOL, new ErrorTag("data-exists"),
29         "Data already exists");
30     private final PatchStatusEntity statusEntity = new PatchStatusEntity("patch1", true, null);
31     private final PatchStatusEntity statusEntityError = new PatchStatusEntity("patch1", false, List.of(error));
32     private final DatabindContext databind = DatabindContext.ofModel(mock(EffectiveModelContext.class));
33     private final JsonPatchStatusBodyWriter writer = new JsonPatchStatusBodyWriter();
34
35     /**
36      * Test if per-operation status is omitted if global error is present.
37      */
38     @Test
39     public void testOutputWithGlobalError() throws IOException {
40         final var outputStream = new ByteArrayOutputStream();
41         final var patchStatusContext = new PatchStatusContext(databind,"patch", List.of(statusEntity),
42             false, List.of(error));
43         writer.writeTo(patchStatusContext, null, null, null, MediaType.APPLICATION_XML_TYPE, null, outputStream);
44
45         assertEquals("""
46             {"ietf-yang-patch:yang-patch-status":{\
47             "patch-id":"patch",\
48             "errors":{"error":[{\
49             "error-type":"protocol",\
50             "error-tag":"data-exists",\
51             "error-message":"Data already exists"\
52             }]}}}""", outputStream.toString(StandardCharsets.UTF_8));
53     }
54
55     /**
56      * Test if per-operation status is present if there is no global error present.
57      */
58     @Test
59     public void testOutputWithoutGlobalError() throws IOException {
60         final var outputStream = new ByteArrayOutputStream();
61         final var patchStatusContext = new PatchStatusContext(databind, "patch", List.of(statusEntityError),
62             false, null);
63         writer.writeTo(patchStatusContext, null, null, null, MediaType.APPLICATION_XML_TYPE, null, outputStream);
64
65         assertEquals("""
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             }]}}]}}}""", outputStream.toString(StandardCharsets.UTF_8));
75     }
76 }