Integrate RestconfNormalizedNodeWriter
[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.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;
34
35 public abstract class AbstractJukeboxTest {
36     @FunctionalInterface
37     public interface FormatMethod {
38
39         void invoke(@NonNull PrettyPrintParam prettyPrint, @NonNull OutputStream out) throws IOException;
40     }
41
42     // container jukebox
43     protected static final QName JUKEBOX_QNAME =
44         QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
45     // container player
46     protected static final QName PLAYER_QNAME = QName.create(JUKEBOX_QNAME, "player");
47     // container library
48     protected static final QName LIBRARY_QNAME = QName.create(JUKEBOX_QNAME, "library");
49     // list song
50     protected static final QName SONG_QNAME = QName.create(JUKEBOX_QNAME, "song");
51     // leaf id
52     protected static final QName SONG_ID_QNAME = QName.create(JUKEBOX_QNAME, "id");
53     // leaf index
54     protected static final QName SONG_INDEX_QNAME = QName.create(JUKEBOX_QNAME, "index");
55     // list artist
56     protected static final QName ARTIST_QNAME = QName.create(JUKEBOX_QNAME, "artist");
57     // list album
58     protected static final QName ALBUM_QNAME = QName.create(JUKEBOX_QNAME, "album");
59     // leaf name
60     protected static final QName NAME_QNAME = QName.create(JUKEBOX_QNAME, "name");
61     // leaf gap
62     protected static final QName GAP_QNAME = QName.create(JUKEBOX_QNAME, "gap");
63     // leaf playlist
64     protected static final QName PLAYLIST_QNAME = QName.create(JUKEBOX_QNAME, "playlist");
65     // leaf description
66     protected static final QName DESCRIPTION_QNAME = QName.create(JUKEBOX_QNAME, "description");
67
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);
71
72     // instance identifier for accessing leaf node "gap"
73     protected static final YangInstanceIdentifier GAP_IID =
74         YangInstanceIdentifier.of(JUKEBOX_QNAME, PLAYER_QNAME, GAP_QNAME);
75
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))
79         .withChild(GAP_LEAF)
80         .build();
81     protected static final ContainerNode EMPTY_JUKEBOX = ImmutableNodes.newContainerBuilder()
82         .withNodeIdentifier(new NodeIdentifier(JUKEBOX_QNAME))
83         .withChild(CONT_PLAYER)
84         .build();
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"))
89         .build();
90     protected static final MapEntryNode SONG1 = ImmutableNodes.newMapEntryBuilder()
91         .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifierWithPredicates.of(SONG_QNAME, SONG_INDEX_QNAME,
92             Uint32.ONE))
93         .withChild(ImmutableNodes.leafNode(SONG_ID_QNAME, "A"))
94         .build();
95     protected static final MapEntryNode SONG2 = ImmutableNodes.newMapEntryBuilder()
96         .withNodeIdentifier(YangInstanceIdentifier.NodeIdentifierWithPredicates.of(SONG_QNAME, SONG_INDEX_QNAME,
97             Uint32.TWO))
98         .withChild(ImmutableNodes.leafNode(SONG_ID_QNAME, "B"))
99         .build();
100     protected static final SystemMapNode PLAYLIST_WITH_SONGS = ImmutableNodes.newSystemMapBuilder()
101         .withNodeIdentifier(new NodeIdentifier(SONG_QNAME))
102             .withChild(SONG1)
103             .withChild(SONG2)
104         .build();
105
106     protected static final @NonNull EffectiveModelContext JUKEBOX_SCHEMA =
107         YangParserTestUtils.parseYangResourceDirectory("/jukebox");
108     protected static final @NonNull DatabindContext JUKEBOX_DATABIND = DatabindContext.ofModel(JUKEBOX_SCHEMA);
109
110     protected static final InputStream stringInputStream(final String str) {
111         return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
112     }
113
114     public static void assertFormat(final String expected, final FormatMethod formatMethod,
115             final boolean prettyPrint) {
116         final var baos = new ByteArrayOutputStream();
117         try {
118             formatMethod.invoke(PrettyPrintParam.of(prettyPrint), baos);
119         } catch (IOException e) {
120             throw new AssertionError(e);
121         }
122         assertEquals(expected, baos.toString(StandardCharsets.UTF_8));
123     }
124 }