-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generating Message Builder instances
Fixes #1
- Loading branch information
Showing
6 changed files
with
243 additions
and
4 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
src/main/java/io/github/murdos/easyrandom/protobuf/ProtobufMessageBuilderRandomizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...test/java/io/github/murdos/easyrandom/protobuf/Protobuf2MessageBuilderGenerationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
...test/java/io/github/murdos/easyrandom/protobuf/Protobuf3MessageBuilderGenerationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters