2 * Copyright (c) 2023 PANTHEON.tech s.r.o. All rights reserved.
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
8 package org.opendaylight.restconf.nb.rfc8040.jersey.providers;
10 import static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.mock;
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;
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();
36 * Test if per-operation status is omitted if global error is present.
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);
46 {"ietf-yang-patch:yang-patch-status":{\
49 "error-type":"protocol",\
50 "error-tag":"data-exists",\
51 "error-message":"Data already exists"\
52 }]}}}""", outputStream.toString(StandardCharsets.UTF_8));
56 * Test if per-operation status is present if there is no global error present.
59 public void testOutputWithoutGlobalError() throws IOException {
60 final var outputStream = new ByteArrayOutputStream();
61 final var patchStatusContext = new PatchStatusContext(databind, "patch", List.of(statusEntityError),
63 writer.writeTo(patchStatusContext, null, null, null, MediaType.APPLICATION_XML_TYPE, null, outputStream);
66 {"ietf-yang-patch:yang-patch-status":{\
68 "edit-status":{"edit":[{\
71 "error-type":"protocol",\
72 "error-tag":"data-exists",\
73 "error-message":"Data already exists"\
74 }]}}]}}}""", outputStream.toString(StandardCharsets.UTF_8));