diff --git a/src/test/java/com/s3ai/dao/DAOTest.java b/src/test/java/com/s3ai/dao/DAOTest.java new file mode 100644 index 0000000000000000000000000000000000000000..1d474bd80bdf2106c9941390ea579b0983589e3c --- /dev/null +++ b/src/test/java/com/s3ai/dao/DAOTest.java @@ -0,0 +1,152 @@ +package com.s3ai.dao; + +import com.s3ai.TestUtils; +import com.s3ai.entities.Cinema; +import com.s3ai.entities.Ticket; +import com.s3ai.entities.User; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import javax.persistence.NoResultException; +import java.io.IOException; +import java.util.List; +import java.util.UUID; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class DAOTest { + + private DAO daoUnderTest; + + @BeforeEach + void setUp() throws IOException { + daoUnderTest = DAO.getInstance(); + TestUtils.prepareTickets(); + } + + @Test + void testGetAllTickets() { + // Setup + final int expectedResult = 100; + + // Run the test + final List<Ticket> result = daoUnderTest.getAllTickets(); + + // Verify the results + assertEquals(100, result.size()); + } + + @Test + void testGetTicketById() { + // Setup + final Ticket expectedResult = TestUtils.getRandomElement(daoUnderTest.getAllTickets()); + + // Run the test + final Ticket result = daoUnderTest.getTicketById(expectedResult.getId()); + + // Verify the results + assertEquals(expectedResult, result); + } + + @Test + void testGetAllCinemas() { + // Setup + final int expectedResult = 100; + + // Run the test + final List<Cinema> result = daoUnderTest.getAllCinemas(); + + // Verify the results + assertEquals(expectedResult, result.size()); + } + + @Test + void testGetCinemaById() { + // Setup + final Cinema expectedResult = TestUtils.getRandomElement(daoUnderTest.getAllCinemas()); + + // Run the test + final Cinema result = daoUnderTest.getCinemaById(expectedResult.getId()); + + // Verify the results + assertEquals(expectedResult, result); + } + + @Test + void testGetAllUsers() { + // Setup + final int expectedResult = 100; + + // Run the test + final List<User> result = daoUnderTest.getAllUsers(); + + // Verify the results + assertEquals(expectedResult, result.size()); + } + + @Test + void testGetUserById() { + // Setup + final User expectedResult = TestUtils.getRandomElement(daoUnderTest.getAllUsers()); + + // Run the test + final User result = daoUnderTest.getUserById(expectedResult.getId()); + + // Verify the results + assertEquals(expectedResult, result); + } + + @Test + void testSaveEntity() { + // Setup + final User user = TestUtils.getRandomUser(); + + // Run the test + daoUnderTest.saveEntity(user); + + // Verify the results + assertEquals(user, daoUnderTest.getUserById(user.getId())); + } + + @Test + void testUpdateEntity() { + // Setup + final Cinema cinema = TestUtils.getRandomElement(daoUnderTest.getAllCinemas()); + ; + cinema.setLocation("TESTING"); + cinema.setName("TEST_NAME"); + cinema.setSeatsCount(300); + // Run the test + daoUnderTest.updateEntity(cinema); + + // Verify the results + assertEquals("TESTING", daoUnderTest.getCinemaById(cinema.getId()).getLocation()); + assertEquals("TEST_NAME", daoUnderTest.getCinemaById(cinema.getId()).getName()); + assertEquals(300, daoUnderTest.getCinemaById(cinema.getId()).getSeatsCount()); + } + + @Test + void testDeleteEntity() { + // Setup + final Ticket ticket = TestUtils.getRandomElement(daoUnderTest.getAllTickets()); + UUID id = ticket.getId(); + // Run the test + daoUnderTest.deleteEntity(ticket); + + // Verify the results + assertThrows(NoResultException.class, () -> daoUnderTest.getTicketById(id)); + } + + @Test + void testGetInstance() { + // Setup + final DAO expectedResult = DAO.getInstance(); + + // Run the test + final DAO result = DAO.getInstance(); + + // Verify the results + assertEquals(expectedResult, result); + } +}