Skip to content

Commit

Permalink
Add comprehensive tests for AddressPartService
Browse files Browse the repository at this point in the history
  • Loading branch information
Lemeri123 committed Jan 14, 2025
1 parent 4cc03a7 commit 83d57fc
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@
import org.openelisglobal.common.dao.BaseDAO;

public interface AddressPartDAO extends BaseDAO<AddressPart, String> {
AddressPart getById(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ public class AddressPartDAOImpl extends BaseDAOImpl<AddressPart, String> impleme
public AddressPartDAOImpl() {
super(AddressPart.class);
}
@Override
public AddressPart getById(String id) {
return entityManager.find(AddressPart.class, id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface AddressPartService extends BaseObjectService<AddressPart, Strin
List<AddressPart> getAll();

AddressPart getAddresPartByName(String name);
AddressPart getAddresPartById(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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<AddressPart> 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());
}
}

0 comments on commit 83d57fc

Please sign in to comment.