initial commit
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m12s
All checks were successful
Build, test and publish the Quarkus libraries / build (push) Successful in 10m12s
This commit is contained in:
commit
2d9bd1710c
17 changed files with 940 additions and 0 deletions
50
quarkus-random-generator/pom.xml
Normal file
50
quarkus-random-generator/pom.xml
Normal file
|
@ -0,0 +1,50 @@
|
|||
<?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-generator</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<!-- https://mvnrepository.com/artifact/com.fasterxml.uuid/java-uuid-generator -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.uuid</groupId>
|
||||
<artifactId>java-uuid-generator</artifactId>
|
||||
<version>${java-uuid-generator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit5-component</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-junit5-mockito</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.quarkus</groupId>
|
||||
<artifactId>quarkus-jacoco</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-junit-jupiter</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</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,26 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.Random;
|
||||
|
||||
@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,16 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
public interface UUIDGenerator {
|
||||
|
||||
UUID generateV4();
|
||||
|
||||
UUID generateV4(Random random);
|
||||
|
||||
UUID generateV5(String name);
|
||||
|
||||
UUID generateV5(UUID namespace, String name);
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import com.fasterxml.uuid.Generators;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
@ApplicationScoped
|
||||
class UUIDGeneratorImpl implements UUIDGenerator {
|
||||
|
||||
@Override
|
||||
public UUID generateV4() {
|
||||
return Generators.randomBasedGenerator().generate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID generateV4(Random random) {
|
||||
return Generators.randomBasedGenerator(random).generate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID generateV5(String name) {
|
||||
return Generators.nameBasedGenerator().generate(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID generateV5(UUID namespace, String name) {
|
||||
return Generators.nameBasedGenerator(namespace).generate(name);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import io.quarkus.test.component.QuarkusComponentTest;
|
||||
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,21 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
@QuarkusTest
|
||||
class RandomProviderImplTest {
|
||||
|
||||
@Inject
|
||||
RandomProviderImpl provider;
|
||||
|
||||
@Test
|
||||
void get() {
|
||||
assertThat(provider.get())
|
||||
.as("Random instance should not be null")
|
||||
.isNotNull();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package ch.phoenixtechnologies.quarkus.commons.random;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import jakarta.inject.Inject;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
|
||||
@QuarkusTest
|
||||
class UUIDGeneratorImplTest {
|
||||
|
||||
@Inject
|
||||
UUIDGeneratorImpl generator;
|
||||
|
||||
@Test
|
||||
void generateV4() {
|
||||
assertThat(generator.generateV4())
|
||||
.as("Generated UUID should not be null")
|
||||
.isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateV4WithLocalRandom() {
|
||||
var expected = UUID.fromString("b22f5354-f5ed-4f02-bca8-8882a6ec4750");
|
||||
|
||||
var actual = generator.generateV4(getRandom());
|
||||
|
||||
assertThat(actual)
|
||||
.as("Generated UUID should match expected value")
|
||||
.isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateV5() {
|
||||
}
|
||||
|
||||
@Test
|
||||
void generateV5WithNamespace() {
|
||||
}
|
||||
|
||||
private static Random getRandom() {
|
||||
return new Random(24353L);
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue