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.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;
36 public abstract class AbstractJukeboxTest {
38 public interface FormatMethod {
40 void invoke(@NonNull FormatParameters format, @NonNull OutputStream out) throws IOException;
44 protected static final QName JUKEBOX_QNAME =
45 QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
47 protected static final QName PLAYER_QNAME = QName.create(JUKEBOX_QNAME, "player");
49 protected static final QName LIBRARY_QNAME = QName.create(JUKEBOX_QNAME, "library");
51 protected static final QName SONG_QNAME = QName.create(JUKEBOX_QNAME, "song");
53 protected static final QName SONG_ID_QNAME = QName.create(JUKEBOX_QNAME, "id");
55 protected static final QName SONG_INDEX_QNAME = QName.create(JUKEBOX_QNAME, "index");
57 protected static final QName ARTIST_QNAME = QName.create(JUKEBOX_QNAME, "artist");
59 protected static final QName ALBUM_QNAME = QName.create(JUKEBOX_QNAME, "album");
61 protected static final QName NAME_QNAME = QName.create(JUKEBOX_QNAME, "name");
63 protected static final QName GAP_QNAME = QName.create(JUKEBOX_QNAME, "gap");
65 protected static final QName PLAYLIST_QNAME = QName.create(JUKEBOX_QNAME, "playlist");
67 protected static final QName DESCRIPTION_QNAME = QName.create(JUKEBOX_QNAME, "description");
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);
73 // instance identifier for accessing leaf node "gap"
74 protected static final YangInstanceIdentifier GAP_IID =
75 YangInstanceIdentifier.of(JUKEBOX_QNAME, PLAYER_QNAME, GAP_QNAME);
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))
82 protected static final ContainerNode EMPTY_JUKEBOX = ImmutableNodes.newContainerBuilder()
83 .withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME))
84 .withChild(CONT_PLAYER)
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"))
91 protected static final MapEntryNode SONG1 = ImmutableNodes.newMapEntryBuilder()
92 .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifierWithPredicates.of(SONG_QNAME, SONG_INDEX_QNAME,
94 .withChild(ImmutableNodes.leafNode(SONG_ID_QNAME, "A"))
96 protected static final MapEntryNode SONG2 = ImmutableNodes.newMapEntryBuilder()
97 .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifierWithPredicates.of(SONG_QNAME, SONG_INDEX_QNAME,
99 .withChild(ImmutableNodes.leafNode(SONG_ID_QNAME, "B"))
101 protected static final SystemMapNode PLAYLIST_WITH_SONGS = ImmutableNodes.newSystemMapBuilder()
102 .withNodeIdentifier(new NodeIdentifier(SONG_QNAME))
107 protected static final @NonNull EffectiveModelContext JUKEBOX_SCHEMA =
108 YangParserTestUtils.parseYangResourceDirectory("/jukebox");
109 protected static final @NonNull DatabindContext JUKEBOX_DATABIND = DatabindContext.ofModel(JUKEBOX_SCHEMA);
111 protected static final InputStream stringInputStream(final String str) {
112 return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
115 public static void assertFormat(final String expected, final FormatMethod formatMethod,
116 final boolean prettyPrint) {
117 final var baos = new ByteArrayOutputStream();
119 formatMethod.invoke(new FormatParameters(PrettyPrintParam.of(prettyPrint)), baos);
120 } catch (IOException e) {
121 throw new AssertionError(e);
123 assertEquals(expected, baos.toString(StandardCharsets.UTF_8));