Rework body formatting wiring
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / AbstractJukeboxTest.java
1 /*
2  * Copyright (c) 2023 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;
9
10 import static org.junit.jupiter.api.Assertions.assertEquals;
11
12 import java.io.ByteArrayInputStream;
13 import java.io.ByteArrayOutputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.OutputStream;
17 import java.nio.charset.StandardCharsets;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.opendaylight.restconf.api.FormatParameters;
20 import org.opendaylight.restconf.api.query.PrettyPrintParam;
21 import org.opendaylight.restconf.server.api.DatabindContext;
22 import org.opendaylight.yangtools.yang.common.Decimal64;
23 import org.opendaylight.yangtools.yang.common.QName;
24 import org.opendaylight.yangtools.yang.common.Uint32;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
31 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
32 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
33 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
34 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35
36 public abstract class AbstractJukeboxTest {
37     @FunctionalInterface
38     public interface FormatMethod {
39
40         void invoke(@NonNull FormatParameters format, @NonNull OutputStream out) throws IOException;
41     }
42
43     // container jukebox
44     protected static final QName JUKEBOX_QNAME =
45         QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
46     // container player
47     protected static final QName PLAYER_QNAME = QName.create(JUKEBOX_QNAME, "player");
48     // container library
49     protected static final QName LIBRARY_QNAME = QName.create(JUKEBOX_QNAME, "library");
50     // list song
51     protected static final QName SONG_QNAME = QName.create(JUKEBOX_QNAME, "song");
52     // leaf id
53     protected static final QName SONG_ID_QNAME = QName.create(JUKEBOX_QNAME, "id");
54     // leaf index
55     protected static final QName SONG_INDEX_QNAME = QName.create(JUKEBOX_QNAME, "index");
56     // list artist
57     protected static final QName ARTIST_QNAME = QName.create(JUKEBOX_QNAME, "artist");
58     // list album
59     protected static final QName ALBUM_QNAME = QName.create(JUKEBOX_QNAME, "album");
60     // leaf name
61     protected static final QName NAME_QNAME = QName.create(JUKEBOX_QNAME, "name");
62     // leaf gap
63     protected static final QName GAP_QNAME = QName.create(JUKEBOX_QNAME, "gap");
64     // leaf playlist
65     protected static final QName PLAYLIST_QNAME = QName.create(JUKEBOX_QNAME, "playlist");
66     // leaf description
67     protected static final QName DESCRIPTION_QNAME = QName.create(JUKEBOX_QNAME, "description");
68
69     protected static final YangInstanceIdentifier JUKEBOX_IID = YangInstanceIdentifier.of(JUKEBOX_QNAME);
70     protected static final YangInstanceIdentifier PLAYLIST_IID =
71         YangInstanceIdentifier.of(JUKEBOX_QNAME, PLAYLIST_QNAME);
72
73     // instance identifier for accessing leaf node "gap"
74     protected static final YangInstanceIdentifier GAP_IID =
75         YangInstanceIdentifier.of(JUKEBOX_QNAME, PLAYER_QNAME, GAP_QNAME);
76
77     protected static final LeafNode<?> GAP_LEAF = ImmutableNodes.leafNode(GAP_QNAME, Decimal64.valueOf("0.2"));
78     protected static final ContainerNode CONT_PLAYER = ImmutableNodes.newContainerBuilder()
79         .withNodeIdentifier(new NodeIdentifier(PLAYER_QNAME))
80         .withChild(GAP_LEAF)
81         .build();
82     protected static final ContainerNode EMPTY_JUKEBOX = ImmutableNodes.newContainerBuilder()
83         .withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME))
84         .withChild(CONT_PLAYER)
85         .build();
86     protected static final MapEntryNode BAND_ENTRY = ImmutableNodes.newMapEntryBuilder()
87         .withNodeIdentifier(NodeIdentifierWithPredicates.of(PLAYLIST_QNAME, NAME_QNAME, "name of band"))
88         .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name of band"))
89         .withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, "band description"))
90         .build();
91     protected static final MapEntryNode SONG1 = ImmutableNodes.newMapEntryBuilder()
92         .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifierWithPredicates.of(SONG_QNAME, SONG_INDEX_QNAME,
93             Uint32.ONE))
94         .withChild(ImmutableNodes.leafNode(SONG_ID_QNAME, "A"))
95         .build();
96     protected static final MapEntryNode SONG2 = ImmutableNodes.newMapEntryBuilder()
97         .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifierWithPredicates.of(SONG_QNAME, SONG_INDEX_QNAME,
98             Uint32.TWO))
99         .withChild(ImmutableNodes.leafNode(SONG_ID_QNAME, "B"))
100         .build();
101     protected static final SystemMapNode PLAYLIST_WITH_SONGS = ImmutableNodes.newSystemMapBuilder()
102         .withNodeIdentifier(new NodeIdentifier(SONG_QNAME))
103             .withChild(SONG1)
104             .withChild(SONG2)
105         .build();
106
107     protected static final @NonNull EffectiveModelContext JUKEBOX_SCHEMA =
108         YangParserTestUtils.parseYangResourceDirectory("/jukebox");
109     protected static final @NonNull DatabindContext JUKEBOX_DATABIND = DatabindContext.ofModel(JUKEBOX_SCHEMA);
110
111     protected static final InputStream stringInputStream(final String str) {
112         return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
113     }
114
115     public static void assertFormat(final String expected, final FormatMethod formatMethod,
116             final boolean prettyPrint) {
117         final var baos = new ByteArrayOutputStream();
118         try {
119             formatMethod.invoke(new FormatParameters(PrettyPrintParam.of(prettyPrint)), baos);
120         } catch (IOException e) {
121             throw new AssertionError(e);
122         }
123         assertEquals(expected, baos.toString(StandardCharsets.UTF_8));
124     }
125 }