Skip to content

Commit

Permalink
Merge pull request #25 from ocadotechnology/test-arranger-19
Browse files Browse the repository at this point in the history
Test arranger 19
  • Loading branch information
mjureczko authored Nov 5, 2021
2 parents 1c1f530 + 916e8de commit dcd732e
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 43 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.ocadotechnology.gembus</groupId>
<artifactId>test-arranger</artifactId>
<version>1.3</version>
<version>1.4-SNAPSHOT</version>
<packaging>jar</packaging>
<name>test-arranger</name>
<description>A tool for arranging test data with pseudo-random values.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ static EasyRandomParameters getEasyRandomDefaultParameters() {
.objectPoolSize(calculateObjectPoolSize())
.objectFactory(new DecoratedObjectFactory(PropertiesWrapper.getCacheEnable()))
.stringLengthRange(STRING_MIN_LENGTH, STRING_MAX_LENGTH)
.seed(calculateSeed());
.seed(SeedHelper.calculateSeed());
}

static EasyRandomParameters getEasyRandomSimplifiedParameters() {
Expand All @@ -62,15 +62,7 @@ static EasyRandomParameters getEasyRandomSimplifiedParameters() {
.objectPoolSize(calculateObjectPoolSize())
.objectFactory(new DecoratedObjectFactory(PropertiesWrapper.getCacheEnable()))
.stringLengthRange(5, 10)
.seed(calculateSeed());
}

static long calculateSeed() {
long seed = EasyRandomParameters.DEFAULT_SEED;
if (PropertiesWrapper.getRandomSeedEnabled()) {
seed = System.nanoTime();
}
return seed;
.seed(SeedHelper.calculateSeed());
}

EnhancedRandom defaultRandom() {
Expand All @@ -85,7 +77,7 @@ EnhancedRandom randomForGivenConfiguration(Class<?> type, Map<Class<?>, CustomAr
EnhancedRandom.Builder randomBuilder = new EnhancedRandom.Builder(parametersSupplier);
CustomArranger<?> arrangerToUpdate = arrangers.get(type);
if (arrangerToUpdate == null) {
return randomBuilder.build(arrangers, calculateSeed());
return randomBuilder.build(arrangers, SeedHelper.calculateSeed());
} else {
return randomWithoutSelfReferenceThroughArranger(arrangers, randomBuilder, type);
}
Expand All @@ -104,16 +96,12 @@ private EnhancedRandom randomWithArrangers(Map<Class<?>, CustomArranger<?>> arra
EnhancedRandom random = randomWithoutSelfReferenceThroughArranger(arrangers, randomBuilder, clazz);
customArranger.setEnhancedRandom(random);
});
return randomBuilder.build(arrangers, calculateSeed());
return randomBuilder.build(arrangers, SeedHelper.calculateSeed());
}

private EnhancedRandom randomWithoutSelfReferenceThroughArranger(Map<Class<?>, CustomArranger<?>> arrangers, EnhancedRandom.Builder enhancedRandomBuilder, Class<?> type) {
final HashMap<Class<?>, CustomArranger<?>> forCustomArranger = new HashMap<>(arrangers);
forCustomArranger.remove(type);
return enhancedRandomBuilder.build(forCustomArranger, customArrangerTypeSpecificSeedRespectingRandomSeedSetting(type));
}

private long customArrangerTypeSpecificSeedRespectingRandomSeedSetting(Class<?> type) {
return calculateSeed() + type.getName().hashCode();
return enhancedRandomBuilder.build(forCustomArranger, SeedHelper.customArrangerTypeSpecificSeedRespectingRandomSeedSetting(type));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
*/
public abstract class CustomArranger<T> {

protected EnhancedRandom enhancedRandom = new EnhancedRandom.Builder(ArrangersConfigurer::getEasyRandomDefaultParameters).build(new HashMap<>(), ArrangersConfigurer.calculateSeed());
protected EnhancedRandom enhancedRandom = new EnhancedRandom.Builder(ArrangersConfigurer::getEasyRandomDefaultParameters).build(new HashMap<>(), SeedHelper.calculateSeed());
protected final Class<T> type = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright © 2020 Ocado (marian.jureczko@ocado.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 com.ocadotechnology.gembus.test;

import org.jeasy.random.EasyRandomParameters;
import org.jeasy.random.annotation.Priority;
import org.jeasy.random.api.Randomizer;
import org.jeasy.random.api.RandomizerRegistry;
import org.jeasy.random.randomizers.misc.BooleanRandomizer;
import org.jeasy.random.randomizers.number.*;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

/**
* The priority should be lower than the BeanValidationRandomizerRegistry one but greater than the InternalRandomizerRegistry one
*/
@Priority(-3)
public class CustomArrangerRandomizerRegistry implements RandomizerRegistry {
private final long seed;
private final Map<Class<?>, Randomizer<?>> randomizers = new HashMap<>();

public CustomArrangerRandomizerRegistry(long seed) {
this.seed = seed + 1;
}

@Override
public void init(EasyRandomParameters parameters) {
randomizers.put(Boolean.class, new BooleanRandomizer(seed));
randomizers.put(Byte.class, new ByteRandomizer(seed));
randomizers.put(Short.class, new ShortRandomizer(seed));
randomizers.put(Integer.class, new IntegerRandomizer(seed));
randomizers.put(Long.class, new LongRandomizer(seed));
randomizers.put(Double.class, new DoubleRandomizer(seed));
randomizers.put(Float.class, new FloatRandomizer(seed));
}

@Override
public Randomizer<?> getRandomizer(final Field field) {
return getRandomizer(field.getType());
}

@Override
public Randomizer<?> getRandomizer(Class<?> type) {
return randomizers.get(type);
}
}
32 changes: 8 additions & 24 deletions src/main/java/com/ocadotechnology/gembus/test/EnhancedRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,8 @@
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.ByteRandomizer;
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.number.ShortRandomizer;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.Random;
import java.util.Set;

import java.util.*;
import java.util.function.Supplier;
import java.util.stream.Stream;

Expand Down Expand Up @@ -63,7 +50,7 @@ private EnhancedRandom(Map<Class<?>, CustomArranger<?>> arrangers, Supplier<Easy
this.parametersSupplier = parametersSupplier;
EasyRandomParameters parameters = parametersSupplier.get();
parameters.seed(seed);
addRandomizersToParameters(Optional.empty(), parameters, arrangers, seed);
addRandomizersToParameters(Optional.empty(), parameters, arrangers);
this.easyRandom = new EasyRandom(parameters);
}

Expand Down Expand Up @@ -117,24 +104,21 @@ private EasyRandomParameters addExclusionToParameters(Set<String> fields) {
return parameters;
}

private void addRandomizersToParameters(Optional<Class> typeToSkip, EasyRandomParameters parameters, Map<Class<?>, CustomArranger<?>> customArrangers, long seed) {
private void addRandomizersToParameters(Optional<Class> typeToSkip, EasyRandomParameters parameters, Map<Class<?>, CustomArranger<?>> customArrangers) {
for (Map.Entry<Class<?>, CustomArranger<?>> entry : customArrangers.entrySet()) {
if (entry.getKey() != typeToSkip.orElse(null)) {
final Class key = entry.getKey();
final Randomizer randomizer = customArrangerToRandomizer(entry.getValue());
parameters.randomize(key, randomizer);
}
}
parameters.randomize(Boolean.class, new BooleanRandomizer(seed + 1));
parameters.randomize(Byte.class, new ByteRandomizer(seed + 1));
parameters.randomize(Short.class, new ShortRandomizer(seed + 1));
parameters.randomize(Integer.class, new IntegerRandomizer(seed + 1));
parameters.randomize(Long.class, new LongRandomizer(seed + 1));
parameters.randomize(Double.class, new DoubleRandomizer(seed + 1));
parameters.randomize(Float.class, new FloatRandomizer(seed + 1));
long newSeed = parameters.getSeed() + SeedHelper.customArrangerTypeSpecificSeedRespectingRandomSeedSetting(typeToSkip.orElse(CustomArranger.class));
parameters.randomizerRegistry(new CustomArrangerRandomizerRegistry(newSeed));
}

private Randomizer<?> customArrangerToRandomizer(CustomArranger arranger) {
return arranger::instance;
}


}
33 changes: 33 additions & 0 deletions src/main/java/com/ocadotechnology/gembus/test/SeedHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright © 2020 Ocado (marian.jureczko@ocado.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 com.ocadotechnology.gembus.test;

import org.jeasy.random.EasyRandomParameters;

public class SeedHelper {

static long calculateSeed() {
long seed = EasyRandomParameters.DEFAULT_SEED;
if (PropertiesWrapper.getRandomSeedEnabled()) {
seed = System.nanoTime();
}
return seed;
}

static long customArrangerTypeSpecificSeedRespectingRandomSeedSetting(Class<?> type) {
return calculateSeed() + type.getName().hashCode();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright © 2020 Ocado (marian.jureczko@ocado.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 com.ocadotechnology.gembus.test;

import org.junit.jupiter.api.Test;

import javax.validation.constraints.Positive;
import java.math.BigDecimal;
import java.math.BigInteger;

import static com.ocadotechnology.gembus.test.Arranger.some;
import static org.assertj.core.api.Assertions.assertThat;

public class ArrangerBeanValidationSupportTest {

@Test
void should_supportPositiveAnnotation() {
//given
for (int i = 0; i < 25; i++) {

//when
PositiveValidatedBean actual = some(PositiveValidatedBean.class);

//then
assertThat(actual.bigDecimal.doubleValue()).isGreaterThan(0);
assertThat(actual.bigInteger.doubleValue()).isGreaterThan(0);
assertThat(actual.intNumber).isGreaterThan(0);
assertThat(actual.longNumber).isGreaterThan(0);
assertThat(actual.floatNumber).isGreaterThan(0);
assertThat(actual.doubleNumber).isGreaterThan(0);
assertThat(actual.shortNumber).isGreaterThan((short) 0);
assertThat(actual.byteNumber).isGreaterThan((byte) 0);
}
}

}

class PositiveValidatedBean {
@Positive BigDecimal bigDecimal;
@Positive BigInteger bigInteger;
@Positive int intNumber;
@Positive Long longNumber;
@Positive float floatNumber;
@Positive Double doubleNumber;
@Positive short shortNumber;
@Positive byte byteNumber;
}

0 comments on commit dcd732e

Please sign in to comment.