From 919c9d0499358f83875220fe5a3ae3a550155b01 Mon Sep 17 00:00:00 2001 From: Jan Date: Tue, 27 Jan 2026 19:27:13 +0100 Subject: [PATCH] Added `PackagingRepositoryIntegrationTest` for MySQL and MSSQL; extended `PackagingRepository` with additional fields in SELECT query and improved SQL pagination logic. --- .../packaging/PackagingRepository.java | 4 +- .../PackagingRepositoryIntegrationTest.java | 279 ++++++++++++++++++ 2 files changed, 281 insertions(+), 2 deletions(-) create mode 100644 src/test/java/de/avatic/lcc/repositories/PackagingRepositoryIntegrationTest.java diff --git a/src/main/java/de/avatic/lcc/repositories/packaging/PackagingRepository.java b/src/main/java/de/avatic/lcc/repositories/packaging/PackagingRepository.java index 8fec135..58079eb 100644 --- a/src/main/java/de/avatic/lcc/repositories/packaging/PackagingRepository.java +++ b/src/main/java/de/avatic/lcc/repositories/packaging/PackagingRepository.java @@ -78,7 +78,7 @@ public class PackagingRepository { private String buildQuery(Integer materialId, Integer supplierId, boolean excludeDeprecated, SearchQueryPagination pagination) { StringBuilder queryBuilder = new StringBuilder(""" - SELECT id, + SELECT id, supplier_node_id, material_id, hu_dimension_id, shu_dimension_id, is_deprecated FROM packaging WHERE 1=1"""); @@ -91,7 +91,7 @@ public class PackagingRepository { if (supplierId != null) { queryBuilder.append(" AND supplier_node_id = ?"); } - queryBuilder.append("ORDER BY id "); + queryBuilder.append(" ORDER BY id "); queryBuilder.append(dialectProvider.buildPaginationClause(pagination.getLimit(), pagination.getOffset())); return queryBuilder.toString(); } diff --git a/src/test/java/de/avatic/lcc/repositories/PackagingRepositoryIntegrationTest.java b/src/test/java/de/avatic/lcc/repositories/PackagingRepositoryIntegrationTest.java new file mode 100644 index 0000000..4e4fc73 --- /dev/null +++ b/src/test/java/de/avatic/lcc/repositories/PackagingRepositoryIntegrationTest.java @@ -0,0 +1,279 @@ +package de.avatic.lcc.repositories; + +import de.avatic.lcc.model.db.packaging.Packaging; +import de.avatic.lcc.repositories.packaging.PackagingRepository; +import de.avatic.lcc.repositories.pagination.SearchQueryPagination; +import de.avatic.lcc.repositories.pagination.SearchQueryResult; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * Integration tests for PackagingRepository. + *

+ * Tests critical functionality across both MySQL and MSSQL: + * - Basic CRUD operations + * - Pagination with ORDER BY (MSSQL requirement) + * - Filtering by materialId and supplierId + * - Boolean literal compatibility (deprecated filtering) + *

+ * Run with: + *

+ * mvn test -Dspring.profiles.active=test,mysql -Dtest=PackagingRepositoryIntegrationTest
+ * mvn test -Dspring.profiles.active=test,mssql -Dtest=PackagingRepositoryIntegrationTest
+ * 
+ */ +class PackagingRepositoryIntegrationTest extends AbstractRepositoryIntegrationTest { + + @Autowired + private PackagingRepository packagingRepository; + + private Integer testMaterialId1; + private Integer testMaterialId2; + private Integer testNodeId1; + private Integer testNodeId2; + + @BeforeEach + void setupTestData() { + // Create test materials (required by foreign key) + testMaterialId1 = createTestMaterial("TEST-MAT-001", "Test Material 1"); + testMaterialId2 = createTestMaterial("TEST-MAT-002", "Test Material 2"); + + // Create test nodes (required by foreign key for supplier_node_id) + testNodeId1 = createTestNode("Test Supplier 1", 1); + testNodeId2 = createTestNode("Test Supplier 2", 1); + } + + @Test + void testInsertAndRetrieve() { + // Given: Create packaging + Packaging packaging = new Packaging(); + packaging.setMaterialId(testMaterialId1); + packaging.setSupplierId(testNodeId1); + packaging.setHuId(1); // Handling unit dimension + packaging.setShuId(1); // Shipping handling unit dimension + packaging.setDeprecated(false); + + // When: Insert + Optional packagingId = packagingRepository.insert(packaging); + + // Then: Should be inserted successfully + assertTrue(packagingId.isPresent(), "Packaging ID should be present"); + assertTrue(packagingId.get() > 0, "Packaging ID should be positive"); + + // When: Retrieve by ID + Optional retrieved = packagingRepository.getById(packagingId.get()); + + // Then: Should retrieve successfully + assertTrue(retrieved.isPresent(), "Packaging should be retrievable after insert"); + assertEquals(packaging.getMaterialId(), retrieved.get().getMaterialId()); + assertEquals(packaging.getSupplierId(), retrieved.get().getSupplierId()); + assertEquals(packaging.getHuId(), retrieved.get().getHuId()); + assertEquals(packaging.getShuId(), retrieved.get().getShuId()); + assertFalse(retrieved.get().getDeprecated()); + } + + @Test + void testUpdate() { + // Given: Create and insert packaging + Packaging packaging = createTestPackaging(testMaterialId1, testNodeId1, 1, 1, false); + Integer packagingId = packagingRepository.insert(packaging).orElseThrow(); + + // When: Update packaging + Packaging toUpdate = packagingRepository.getById(packagingId).orElseThrow(); + toUpdate.setHuId(2); + toUpdate.setShuId(2); + packagingRepository.update(toUpdate); + + // Then: Verify update + Packaging updated = packagingRepository.getById(packagingId).orElseThrow(); + assertEquals(2, updated.getHuId()); + assertEquals(2, updated.getShuId()); + } + + @Test + void testSetDeprecatedById() { + // Given: Create packaging + Packaging packaging = createTestPackaging(1, 1, 1, 1, false); + Integer packagingId = packagingRepository.insert(packaging).orElseThrow(); + + // When: Deprecate + Optional result = packagingRepository.setDeprecatedById(packagingId); + + // Then: Should be deprecated + assertTrue(result.isPresent()); + Packaging deprecated = packagingRepository.getById(packagingId).orElseThrow(); + assertTrue(deprecated.getDeprecated(), "Packaging should be marked as deprecated"); + } + + @Test + void testListPackagingWithPagination() { + // Given: Create multiple packagings + for (int i = 0; i < 5; i++) { + Packaging packaging = createTestPackaging(1, 1, 1, 1, false); + packagingRepository.insert(packaging); + } + + // When: List with pagination (page 1, size 3) + SearchQueryPagination pagination = new SearchQueryPagination(1, 3); + SearchQueryResult result = packagingRepository.listPackaging( + null, null, false, pagination + ); + + // Then: Verify pagination works + assertNotNull(result); + assertNotNull(result.toList()); + assertTrue(result.toList().size() <= 3, "Should return at most 3 packagings per page"); + } + + @Test + void testListPackagingFilterByMaterialId() { + // Given: Create packagings with different materials + Packaging packaging1 = createTestPackaging(1, 1, 1, 1, false); + packagingRepository.insert(packaging1); + + Packaging packaging2 = createTestPackaging(2, 1, 1, 1, false); + packagingRepository.insert(packaging2); + + // When: Filter by materialId=1 + SearchQueryPagination pagination = new SearchQueryPagination(1, 10); + SearchQueryResult result = packagingRepository.listPackaging( + 1, null, false, pagination + ); + + // Then: Should only return material 1 packagings + assertNotNull(result); + for (Packaging p : result.toList()) { + assertEquals(1, p.getMaterialId(), "Should only return packagings with materialId=1"); + } + } + + @Test + void testListPackagingFilterBySupplierId() { + // Given: Create packagings with different suppliers + Packaging packaging1 = createTestPackaging(1, 1, 1, 1, false); + packagingRepository.insert(packaging1); + + Packaging packaging2 = createTestPackaging(1, 2, 1, 1, false); + packagingRepository.insert(packaging2); + + // When: Filter by supplierId=1 + SearchQueryPagination pagination = new SearchQueryPagination(1, 10); + SearchQueryResult result = packagingRepository.listPackaging( + null, 1, false, pagination + ); + + // Then: Should only return supplier 1 packagings + assertNotNull(result); + for (Packaging p : result.toList()) { + assertEquals(1, p.getSupplierId(), "Should only return packagings with supplierId=1"); + } + } + + @Test + void testListPackagingExcludeDeprecated() { + // Given: Create deprecated and non-deprecated packagings + Packaging deprecated = createTestPackaging(1, 1, 1, 1, true); + packagingRepository.insert(deprecated); + + Packaging active = createTestPackaging(1, 1, 1, 1, false); + packagingRepository.insert(active); + + // When: List excluding deprecated + SearchQueryPagination pagination = new SearchQueryPagination(1, 10); + SearchQueryResult result = packagingRepository.listPackaging( + null, null, true, pagination + ); + + // Then: Should not include deprecated packagings + assertNotNull(result); + for (Packaging p : result.toList()) { + assertFalse(p.getDeprecated(), "Should not include deprecated packagings"); + } + } + + @Test + void testGetByMaterialId() { + // Given: Create packagings for specific material + Packaging packaging1 = createTestPackaging(10, 1, 1, 1, false); + packagingRepository.insert(packaging1); + + Packaging packaging2 = createTestPackaging(10, 2, 1, 1, false); + packagingRepository.insert(packaging2); + + // When: Get by materialId + List packagings = packagingRepository.getByMaterialId(10); + + // Then: Should return all packagings for that material + assertNotNull(packagings); + assertTrue(packagings.size() >= 2, "Should find at least 2 packagings for material 10"); + for (Packaging p : packagings) { + assertEquals(10, p.getMaterialId()); + } + } + + @Test + void testGetByMaterialIdAndSupplierId() { + // Given: Create packaging with specific material and supplier + Packaging packaging = createTestPackaging(15, 15, 1, 1, false); + Integer packagingId = packagingRepository.insert(packaging).orElseThrow(); + + // When: Get by materialId and supplierId + Optional result = packagingRepository.getByMaterialIdAndSupplierId(15, 15); + + // Then: Should find the packaging + assertTrue(result.isPresent(), "Should find packaging with materialId=15 and supplierId=15"); + assertEquals(packagingId, result.get().getId()); + assertEquals(15, result.get().getMaterialId()); + assertEquals(15, result.get().getSupplierId()); + } + + @Test + void testGetByMaterialIdAndSupplierIdNotFound() { + // When: Get by non-existent combination + Optional result = packagingRepository.getByMaterialIdAndSupplierId(99999, 99999); + + // Then: Should return empty + assertFalse(result.isPresent(), "Should not find packaging with non-existent IDs"); + } + + @Test + void testListAllPackaging() { + // Given: Create packagings + Packaging packaging1 = createTestPackaging(1, 1, 1, 1, false); + packagingRepository.insert(packaging1); + + Packaging packaging2 = createTestPackaging(2, 2, 1, 1, false); + packagingRepository.insert(packaging2); + + // When: List all + List packagings = packagingRepository.listAllPackaging(); + + // Then: Should return all packagings ordered by id + assertNotNull(packagings); + assertFalse(packagings.isEmpty()); + + // Verify ordering + for (int i = 1; i < packagings.size(); i++) { + assertTrue(packagings.get(i - 1).getId() <= packagings.get(i).getId(), + "Packagings should be ordered by ID"); + } + } + + // ========== Helper Methods ========== + + private Packaging createTestPackaging(Integer materialId, Integer supplierId, Integer huId, Integer shuId, boolean deprecated) { + Packaging packaging = new Packaging(); + packaging.setMaterialId(materialId); + packaging.setSupplierId(supplierId); + packaging.setHuId(huId); + packaging.setShuId(shuId); + packaging.setDeprecated(deprecated); + return packaging; + } +}