Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added comprehensive tests for AddressPartService #1404

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}