package services; import dao.*; import org.junit.jupiter.api.*; import request.RegisterRequest; import result.RegisterResult; import static org.junit.jupiter.api.Assertions.*; public class RegisterServiceTest { private Database db; @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 that a registerResult's success variable returns false whenever a register request is given that contains a duplicate username * @throws DataAccessException */ @Test public void registerServicePass() throws DataAccessException { RegisterService rService = new RegisterService(); RegisterRequest request = new RegisterRequest("Daniel", "secretkey", "dg123@gmail.com", "Daniel", "Goode", "m"); RegisterResult rResult = rService.register(request); assertNotNull(rResult.getAuthtoken()); assertEquals(rResult.getUsername(), request.getUsername()); assertNotNull(rResult.getPersonID()); assertTrue(rResult.isSuccess()); } /** * Creates a registerRequest, attempts to register the user in the database, and checks to make sure that * the register result was a success and contains the same info as the request * @throws DataAccessException */ @Test public void registerServiceFail() throws DataAccessException { RegisterService rService = new RegisterService(); RegisterRequest request = new RegisterRequest("Daniel", "secretkey", "dg123@gmail.com", "Daniel", "Goode", "m"); RegisterResult rResult = rService.register(request); //Creating a second request that has the same username as the first; the result of this should be unsuccessful RegisterRequest duplicateRequest = new RegisterRequest("Daniel", "coolpassword", "daniel@outlook.com", "Daniel", "Friedman", "m"); RegisterResult rResult2 = rService.register(duplicateRequest); assertTrue(rResult2.getMessage().contains("Error:")); assertFalse(rResult2.isSuccess()); } }