Allow odl-pretty-print to be configured to true
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / jersey / providers / XmlNormalizedNodeBodyWriterTest.java
1 /*
2  * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others.  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 javax.ws.rs.core.MediaType;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.junit.MockitoJUnitRunner;
20 import org.opendaylight.restconf.nb.rfc8040.AbstractInstanceIdentifierTest;
21 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
22 import org.opendaylight.restconf.nb.rfc8040.legacy.WriterParameters;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
26 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack.Inference;
29
30 @RunWith(MockitoJUnitRunner.StrictStubs.class)
31 public class XmlNormalizedNodeBodyWriterTest extends AbstractInstanceIdentifierTest {
32     @Test
33     public void testWriteEmptyRootContainer() throws IOException {
34         final EffectiveModelContext schemaContext = mock(EffectiveModelContext.class);
35
36         final NormalizedNodePayload nodePayload = new NormalizedNodePayload(Inference.ofDataTreePath(schemaContext),
37             ImmutableNodes.newContainerBuilder().withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME)).build(),
38             WriterParameters.EMPTY);
39
40         final ByteArrayOutputStream output = new ByteArrayOutputStream();
41         final XmlNormalizedNodeBodyWriter xmlWriter = new XmlNormalizedNodeBodyWriter();
42         xmlWriter.writeTo(nodePayload, null, null, null, MediaType.APPLICATION_XML_TYPE, null, output);
43
44         // FIXME: NETCONF-855: this is wrong, the namespace should be 'urn:ietf:params:xml:ns:yang:ietf-restconf'
45         assertEquals("<data xmlns=\"urn:ietf:params:xml:ns:netconf:base:1.0\"></data>",
46             output.toString(StandardCharsets.UTF_8));
47     }
48
49     @Test
50     public void testRootContainerWrite() throws IOException {
51         final NormalizedNodePayload nodePayload = new NormalizedNodePayload(
52             Inference.ofDataTreePath(IID_SCHEMA),
53             ImmutableNodes.newContainerBuilder()
54                 .withNodeIdentifier(new NodeIdentifier(SchemaContext.NAME))
55                 .withChild(ImmutableNodes.newContainerBuilder()
56                     .withNodeIdentifier(new NodeIdentifier(
57                         QName.create("foo:module", "2016-09-29", "foo-bar-container")))
58                     .build())
59                 .withChild(ImmutableNodes.newContainerBuilder()
60                     .withNodeIdentifier(new NodeIdentifier(
61                         QName.create("bar:module", "2016-09-29", "foo-bar-container")))
62                     .build())
63                 .build(), WriterParameters.EMPTY);
64
65         final ByteArrayOutputStream output = new ByteArrayOutputStream();
66         final XmlNormalizedNodeBodyWriter xmlWriter = new XmlNormalizedNodeBodyWriter();
67         xmlWriter.writeTo(nodePayload, null, null, null, MediaType.APPLICATION_XML_TYPE, null, output);
68
69         assertEquals("""
70             <data xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">\
71             <foo-bar-container xmlns="bar:module"></foo-bar-container>\
72             <foo-bar-container xmlns="foo:module"></foo-bar-container>\
73             </data>""", output.toString(StandardCharsets.UTF_8));
74     }
75 }