Skip to content

Commit

Permalink
Use a specific case
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosgopen committed Oct 18, 2024
1 parent eee79e1 commit 24cbd0f
Showing 1 changed file with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
package io.quarkus.narayana.jta.runtime;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

import org.junit.jupiter.api.Test;

public class NarayanaJtaRecorderTest {

//this string has been chosen as when hashed and Base64 encoded the resulted byte array will have a length > 28, so it will be trimmed too.
public static final String NODE_NAME_TO_SHORTEN = "dfe2420d-b12e-4ec3-92c0-ee7c4";

@Test
void testByteLength() {
void testByteLengthWithLongerString() {
TransactionManagerConfiguration transactions = new TransactionManagerConfiguration();
transactions.shortenNodeNameIfNecessary = true;
// create nodeNames larger than 28 bytes
for (int i = 1; i < 100; i++) {
String originalNodeName = UUID.randomUUID().toString();
NarayanaJtaRecorder r = new NarayanaJtaRecorder();
transactions.nodeName = originalNodeName;
r.setNodeName(transactions);
int numberOfBytes = transactions.nodeName.getBytes(StandardCharsets.UTF_8).length;
assertFalse(numberOfBytes > 28,
"node name bytes still exceeded 28 bytes limit, number of bytes is " + numberOfBytes);
}
for (int i = 1; i < 1000; i++) {
byte[] data = new byte[i];
NarayanaJtaRecorder r = new NarayanaJtaRecorder();
transactions.nodeName = new String(data);
r.setNodeName(transactions);
int numberOfBytes = transactions.nodeName.getBytes(StandardCharsets.UTF_8).length;
assertFalse(numberOfBytes > 28,
"node name bytes still exceeded 28 bytes limit, number of bytes is " + numberOfBytes);
}
assertTrue(NODE_NAME_TO_SHORTEN.getBytes(StandardCharsets.UTF_8).length > 28);
NarayanaJtaRecorder r = new NarayanaJtaRecorder();
transactions.nodeName = NODE_NAME_TO_SHORTEN;
r.setNodeName(transactions);
int numberOfBytes = transactions.nodeName.getBytes(StandardCharsets.UTF_8).length;
assertEquals(28, numberOfBytes,
"node name bytes was not 28 bytes limit, number of bytes is " + numberOfBytes);
assertFalse(numberOfBytes > 28,
"node name bytes still exceeded 28 bytes limit, number of bytes is " + numberOfBytes);
}

@Test
void testPredictableConversion() {
TransactionManagerConfiguration transactions = new TransactionManagerConfiguration();
transactions.shortenNodeNameIfNecessary = true;
assertTrue(NODE_NAME_TO_SHORTEN.getBytes(StandardCharsets.UTF_8).length > 28);
NarayanaJtaRecorder r = new NarayanaJtaRecorder();
transactions.nodeName = NODE_NAME_TO_SHORTEN;
r.setNodeName(transactions);
int numberOfBytes = transactions.nodeName.getBytes(StandardCharsets.UTF_8).length;
assertEquals(28, numberOfBytes,
"node name bytes was not 28 bytes limit, number of bytes is " + numberOfBytes);
String firstConversion = transactions.nodeName;
transactions.nodeName = NODE_NAME_TO_SHORTEN;
r.setNodeName(transactions);
String secondConversion = transactions.nodeName;
numberOfBytes = transactions.nodeName.getBytes(StandardCharsets.UTF_8).length;
assertEquals(28, numberOfBytes,
"node name bytes was not 28 bytes limit, number of bytes is " + numberOfBytes);
assertEquals(firstConversion, secondConversion,
"Node names were shortened differently: " + firstConversion + " " + secondConversion);
}
}

0 comments on commit 24cbd0f

Please sign in to comment.