Add files via upload

This commit is contained in:
Cody Cook 2023-12-06 18:37:58 -08:00 committed by GitHub
parent 4f20a69a1a
commit c2cd95eaa1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 505 additions and 0 deletions

101
Contact.java Normal file
View file

@ -0,0 +1,101 @@
/*
Cody Cook
CS-320-H7022 Software Test Automation & QA 23EW2
2023/11/11
*/
public class Contact {
// declare private string
private String id;
private String firstName;
private String lastName;
private String phone;
private String address;
// declare final integers for lengths
private final int lengthId = 10;
private final int lengthName = 10;
private final int lengthPhone = 10;
private final int lengthAddress = 30;
// set constructor
public Contact(String id, String firstName, String lastName, String phone, String address) {
this.setId(id);
this.setFirstName(firstName);
this.setLastName(lastName);
this.setLastName(lastName);
this.setPhone(phone);
this.setAddress(address);
}
// Getter methods for the contact fields
public String getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getPhone() {
return phone;
}
public String getAddress() {
return address;
}
private void setId(String id) {
/* Contact should not look at ALL Contacts to determine if this is a unique ID
- That will fail when trying to add to the list in ContactService. */
// Appears with low coverage as a private function cannot be called in a test case
// Add validation logic to check character limitations
if (id == null || id.length() > this.lengthId || id.length() == 0) {
throw new IllegalArgumentException("ID must be " + lengthId + " characters or less.");
}
this.id = id;
}
// Setter methods for the contact fields that can be updated
public void setFirstName(String firstName) {
// Add validation logic to check character limitations
if (firstName != null && firstName.length() <= lengthName && firstName.length() > 0) {
this.firstName = firstName;
} else {
throw new IllegalArgumentException("First name must be " + lengthName + " characters or less.");
}
}
public void setLastName(String lastName) {
// Add validation logic to check character limitations
if (lastName != null && lastName.length() <= lengthName && lastName.length() > 0) {
this.lastName = lastName;
} else {
throw new IllegalArgumentException("Last name must be " + lengthName + " characters or less.");
}
}
public void setPhone(String phone) {
// Add validation logic to check character limitations
if (phone != null && phone.length() == lengthPhone) {
this.phone = phone;
} else {
throw new IllegalArgumentException("Phone must be " + lengthPhone + " characters.");
}
}
public void setAddress(String address) {
// Add validation logic to check character limitations
if (address != null && address.length() <= lengthAddress && address.length() > 0) {
this.address = address;
} else {
throw new IllegalArgumentException("Address must be " + lengthAddress + " characters or less.");
}
}
}

74
ContactService.java Normal file
View file

@ -0,0 +1,74 @@
/*
Cody Cook
CS-320-H7022 Software Test Automation & QA 23EW2
2023/11/11
*/
import java.util.ArrayList;
import java.util.List;
public class ContactService {
private List<Contact> contacts;
public ContactService() {
// Initialize the list of contacts
this.contacts = new ArrayList<>();
}
public void addContact(Contact contact) {
// Check if a contact with the same ID already exists
if (getContactById(contact.getId()) != null) {
throw new IllegalArgumentException("Contact with the same ID already exists.");
}
// Add the contact to the list
contacts.add(contact);
}
public void deleteContact(String contactId) {
// Find and remove the contact with the specified contactId
boolean removed = contacts.removeIf(contact -> contact.getId().equals(contactId));
if (!removed) {
throw new IllegalArgumentException("Contact with ID " + contactId + " not found.");
}
}
public void updateContact(String contactId, String newFirstName, String newLastName, String newPhone,
String newAddress) {
// Find the contact by ID
Contact contactToUpdate = getContactById(contactId);
if (contactToUpdate == null) {
throw new IllegalArgumentException("Contact with ID " + contactId + " not found.");
}
// Update the contact's fields if new values are provided
if (newFirstName != null) {
contactToUpdate.setFirstName(newFirstName);
}
if (newLastName != null) {
contactToUpdate.setLastName(newLastName);
}
if (newPhone != null) {
contactToUpdate.setPhone(newPhone);
}
if (newAddress != null) {
contactToUpdate.setAddress(newAddress);
}
}
public Contact getContactById(String contactId) {
// Find and return the contact with the specified ID, or null if not found
for (Contact contact : contacts) {
if (contact.getId().equals(contactId)) {
return contact;
}
}
return null;
}
public List<Contact> getAllContacts() {
// Return a list of all contacts
return new ArrayList<>(contacts);
}
}

