LegendPei commented on code in PR #7760: URL: https://github.com/apache/incubator-seata/pull/7760#discussion_r2558411622
########## json-common/src/test/java/org/apache/seata/common/json/GsonJsonSerializerTest.java: ########## @@ -0,0 +1,241 @@ +/* + * 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.seata.common.json; + +import org.apache.seata.common.exception.JsonParseException; +import org.apache.seata.common.json.impl.GsonJsonSerializer; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class GsonJsonSerializerTest { + + private JsonSerializer jsonSerializer; + + @BeforeEach + void setUp() { + // Use factory to get Gson serializer by name + jsonSerializer = JsonSerializerFactory.getSerializer("gson"); + } + + @Test + public void testToJSONString_basicObject() { + TestObject obj = new TestObject("test", 123); + String json = jsonSerializer.toJSONString(obj); + + assertThat(json).contains("\"name\":\"test\""); + assertThat(json).contains("\"value\":123"); + assertThat(json).doesNotContain("@type"); + } + + @Test + public void testParseObject_basicObject() { + String json = "{\"name\":\"test\",\"value\":123}"; + TestObject obj = jsonSerializer.parseObject(json, TestObject.class); + + assertThat(obj).isNotNull(); + assertThat(obj.getName()).isEqualTo("test"); + assertThat(obj.getValue()).isEqualTo(123); + } + + @Test + public void testToJSONString_and_parseObject() { + TestObject original = new TestObject("school", 456); + String json = jsonSerializer.toJSONString(original); + TestObject restored = jsonSerializer.parseObject(json, TestObject.class); + + assertThat(restored.getName()).isEqualTo(original.getName()); + assertThat(restored.getValue()).isEqualTo(original.getValue()); + } + + @Test + public void testUseAutoType_withType() { + String json = "{\"@type\":\"some.type\",\"name\":\"test\"}"; + boolean hasAutoType = ((GsonJsonSerializer) jsonSerializer).useAutoType(json); + assertThat(hasAutoType).isFalse(); + } + + @Test + public void testUseAutoType_withoutType() { + String json = "{\"name\":\"test\"}"; + boolean hasAutoType = ((GsonJsonSerializer) jsonSerializer).useAutoType(json); + assertThat(hasAutoType).isFalse(); + } + + @Test + public void testToJSONString_prettyPrint() { Review Comment: 'testToJSONString_prettyPrint' is correct because it tests the toJSONString method -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
