-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from ocadotechnology/test-arranger-19
Test arranger 19
- Loading branch information
Showing
7 changed files
with
169 additions
and
43 deletions.
There are no files selected for viewing
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
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
61 changes: 61 additions & 0 deletions
61
src/main/java/com/ocadotechnology/gembus/test/CustomArrangerRandomizerRegistry.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,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); | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
src/main/java/com/ocadotechnology/gembus/test/SeedHelper.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,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(); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
src/test/java/com/ocadotechnology/gembus/test/ArrangerBeanValidationSupportTest.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,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; | ||
} |