101
ContactServiceTest.java Normal file
View file

@ -0,0 +1,101 @@
/*
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");
});
}
}

229
ContactTest.java Normal file
View file

@ -0,0 +1,229 @@
/*
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 ContactTest {
/* Constructor Tests */
@Test
public void testConstructorValid() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertNotNull(contact);
assertEquals("1", contact.getId());
assertEquals("Cody", contact.getFirstName());
assertEquals("Cook", contact.getLastName());
assertEquals("4086342295", contact.getPhone());
assertEquals("315 Celebration Dr, 95035", contact.getAddress());
}
public void testSetIdTooLong() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("12345678901", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035"));
}
public void testSetIdNull() {
assertThrows(IllegalArgumentException.class,
() -> new Contact(null, "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035"));
}
public void testSetIdBlank() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorFirstNameTooLong() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", "Kohdeeohdee", "Cook", "4086342295", "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorFirstNameNull() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", null, "Cook", "4086342295", "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorFirstNameBlank() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", "", "Cook", "4086342295", "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorLastNameTooLong() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", "Cody", "KookKookKook", "4086342295", "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorLastNameNull() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", "Cody", null, "4086342295", "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorLastNameBlank() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", "Cody", "", "4086342295", "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorPhoneTooShort() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", "Cody", "Cook", "408634229", "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorPhoneTooLong() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", "Cody", "Cook", "14086342295", "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorPhoneNull() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", "Cody", "Cook", null, "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorPhoneBlank() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", "Cody", "Cook", "", "315 Celebration Dr, 95035"));
}
@Test
public void testConstructorAddressTooLong() {
assertThrows(IllegalArgumentException.class,
() -> new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, Milpitas, CA 95035"));
}
@Test
public void testConstructorAddressNull() {
assertThrows(IllegalArgumentException.class, () -> new Contact("1", "Cody", "Cook", "4086342295", null));
}
@Test
public void testConstructorAddressBlank() {
assertThrows(IllegalArgumentException.class, () -> new Contact("1", "Cody", "Cook", "4086342295", ""));
}
/* firstName tests */
@Test
public void testSetFirstNameValid() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
contact.setFirstName("Kohdee");
assertEquals("Kohdee", contact.getFirstName());
}
@Test
public void testSetFirstNameTooLong() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setFirstName("Kohdeeohdee"));
}
public void testSetFirstNameNull() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setFirstName(null));
}
public void testSetFirstNameBlank() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setFirstName(""));
}
/* lastName tests */
@Test
public void testSetLastNameValid() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
contact.setLastName("Kook");
assertEquals("Kook", contact.getLastName());
}
@Test
public void testSetLastNameTooLong() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setLastName("Kookkookkook"));
}
@Test
public void testSetLastNameNull() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setLastName(null));
}
@Test
public void testSetLastNameBlank() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setLastName(null));
}
/* phone tests */
@Test
public void testSetPhoneValid() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
contact.setPhone("8018109276");
assertEquals("8018109276", contact.getPhone());
}
@Test
public void testSetPhoneTooLong() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setPhone("14086342295"));
}
@Test
public void testSetPhoneTooShort() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setPhone("6342295"));
}
@Test
public void testSetPhoneNull() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setPhone(null));
}
@Test
public void testSetPhoneBlank() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setPhone(""));
}
/* address tests */
@Test
public void testSetAddressValid() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
contact.setAddress("315 Celebration Drive, 95035");
assertEquals("315 Celebration Drive, 95035", contact.getAddress());
}
@Test
public void testSetAddressTooLong() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class,
() -> contact.setAddress("315 Celerbation Drive, Milpias, California 95035"));
}
@Test
public void testSetAddressNull() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setAddress(null));
}
@Test
public void testSetAddressBlank() {
Contact contact = new Contact("1", "Cody", "Cook", "4086342295", "315 Celebration Dr, 95035");
assertThrows(IllegalArgumentException.class, () -> contact.setAddress(""));
}
}