Get source code updated for new package #10

Merged
honeymoose merged 14 commits from active_mq into main 2024-04-30 09:39:55 -04:00
6 changed files with 194 additions and 0 deletions
Showing only changes of commit 87a004e47a - Show all commits

View File

@ -0,0 +1,9 @@
# Root logger
log4j.rootLogger=INFO, file, stdout
# Write to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

View File

@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>

View File

@ -0,0 +1,64 @@
package com.ossez.uuid;
import static org.apache.commons.codec.binary.Base64.decodeBase64;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.UUID;
import org.apache.commons.lang3.Conversion;
import org.junit.jupiter.api.Test;
public class DecodeUUIDStringFromBase64UnitTest {
private final UUID originalUUID = UUID.fromString("cc5f93f7-8cf1-4a51-83c6-e740313a0c6c");
@Test
public void givenEncodedString_whenDecodingUsingBase64Decoder_thenGiveExpectedUUID() {
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
byte[] decodedBytes = Base64.getDecoder()
.decode(expectedEncodedString);
UUID uuid = convertToUUID(decodedBytes);
assertEquals(originalUUID, uuid);
}
@Test
public void givenEncodedString_whenDecodingUsingByteBufferAndBase64UrlDecoder_thenGiveExpectedUUID() {
String expectedEncodedString = "zF-T94zxSlGDxudAMToMbA";
byte[] decodedBytes = Base64.getUrlDecoder()
.decode(expectedEncodedString);
ByteBuffer byteBuffer = ByteBuffer.wrap(decodedBytes);
long mostSignificantBits = byteBuffer.getLong();
long leastSignificantBits = byteBuffer.getLong();
UUID uuid = new UUID(mostSignificantBits, leastSignificantBits);
assertEquals(originalUUID, uuid);
}
@Test
public void givenEncodedString_whenDecodingUsingApacheUtils_thenGiveExpectedUUID() {
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
byte[] decodedBytes = decodeBase64(expectedEncodedString);
UUID uuid = Conversion.byteArrayToUuid(decodedBytes, 0);
assertEquals(originalUUID, uuid);
}
private UUID convertToUUID(byte[] src) {
long mostSignificantBits = convertBytesToLong(src, 0);
long leastSignificantBits = convertBytesToLong(src, 8);
return new UUID(mostSignificantBits, leastSignificantBits);
}
private long convertBytesToLong(byte[] uuidBytes, int start) {
long result = 0;
for(int i = 0; i < 8; i++) {
int shift = i * 8;
long bits = (255L & (long)uuidBytes[i + start]) << shift;
long mask = 255L << shift;
result = result & ~mask | bits;
}
return result;
}
}

View File

@ -0,0 +1,62 @@
package com.ossez.uuid;
import static org.apache.commons.codec.binary.Base64.encodeBase64URLSafeString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.nio.ByteBuffer;
import java.util.Base64;
import java.util.UUID;
import org.apache.commons.lang3.Conversion;
import org.junit.jupiter.api.Test;
public class EncodeUUIDToBase64StringUnitTest {
private final UUID originalUUID = UUID.fromString("cc5f93f7-8cf1-4a51-83c6-e740313a0c6c");
@Test
public void givenUUID_whenEncodingUsingBase64Encoder_thenGiveExpectedEncodedString() {
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
byte[] uuidBytes = convertToByteArray(originalUUID);
String encodedUUID = Base64.getEncoder().withoutPadding()
.encodeToString(uuidBytes);
assertEquals(expectedEncodedString, encodedUUID);
}
@Test
public void givenUUID_whenEncodingUsingByteBufferAndBase64UrlEncoder_thenGiveExpectedEncodedString() {
String expectedEncodedString = "zF-T94zxSlGDxudAMToMbA";
ByteBuffer byteBuffer = ByteBuffer.wrap(new byte[16]);
byteBuffer.putLong(originalUUID.getMostSignificantBits());
byteBuffer.putLong(originalUUID.getLeastSignificantBits());
String encodedUUID = Base64.getUrlEncoder().withoutPadding()
.encodeToString(byteBuffer.array());
assertEquals(expectedEncodedString, encodedUUID);
}
@Test
public void givenUUID_whenEncodingUsingApacheUtils_thenGiveExpectedEncodedString() {
String expectedEncodedString = "UUrxjPeTX8xsDDoxQOfGgw";
byte[] bytes = Conversion.uuidToByteArray(originalUUID, new byte[16], 0, 16);
String encodedUUID = encodeBase64URLSafeString(bytes);
assertEquals(expectedEncodedString, encodedUUID);
}
private byte[] convertToByteArray(UUID uuid) {
byte[] result = new byte[16];
long mostSignificantBits = uuid.getMostSignificantBits();
fillByteArray(0, 8, result, mostSignificantBits);
long leastSignificantBits = uuid.getLeastSignificantBits();
fillByteArray(8, 16, result, leastSignificantBits);
return result;
}
private static void fillByteArray(int start, int end, byte[] result, long bits) {
for (int i = start; i < end; i++) {
int shift = i * 8;
result[i] = (byte) ((int) (255L & bits >> shift));
}
}
}

View File

@ -0,0 +1,22 @@
package com.ossez.uuid;
import org.junit.jupiter.api.Test;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
public class UUIDPositiveLongGeneratorUnitTest {
@Test
public void whenGetMostSignificantBits_thenAssertPositive() {
long randomPositiveLong = Math.abs(UUID.randomUUID().getMostSignificantBits());
assertThat(randomPositiveLong).isNotNegative();
}
@Test
public void whenGetLeastSignificantBits_thenAssertPositive() {
long randomPositiveLong = Math.abs(UUID.randomUUID().getLeastSignificantBits());
assertThat(randomPositiveLong).isNotNegative();
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework" level="WARN" />
<logger name="org.springframework.transaction" level="WARN" />
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
<root level="INFO">
<appender-ref ref="STDOUT" />
</root>
</configuration>