Skip to content

Commit

Permalink
Add support for generating Message Builder instances
Browse files Browse the repository at this point in the history
Fixes #1
  • Loading branch information
murdos committed May 16, 2021
1 parent bd5f029 commit 410d29b
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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<Message.Builder> {

private final ProtobufMessageRandomizer protobufMessageRandomizer;

public ProtobufMessageBuilderRandomizer(Class<Message.Builder> messageBuilderClass, EasyRandom easyRandom, EasyRandomParameters parameters) {
this.protobufMessageRandomizer = new ProtobufMessageRandomizer(
retrieveMessageClassFromBuilderClass(messageBuilderClass),
easyRandom,
parameters
);
}

private static Class<Message> retrieveMessageClassFromBuilderClass(Class<Message.Builder> messageBuilderClass) {
return (Class<Message>) messageBuilderClass.getEnclosingClass();
}

@Override
public Message.Builder getRandomValue() {
return protobufMessageRandomizer.getRandomValue().toBuilder();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,11 @@ public Message getRandomValue() {
Builder builder = defaultInstance.newBuilderForType();
Descriptor descriptor = builder.getDescriptorForType();
List<Descriptors.OneofDescriptor> oneofs = descriptor.getOneofs();
List<FieldDescriptor> plainFields = descriptor.getFields().stream().filter(field-> field.getContainingOneof() == null).collect(Collectors.toList());
for (FieldDescriptor field : plainFields) {
populateField(field, builder);
List<FieldDescriptor> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ public Randomizer<?> getRandomizer(Class<?> type) {
}
return new ProtobufMessageRandomizer((Class<Message>) type, easyRandom, parameters);
}
if (Message.Builder.class.isAssignableFrom(type)) {
if (easyRandom == null) {
easyRandom = new EasyRandom(parameters);
}
return new ProtobufMessageBuilderRandomizer((Class<Message.Builder>) type, easyRandom, parameters);
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ void shouldGenerateTheSameValueForTheSameSeed() {
}

@Test
void shouldGenerateDifferentObject() {
void shouldSequentiallyGenerateDifferentObjects() {
EasyRandomParameters parameters = new EasyRandomParameters()
.seed(123L)
.collectionSizeRange(3, 10);
Expand Down

0 comments on commit 410d29b

Please sign in to comment.