split random number and uuid generator modules
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m15s
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m15s
This commit is contained in:
parent
15c6263395
commit
0ae750fc0c
13 changed files with 29 additions and 5 deletions
16
quarkus-random-number-generator/pom.xml
Normal file
16
quarkus-random-number-generator/pom.xml
Normal file
|
@ -0,0 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>ch.phoenixtechnologies.quarkus</groupId>
|
||||
<artifactId>quarkus-commons</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>quarkus-random-number-generator</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,11 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
public interface RandomNumberGenerator {
|
||||
|
||||
BigInteger generate(int numBits);
|
||||
|
||||
BigInteger generate(int numBits, Random random);
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import io.quarkus.arc.DefaultBean;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
@DefaultBean
|
||||
@ApplicationScoped
|
||||
class RandomNumberGeneratorImpl implements RandomNumberGenerator {
|
||||
|
||||
private final RandomProvider provider;
|
||||
|
||||
RandomNumberGeneratorImpl(RandomProvider provider) {
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigInteger generate(int numBits) {
|
||||
return generate(numBits, provider.get());
|
||||
}
|
||||
|
||||
@Override
|
||||
public BigInteger generate(int numBits, Random random) {
|
||||
return new BigInteger(numBits, random);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Provides Random instances that can be used for any purpose,
|
||||
* such as generating random numbers or strings.
|
||||
*/
|
||||
public interface RandomProvider {
|
||||
|
||||
/**
|
||||
* Get a new Random instance.
|
||||
*
|
||||
* @return The Random instance
|
||||
* @throws IllegalStateException If the Random instance cannot be created
|
||||
*/
|
||||
Random get();
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import io.quarkus.arc.DefaultBean;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Random;
|
||||
|
||||
@DefaultBean
|
||||
@ApplicationScoped
|
||||
class RandomProviderImpl implements RandomProvider {
|
||||
|
||||
@Override
|
||||
public Random get() {
|
||||
try {
|
||||
return SecureRandom.getInstanceStrong();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new IllegalStateException("Unable to obtain strong SecureRandom instance", e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.quarkus.test.junit.mockito.InjectSpy;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@QuarkusTest
|
||||
class RandomNumberGeneratorImplTest {
|
||||
|
||||
@Inject
|
||||
RandomNumberGeneratorImpl generator;
|
||||
|
||||
@InjectSpy
|
||||
RandomProvider provider;
|
||||
|
||||
@Test
|
||||
void generateWithRandomFromProvider() {
|
||||
when(provider.get()).thenReturn(getRandom());
|
||||
|
||||
var expected = BigInteger.valueOf(366467854850L);
|
||||
|
||||
var actual = generator.generate(40);
|
||||
|
||||
assertThat(actual)
|
||||
.as("Generated random number should match expected value")
|
||||
.isEqualTo(expected);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateWithLocalRandom() {
|
||||
var expected = BigInteger.valueOf(366467854850L);
|
||||
|
||||
var random = getRandom();
|
||||
var actual = generator.generate(40, random);
|
||||
|
||||
assertThat(actual)
|
||||
.as("Generated random number should match expected value")
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
private static Random getRandom() {
|
||||
return new Random(24353L);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.security.Security;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
@QuarkusTest
|
||||
@SuppressWarnings("removal")
|
||||
class RandomProviderImplTest {
|
||||
|
||||
public static final String SECURE_RANDOM_STRONG_ALGORITHMS = "securerandom.strongAlgorithms";
|
||||
|
||||
@Inject
|
||||
RandomProviderImpl provider;
|
||||
|
||||
@Test
|
||||
void get() {
|
||||
assertThat(provider.get())
|
||||
.as("Random instance should not be null")
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void getWhenNoSuchAlgorithm() {
|
||||
var algo = getSecurityProperty();
|
||||
|
||||
setSecurityProperty("non-existent-algorithm");
|
||||
|
||||
try {
|
||||
assertThatThrownBy(() -> provider.get())
|
||||
.as("Should throw IllegalStateException when algorithm is not found")
|
||||
.isInstanceOf(IllegalStateException.class)
|
||||
.hasMessageContaining("Unable to obtain strong SecureRandom instance");
|
||||
} finally {
|
||||
setSecurityProperty(algo);
|
||||
}
|
||||
}
|
||||
|
||||
private static String getSecurityProperty() {
|
||||
return AccessController.doPrivileged(
|
||||
(PrivilegedAction<String>) () -> Security.getProperty(
|
||||
SECURE_RANDOM_STRONG_ALGORITHMS));
|
||||
}
|
||||
|
||||
private static void setSecurityProperty(String datum) {
|
||||
AccessController.doPrivileged(
|
||||
(PrivilegedAction<Void>) () -> {
|
||||
Security.setProperty(
|
||||
SECURE_RANDOM_STRONG_ALGORITHMS, datum);
|
||||
return (Void) null;
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue