This is an automated email from the ASF dual-hosted git repository.
gongchao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/hertzbeat.git
The following commit(s) were added to refs/heads/master by this push:
new 21a778852 [Improve] add ProtoJsonUtil unit test (#2228)
21a778852 is described below
commit 21a77885226aafdf7b4b3afe94704174d93fd8a7
Author: YuLuo <[email protected]>
AuthorDate: Sat Jul 6 23:23:36 2024 +0800
[Improve] add ProtoJsonUtil unit test (#2228)
Signed-off-by: yuluo-yx <[email protected]>
Co-authored-by: tomsun28 <[email protected]>
---
.../hertzbeat/common/util/ProtoJsonUtil.java | 13 +
.../hertzbeat/common/util/ProtoJsonUtilTest.java | 38 +-
.../hertzbeat/common/util/entity/PersonTest.java | 837 +++++++++++++++++++++
.../ProtoJsonUtilTest.java => proto/person.proto} | 25 +-
4 files changed, 894 insertions(+), 19 deletions(-)
diff --git
a/common/src/main/java/org/apache/hertzbeat/common/util/ProtoJsonUtil.java
b/common/src/main/java/org/apache/hertzbeat/common/util/ProtoJsonUtil.java
index a7e71a294..e3d68b625 100644
--- a/common/src/main/java/org/apache/hertzbeat/common/util/ProtoJsonUtil.java
+++ b/common/src/main/java/org/apache/hertzbeat/common/util/ProtoJsonUtil.java
@@ -19,6 +19,7 @@ package org.apache.hertzbeat.common.util;
import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;
+import java.util.Objects;
import lombok.extern.slf4j.Slf4j;
/**
@@ -39,6 +40,12 @@ public final class ProtoJsonUtil {
* @return json
*/
public static String toJsonStr(Message proto) {
+
+ if (Objects.isNull(proto)) {
+ log.error("proto is null");
+ return null;
+ }
+
try {
return PRINTER.print(proto);
} catch (Exception e) {
@@ -54,6 +61,12 @@ public final class ProtoJsonUtil {
* @return protobuf
*/
public static Message toProtobuf(String json, Message.Builder builder) {
+
+ if (Objects.isNull(json) || Objects.isNull(builder)) {
+ log.error("json or builder is null");
+ return null;
+ }
+
try {
PARSER.merge(json, builder);
return builder.build();
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/util/ProtoJsonUtilTest.java
b/common/src/test/java/org/apache/hertzbeat/common/util/ProtoJsonUtilTest.java
index d1c6688e3..394f9bbd9 100644
---
a/common/src/test/java/org/apache/hertzbeat/common/util/ProtoJsonUtilTest.java
+++
b/common/src/test/java/org/apache/hertzbeat/common/util/ProtoJsonUtilTest.java
@@ -17,23 +17,59 @@
package org.apache.hertzbeat.common.util;
+import org.apache.hertzbeat.common.util.entity.PersonTest.Person;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import com.google.protobuf.InvalidProtocolBufferException;
+import com.google.protobuf.util.JsonFormat;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
/**
* Test case for {@link ProtoJsonUtil}
*/
class ProtoJsonUtilTest {
+ private Person samplePerson;
+ private String sampleJson;
+
@BeforeEach
void setUp() {
+
+ samplePerson = Person.newBuilder()
+ .setName("John Doe")
+ .setId(123)
+ .setEmail("[email protected]")
+ .build();
+
+ sampleJson = "{ \"name\": \"John Doe\", \"id\": 123, \"email\":
\"[email protected]\" }";
}
@Test
- void toJsonStr() {
+ void toJsonStr() throws InvalidProtocolBufferException {
+
+ String json = ProtoJsonUtil.toJsonStr(samplePerson);
+ String expectedJson = JsonFormat.printer().print(samplePerson);
+ assertEquals(expectedJson, json);
+
+ json = ProtoJsonUtil.toJsonStr(null);
+ assertNull(json);
}
@Test
void toProtobuf() {
+
+ Person.Builder builder = Person.newBuilder();
+ Person person = (Person) ProtoJsonUtil.toProtobuf(sampleJson, builder);
+ assertEquals(samplePerson, person);
+
+ String invalidJson = "{ \"name\": \"John Doe\", \"id\":
\"not-a-number\" }";
+ builder = Person.newBuilder();
+ person = (Person) ProtoJsonUtil.toProtobuf(invalidJson, builder);
+
+ assertNull(person);
}
+
}
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/util/entity/PersonTest.java
b/common/src/test/java/org/apache/hertzbeat/common/util/entity/PersonTest.java
new file mode 100644
index 000000000..93e6955bf
--- /dev/null
+++
b/common/src/test/java/org/apache/hertzbeat/common/util/entity/PersonTest.java
@@ -0,0 +1,837 @@
+/*
+ * 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.
+ */
+
+// Generated by the protocol buffer compiler. DO NOT EDIT!
+// source: person.proto
+
+package org.apache.hertzbeat.common.util.entity;
+
+@SuppressWarnings("all")
+public final class PersonTest {
+ private PersonTest() {}
+ public static void registerAllExtensions(
+ com.google.protobuf.ExtensionRegistryLite registry) {
+ }
+
+ public static void registerAllExtensions(
+ com.google.protobuf.ExtensionRegistry registry) {
+ registerAllExtensions(
+ (com.google.protobuf.ExtensionRegistryLite) registry);
+ }
+ public interface PersonOrBuilder extends
+ //
@@protoc_insertion_point(interface_extends:org.apache.hertzbeat.common.util.entity.Person)
+ com.google.protobuf.MessageOrBuilder {
+
+ /**
+ * <code>string name = 1;</code>
+ * @return The name.
+ */
+ java.lang.String getName();
+ /**
+ * <code>string name = 1;</code>
+ * @return The bytes for name.
+ */
+ com.google.protobuf.ByteString
+ getNameBytes();
+
+ /**
+ * <code>int32 id = 2;</code>
+ * @return The id.
+ */
+ int getId();
+
+ /**
+ * <code>string email = 3;</code>
+ * @return The email.
+ */
+ java.lang.String getEmail();
+ /**
+ * <code>string email = 3;</code>
+ * @return The bytes for email.
+ */
+ com.google.protobuf.ByteString
+ getEmailBytes();
+ }
+ /**
+ * Protobuf type {@code org.apache.hertzbeat.common.util.entity.Person}
+ */
+ public static final class Person extends
+ com.google.protobuf.GeneratedMessageV3 implements
+ //
@@protoc_insertion_point(message_implements:org.apache.hertzbeat.common.util.entity.Person)
+ PersonOrBuilder {
+ private static final long serialVersionUID = 0L;
+ // Use Person.newBuilder() to construct.
+ private Person(com.google.protobuf.GeneratedMessageV3.Builder<?> builder) {
+ super(builder);
+ }
+ private Person() {
+ name_ = "";
+ email_ = "";
+ }
+
+ @java.lang.Override
+ @SuppressWarnings({"unused"})
+ protected java.lang.Object newInstance(
+ UnusedPrivateParameter unused) {
+ return new Person();
+ }
+
+ @java.lang.Override
+ public final com.google.protobuf.UnknownFieldSet
+ getUnknownFields() {
+ return this.unknownFields;
+ }
+ public static final com.google.protobuf.Descriptors.Descriptor
+ getDescriptor() {
+ return
org.apache.hertzbeat.common.util.entity.PersonTest.internal_static_org_apache_hertzbeat_common_util_entity_Person_descriptor;
+ }
+
+ @java.lang.Override
+ protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
+ internalGetFieldAccessorTable() {
+ return
org.apache.hertzbeat.common.util.entity.PersonTest.internal_static_org_apache_hertzbeat_common_util_entity_Person_fieldAccessorTable
+ .ensureFieldAccessorsInitialized(
+ org.apache.hertzbeat.common.util.entity.PersonTest.Person.class,
org.apache.hertzbeat.common.util.entity.PersonTest.Person.Builder.class);
+ }
+
+ public static final int NAME_FIELD_NUMBER = 1;
+ private volatile java.lang.Object name_;
+ /**
+ * <code>string name = 1;</code>
+ * @return The name.
+ */
+ @java.lang.Override
+ public java.lang.String getName() {
+ java.lang.Object ref = name_;
+ if (ref instanceof java.lang.String) {
+ return (java.lang.String) ref;
+ } else {
+ com.google.protobuf.ByteString bs =
+ (com.google.protobuf.ByteString) ref;
+ java.lang.String s = bs.toStringUtf8();
+ name_ = s;
+ return s;
+ }
+ }
+ /**
+ * <code>string name = 1;</code>
+ * @return The bytes for name.
+ */
+ @java.lang.Override
+ public com.google.protobuf.ByteString
+ getNameBytes() {
+ java.lang.Object ref = name_;
+ if (ref instanceof java.lang.String) {
+ com.google.protobuf.ByteString b =
+ com.google.protobuf.ByteString.copyFromUtf8(
+ (java.lang.String) ref);
+ name_ = b;
+ return b;
+ } else {
+ return (com.google.protobuf.ByteString) ref;
+ }
+ }
+
+ public static final int ID_FIELD_NUMBER = 2;
+ private int id_;
+ /**
+ * <code>int32 id = 2;</code>
+ * @return The id.
+ */
+ @java.lang.Override
+ public int getId() {
+ return id_;
+ }
+
+ public static final int EMAIL_FIELD_NUMBER = 3;
+ private volatile java.lang.Object email_;
+ /**
+ * <code>string email = 3;</code>
+ * @return The email.
+ */
+ @java.lang.Override
+ public java.lang.String getEmail() {
+ java.lang.Object ref = email_;
+ if (ref instanceof java.lang.String) {
+ return (java.lang.String) ref;
+ } else {
+ com.google.protobuf.ByteString bs =
+ (com.google.protobuf.ByteString) ref;
+ java.lang.String s = bs.toStringUtf8();
+ email_ = s;
+ return s;
+ }
+ }
+ /**
+ * <code>string email = 3;</code>
+ * @return The bytes for email.
+ */
+ @java.lang.Override
+ public com.google.protobuf.ByteString
+ getEmailBytes() {
+ java.lang.Object ref = email_;
+ if (ref instanceof java.lang.String) {
+ com.google.protobuf.ByteString b =
+ com.google.protobuf.ByteString.copyFromUtf8(
+ (java.lang.String) ref);
+ email_ = b;
+ return b;
+ } else {
+ return (com.google.protobuf.ByteString) ref;
+ }
+ }
+
+ private byte memoizedIsInitialized = -1;
+ @java.lang.Override
+ public final boolean isInitialized() {
+ byte isInitialized = memoizedIsInitialized;
+ if (isInitialized == 1) return true;
+ if (isInitialized == 0) return false;
+
+ memoizedIsInitialized = 1;
+ return true;
+ }
+
+ @java.lang.Override
+ public void writeTo(com.google.protobuf.CodedOutputStream output)
+ throws java.io.IOException {
+ if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) {
+ com.google.protobuf.GeneratedMessageV3.writeString(output, 1, name_);
+ }
+ if (id_ != 0) {
+ output.writeInt32(2, id_);
+ }
+ if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(email_)) {
+ com.google.protobuf.GeneratedMessageV3.writeString(output, 3, email_);
+ }
+ getUnknownFields().writeTo(output);
+ }
+
+ @java.lang.Override
+ public int getSerializedSize() {
+ int size = memoizedSize;
+ if (size != -1) return size;
+
+ size = 0;
+ if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(name_)) {
+ size += com.google.protobuf.GeneratedMessageV3.computeStringSize(1,
name_);
+ }
+ if (id_ != 0) {
+ size += com.google.protobuf.CodedOutputStream
+ .computeInt32Size(2, id_);
+ }
+ if (!com.google.protobuf.GeneratedMessageV3.isStringEmpty(email_)) {
+ size += com.google.protobuf.GeneratedMessageV3.computeStringSize(3,
email_);
+ }
+ size += getUnknownFields().getSerializedSize();
+ memoizedSize = size;
+ return size;
+ }
+
+ @java.lang.Override
+ public boolean equals(final java.lang.Object obj) {
+ if (obj == this) {
+ return true;
+ }
+ if (!(obj instanceof
org.apache.hertzbeat.common.util.entity.PersonTest.Person)) {
+ return super.equals(obj);
+ }
+ org.apache.hertzbeat.common.util.entity.PersonTest.Person other =
(org.apache.hertzbeat.common.util.entity.PersonTest.Person) obj;
+
+ if (!getName()
+ .equals(other.getName())) return false;
+ if (getId()
+ != other.getId()) return false;
+ if (!getEmail()
+ .equals(other.getEmail())) return false;
+ if (!getUnknownFields().equals(other.getUnknownFields())) return false;
+ return true;
+ }
+
+ @java.lang.Override
+ public int hashCode() {
+ if (memoizedHashCode != 0) {
+ return memoizedHashCode;
+ }
+ int hash = 41;
+ hash = (19 * hash) + getDescriptor().hashCode();
+ hash = (37 * hash) + NAME_FIELD_NUMBER;
+ hash = (53 * hash) + getName().hashCode();
+ hash = (37 * hash) + ID_FIELD_NUMBER;
+ hash = (53 * hash) + getId();
+ hash = (37 * hash) + EMAIL_FIELD_NUMBER;
+ hash = (53 * hash) + getEmail().hashCode();
+ hash = (29 * hash) + getUnknownFields().hashCode();
+ memoizedHashCode = hash;
+ return hash;
+ }
+
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseFrom(
+ java.nio.ByteBuffer data)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseFrom(
+ java.nio.ByteBuffer data,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data, extensionRegistry);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseFrom(
+ com.google.protobuf.ByteString data)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseFrom(
+ com.google.protobuf.ByteString data,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data, extensionRegistry);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseFrom(byte[] data)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseFrom(
+ byte[] data,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ return PARSER.parseFrom(data, extensionRegistry);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseFrom(java.io.InputStream input)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseWithIOException(PARSER, input);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseFrom(
+ java.io.InputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseWithIOException(PARSER, input, extensionRegistry);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseDelimitedFrom(java.io.InputStream input)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseDelimitedWithIOException(PARSER, input);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseDelimitedFrom(
+ java.io.InputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseDelimitedWithIOException(PARSER, input, extensionRegistry);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseFrom(
+ com.google.protobuf.CodedInputStream input)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseWithIOException(PARSER, input);
+ }
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
parseFrom(
+ com.google.protobuf.CodedInputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws java.io.IOException {
+ return com.google.protobuf.GeneratedMessageV3
+ .parseWithIOException(PARSER, input, extensionRegistry);
+ }
+
+ @java.lang.Override
+ public Builder newBuilderForType() { return newBuilder(); }
+ public static Builder newBuilder() {
+ return DEFAULT_INSTANCE.toBuilder();
+ }
+ public static Builder
newBuilder(org.apache.hertzbeat.common.util.entity.PersonTest.Person prototype)
{
+ return DEFAULT_INSTANCE.toBuilder().mergeFrom(prototype);
+ }
+ @java.lang.Override
+ public Builder toBuilder() {
+ return this == DEFAULT_INSTANCE
+ ? new Builder() : new Builder().mergeFrom(this);
+ }
+
+ @java.lang.Override
+ protected Builder newBuilderForType(
+ com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
+ Builder builder = new Builder(parent);
+ return builder;
+ }
+ /**
+ * Protobuf type {@code org.apache.hertzbeat.common.util.entity.Person}
+ */
+ public static final class Builder extends
+ com.google.protobuf.GeneratedMessageV3.Builder<Builder> implements
+ //
@@protoc_insertion_point(builder_implements:org.apache.hertzbeat.common.util.entity.Person)
+ org.apache.hertzbeat.common.util.entity.PersonTest.PersonOrBuilder {
+ public static final com.google.protobuf.Descriptors.Descriptor
+ getDescriptor() {
+ return
org.apache.hertzbeat.common.util.entity.PersonTest.internal_static_org_apache_hertzbeat_common_util_entity_Person_descriptor;
+ }
+
+ @java.lang.Override
+ protected com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
+ internalGetFieldAccessorTable() {
+ return
org.apache.hertzbeat.common.util.entity.PersonTest.internal_static_org_apache_hertzbeat_common_util_entity_Person_fieldAccessorTable
+ .ensureFieldAccessorsInitialized(
+
org.apache.hertzbeat.common.util.entity.PersonTest.Person.class,
org.apache.hertzbeat.common.util.entity.PersonTest.Person.Builder.class);
+ }
+
+ // Construct using
org.apache.hertzbeat.common.util.entity.PersonTest.Person.newBuilder()
+ private Builder() {
+
+ }
+
+ private Builder(
+ com.google.protobuf.GeneratedMessageV3.BuilderParent parent) {
+ super(parent);
+
+ }
+ @java.lang.Override
+ public Builder clear() {
+ super.clear();
+ name_ = "";
+
+ id_ = 0;
+
+ email_ = "";
+
+ return this;
+ }
+
+ @java.lang.Override
+ public com.google.protobuf.Descriptors.Descriptor
+ getDescriptorForType() {
+ return
org.apache.hertzbeat.common.util.entity.PersonTest.internal_static_org_apache_hertzbeat_common_util_entity_Person_descriptor;
+ }
+
+ @java.lang.Override
+ public org.apache.hertzbeat.common.util.entity.PersonTest.Person
getDefaultInstanceForType() {
+ return
org.apache.hertzbeat.common.util.entity.PersonTest.Person.getDefaultInstance();
+ }
+
+ @java.lang.Override
+ public org.apache.hertzbeat.common.util.entity.PersonTest.Person build()
{
+ org.apache.hertzbeat.common.util.entity.PersonTest.Person result =
buildPartial();
+ if (!result.isInitialized()) {
+ throw newUninitializedMessageException(result);
+ }
+ return result;
+ }
+
+ @java.lang.Override
+ public org.apache.hertzbeat.common.util.entity.PersonTest.Person
buildPartial() {
+ org.apache.hertzbeat.common.util.entity.PersonTest.Person result = new
org.apache.hertzbeat.common.util.entity.PersonTest.Person(this);
+ result.name_ = name_;
+ result.id_ = id_;
+ result.email_ = email_;
+ onBuilt();
+ return result;
+ }
+
+ @java.lang.Override
+ public Builder clone() {
+ return super.clone();
+ }
+ @java.lang.Override
+ public Builder setField(
+ com.google.protobuf.Descriptors.FieldDescriptor field,
+ java.lang.Object value) {
+ return super.setField(field, value);
+ }
+ @java.lang.Override
+ public Builder clearField(
+ com.google.protobuf.Descriptors.FieldDescriptor field) {
+ return super.clearField(field);
+ }
+ @java.lang.Override
+ public Builder clearOneof(
+ com.google.protobuf.Descriptors.OneofDescriptor oneof) {
+ return super.clearOneof(oneof);
+ }
+ @java.lang.Override
+ public Builder setRepeatedField(
+ com.google.protobuf.Descriptors.FieldDescriptor field,
+ int index, java.lang.Object value) {
+ return super.setRepeatedField(field, index, value);
+ }
+ @java.lang.Override
+ public Builder addRepeatedField(
+ com.google.protobuf.Descriptors.FieldDescriptor field,
+ java.lang.Object value) {
+ return super.addRepeatedField(field, value);
+ }
+ @java.lang.Override
+ public Builder mergeFrom(com.google.protobuf.Message other) {
+ if (other instanceof
org.apache.hertzbeat.common.util.entity.PersonTest.Person) {
+ return
mergeFrom((org.apache.hertzbeat.common.util.entity.PersonTest.Person)other);
+ } else {
+ super.mergeFrom(other);
+ return this;
+ }
+ }
+
+ public Builder
mergeFrom(org.apache.hertzbeat.common.util.entity.PersonTest.Person other) {
+ if (other ==
org.apache.hertzbeat.common.util.entity.PersonTest.Person.getDefaultInstance())
return this;
+ if (!other.getName().isEmpty()) {
+ name_ = other.name_;
+ onChanged();
+ }
+ if (other.getId() != 0) {
+ setId(other.getId());
+ }
+ if (!other.getEmail().isEmpty()) {
+ email_ = other.email_;
+ onChanged();
+ }
+ this.mergeUnknownFields(other.getUnknownFields());
+ onChanged();
+ return this;
+ }
+
+ @java.lang.Override
+ public final boolean isInitialized() {
+ return true;
+ }
+
+ @java.lang.Override
+ public Builder mergeFrom(
+ com.google.protobuf.CodedInputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws java.io.IOException {
+ if (extensionRegistry == null) {
+ throw new java.lang.NullPointerException();
+ }
+ try {
+ boolean done = false;
+ while (!done) {
+ int tag = input.readTag();
+ switch (tag) {
+ case 0:
+ done = true;
+ break;
+ case 10: {
+ name_ = input.readStringRequireUtf8();
+
+ break;
+ } // case 10
+ case 16: {
+ id_ = input.readInt32();
+
+ break;
+ } // case 16
+ case 26: {
+ email_ = input.readStringRequireUtf8();
+
+ break;
+ } // case 26
+ default: {
+ if (!super.parseUnknownField(input, extensionRegistry, tag)) {
+ done = true; // was an endgroup tag
+ }
+ break;
+ } // default:
+ } // switch (tag)
+ } // while (!done)
+ } catch (com.google.protobuf.InvalidProtocolBufferException e) {
+ throw e.unwrapIOException();
+ } finally {
+ onChanged();
+ } // finally
+ return this;
+ }
+
+ private java.lang.Object name_ = "";
+ /**
+ * <code>string name = 1;</code>
+ * @return The name.
+ */
+ public java.lang.String getName() {
+ java.lang.Object ref = name_;
+ if (!(ref instanceof java.lang.String)) {
+ com.google.protobuf.ByteString bs =
+ (com.google.protobuf.ByteString) ref;
+ java.lang.String s = bs.toStringUtf8();
+ name_ = s;
+ return s;
+ } else {
+ return (java.lang.String) ref;
+ }
+ }
+ /**
+ * <code>string name = 1;</code>
+ * @return The bytes for name.
+ */
+ public com.google.protobuf.ByteString
+ getNameBytes() {
+ java.lang.Object ref = name_;
+ if (ref instanceof String) {
+ com.google.protobuf.ByteString b =
+ com.google.protobuf.ByteString.copyFromUtf8(
+ (java.lang.String) ref);
+ name_ = b;
+ return b;
+ } else {
+ return (com.google.protobuf.ByteString) ref;
+ }
+ }
+ /**
+ * <code>string name = 1;</code>
+ * @param value The name to set.
+ * @return This builder for chaining.
+ */
+ public Builder setName(
+ java.lang.String value) {
+ if (value == null) {
+ throw new NullPointerException();
+ }
+
+ name_ = value;
+ onChanged();
+ return this;
+ }
+ /**
+ * <code>string name = 1;</code>
+ * @return This builder for chaining.
+ */
+ public Builder clearName() {
+
+ name_ = getDefaultInstance().getName();
+ onChanged();
+ return this;
+ }
+ /**
+ * <code>string name = 1;</code>
+ * @param value The bytes for name to set.
+ * @return This builder for chaining.
+ */
+ public Builder setNameBytes(
+ com.google.protobuf.ByteString value) {
+ if (value == null) {
+ throw new NullPointerException();
+ }
+ checkByteStringIsUtf8(value);
+
+ name_ = value;
+ onChanged();
+ return this;
+ }
+
+ private int id_ ;
+ /**
+ * <code>int32 id = 2;</code>
+ * @return The id.
+ */
+ @java.lang.Override
+ public int getId() {
+ return id_;
+ }
+ /**
+ * <code>int32 id = 2;</code>
+ * @param value The id to set.
+ * @return This builder for chaining.
+ */
+ public Builder setId(int value) {
+
+ id_ = value;
+ onChanged();
+ return this;
+ }
+ /**
+ * <code>int32 id = 2;</code>
+ * @return This builder for chaining.
+ */
+ public Builder clearId() {
+
+ id_ = 0;
+ onChanged();
+ return this;
+ }
+
+ private java.lang.Object email_ = "";
+ /**
+ * <code>string email = 3;</code>
+ * @return The email.
+ */
+ public java.lang.String getEmail() {
+ java.lang.Object ref = email_;
+ if (!(ref instanceof java.lang.String)) {
+ com.google.protobuf.ByteString bs =
+ (com.google.protobuf.ByteString) ref;
+ java.lang.String s = bs.toStringUtf8();
+ email_ = s;
+ return s;
+ } else {
+ return (java.lang.String) ref;
+ }
+ }
+ /**
+ * <code>string email = 3;</code>
+ * @return The bytes for email.
+ */
+ public com.google.protobuf.ByteString
+ getEmailBytes() {
+ java.lang.Object ref = email_;
+ if (ref instanceof String) {
+ com.google.protobuf.ByteString b =
+ com.google.protobuf.ByteString.copyFromUtf8(
+ (java.lang.String) ref);
+ email_ = b;
+ return b;
+ } else {
+ return (com.google.protobuf.ByteString) ref;
+ }
+ }
+ /**
+ * <code>string email = 3;</code>
+ * @param value The email to set.
+ * @return This builder for chaining.
+ */
+ public Builder setEmail(
+ java.lang.String value) {
+ if (value == null) {
+ throw new NullPointerException();
+ }
+
+ email_ = value;
+ onChanged();
+ return this;
+ }
+ /**
+ * <code>string email = 3;</code>
+ * @return This builder for chaining.
+ */
+ public Builder clearEmail() {
+
+ email_ = getDefaultInstance().getEmail();
+ onChanged();
+ return this;
+ }
+ /**
+ * <code>string email = 3;</code>
+ * @param value The bytes for email to set.
+ * @return This builder for chaining.
+ */
+ public Builder setEmailBytes(
+ com.google.protobuf.ByteString value) {
+ if (value == null) {
+ throw new NullPointerException();
+ }
+ checkByteStringIsUtf8(value);
+
+ email_ = value;
+ onChanged();
+ return this;
+ }
+ @java.lang.Override
+ public final Builder setUnknownFields(
+ final com.google.protobuf.UnknownFieldSet unknownFields) {
+ return super.setUnknownFields(unknownFields);
+ }
+
+ @java.lang.Override
+ public final Builder mergeUnknownFields(
+ final com.google.protobuf.UnknownFieldSet unknownFields) {
+ return super.mergeUnknownFields(unknownFields);
+ }
+
+
+ //
@@protoc_insertion_point(builder_scope:org.apache.hertzbeat.common.util.entity.Person)
+ }
+
+ //
@@protoc_insertion_point(class_scope:org.apache.hertzbeat.common.util.entity.Person)
+ private static final
org.apache.hertzbeat.common.util.entity.PersonTest.Person DEFAULT_INSTANCE;
+ static {
+ DEFAULT_INSTANCE = new
org.apache.hertzbeat.common.util.entity.PersonTest.Person();
+ }
+
+ public static org.apache.hertzbeat.common.util.entity.PersonTest.Person
getDefaultInstance() {
+ return DEFAULT_INSTANCE;
+ }
+
+ private static final com.google.protobuf.Parser<Person>
+ PARSER = new com.google.protobuf.AbstractParser<Person>() {
+ @java.lang.Override
+ public Person parsePartialFrom(
+ com.google.protobuf.CodedInputStream input,
+ com.google.protobuf.ExtensionRegistryLite extensionRegistry)
+ throws com.google.protobuf.InvalidProtocolBufferException {
+ Builder builder = newBuilder();
+ try {
+ builder.mergeFrom(input, extensionRegistry);
+ } catch (com.google.protobuf.InvalidProtocolBufferException e) {
+ throw e.setUnfinishedMessage(builder.buildPartial());
+ } catch (com.google.protobuf.UninitializedMessageException e) {
+ throw
e.asInvalidProtocolBufferException().setUnfinishedMessage(builder.buildPartial());
+ } catch (java.io.IOException e) {
+ throw new com.google.protobuf.InvalidProtocolBufferException(e)
+ .setUnfinishedMessage(builder.buildPartial());
+ }
+ return builder.buildPartial();
+ }
+ };
+
+ public static com.google.protobuf.Parser<Person> parser() {
+ return PARSER;
+ }
+
+ @java.lang.Override
+ public com.google.protobuf.Parser<Person> getParserForType() {
+ return PARSER;
+ }
+
+ @java.lang.Override
+ public org.apache.hertzbeat.common.util.entity.PersonTest.Person
getDefaultInstanceForType() {
+ return DEFAULT_INSTANCE;
+ }
+
+ }
+
+ private static final com.google.protobuf.Descriptors.Descriptor
+ internal_static_org_apache_hertzbeat_common_util_entity_Person_descriptor;
+ private static final
+ com.google.protobuf.GeneratedMessageV3.FieldAccessorTable
+
internal_static_org_apache_hertzbeat_common_util_entity_Person_fieldAccessorTable;
+
+ public static com.google.protobuf.Descriptors.FileDescriptor
+ getDescriptor() {
+ return descriptor;
+ }
+ private static com.google.protobuf.Descriptors.FileDescriptor
+ descriptor;
+ static {
+ java.lang.String[] descriptorData = {
+ "\n\014person.proto\022\'org.apache.hertzbeat.com" +
+ "mon.util.entity\"1\n\006Person\022\014\n\004name\030\001 \001(\t\022" +
+ "\n\n\002id\030\002 \001(\005\022\r\n\005email\030\003
\001(\tB\014B\nPersonTest" +
+ "b\006proto3"
+ };
+ descriptor = com.google.protobuf.Descriptors.FileDescriptor
+ .internalBuildGeneratedFileFrom(descriptorData,
+ new com.google.protobuf.Descriptors.FileDescriptor[] {
+ });
+ internal_static_org_apache_hertzbeat_common_util_entity_Person_descriptor =
+ getDescriptor().getMessageTypes().get(0);
+
internal_static_org_apache_hertzbeat_common_util_entity_Person_fieldAccessorTable
= new
+ com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
+
internal_static_org_apache_hertzbeat_common_util_entity_Person_descriptor,
+ new java.lang.String[] { "Name", "Id", "Email", });
+ }
+
+ // @@protoc_insertion_point(outer_class_scope)
+}
diff --git
a/common/src/test/java/org/apache/hertzbeat/common/util/ProtoJsonUtilTest.java
b/common/src/test/proto/person.proto
similarity index 72%
copy from
common/src/test/java/org/apache/hertzbeat/common/util/ProtoJsonUtilTest.java
copy to common/src/test/proto/person.proto
index d1c6688e3..6335e76a5 100644
---
a/common/src/test/java/org/apache/hertzbeat/common/util/ProtoJsonUtilTest.java
+++ b/common/src/test/proto/person.proto
@@ -15,25 +15,14 @@
* limitations under the License.
*/
-package org.apache.hertzbeat.common.util;
+syntax = "proto3";
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
+package org.apache.hertzbeat.common.util.entity;
-/**
- * Test case for {@link ProtoJsonUtil}
- */
-class ProtoJsonUtilTest {
-
- @BeforeEach
- void setUp() {
- }
-
- @Test
- void toJsonStr() {
- }
+option java_outer_classname = "PersonTest";
- @Test
- void toProtobuf() {
- }
+message Person {
+ string name = 1;
+ int32 id = 2;
+ string email = 3;
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]