tests created
This commit is contained in:
parent
a13be5d0c1
commit
b40cfbc901
@ -76,7 +76,7 @@ public class Queries {
|
||||
}
|
||||
|
||||
// ZAPYTANIE STRONICOWANE
|
||||
public static List<FilmModel> getAllFilmsByPage(int pageNumber, Session session) {
|
||||
public List<FilmModel> getAllFilmsByPage(int pageNumber, Session session) {
|
||||
//calculate total number
|
||||
Query queryTotal = session.createQuery("Select count(f) from FilmModel f");
|
||||
long countResult = (long)queryTotal.getSingleResult();
|
||||
|
@ -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<Object[]> mockQuery = mock(Query.class);
|
||||
when(mockSession.createQuery(anyString(), eq(Object[].class))).thenReturn(mockQuery);
|
||||
|
||||
String language = "English";
|
||||
String releaseYear = "2023";
|
||||
|
||||
// Test poprawnego zapytania
|
||||
List<Object[]> expectedResult = List.of(
|
||||
new Object[] { 1, "Film Title 1", "English" },
|
||||
new Object[] { 2, "Film Title 2", "English" }
|
||||
);
|
||||
when(mockQuery.getResultList()).thenReturn(expectedResult);
|
||||
|
||||
List<Object[]> result = queries.getFilmByLangYear(language, releaseYear);
|
||||
|
||||
assertNotNull(result);
|
||||
assertEquals(expectedResult.size(), result.size());
|
||||
|
||||
// Test sytuacji bez wyników
|
||||
when(mockQuery.getResultList()).thenReturn(List.of());
|
||||
List<Object[]> 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));
|
||||
}
|
||||
}
|
184
src/test/java/org/example/QueriesTest.java
Normal file
184
src/test/java/org/example/QueriesTest.java
Normal file
@ -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<Object[]> mockQuery = mock(Query.class);
|
||||
|
||||
List<Object[]> 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<Object[]> 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<FilmModel> 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<FilmModel> 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<Object[]> mockQuery = mock(Query.class);
|
||||
|
||||
List<Object[]> 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<Object[]> result = queries.getFilmByLangCategory("Action", "English");
|
||||
|
||||
assertEquals(expectedResults, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetFilmByLangCategoryWithoutResults() {
|
||||
Session mockSession = mock(Session.class);
|
||||
Query<Object[]> mockQuery = mock(Query.class);
|
||||
|
||||
List<Object[]> 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<Object[]> result = queries.getFilmByLangCategory("Comedy", "French");
|
||||
|
||||
assertTrue(result.isEmpty());
|
||||
}
|
||||
|
||||
// ZAPYTANIE STRONICOWANE
|
||||
@Test
|
||||
public void testGetFilmByLangYearWithoutResults() {
|
||||
Session mockSession = mock(Session.class);
|
||||
Query<Object[]> mockQuery = mock(Query.class);
|
||||
|
||||
List<Object[]> 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<Object[]> 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<FilmModel> 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<FilmModel> 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)));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user