pabloem commented on code in PR #22771: URL: https://github.com/apache/beam/pull/22771#discussion_r966293230
########## sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/utils/JsonUtilsTest.java: ########## @@ -0,0 +1,308 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.schemas.utils; + +import static org.junit.Assert.assertEquals; + +import java.util.Arrays; +import java.util.List; +import java.util.Objects; +import org.apache.beam.sdk.schemas.JavaBeanSchema; +import org.apache.beam.sdk.schemas.annotations.DefaultSchema; +import org.apache.beam.sdk.schemas.annotations.SchemaCreate; +import org.apache.beam.sdk.transforms.SerializableFunction; +import org.apache.beam.sdk.values.Row; +import org.apache.beam.sdk.values.TypeDescriptor; +import org.apache.beam.vendor.grpc.v1p48p1.com.google.gson.Gson; +import org.apache.beam.vendor.grpc.v1p48p1.com.google.gson.JsonObject; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +/** Tests for {@link JsonUtils}. */ +@RunWith(JUnit4.class) +public class JsonUtilsTest { + private static final JavaBeanSchema JAVA_BEAN_SCHEMA = new JavaBeanSchema(); + private static final Gson GSON = new Gson(); + + final List<TestCase<? extends RowEncodable>> testCases = + Arrays.asList( + testCase(Cat.of("Figaro", 12.3, 8, true)), + testCase(NestedCat.of(Cat.of("QuantumCat", -100.123, 999, false))), + testCase( + ArrayOfCats.of( + Arrays.asList( + Cat.of("Chill Cat", 1903.1514, 1, true), + Cat.of("Toasty Cat", 5.9, 4, true), + Cat.of("Hangry Cat", 3.14, 3, true))))); + + @Test + public void testGetJsonBytesToRowFunction() { + for (TestCase<? extends RowEncodable> caze : testCases) { + Row expected = caze.row; + Row actual = JsonUtils.getJsonBytesToRowFunction(expected.getSchema()).apply(caze.jsonBytes); + assertEquals(caze.userT.toString(), expected, actual); + } + } + + @Test + public void testGetJsonStringToRowFunction() { + for (TestCase<? extends RowEncodable> caze : testCases) { + Row expected = caze.row; + Row actual = + JsonUtils.getJsonStringToRowFunction(expected.getSchema()).apply(caze.jsonString); + assertEquals(caze.userT.toString(), expected, actual); + } + } + + @Test + public void testGetRowToJsonBytesFunction() { + for (TestCase<? extends RowEncodable> caze : testCases) { + byte[] expected = caze.jsonBytes; + byte[] actual = JsonUtils.getRowToJsonBytesFunction(caze.row.getSchema()).apply(caze.row); + assertJsonEquals(caze.userT.toString(), expected, actual); + } + } + + @Test + public void testGetRowToJsonStringsFunction() { + for (TestCase<? extends RowEncodable> caze : testCases) { + String expected = caze.jsonString; + String actual = JsonUtils.getRowToJsonStringsFunction(caze.row.getSchema()).apply(caze.row); + assertJsonEquals(caze.userT.toString(), expected, actual); + } + } + + private static <T extends RowEncodable> TestCase<T> testCase(T cat) { + return new TestCase<>(cat); + } + + private static class TestCase<T extends RowEncodable> { + + final String name; + final T userT; + final String jsonString; + final byte[] jsonBytes; + final Row row; + + private TestCase(T userT) { + this.name = userT.toString(); + this.userT = userT; + this.jsonString = GSON.toJson(userT); + this.jsonBytes = JsonUtils.jsonStringToByteArray(jsonString); + this.row = userT.toRow(); + } + } + + @DefaultSchema(JavaBeanSchema.class) + private static class Cat implements RowEncodable { + private static final TypeDescriptor<Cat> TYPE_DESCRIPTOR = TypeDescriptor.of(Cat.class); + private static final SerializableFunction<Cat, Row> TO_ROW_FUNCTION = + JAVA_BEAN_SCHEMA.toRowFunction(TYPE_DESCRIPTOR); + + static Cat of(String name, double weight, int age, boolean spayNeutered) { + return new Cat(name, weight, age, spayNeutered); + } + + private final String name; + private final double weight; + private final int age; + private final boolean spayNeutered; Review Comment: I wonder if it makes sensse to have other types (float, long, DateTime) -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
