diff --git a/oap-formats/oap-logstream/oap-logstream/src/main/java/oap/logstream/MemoryLoggerBackend.java b/oap-formats/oap-logstream/oap-logstream-test/src/main/java/oap/logstream/MemoryLoggerBackend.java similarity index 73% rename from oap-formats/oap-logstream/oap-logstream/src/main/java/oap/logstream/MemoryLoggerBackend.java rename to oap-formats/oap-logstream/oap-logstream-test/src/main/java/oap/logstream/MemoryLoggerBackend.java index 2621414c76..1efa9f99eb 100644 --- a/oap-formats/oap-logstream/oap-logstream/src/main/java/oap/logstream/MemoryLoggerBackend.java +++ b/oap-formats/oap-logstream/oap-logstream-test/src/main/java/oap/logstream/MemoryLoggerBackend.java @@ -24,17 +24,14 @@ package oap.logstream; -import com.google.common.base.Preconditions; -import it.unimi.dsi.fastutil.ints.IntArrayList; import lombok.SneakyThrows; import oap.io.Closeables; import oap.logstream.LogStreamProtocol.ProtocolVersion; -import oap.logstream.formats.rowbinary.RowBinaryInputStream; +import oap.logstream.formats.RowBinaryAssertion; import oap.util.LinkedHashMaps; -import org.apache.commons.lang3.ArrayUtils; +import org.testng.Assert; import java.io.BufferedReader; -import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.StringReader; import java.util.ArrayList; @@ -130,41 +127,23 @@ public void reset() { } @SneakyThrows - public List> asRowBinary( Predicate filter, String... headers ) { - List> ret = new ArrayList<>(); + public RowBinaryAssertion assertRowBinary( Predicate filter ) { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + String[] headers = null; + byte[][] types = null; for( LogId id : outputs.keySet() ) { if( filter.test( id ) ) { - IntArrayList rows = new IntArrayList(); - if( headers.length == 0 ) { - for( int i = 0; i < id.headers.length; i++ ) { - rows.add( i ); - } - } else { - for( String header : headers ) { - int index = ArrayUtils.indexOf( id.headers, header ); - - Preconditions.checkArgument( index >= 0, "header " + header + " not found" ); - - rows.add( index ); - } - } - - ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( outputs.getOrDefault( id, new ByteArrayOutputStream() ).toByteArray() ); - RowBinaryInputStream rowBinaryInputStream = new RowBinaryInputStream( byteArrayInputStream, id.headers, id.types ); - - List objects; - while( ( objects = rowBinaryInputStream.readRow() ) != null ) { - ArrayList filtered = new ArrayList<>(); - for( int i : rows ) { - filtered.add( objects.get( i ) ); - } - - ret.add( filtered ); - } + baos.write( outputs.getOrDefault( id, new ByteArrayOutputStream() ).toByteArray() ); + headers = id.headers; + types = id.types; } } - return ret; + if( headers == null ) { + Assert.fail( "No logs found!" ); + } + + return RowBinaryAssertion.assertRowBinary( baos.toByteArray(), headers, types ); } } diff --git a/oap-formats/oap-logstream/oap-logstream-test/src/main/java/oap/logstream/formats/RowBinaryAssertion.java b/oap-formats/oap-logstream/oap-logstream-test/src/main/java/oap/logstream/formats/RowBinaryAssertion.java index 1464db02f6..c6594977eb 100644 --- a/oap-formats/oap-logstream/oap-logstream-test/src/main/java/oap/logstream/formats/RowBinaryAssertion.java +++ b/oap-formats/oap-logstream/oap-logstream-test/src/main/java/oap/logstream/formats/RowBinaryAssertion.java @@ -37,10 +37,18 @@ public static RowBinaryAssertion assertRowBinary( byte[] bytes ) { return assertRowBinary( new ByteArrayInputStream( bytes ) ); } + public static RowBinaryAssertion assertRowBinary( byte[] bytes, String[] headers, byte[][] types ) { + return assertRowBinary( new ByteArrayInputStream( bytes ), headers, types ); + } + public static RowBinaryAssertion assertRowBinary( InputStream inputStream ) { return new RowBinaryAssertion( new RowBinaryData( null, null, inputStream ) ); } + public static RowBinaryAssertion assertRowBinary( InputStream inputStream, String[] headers, byte[][] types ) { + return new RowBinaryAssertion( new RowBinaryData( headers, types, inputStream ) ); + } + public static Row row( Object... cols ) { return new Row( cols ); } diff --git a/oap-formats/oap-logstream/oap-logstream-test/src/test/java/oap/logstream/formats/rowbinary/RowBinaryObjectLoggerTest.java b/oap-formats/oap-logstream/oap-logstream-test/src/test/java/oap/logstream/formats/rowbinary/RowBinaryObjectLoggerTest.java index 81a1738d83..29c0ae8241 100644 --- a/oap-formats/oap-logstream/oap-logstream-test/src/test/java/oap/logstream/formats/rowbinary/RowBinaryObjectLoggerTest.java +++ b/oap-formats/oap-logstream/oap-logstream-test/src/test/java/oap/logstream/formats/rowbinary/RowBinaryObjectLoggerTest.java @@ -99,12 +99,12 @@ public void testLog() { logger.log( new TestData( "ff", "cc", 12, List.of( "1" ), null, Map.of( "map_val_long_as_int", 333L ) ), "prefix", Map.of(), "mylog" ); logger.log( new TestData( null, "dd", 44, null, List.of( "2" ), Map.of( "map_val_long_as_int", 1L ) ), "prefix", Map.of(), "mylog" ); - List> bytes = memoryLoggerBackend.asRowBinary( _ -> true ); - - assertThat( bytes ).isEqualTo( List.of( - List.of( "ff", 12, "ff", List.of( "1" ), 333 ), - List.of( "", 44, "dd", List.of( "2" ), 1 ) - ) ); + memoryLoggerBackend + .assertRowBinary( _ -> true ) + .containsExactlyInAnyOrderEntriesOf( + List.of( "ff", 12, "ff", List.of( "1" ), 333 ), + List.of( "", 44, "dd", List.of( "2" ), 1 ) + ); } @Test @@ -173,14 +173,10 @@ public void testOptimization() { MutableObject headers = new MutableObject<>(); - List> bytes = memoryLoggerBackend.asRowBinary( lid -> { - headers.setValue( lid.headers ); - return true; - } ); - - assertThat( headers.get() ).isEqualTo( new String[] {"a", "or", "b", "a2", "aa2", "b2"} ); - - assertThat( bytes ).isEqualTo( List.of( List.of( "a1", "a1", 1, "a2", "aa2", 2 ) ) ); + memoryLoggerBackend + .assertRowBinary( lid -> true ) + .containOnlyHeaders( "a", "or", "b", "a2", "aa2", "b2" ) + .containsExactlyInAnyOrderEntriesOf( List.of( "a1", "a1", 1, "a2", "aa2", 2 ) ); assertThat( listener.javaCode ) .isEqualTo( "{{ /* model MODEL1 id a path a type STRING defaultValue '' */a ?? \"\" }}" @@ -249,14 +245,10 @@ public void testOptimization2() { MutableObject headers = new MutableObject<>(); - List> bytes = memoryLoggerBackend.asRowBinary( lid -> { - headers.setValue( lid.headers ); - return true; - } ); - - assertThat( headers.get() ).isEqualTo( new String[] {"a", "a1", "a2", "aa2"} ); - - assertThat( bytes ).isEqualTo( List.of( List.of( "a1", "", "a2", "a2xaa2" ) ) ); + memoryLoggerBackend + .assertRowBinary( lid -> true ) + .containOnlyHeaders( "a", "a1", "a2", "aa2" ) + .containsExactlyInAnyOrderEntriesOf( List.of( "a1", "", "a2", "a2xaa2" ) ); assertThat( listener.javaCode ) .isEqualTo( "{{ /* model MODEL1 id a path a type STRING defaultValue '' */a ?? \"\" }}" diff --git a/pom.xml b/pom.xml index 394b2ae613..c16e37aa15 100644 --- a/pom.xml +++ b/pom.xml @@ -57,7 +57,7 @@ - 25.6.10 + 25.6.11 25.0.1 25.0.0