/* Cody Cook CS-320-H7022 Software Test Automation & QA 23EW2 2023/11/11 */ import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class ContactServiceTest { /* addContact tests */ @Test public void testAddContactValid() { ContactService contactService = new ContactService(); Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035"); contactService.addContact(contact); assertEquals(1, contactService.getAllContacts().size()); } @Test public void testAddContactsValid() { ContactService contactService = new ContactService(); Contact contact1 = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035"); contactService.addContact(contact1); Contact contact2 = new Contact("2", "Kohdee", "Kook", "8018109276", "1 North Temple Drive, 84106"); contactService.addContact(contact2); assertEquals(2, contactService.getAllContacts().size()); } @Test public void testAddContactDuplicateId() { ContactService contactService = new ContactService(); Contact contact1 = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035"); Contact contact2 = new Contact("1", "Kohdee", "Kook", "8018109276", "1 North Temple Drive, 84106"); contactService.addContact(contact1); assertThrows(IllegalArgumentException.class, () -> { contactService.addContact(contact2); }); } @Test public void testAddContactsDuplicateId() { ContactService contactService = new ContactService(); Contact contact1 = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035"); contactService.addContact(contact1); Contact contact2 = new Contact("2", "Kohdee", "Kook", "8018109276", "1 North Temple Drive, 84106"); contactService.addContact(contact2); Contact contact3 = new Contact("2", "Barack", "Obama", "2013110519", "White House, Washington DC"); assertThrows(IllegalArgumentException.class, () -> { contactService.addContact(contact3); }); } @Test public void testDeleteContact() { ContactService contactService = new ContactService(); Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035"); contactService.addContact(contact); assertEquals(1, contactService.getAllContacts().size()); contactService.deleteContact("1"); assertEquals(0, contactService.getAllContacts().size()); } @Test public void testDeleteInvalidContact() { ContactService contactService = new ContactService(); Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035"); contactService.addContact(contact); assertThrows(IllegalArgumentException.class, () -> { contactService.deleteContact("2"); }); } /* updating contact tests */ @Test public void testUpdateContact() { ContactService contactService = new ContactService(); Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035"); contactService.addContact(contact); contactService.updateContact("1", "Kohdee", "Kook", "8018109276", "1 North Temple Drive, 84106"); Contact contactValidator = contactService.getContactById("1"); assertNotNull(contactValidator); assertEquals("1", contactValidator.getId()); assertEquals("Kohdee", contactValidator.getFirstName()); assertEquals("Kook", contactValidator.getLastName()); assertEquals("8018109276", contactValidator.getPhone()); assertEquals("1 North Temple Drive, 84106", contactValidator.getAddress()); } @Test public void testUpdateNonExistentContact() { ContactService contactService = new ContactService(); assertThrows(IllegalArgumentException.class, () -> { contactService.updateContact("2", "Kohdee", "Kook", "8018109276", "1 North Temple Drive, 84106"); }); } }