From 83d57fc0ff6c44395354196ac363748ba8d33a18 Mon Sep 17 00:00:00 2001 From: Lemeri123 Date: Tue, 14 Jan 2025 16:18:41 +0300 Subject: [PATCH] Add comprehensive tests for AddressPartService --- .../address/dao/AddressPartDAO.java | 1 + .../address/daoimpl/AddressPartDAOImpl.java | 4 + .../address/service/AddressPartService.java | 1 + .../service/AddressPartServiceImpl.java | 5 ++ .../address/AddressPartServiceTest.java | 81 +++++++++++++++++++ 5 files changed, 92 insertions(+) diff --git a/src/main/java/org/openelisglobal/address/dao/AddressPartDAO.java b/src/main/java/org/openelisglobal/address/dao/AddressPartDAO.java index 55db6159a6..cf517afb58 100644 --- a/src/main/java/org/openelisglobal/address/dao/AddressPartDAO.java +++ b/src/main/java/org/openelisglobal/address/dao/AddressPartDAO.java @@ -17,4 +17,5 @@ import org.openelisglobal.common.dao.BaseDAO; public interface AddressPartDAO extends BaseDAO { + AddressPart getById(String id); } diff --git a/src/main/java/org/openelisglobal/address/daoimpl/AddressPartDAOImpl.java b/src/main/java/org/openelisglobal/address/daoimpl/AddressPartDAOImpl.java index ea498b853a..fe02c1140f 100644 --- a/src/main/java/org/openelisglobal/address/daoimpl/AddressPartDAOImpl.java +++ b/src/main/java/org/openelisglobal/address/daoimpl/AddressPartDAOImpl.java @@ -26,4 +26,8 @@ public class AddressPartDAOImpl extends BaseDAOImpl impleme public AddressPartDAOImpl() { super(AddressPart.class); } + @Override + public AddressPart getById(String id) { + return entityManager.find(AddressPart.class, id); + } } diff --git a/src/main/java/org/openelisglobal/address/service/AddressPartService.java b/src/main/java/org/openelisglobal/address/service/AddressPartService.java index 3066436c87..184a86a5f6 100644 --- a/src/main/java/org/openelisglobal/address/service/AddressPartService.java +++ b/src/main/java/org/openelisglobal/address/service/AddressPartService.java @@ -8,4 +8,5 @@ public interface AddressPartService extends BaseObjectService getAll(); AddressPart getAddresPartByName(String name); + AddressPart getAddresPartById(String id); } diff --git a/src/main/java/org/openelisglobal/address/service/AddressPartServiceImpl.java b/src/main/java/org/openelisglobal/address/service/AddressPartServiceImpl.java index 98d11d2d7b..d14fd9881c 100644 --- a/src/main/java/org/openelisglobal/address/service/AddressPartServiceImpl.java +++ b/src/main/java/org/openelisglobal/address/service/AddressPartServiceImpl.java @@ -27,4 +27,9 @@ protected AddressPartDAO getBaseObjectDAO() { public AddressPart getAddresPartByName(String name) { return getMatch("partName", name).orElse(null); } + + @Override + public AddressPart getAddresPartById(String id) { + return baseObjectDAO.getById(id); + } } diff --git a/src/test/java/org/openelisglobal/address/AddressPartServiceTest.java b/src/test/java/org/openelisglobal/address/AddressPartServiceTest.java index 730d552ee5..823cbf1f5b 100644 --- a/src/test/java/org/openelisglobal/address/AddressPartServiceTest.java +++ b/src/test/java/org/openelisglobal/address/AddressPartServiceTest.java @@ -8,6 +8,7 @@ import org.openelisglobal.address.service.AddressPartService; import org.openelisglobal.address.valueholder.AddressPart; import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; public class AddressPartServiceTest extends BaseWebContextSensitiveTest { @@ -103,4 +104,84 @@ public void getAddressPartByNam_shouldReturnAddressPartByName() throws Exception Assert.assertEquals("022", part.getDisplayOrder()); } + + @Test(expected = Exception.class) +public void saveAddressPart_withEmptyPartName_shouldThrowException() throws Exception { + AddressPart part = new AddressPart(); + part.setPartName(""); + part.setDisplayOrder("001"); + + partService.save(part); +} + +@Test(expected = Exception.class) +public void saveDuplicateAddressPart_shouldThrowException() throws Exception { + AddressPart part1 = new AddressPart(); + part1.setPartName("DuplicatePartName"); + part1.setDisplayOrder("001"); + + AddressPart part2 = new AddressPart(); + part2.setPartName("DuplicatePartName"); + part2.setDisplayOrder("002"); + + partService.save(part1); + partService.save(part2); +} + +@Test +public void getAddressPartByName_withNonExistentName_shouldReturnNull() throws Exception { + AddressPart part = partService.getAddresPartByName("NonExistentName"); + + Assert.assertNull(part); +} + +@Test +public void getAll_whenNoPartsExist_shouldReturnEmptyList() throws Exception { + List parts = partService.getAll(); + + Assert.assertTrue(parts.isEmpty()); +} + +@Test +public void deleteNonExistentAddressPart_shouldNotThrowException() throws Exception { + AddressPart part = new AddressPart(); + part.setId("NonExistentId"); + + partService.delete(part); + + Assert.assertEquals(0, partService.getAll().size()); +} + +@Test(expected = Exception.class) +public void saveAddressPart_withInvalidDisplayOrder_shouldThrowException() throws Exception { + AddressPart part = new AddressPart(); + part.setPartName("ValidName"); + part.setDisplayOrder("InvalidOrder"); + + partService.save(part); +} + +@Test +public void saveAddressPart_withNullOptionalFields_shouldSaveSuccessfully() throws Exception { + AddressPart part = new AddressPart(); + part.setPartName("NameOnly"); + + partService.save(part); + + AddressPart savedPart = partService.get(part.getId()); + Assert.assertEquals("NameOnly", savedPart.getPartName()); + Assert.assertNull(savedPart.getDisplayOrder()); +} + +@Test +public void saveAddressPart_withLongPartName_shouldSaveSuccessfully() throws Exception { + AddressPart part = new AddressPart(); + part.setPartName("A".repeat(255)); + part.setDisplayOrder("001"); + + partService.save(part); + + AddressPart savedPart = partService.get(part.getId()); + Assert.assertEquals("A".repeat(255), savedPart.getPartName()); +} }