diff --git a/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufMessageBuilderRandomizer.java b/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufMessageBuilderRandomizer.java new file mode 100644 index 0000000..c2cc919 --- /dev/null +++ b/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufMessageBuilderRandomizer.java @@ -0,0 +1,43 @@ +/* + * Copyright © 2020 Aurélien Mino (aurelien.mino@gmail.com) + * + * Licensed 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 io.github.murdos.easyrandom.protobuf; + +import com.google.protobuf.Message; +import org.jeasy.random.EasyRandom; +import org.jeasy.random.EasyRandomParameters; +import org.jeasy.random.api.Randomizer; + +public class ProtobufMessageBuilderRandomizer implements Randomizer { + + private final ProtobufMessageRandomizer protobufMessageRandomizer; + + public ProtobufMessageBuilderRandomizer(Class messageBuilderClass, EasyRandom easyRandom, EasyRandomParameters parameters) { + this.protobufMessageRandomizer = new ProtobufMessageRandomizer( + retrieveMessageClassFromBuilderClass(messageBuilderClass), + easyRandom, + parameters + ); + } + + private static Class retrieveMessageClassFromBuilderClass(Class messageBuilderClass) { + return (Class) messageBuilderClass.getEnclosingClass(); + } + + @Override + public Message.Builder getRandomValue() { + return protobufMessageRandomizer.getRandomValue().toBuilder(); + } +} diff --git a/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufMessageRandomizer.java b/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufMessageRandomizer.java index 8f5375c..34c5616 100644 --- a/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufMessageRandomizer.java +++ b/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufMessageRandomizer.java @@ -80,9 +80,11 @@ public Message getRandomValue() { Builder builder = defaultInstance.newBuilderForType(); Descriptor descriptor = builder.getDescriptorForType(); List oneofs = descriptor.getOneofs(); - List plainFields = descriptor.getFields().stream().filter(field-> field.getContainingOneof() == null).collect(Collectors.toList()); - for (FieldDescriptor field : plainFields) { - populateField(field, builder); + List plainFields = descriptor.getFields().stream() + .filter(field-> field.getContainingOneof() == null) + .collect(Collectors.toList()); + for (FieldDescriptor fieldDescriptor : plainFields) { + populateField(fieldDescriptor, builder); } for (Descriptors.OneofDescriptor oneofDescriptor : oneofs) { populateOneof(oneofDescriptor, builder); diff --git a/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufRandomizerRegistry.java b/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufRandomizerRegistry.java index 93804d4..6f184a8 100644 --- a/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufRandomizerRegistry.java +++ b/src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufRandomizerRegistry.java @@ -53,6 +53,12 @@ public Randomizer getRandomizer(Class type) { } return new ProtobufMessageRandomizer((Class) type, easyRandom, parameters); } + if (Message.Builder.class.isAssignableFrom(type)) { + if (easyRandom == null) { + easyRandom = new EasyRandom(parameters); + } + return new ProtobufMessageBuilderRandomizer((Class) type, easyRandom, parameters); + } return null; } } diff --git a/src/test/java/io/github/murdos/easyrandom/protobuf/Protobuf2MessageBuilderGenerationTest.java b/src/test/java/io/github/murdos/easyrandom/protobuf/Protobuf2MessageBuilderGenerationTest.java new file mode 100644 index 0000000..69e87d9 --- /dev/null +++ b/src/test/java/io/github/murdos/easyrandom/protobuf/Protobuf2MessageBuilderGenerationTest.java @@ -0,0 +1,76 @@ +/* + * Copyright © 2020 Aurélien Mino (aurelien.mino@gmail.com) + * + * Licensed 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 io.github.murdos.easyrandom.protobuf; + +import com.google.protobuf.StringValue; +import io.github.murdos.easyrandom.protobuf.testing.proto2.Proto2Enum; +import io.github.murdos.easyrandom.protobuf.testing.proto2.Proto2Message; +import org.jeasy.random.EasyRandom; +import org.jeasy.random.EasyRandomParameters; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class Protobuf2MessageBuilderGenerationTest { + + @Test + void shouldGenerateTheSameValueForTheSameSeed() { + EasyRandomParameters parameters = new EasyRandomParameters() + .seed(123L) + .collectionSizeRange(3, 10); + EasyRandom easyRandom = new EasyRandom(parameters); + + Proto2Message.Builder protoBuilderInstance = easyRandom.nextObject(Proto2Message.Builder.class); + + assertThat(protoBuilderInstance.getDoubleField()).isEqualTo(0.7231742029971469); + assertThat(protoBuilderInstance.getFloatField()).isEqualTo(0.99089885f); + assertThat(protoBuilderInstance.getInt32Field()).isEqualTo(1295249578); + assertThat(protoBuilderInstance.getInt64Field()).isEqualTo(4672433029010564658L); + assertThat(protoBuilderInstance.getUint32Field()).isEqualTo(-1680189627); + assertThat(protoBuilderInstance.getUint64Field()).isEqualTo(4775521195821725379L); + assertThat(protoBuilderInstance.getSint32Field()).isEqualTo(-1621910390); + assertThat(protoBuilderInstance.getSint64Field()).isEqualTo(-2298228485105199876L); + assertThat(protoBuilderInstance.getFixed32Field()).isEqualTo(-1219562352); + assertThat(protoBuilderInstance.getFixed64Field()).isEqualTo(2992351518418085755L); + assertThat(protoBuilderInstance.getSfixed32Field()).isEqualTo(-1366603797); + assertThat(protoBuilderInstance.getSfixed64Field()).isEqualTo(-3758321679654915806L); + assertThat(protoBuilderInstance.getBoolField()).isTrue(); + assertThat(protoBuilderInstance.getStringField()).isEqualTo("wSxRIexQAaxVLAiN"); + assertThat(protoBuilderInstance.getBytesField().toByteArray()).containsExactly( + 53,114,79,60,-14,-35,50,97,116,107,41,53,-39,-28,114,79,-111, + 98,-14,-11,-97,102,-22,83,-126,104,-108,-59,-97,93,-122,-67 + ); + + assertThat(protoBuilderInstance.getEnumField()).isEqualTo(Proto2Enum.THIRD_VALUE); + assertThat(protoBuilderInstance.getStringValueField()) + .isNotNull() + .extracting(StringValue::getValue).isEqualTo("tg"); + assertThat(protoBuilderInstance.getRepeatedStringFieldList()).containsExactly( + "AJVH", + "WuGaTPB", + "NuGSIFWDPVPqKClkqNpxLIRO", + "jukCwoSTgRGMwWnAeflhVmclqMX", + "bWyqZZW" + ); + + assertThat(protoBuilderInstance.hasEmbeddedMessage()).isTrue(); + assertThat(protoBuilderInstance.getEmbeddedMessage()).satisfies(embeddedMessage -> { + assertThat(embeddedMessage.getStringField()).isEqualTo("LRHCsQ"); + assertThat(embeddedMessage.getEnumField()).isEqualTo(Proto2Enum.THIRD_VALUE); + }); + assertThat(protoBuilderInstance.getOneofFieldCase().getNumber()).isNotEqualTo(Proto2Message.OneofFieldCase.ONEOFFIELD_NOT_SET); + } +} diff --git a/src/test/java/io/github/murdos/easyrandom/protobuf/Protobuf3MessageBuilderGenerationTest.java b/src/test/java/io/github/murdos/easyrandom/protobuf/Protobuf3MessageBuilderGenerationTest.java new file mode 100644 index 0000000..3c02fec --- /dev/null +++ b/src/test/java/io/github/murdos/easyrandom/protobuf/Protobuf3MessageBuilderGenerationTest.java @@ -0,0 +1,112 @@ +/* + * Copyright © 2020 Aurélien Mino (aurelien.mino@gmail.com) + * + * Licensed 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 io.github.murdos.easyrandom.protobuf; + +import com.google.protobuf.StringValue; +import io.github.murdos.easyrandom.protobuf.testing.proto3.EmbeddedProto3Message; +import io.github.murdos.easyrandom.protobuf.testing.proto3.Proto3Enum; +import io.github.murdos.easyrandom.protobuf.testing.proto3.Proto3Message; +import org.jeasy.random.EasyRandom; +import org.jeasy.random.EasyRandomParameters; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class Protobuf3MessageBuilderGenerationTest { + + @Test + void shouldGenerateTheSameValueForTheSameSeed() { + EasyRandomParameters parameters = new EasyRandomParameters() + .seed(123L) + .collectionSizeRange(3, 10); + EasyRandom easyRandom = new EasyRandom(parameters); + + Proto3Message.Builder protoBuilderInstance = easyRandom.nextObject(Proto3Message.Builder.class); + + assertThat(protoBuilderInstance.getDoubleField()).isEqualTo(0.7231742029971469); + assertThat(protoBuilderInstance.getFloatField()).isEqualTo(0.99089885f); + assertThat(protoBuilderInstance.getInt32Field()).isEqualTo(1295249578); + assertThat(protoBuilderInstance.getInt64Field()).isEqualTo(4672433029010564658L); + assertThat(protoBuilderInstance.getUint32Field()).isEqualTo(-1680189627); + assertThat(protoBuilderInstance.getUint64Field()).isEqualTo(4775521195821725379L); + assertThat(protoBuilderInstance.getSint32Field()).isEqualTo(-1621910390); + assertThat(protoBuilderInstance.getSint64Field()).isEqualTo(-2298228485105199876L); + assertThat(protoBuilderInstance.getFixed32Field()).isEqualTo(-1219562352); + assertThat(protoBuilderInstance.getFixed64Field()).isEqualTo(2992351518418085755L); + assertThat(protoBuilderInstance.getSfixed32Field()).isEqualTo(-1366603797); + assertThat(protoBuilderInstance.getSfixed64Field()).isEqualTo(-3758321679654915806L); + assertThat(protoBuilderInstance.getBoolField()).isTrue(); + assertThat(protoBuilderInstance.getStringField()).isEqualTo("wSxRIexQAaxVLAiN"); + assertThat(protoBuilderInstance.getBytesField().toByteArray()).containsExactly( + 53,114,79,60,-14,-35,50,97,116,107,41,53,-39,-28,114,79,-111, + 98,-14,-11,-97,102,-22,83,-126,104,-108,-59,-97,93,-122,-67 + ); + + assertThat(protoBuilderInstance.getEnumField()).isEqualTo(Proto3Enum.SECOND_VALUE); + assertThat(protoBuilderInstance.getStringValueField()) + .isNotNull() + .extracting(StringValue::getValue).isEqualTo("tg"); + assertThat(protoBuilderInstance.getRepeatedStringFieldList()).containsExactly( + "AJVH", + "WuGaTPB", + "NuGSIFWDPVPqKClkqNpxLIRO", + "jukCwoSTgRGMwWnAeflhVmclqMX", + "bWyqZZW" + ); + + assertThat(protoBuilderInstance.hasEmbeddedMessage()).isTrue(); + assertThat(protoBuilderInstance.getEmbeddedMessage()).satisfies(embeddedMessage -> { + assertThat(embeddedMessage.getStringField()).isEqualTo("LRHCsQ"); + assertThat(embeddedMessage.getEnumField()).isEqualTo(Proto3Enum.UNKNOWN); + }); + assertThat(protoBuilderInstance.getOneofFieldCase()).isEqualTo(Proto3Message.OneofFieldCase.THIRDCHOICE); + } + + @Test + void shouldSequentiallyGenerateDifferentObjects() { + EasyRandomParameters parameters = new EasyRandomParameters() + .seed(123L) + .collectionSizeRange(3, 10); + EasyRandom easyRandom = new EasyRandom(parameters); + + Proto3Message.Builder firstInstance = easyRandom.nextObject(Proto3Message.Builder.class); + Proto3Message.Builder secondInstance = easyRandom.nextObject(Proto3Message.Builder.class); + + assertThat(firstInstance.getDoubleField()).isNotEqualTo(secondInstance.getDoubleField()); + assertThat(firstInstance.getFloatField()).isNotEqualTo(secondInstance.getFloatField()); + assertThat(firstInstance.getInt32Field()).isNotEqualTo(secondInstance.getInt32Field()); + assertThat(firstInstance.getInt64Field()).isNotEqualTo(secondInstance.getInt64Field()); + assertThat(firstInstance.getUint32Field()).isNotEqualTo(secondInstance.getUint32Field()); + assertThat(firstInstance.getUint64Field()).isNotEqualTo(secondInstance.getUint64Field()); + assertThat(firstInstance.getSint32Field()).isNotEqualTo(secondInstance.getSint32Field()); + assertThat(firstInstance.getSint64Field()).isNotEqualTo(secondInstance.getSint64Field()); + assertThat(firstInstance.getFixed32Field()).isNotEqualTo(secondInstance.getFixed32Field()); + assertThat(firstInstance.getFixed64Field()).isNotEqualTo(secondInstance.getFixed64Field()); + assertThat(firstInstance.getSfixed32Field()).isNotEqualTo(secondInstance.getSfixed32Field()); + assertThat(firstInstance.getSfixed64Field()).isNotEqualTo(secondInstance.getSfixed64Field()); + + assertThat(firstInstance.getStringField()).isNotEqualTo(secondInstance.getStringField()); + assertThat(firstInstance.getBytesField()).isNotEqualTo(secondInstance.getBytesField()); + + assertThat(firstInstance.getStringValueField()).isNotEqualTo(secondInstance.getStringValueField()); + assertThat(firstInstance.getRepeatedStringFieldList()).isNotEqualTo(secondInstance.getRepeatedStringFieldList()); + + assertThat(firstInstance.hasEmbeddedMessage()).isTrue(); + assertThat(firstInstance.getEmbeddedMessage()).satisfies(embeddedMessage -> { + assertThat(embeddedMessage.getStringField()).isNotEqualTo(secondInstance.getEmbeddedMessage().getStringField()); + }); + } +} diff --git a/src/test/java/io/github/murdos/easyrandom/protobuf/Protobuf3MessageGenerationTest.java b/src/test/java/io/github/murdos/easyrandom/protobuf/Protobuf3MessageGenerationTest.java index af47cbd..57a8145 100644 --- a/src/test/java/io/github/murdos/easyrandom/protobuf/Protobuf3MessageGenerationTest.java +++ b/src/test/java/io/github/murdos/easyrandom/protobuf/Protobuf3MessageGenerationTest.java @@ -119,7 +119,7 @@ void shouldGenerateTheSameValueForTheSameSeed() { } @Test - void shouldGenerateDifferentObject() { + void shouldSequentiallyGenerateDifferentObjects() { EasyRandomParameters parameters = new EasyRandomParameters() .seed(123L) .collectionSizeRange(3, 10);