|
|
@ -19,20 +19,34 @@ import com.github.os72.protobuf.dynamic.DynamicSchema; |
|
|
import com.google.protobuf.Descriptors; |
|
|
import com.google.protobuf.Descriptors; |
|
|
import com.google.protobuf.DynamicMessage; |
|
|
import com.google.protobuf.DynamicMessage; |
|
|
import com.squareup.wire.schema.internal.parser.ProtoFileElement; |
|
|
import com.squareup.wire.schema.internal.parser.ProtoFileElement; |
|
|
|
|
|
import org.junit.jupiter.api.AfterEach; |
|
|
|
|
|
import org.junit.jupiter.api.BeforeEach; |
|
|
import org.junit.jupiter.api.Test; |
|
|
import org.junit.jupiter.api.Test; |
|
|
import org.junit.jupiter.api.extension.ExtendWith; |
|
|
import org.junit.jupiter.api.parallel.Isolated; |
|
|
import org.mockito.junit.jupiter.MockitoExtension; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Set; |
|
|
import java.util.Set; |
|
|
|
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
|
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
|
|
|
|
import static org.junit.jupiter.api.Assertions.assertFalse; |
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
|
import static org.junit.jupiter.api.Assertions.assertNotNull; |
|
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
|
|
import static org.junit.jupiter.api.Assertions.assertTrue; |
|
|
|
|
|
|
|
|
@ExtendWith(MockitoExtension.class) |
|
|
@Isolated("DynamicProtoUtils static settings being modified") |
|
|
public class DynamicProtoUtilsTest { |
|
|
public class DynamicProtoUtilsTest { |
|
|
|
|
|
|
|
|
|
|
|
@BeforeEach |
|
|
|
|
|
public void before() { |
|
|
|
|
|
// Restore default state before each test
|
|
|
|
|
|
DynamicProtoUtils.setPreserveProtoFieldNames(false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@AfterEach |
|
|
|
|
|
public void after() { |
|
|
|
|
|
// Restore default state after each test
|
|
|
|
|
|
DynamicProtoUtils.setPreserveProtoFieldNames(false); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
public void testProtoSchemaWithMessageNestedTypes() throws Exception { |
|
|
public void testProtoSchemaWithMessageNestedTypes() throws Exception { |
|
|
String schema = "syntax = \"proto3\";\n" + |
|
|
String schema = "syntax = \"proto3\";\n" + |
|
|
@ -166,4 +180,96 @@ public class DynamicProtoUtilsTest { |
|
|
DynamicProtoUtils.dynamicMsgToJson(sampleMsgDescriptor, sampleMsgWithOneOfSubMessage.toByteArray())); |
|
|
DynamicProtoUtils.dynamicMsgToJson(sampleMsgDescriptor, sampleMsgWithOneOfSubMessage.toByteArray())); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
public void testProtoSchemaDefaultBehaviorConvertsToCamelCase() throws Exception { |
|
|
|
|
|
// Explicitly set to false to test default behavior (camelCase conversion)
|
|
|
|
|
|
DynamicProtoUtils.setPreserveProtoFieldNames(false); |
|
|
|
|
|
|
|
|
|
|
|
String schema = "syntax = \"proto3\";\n" + |
|
|
|
|
|
"\n" + |
|
|
|
|
|
"package firmware;\n" + |
|
|
|
|
|
"\n" + |
|
|
|
|
|
"message FirmwareStatus {\n" + |
|
|
|
|
|
" string current_fw_title = 1;\n" + |
|
|
|
|
|
" string current_fw_version = 2;\n" + |
|
|
|
|
|
" string fw_state = 3;\n" + |
|
|
|
|
|
" string target_fw_title = 4;\n" + |
|
|
|
|
|
" string target_fw_version = 5;\n" + |
|
|
|
|
|
"}"; |
|
|
|
|
|
ProtoFileElement protoFileElement = DynamicProtoUtils.getProtoFileElement(schema); |
|
|
|
|
|
DynamicSchema dynamicSchema = DynamicProtoUtils.getDynamicSchema(protoFileElement, "test schema with snake_case fields"); |
|
|
|
|
|
assertNotNull(dynamicSchema); |
|
|
|
|
|
|
|
|
|
|
|
DynamicMessage.Builder firmwareStatusBuilder = dynamicSchema.newMessageBuilder("firmware.FirmwareStatus"); |
|
|
|
|
|
Descriptors.Descriptor firmwareStatusDescriptor = firmwareStatusBuilder.getDescriptorForType(); |
|
|
|
|
|
assertNotNull(firmwareStatusDescriptor); |
|
|
|
|
|
|
|
|
|
|
|
DynamicMessage firmwareStatus = firmwareStatusBuilder |
|
|
|
|
|
.setField(firmwareStatusDescriptor.findFieldByName("current_fw_title"), "firmware_v1") |
|
|
|
|
|
.setField(firmwareStatusDescriptor.findFieldByName("current_fw_version"), "1.0.0") |
|
|
|
|
|
.setField(firmwareStatusDescriptor.findFieldByName("fw_state"), "DOWNLOADING") |
|
|
|
|
|
.setField(firmwareStatusDescriptor.findFieldByName("target_fw_title"), "firmware_v2") |
|
|
|
|
|
.setField(firmwareStatusDescriptor.findFieldByName("target_fw_version"), "2.0.0") |
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
String json = DynamicProtoUtils.dynamicMsgToJson(firmwareStatusDescriptor, firmwareStatus.toByteArray()); |
|
|
|
|
|
|
|
|
|
|
|
// Default behavior: field names converted to camelCase
|
|
|
|
|
|
assertTrue(json.contains("\"currentFwTitle\""), "JSON should contain camelCase field 'currentFwTitle'"); |
|
|
|
|
|
assertTrue(json.contains("\"currentFwVersion\""), "JSON should contain camelCase field 'currentFwVersion'"); |
|
|
|
|
|
assertTrue(json.contains("\"fwState\""), "JSON should contain camelCase field 'fwState'"); |
|
|
|
|
|
assertTrue(json.contains("\"targetFwTitle\""), "JSON should contain camelCase field 'targetFwTitle'"); |
|
|
|
|
|
assertTrue(json.contains("\"targetFwVersion\""), "JSON should contain camelCase field 'targetFwVersion'"); |
|
|
|
|
|
|
|
|
|
|
|
// Verify snake_case versions are NOT present
|
|
|
|
|
|
assertFalse(json.contains("\"current_fw_title\""), "JSON should NOT contain snake_case field 'current_fw_title'"); |
|
|
|
|
|
assertFalse(json.contains("\"fw_state\""), "JSON should NOT contain snake_case field 'fw_state'"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
public void testProtoSchemaPreservesSnakeCaseFieldNamesWhenEnabled() throws Exception { |
|
|
|
|
|
// Explicitly set to true to test preserve behavior (snake_case preservation)
|
|
|
|
|
|
DynamicProtoUtils.setPreserveProtoFieldNames(true); |
|
|
|
|
|
|
|
|
|
|
|
String schema = "syntax = \"proto3\";\n" + |
|
|
|
|
|
"\n" + |
|
|
|
|
|
"package firmware;\n" + |
|
|
|
|
|
"\n" + |
|
|
|
|
|
"message FirmwareStatus {\n" + |
|
|
|
|
|
" string current_fw_title = 1;\n" + |
|
|
|
|
|
" string current_fw_version = 2;\n" + |
|
|
|
|
|
" string fw_state = 3;\n" + |
|
|
|
|
|
" string target_fw_title = 4;\n" + |
|
|
|
|
|
" string target_fw_version = 5;\n" + |
|
|
|
|
|
"}"; |
|
|
|
|
|
ProtoFileElement protoFileElement = DynamicProtoUtils.getProtoFileElement(schema); |
|
|
|
|
|
DynamicSchema dynamicSchema = DynamicProtoUtils.getDynamicSchema(protoFileElement, "test schema with snake_case fields"); |
|
|
|
|
|
assertNotNull(dynamicSchema); |
|
|
|
|
|
|
|
|
|
|
|
DynamicMessage.Builder firmwareStatusBuilder = dynamicSchema.newMessageBuilder("firmware.FirmwareStatus"); |
|
|
|
|
|
Descriptors.Descriptor firmwareStatusDescriptor = firmwareStatusBuilder.getDescriptorForType(); |
|
|
|
|
|
assertNotNull(firmwareStatusDescriptor); |
|
|
|
|
|
|
|
|
|
|
|
DynamicMessage firmwareStatus = firmwareStatusBuilder |
|
|
|
|
|
.setField(firmwareStatusDescriptor.findFieldByName("current_fw_title"), "firmware_v1") |
|
|
|
|
|
.setField(firmwareStatusDescriptor.findFieldByName("current_fw_version"), "1.0.0") |
|
|
|
|
|
.setField(firmwareStatusDescriptor.findFieldByName("fw_state"), "DOWNLOADING") |
|
|
|
|
|
.setField(firmwareStatusDescriptor.findFieldByName("target_fw_title"), "firmware_v2") |
|
|
|
|
|
.setField(firmwareStatusDescriptor.findFieldByName("target_fw_version"), "2.0.0") |
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
String json = DynamicProtoUtils.dynamicMsgToJson(firmwareStatusDescriptor, firmwareStatus.toByteArray()); |
|
|
|
|
|
|
|
|
|
|
|
// When flag is enabled, verify snake_case is preserved
|
|
|
|
|
|
assertTrue(json.contains("\"current_fw_title\""), "JSON should contain snake_case field 'current_fw_title'"); |
|
|
|
|
|
assertTrue(json.contains("\"current_fw_version\""), "JSON should contain snake_case field 'current_fw_version'"); |
|
|
|
|
|
assertTrue(json.contains("\"fw_state\""), "JSON should contain snake_case field 'fw_state'"); |
|
|
|
|
|
assertTrue(json.contains("\"target_fw_title\""), "JSON should contain snake_case field 'target_fw_title'"); |
|
|
|
|
|
assertTrue(json.contains("\"target_fw_version\""), "JSON should contain snake_case field 'target_fw_version'"); |
|
|
|
|
|
|
|
|
|
|
|
// Verify camelCase versions are NOT present
|
|
|
|
|
|
assertFalse(json.contains("\"currentFwTitle\""), "JSON should NOT contain camelCase field 'currentFwTitle'"); |
|
|
|
|
|
assertFalse(json.contains("\"fwState\""), "JSON should NOT contain camelCase field 'fwState'"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|