2 * Copyright (c) 2023 PANTHEON.tech, s.r.o. and others. 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;
10 import static org.junit.jupiter.api.Assertions.assertEquals;
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.query.PrettyPrintParam;
20 import org.opendaylight.restconf.server.api.DatabindContext;
21 import org.opendaylight.yangtools.yang.common.Decimal64;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.Uint32;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.MapEntryNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.SystemMapNode;
31 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
32 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
33 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
35 public abstract class AbstractJukeboxTest {
37 public interface FormatMethod {
39 void invoke(@NonNull PrettyPrintParam prettyPrint, @NonNull OutputStream out) throws IOException;
43 protected static final QName JUKEBOX_QNAME =
44 QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
46 protected static final QName PLAYER_QNAME = QName.create(JUKEBOX_QNAME, "player");
48 protected static final QName LIBRARY_QNAME = QName.create(JUKEBOX_QNAME, "library");
50 protected static final QName SONG_QNAME = QName.create(JUKEBOX_QNAME, "song");
52 protected static final QName SONG_ID_QNAME = QName.create(JUKEBOX_QNAME, "id");
54 protected static final QName SONG_INDEX_QNAME = QName.create(JUKEBOX_QNAME, "index");
56 protected static final QName ARTIST_QNAME = QName.create(JUKEBOX_QNAME, "artist");
58 protected static final QName ALBUM_QNAME = QName.create(JUKEBOX_QNAME, "album");
60 protected static final QName NAME_QNAME = QName.create(JUKEBOX_QNAME, "name");
62 protected static final QName GAP_QNAME = QName.create(JUKEBOX_QNAME, "gap");
64 protected static final QName PLAYLIST_QNAME = QName.create(JUKEBOX_QNAME, "playlist");
66 protected static final QName DESCRIPTION_QNAME = QName.create(JUKEBOX_QNAME, "description");
68 protected static final YangInstanceIdentifier JUKEBOX_IID = YangInstanceIdentifier.of(JUKEBOX_QNAME);
69 protected static final YangInstanceIdentifier PLAYLIST_IID =
70 YangInstanceIdentifier.of(JUKEBOX_QNAME, PLAYLIST_QNAME);
72 // instance identifier for accessing leaf node "gap"
73 protected static final YangInstanceIdentifier GAP_IID =
74 YangInstanceIdentifier.of(JUKEBOX_QNAME, PLAYER_QNAME, GAP_QNAME);
76 protected static final LeafNode<?> GAP_LEAF = ImmutableNodes.leafNode(GAP_QNAME, Decimal64.valueOf("0.2"));
77 protected static final ContainerNode CONT_PLAYER = ImmutableNodes.newContainerBuilder()
78 .withNodeIdentifier(new NodeIdentifier(PLAYER_QNAME))
81 protected static final ContainerNode EMPTY_JUKEBOX = ImmutableNodes.newContainerBuilder()
82 .withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME))
83 .withChild(CONT_PLAYER)
85 protected static final MapEntryNode BAND_ENTRY = ImmutableNodes.newMapEntryBuilder()
86 .withNodeIdentifier(NodeIdentifierWithPredicates.of(PLAYLIST_QNAME, NAME_QNAME, "name of band"))
87 .withChild(ImmutableNodes.leafNode(NAME_QNAME, "name of band"))
88 .withChild(ImmutableNodes.leafNode(DESCRIPTION_QNAME, "band description"))
90 protected static final MapEntryNode SONG1 = ImmutableNodes.newMapEntryBuilder()
91 .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifierWithPredicates.of(SONG_QNAME, SONG_INDEX_QNAME,
93 .withChild(ImmutableNodes.leafNode(SONG_ID_QNAME, "A"))
95 protected static final MapEntryNode SONG2 = ImmutableNodes.newMapEntryBuilder()
96 .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifierWithPredicates.of(SONG_QNAME, SONG_INDEX_QNAME,
98 .withChild(ImmutableNodes.leafNode(SONG_ID_QNAME, "B"))
100 protected static final SystemMapNode PLAYLIST_WITH_SONGS = ImmutableNodes.newSystemMapBuilder()
101 .withNodeIdentifier(new NodeIdentifier(SONG_QNAME))
106 protected static final @NonNull EffectiveModelContext JUKEBOX_SCHEMA =
107 YangParserTestUtils.parseYangResourceDirectory("/jukebox");
108 protected static final @NonNull DatabindContext JUKEBOX_DATABIND = DatabindContext.ofModel(JUKEBOX_SCHEMA);
110 protected static final InputStream stringInputStream(final String str) {
111 return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
114 public static void assertFormat(final String expected, final FormatMethod formatMethod,
115 final boolean prettyPrint) {
116 final var baos = new ByteArrayOutputStream();
118 formatMethod.invoke(PrettyPrintParam.of(prettyPrint), baos);
119 } catch (IOException e) {
120 throw new AssertionError(e);
122 assertEquals(expected, baos.toString(StandardCharsets.UTF_8));