From b40cfbc901c6cad16c7b91dd5824f92ffcdaf81b Mon Sep 17 00:00:00 2001 From: Marcin Hutek Date: Tue, 2 Jan 2024 19:25:27 +0100 Subject: [PATCH] tests created --- src/main/java/org/example/Queries.java | 2 +- .../org/example/HibernateExampleTest.java | 51 ----- src/test/java/org/example/QueriesTest.java | 184 ++++++++++++++++++ 3 files changed, 185 insertions(+), 52 deletions(-) delete mode 100644 src/test/java/org/example/HibernateExampleTest.java create mode 100644 src/test/java/org/example/QueriesTest.java diff --git a/src/main/java/org/example/Queries.java b/src/main/java/org/example/Queries.java index 7c6029e..1703726 100644 --- a/src/main/java/org/example/Queries.java +++ b/src/main/java/org/example/Queries.java @@ -76,7 +76,7 @@ public class Queries { } // ZAPYTANIE STRONICOWANE - public static List getAllFilmsByPage(int pageNumber, Session session) { + public List getAllFilmsByPage(int pageNumber, Session session) { //calculate total number Query queryTotal = session.createQuery("Select count(f) from FilmModel f"); long countResult = (long)queryTotal.getSingleResult(); diff --git a/src/test/java/org/example/HibernateExampleTest.java b/src/test/java/org/example/HibernateExampleTest.java deleted file mode 100644 index 91ca475..0000000 --- a/src/test/java/org/example/HibernateExampleTest.java +++ /dev/null @@ -1,51 +0,0 @@ -import org.example.Queries; -import org.hibernate.Session; -import org.hibernate.query.Query; -import org.junit.jupiter.api.Test; - -import java.util.List; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.*; - -public class QueriesTest { - - @Test - public void testGetFilmByLangYear() { - // Przygotowanie testowych danych - Session mockSession = mock(Session.class); - Queries queries = new Queries(mockSession); - - Query mockQuery = mock(Query.class); - when(mockSession.createQuery(anyString(), eq(Object[].class))).thenReturn(mockQuery); - - String language = "English"; - String releaseYear = "2023"; - - // Test poprawnego zapytania - List expectedResult = List.of( - new Object[] { 1, "Film Title 1", "English" }, - new Object[] { 2, "Film Title 2", "English" } - ); - when(mockQuery.getResultList()).thenReturn(expectedResult); - - List result = queries.getFilmByLangYear(language, releaseYear); - - assertNotNull(result); - assertEquals(expectedResult.size(), result.size()); - - // Test sytuacji bez wyników - when(mockQuery.getResultList()).thenReturn(List.of()); - List emptyResult = queries.getFilmByLangYear(language, releaseYear); - assertNotNull(emptyResult); - assertTrue(emptyResult.isEmpty()); - - // Inne testy, np. obsługa parametrów null - // ... - - // Sprawdzenie, czy metoda została wywołana z oczekiwanymi parametrami - verify(mockQuery).setParameter("language", language); - verify(mockQuery).setParameter("releaseYear", releaseYear); - verify(mockSession).createQuery(anyString(), eq(Object[].class)); - } -} diff --git a/src/test/java/org/example/QueriesTest.java b/src/test/java/org/example/QueriesTest.java new file mode 100644 index 0000000..3027633 --- /dev/null +++ b/src/test/java/org/example/QueriesTest.java @@ -0,0 +1,184 @@ +package org.example; + +import org.hibernate.Session; +import org.hibernate.query.Query; +import org.junit.jupiter.api.Test; + +import java.time.ZonedDateTime; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class QueriesTest { + + private FilmModel createFilm(String title) { + FilmModel film = new FilmModel(); + film.setFilmTitle(title); + return film; + } + + // ZAPYTANIE ZA PARAMETREM + @Test + public void testGetFilmByLangYearWithResults() { + // Ustawienie danych testowych + Session mockSession = mock(Session.class); + Query mockQuery = mock(Query.class); + + List expectedResults = Arrays.asList( + new Object[]{1, "Film1", "English"}, + new Object[]{2, "Film2", "Spanish"} + ); + + when(mockSession.createQuery(anyString(), eq(Object[].class))).thenReturn(mockQuery); + when(mockQuery.setParameter("language", "English")).thenReturn(mockQuery); + when(mockQuery.setParameter("releaseYear", "2022")).thenReturn(mockQuery); + when(mockQuery.getResultList()).thenReturn(expectedResults); + + Queries queries = new Queries(mockSession); + List result = queries.getFilmByLangYear("English", "2022"); + + assertEquals(expectedResults, result); + } + + @Test + public void testGetAllFilmsByPage() { + Session mockSession = mock(Session.class); + Query mockQueryTotal = mock(Query.class); + Query mockQuery = mock(Query.class); + + long totalCount = 3; + when(mockSession.createQuery("Select count(f) from FilmModel f")).thenReturn(mockQueryTotal); + when(mockQueryTotal.getSingleResult()).thenReturn(totalCount); + + List filmList = Arrays.asList( + createFilm("Test1"), + createFilm("Test2"), + createFilm("Test3") + ); + + when(mockSession.createQuery("Select f from FilmModel f")).thenReturn(mockQuery); + when(mockQuery.setFirstResult(anyInt())).thenReturn(mockQuery); + when(mockQuery.setMaxResults(anyInt())).thenReturn(mockQuery); + when(mockQuery.getResultList()).thenReturn(filmList); + + Queries queries = new Queries(mockSession); + List result = queries.getAllFilmsByPage(1, mockSession); + + assertEquals(filmList, result); + verify(mockQueryTotal).getSingleResult(); + verify(mockQuery).setFirstResult(0); + verify(mockQuery).setMaxResults(10); + } + + // ZAPYTANIE Z PARAMETREM + @Test + public void testGetFilmByLangCategoryWithResults() { + Session mockSession = mock(Session.class); + Query mockQuery = mock(Query.class); + + List expectedResults = Arrays.asList( + new Object[]{1, "Film1", "English", "Action"}, + new Object[]{2, "Film2", "Spanish", "Drama"} + ); + + when(mockSession.createQuery(anyString(), eq(Object[].class))).thenReturn(mockQuery); + when(mockQuery.setParameter("categoryName", "Action")).thenReturn(mockQuery); + when(mockQuery.setParameter("language", "English")).thenReturn(mockQuery); + when(mockQuery.getResultList()).thenReturn(expectedResults); + + Queries queries = new Queries(mockSession); + List result = queries.getFilmByLangCategory("Action", "English"); + + assertEquals(expectedResults, result); + } + + @Test + public void testGetFilmByLangCategoryWithoutResults() { + Session mockSession = mock(Session.class); + Query mockQuery = mock(Query.class); + + List expectedResults = Collections.emptyList(); // Brak wynikow + + when(mockSession.createQuery(anyString(), eq(Object[].class))).thenReturn(mockQuery); + when(mockQuery.setParameter("categoryName", "Comedy")).thenReturn(mockQuery); + when(mockQuery.setParameter("language", "French")).thenReturn(mockQuery); + when(mockQuery.getResultList()).thenReturn(expectedResults); + + Queries queries = new Queries(mockSession); + List result = queries.getFilmByLangCategory("Comedy", "French"); + + assertTrue(result.isEmpty()); + } + + // ZAPYTANIE STRONICOWANE + @Test + public void testGetFilmByLangYearWithoutResults() { + Session mockSession = mock(Session.class); + Query mockQuery = mock(Query.class); + + List expectedResults = Collections.emptyList(); // Brak wynikow + + when(mockSession.createQuery(anyString(), eq(Object[].class))).thenReturn(mockQuery); + when(mockQuery.setParameter("language", "French")).thenReturn(mockQuery); + when(mockQuery.setParameter("releaseYear", "2022")).thenReturn(mockQuery); + when(mockQuery.getResultList()).thenReturn(expectedResults); + + Queries queries = new Queries(mockSession); + List result = queries.getFilmByLangYear("French", "2022"); + + assertTrue(result.isEmpty()); + } + + @Test + public void testGetAllFilmsByPageWhenNoFilms() { + Session mockSession = mock(Session.class); + Query mockQueryTotal = mock(Query.class); + Query mockQuery = mock(Query.class); + + long totalCount = 0; + when(mockSession.createQuery("Select count(f) from FilmModel f")).thenReturn(mockQueryTotal); + when(mockQueryTotal.getSingleResult()).thenReturn(totalCount); + + List filmList = Arrays.asList(); // pusta lista filmów + + when(mockSession.createQuery("Select f from FilmModel f")).thenReturn(mockQuery); + when(mockQuery.getResultList()).thenReturn(filmList); + + Queries queries = new Queries(mockSession); + List result = queries.getAllFilmsByPage(1, mockSession); + + assertEquals(filmList, result); + verify(mockQueryTotal).getSingleResult(); + } + + // SPRAWDZENIE CZY UTWORZONY OBIEKT ZAWIERA DANE ZGODNE ZE WSTEPNIE WPROWADZONYMI W DEFINICJI METODY + @Test + public void testCreateFilmModelWithExpectedData() { + Session mockSession = mock(Session.class); + Queries queries = new Queries(mockSession); + FilmModel filmModel = queries.createFilmModel(); + + // Sprawdzenie, czy utworzony obiekt zawiera oczekiwane dane + assertNotNull(filmModel); + assertEquals("Test Film Title", filmModel.getFilmTitle()); + + FilmInfoModel filmInfoModel = filmModel.getFilmInfo(); + assertNotNull(filmInfoModel); + assertEquals("Test Movie Description", filmInfoModel.getFilmDescription()); + assertEquals(2023, filmInfoModel.getReleaseYear()); + assertEquals(1, filmInfoModel.getFilmLanguage()); + assertEquals(100, filmInfoModel.getFilmLength()); + assertEquals("test", filmInfoModel.getFilmRating()); + assertEquals(1, filmInfoModel.getFilmCategory()); + + ZonedDateTime currentTimestamp = ZonedDateTime.now(); + ZonedDateTime filmInfoTimestamp = filmInfoModel.getLastUpdate(); + + // Test timestampow z pozostawieniem pewnej tolerancji czasowej + assertTrue(filmInfoTimestamp.isBefore(currentTimestamp.plusSeconds(5)) && + filmInfoTimestamp.isAfter(currentTimestamp.minusSeconds(5))); + } +}