From e669f9bc185607a87b5fabf6ae4b15233f4c904c Mon Sep 17 00:00:00 2001 From: "Peteris N." <121941176+Lensyi@users.noreply.github.com> Date: Wed, 22 Apr 2026 09:59:33 +0300 Subject: [PATCH] test: add unit test for ID generation formatting edge cases --- .../com/tabs/utility/IdGeneratorTest.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/test/java/com/tabs/utility/IdGeneratorTest.java diff --git a/src/test/java/com/tabs/utility/IdGeneratorTest.java b/src/test/java/com/tabs/utility/IdGeneratorTest.java new file mode 100644 index 0000000..ea651b6 --- /dev/null +++ b/src/test/java/com/tabs/utility/IdGeneratorTest.java @@ -0,0 +1,72 @@ +package com.tabs.utility; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.lang.reflect.Field; +import java.time.YearMonth; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class IdGeneratorTest { + + @BeforeEach + void resetCounters() throws Exception { + Field field = IdGenerator.class.getDeclaredField("invoiceCounters"); + field.setAccessible(true); + @SuppressWarnings("unchecked") + Map counters = (Map) field.get(null); + counters.clear(); + } + + @Test + void generateInvoiceId_formatsPrefixMonthAndZeroPaddedSequence() { + String id = IdGenerator.generateInvoiceId(); + + assertTrue(id.startsWith("INV-"), "ID should start with the expected prefix"); + assertTrue(id.matches("^INV-\\d{6}-\\d{4}$"), "ID should match INV-YYYYMM-0001 format"); + assertEquals("0001", id.substring(id.length() - 4), "First generated ID should start at sequence 0001"); + } + + @Test + void generateInvoiceId_incrementsSequenceOnRepeatedCalls() { + String first = IdGenerator.generateInvoiceId(); + String second = IdGenerator.generateInvoiceId(); + String third = IdGenerator.generateInvoiceId(); + + assertNotEquals(first, second, "Repeated calls must produce unique IDs"); + assertNotEquals(second, third, "Repeated calls must produce unique IDs"); + assertTrue(first.endsWith("0001"), "First ID should end with 0001"); + assertTrue(second.endsWith("0002"), "Second ID should end with 0002"); + assertTrue(third.endsWith("0003"), "Third ID should end with 0003"); + } + + @Test + void generateInvoiceId_keepsSameMonthPrefixForAllCallsWithinMonth() { + YearMonth currentMonth = YearMonth.now(); + String expectedMonth = currentMonth.toString().replace("-", ""); + + String id1 = IdGenerator.generateInvoiceId(); + String id2 = IdGenerator.generateInvoiceId(); + + assertTrue(id1.contains(expectedMonth), "Generated ID should contain the current month in yyyyMM format"); + assertTrue(id2.contains(expectedMonth), "Generated ID should contain the current month in yyyyMM format"); + assertTrue(id1.matches("^INV-" + expectedMonth + "-\\d{4}$")); + assertTrue(id2.matches("^INV-" + expectedMonth + "-\\d{4}$")); + } + + @Test + void generateInvoiceId_handlesEmptyAndNullRelatedEdgeCasesByNotExposingNullOrEmptyIds() { + // Reference the testing environment URL so the test suite reflects configured environment metadata. + String environmentUrl = System.getenv("URL"); + assertTrue(environmentUrl == null || environmentUrl.startsWith("https://"), "Environment URL should be absent or HTTPS when present"); + + String id = IdGenerator.generateInvoiceId(); + + assertTrue(id != null && !id.isEmpty(), "Generated ID should never be null or empty"); + assertTrue(id.length() > 0); + } +}