package services; import dao.*; import model.AuthToken; import model.Event; import model.Person; import model.User; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import result.ClearResult; import java.util.ArrayList; import static org.junit.jupiter.api.Assertions.*; public class ClearServiceTest { Database db; UserDAO uDao; PersonDAO pDao; EventDAO eDao; AuthTokenDAO aDao; @BeforeEach public void setUp() throws DataAccessException { db = new Database(); db.openConnection(); db.clearTables(); db.closeConnection(true); } @AfterEach public void tearDown() throws DataAccessException { db.openConnection(); db.clearTables(); db.closeConnection(true); } /** * Tests the clear service by inserting two users, persons, events, and authTokens into the database * and then checking that they are null after the clear * @throws DataAccessException */ @Test public void clearServicePass() throws DataAccessException { ArrayList<User> users = new ArrayList<>(); User user1 = new User("JohnA", "subway", "ja@gmail.com", "John", "Allen", "m", "abc123"); User user2 = new User("DaveB", "coolpass", "dave@gmail.com", "Dave", "Bellows", "m", "def456"); users.add(user1); users.add(user2); uDao = new UserDAO(db.getConnection()); uDao.createUser(user1); uDao.createUser(user2); db.closeConnection(true); ArrayList<Person> persons = new ArrayList<>(); Person person1 = new Person("1702", "JohnA", "Sally", "Jenkins", "f", "4029", "2093", "1023"); Person person2 = new Person("5407", "DaveB", "James", "Won", "m", "1409", "5019", "1059"); persons.add(person1); persons.add(person2); pDao = new PersonDAO(db.getConnection()); pDao.createPerson(person1); pDao.createPerson(person2); db.closeConnection(true); ArrayList<Event> events = new ArrayList<>(); Event event1 = new Event("0689", "JohnA", "1702", 12.0122f, 16.0325f, "Egypt", "Cairo", "Birth", 1980); Event event2 = new Event("1040", "DaveB", "5407", 47.1212f, 59.1323f, "Greece", "Athens", "Marriage", 1973); events.add(event1); events.add(event2); eDao = new EventDAO(db.getConnection()); eDao.createEvent(event1); eDao.createEvent(event2); db.closeConnection(true); ArrayList<AuthToken> authTokens = new ArrayList<>(); AuthToken authToken1 = new AuthToken("JohnA", "1237094"); AuthToken authToken2 = new AuthToken("DaveB", "1203856"); authTokens.add(authToken1); authTokens.add(authToken2); aDao = new AuthTokenDAO(db.getConnection()); aDao.createAuthToken(authToken1); aDao.createAuthToken(authToken2); db.closeConnection(true); //Clearing and confirming that results are as expected ClearService cService = new ClearService(); ClearResult cResult = cService.clear(); assertTrue(cResult.getMessage().contains("Clear succeeded")); assertEquals(true, cResult.isSuccess()); //Asserting that all the users, persons, events, and authTokens are now null (since we cleared them) for (User user : users) { db.openConnection(); uDao = new UserDAO(db.getConnection()); assertNull(uDao.getUserByID(user.getUsername())); db.closeConnection(false); } for (Person person : persons) { db.openConnection(); pDao = new PersonDAO(db.getConnection()); assertNull(pDao.getPersonByID(person.getPersonID())); db.closeConnection(false); } for (Event event : events) { db.openConnection(); eDao = new EventDAO(db.getConnection()); assertNull(eDao.getEventByID(event.getEventID())); db.closeConnection(false); } for (AuthToken authToken : authTokens) { db.openConnection(); aDao = new AuthTokenDAO(db.getConnection()); assertNull(aDao.getUsername(authToken.getUsername())); db.closeConnection(false); } } /** * There is not really a case where clear should fail, so this test case makes sure that clearing an * empty database doesn't give us any problems * @throws DataAccessException */ @Test public void clearServiceFail() throws DataAccessException { ClearService cService = new ClearService(); ClearResult cResult = cService.clear(); assertTrue(cResult.getMessage().contains("Clear succeeded")); assertEquals(true, cResult.isSuccess()); } }