Skip to content

Commit

Permalink
Reformat code with prettier-java
Browse files Browse the repository at this point in the history
  • Loading branch information
murdos committed May 16, 2021
1 parent 8ce6c6f commit b629d30
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
package io.github.murdos.easyrandom.protobuf;

import com.google.protobuf.ByteString;
import org.jeasy.random.api.Randomizer;

import java.util.Random;
import org.jeasy.random.api.Randomizer;

/**
* Generate a random Protobuf {@link ByteString}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,17 @@ public class ProtobufMessageBuilderRandomizer implements Randomizer<Message.Buil

private final ProtobufMessageRandomizer protobufMessageRandomizer;

public ProtobufMessageBuilderRandomizer(Class<Message.Builder> messageBuilderClass, EasyRandom easyRandom, EasyRandomParameters parameters) {
this.protobufMessageRandomizer = new 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package io.github.murdos.easyrandom.protobuf;

import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.*;

import com.google.protobuf.Descriptors;
import com.google.protobuf.Descriptors.Descriptor;
import com.google.protobuf.Descriptors.EnumDescriptor;
Expand All @@ -23,27 +25,17 @@
import com.google.protobuf.Descriptors.FieldDescriptor.JavaType;
import com.google.protobuf.Message;
import com.google.protobuf.Message.Builder;
import org.jeasy.random.EasyRandom;
import org.jeasy.random.EasyRandomParameters;
import org.jeasy.random.api.Randomizer;
import org.jeasy.random.randomizers.misc.BooleanRandomizer;
import org.jeasy.random.randomizers.number.DoubleRandomizer;
import org.jeasy.random.randomizers.number.FloatRandomizer;
import org.jeasy.random.randomizers.number.IntegerRandomizer;
import org.jeasy.random.randomizers.number.LongRandomizer;
import org.jeasy.random.randomizers.range.IntegerRangeRandomizer;
import org.jeasy.random.randomizers.text.StringRandomizer;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.EnumMap;
import java.util.List;
import java.util.Random;
import java.util.function.BiFunction;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.*;
import org.jeasy.random.EasyRandom;
import org.jeasy.random.EasyRandomParameters;
import org.jeasy.random.api.Randomizer;
import org.jeasy.random.randomizers.range.IntegerRangeRandomizer;
import org.jeasy.random.randomizers.text.StringRandomizer;

/**
* Generate a random Protobuf {@link Message}.
Expand All @@ -55,7 +47,11 @@ public class ProtobufMessageRandomizer implements Randomizer<Message> {
private final EasyRandom easyRandom;
private final EasyRandomParameters parameters;

public ProtobufMessageRandomizer(Class<Message> messageClass, EasyRandom easyRandom, EasyRandomParameters parameters) {
public ProtobufMessageRandomizer(
Class<Message> messageClass,
EasyRandom easyRandom,
EasyRandomParameters parameters
) {
this.messageClass = messageClass;
this.easyRandom = easyRandom;
this.parameters = parameters;
Expand All @@ -66,12 +62,22 @@ public ProtobufMessageRandomizer(Class<Message> messageClass, EasyRandom easyRan
this.fieldGenerators.put(FLOAT, (field, containingBuilder) -> easyRandom.nextFloat());
this.fieldGenerators.put(DOUBLE, (field, containingBuilder) -> easyRandom.nextDouble());
this.fieldGenerators.put(BOOLEAN, (field, containingBuilder) -> easyRandom.nextBoolean());
this.fieldGenerators.put(STRING, (field, containingBuilder) -> new StringRandomizer(easyRandom.nextLong()).getRandomValue());
this.fieldGenerators.put(BYTE_STRING, (field, containingBuilder) -> new ByteStringRandomizer(easyRandom.nextLong()).getRandomValue());
this.fieldGenerators.put(
STRING,
(field, containingBuilder) -> new StringRandomizer(easyRandom.nextLong()).getRandomValue()
);
this.fieldGenerators.put(
BYTE_STRING,
(field, containingBuilder) -> new ByteStringRandomizer(easyRandom.nextLong()).getRandomValue()
);
this.fieldGenerators.put(ENUM, (field, containingBuilder) -> getRandomEnumValue(field.getEnumType()));
this.fieldGenerators.put(MESSAGE, (field, containingBuilder) -> easyRandom.nextObject(
containingBuilder.newBuilderForField(field).getDefaultInstanceForType().getClass()
));
this.fieldGenerators.put(
MESSAGE,
(field, containingBuilder) ->
easyRandom.nextObject(
containingBuilder.newBuilderForField(field).getDefaultInstanceForType().getClass()
)
);
}

@Override
Expand All @@ -80,9 +86,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());
List<FieldDescriptor> plainFields = descriptor
.getFields()
.stream()
.filter(field -> field.getContainingOneof() == null)
.collect(Collectors.toList());
for (FieldDescriptor fieldDescriptor : plainFields) {
populateField(fieldDescriptor, builder);
}
Expand All @@ -105,9 +113,9 @@ private void populateField(FieldDescriptor field, Builder containingBuilder) {
BiFunction<FieldDescriptor, Builder, Object> generator = this.fieldGenerators.get(field.getJavaType());
if (field.isRepeated()) {
IntegerRangeRandomizer collectionSizeRandomizer = new IntegerRangeRandomizer(
parameters.getCollectionSizeRange().getMin(),
parameters.getCollectionSizeRange().getMax(),
easyRandom.nextLong()
parameters.getCollectionSizeRange().getMin(),
parameters.getCollectionSizeRange().getMax(),
easyRandom.nextLong()
);
for (int i = 0; i < collectionSizeRandomizer.getRandomValue(); i++) {
containingBuilder.addRepeatedField(field, generator.apply(field, containingBuilder));
Expand All @@ -121,7 +129,7 @@ private void populateOneof(Descriptors.OneofDescriptor oneofDescriptor, Builder
int fieldCount = oneofDescriptor.getFieldCount();
int oneofCase = easyRandom.nextInt(fieldCount);
FieldDescriptor selectedCase = oneofDescriptor.getField(oneofCase);
populateField(selectedCase,builder);
populateField(selectedCase, builder);
}

private EnumValueDescriptor getRandomEnumValue(EnumDescriptor enumDescriptor) {
Expand All @@ -133,5 +141,4 @@ private EnumValueDescriptor getRandomEnumValue(EnumDescriptor enumDescriptor) {
public String toString() {
return this.getClass().getSimpleName();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@
package io.github.murdos.easyrandom.protobuf;

import com.google.protobuf.Message;
import java.lang.reflect.Field;
import org.jeasy.random.EasyRandom;
import org.jeasy.random.EasyRandomParameters;
import org.jeasy.random.annotation.Priority;
import org.jeasy.random.api.Randomizer;
import org.jeasy.random.api.RandomizerRegistry;

import java.lang.reflect.Field;

/**
* A registry of randomizers for Protobuf messages.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
*/
package io.github.murdos.easyrandom.protobuf;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.protobuf.ByteString;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class ByteStringRandomizerTest {

private static final long SEED = 123L;
Expand All @@ -32,11 +32,41 @@ void generatedByteStringShouldNotBeNull() {

@Test
void shouldGenerateTheSameValueForTheSameSeed() {
int[] expectedByteArray = {-35, -15, 33, -71, -107, 4, -68, 60, -47, -116, -85, -3, -86, -16, 51, 77, 22, -47,
-41, 64, 50, 38, -6, -110, 69, 87, -38, -101, 58, 15, 70, 66};
int[] expectedByteArray = {
-35,
-15,
33,
-71,
-107,
4,
-68,
60,
-47,
-116,
-85,
-3,
-86,
-16,
51,
77,
22,
-47,
-41,
64,
50,
38,
-6,
-110,
69,
87,
-38,
-101,
58,
15,
70,
66,
};
ByteString generatedValue = new ByteStringRandomizer(SEED).getRandomValue();
assertThat(generatedValue.toByteArray()).containsExactly(expectedByteArray);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@
*/
package io.github.murdos.easyrandom.protobuf;

import static org.assertj.core.api.Assertions.assertThat;

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);
EasyRandomParameters parameters = new EasyRandomParameters().seed(123L).collectionSizeRange(3, 10);
EasyRandom easyRandom = new EasyRandom(parameters);

Proto2Message.Builder protoBuilderInstance = easyRandom.nextObject(Proto2Message.Builder.class);
Expand All @@ -49,28 +47,59 @@ void shouldGenerateTheSameValueForTheSameSeed() {
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.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"
);
.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);
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);
}
}
Loading

0 comments on commit b629d30

Please sign in to comment.