Rework body formatting wiring
[netconf.git] / restconf / restconf-nb / src / test / java / org / opendaylight / restconf / nb / rfc8040 / AbstractJukeboxTest.java
index d0e592ab209752944e16ac72f4675a0605e3cbe3..cd0453594bc6f3180dae146ad2bc5f6620876076 100644 (file)
@@ -7,10 +7,17 @@
  */
 package org.opendaylight.restconf.nb.rfc8040;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
 import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
 import java.io.InputStream;
+import java.io.OutputStream;
 import java.nio.charset.StandardCharsets;
 import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.restconf.api.FormatParameters;
+import org.opendaylight.restconf.api.query.PrettyPrintParam;
 import org.opendaylight.restconf.server.api.DatabindContext;
 import org.opendaylight.yangtools.yang.common.Decimal64;
 import org.opendaylight.yangtools.yang.common.QName;
@@ -27,6 +34,12 @@ import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 public abstract class AbstractJukeboxTest {
+    @FunctionalInterface
+    public interface FormatMethod {
+
+        void invoke(@NonNull FormatParameters format, @NonNull OutputStream out) throws IOException;
+    }
+
     // container jukebox
     protected static final QName JUKEBOX_QNAME =
         QName.create("http://example.com/ns/example-jukebox", "2015-04-04", "jukebox");
@@ -98,4 +111,15 @@ public abstract class AbstractJukeboxTest {
     protected static final InputStream stringInputStream(final String str) {
         return new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
     }
+
+    public static void assertFormat(final String expected, final FormatMethod formatMethod,
+            final boolean prettyPrint) {
+        final var baos = new ByteArrayOutputStream();
+        try {
+            formatMethod.invoke(new FormatParameters(PrettyPrintParam.of(prettyPrint)), baos);
+        } catch (IOException e) {
+            throw new AssertionError(e);
+        }
+        assertEquals(expected, baos.toString(StandardCharsets.UTF_8));
+    }
 }