001package edu.pdx.cs.joy.rmi;
002
003import org.junit.jupiter.api.Test;
004
005import java.rmi.RemoteException;
006import java.util.Iterator;
007import java.util.SortedSet;
008
009import static org.hamcrest.CoreMatchers.nullValue;
010import static org.hamcrest.MatcherAssert.assertThat;
011import static org.junit.jupiter.api.Assertions.*;
012
013/**
014 * Tests the behavior of the {@link MovieDatabase} class.
015 */
016public class MovieDatabaseTest {
017
018  private MovieDatabase getMovieDatabase() throws RemoteException {
019    return new MovieDatabaseImpl();
020  }
021
022  @Test
023  public void testCreateMovie() throws RemoteException {
024    MovieDatabase db = getMovieDatabase();
025    String title = "Movie 1";
026    int year = 2008;
027    long id = db.createMovie(title, year);
028    Movie movie = db.getMovie(id);
029    assertNotNull(movie);
030    assertEquals(title, movie.getTitle());
031    assertEquals(year, movie.getYear());
032    assertEquals(0, db.getFilmography(99L).size());
033  }
034
035  @Test
036  public void testFilmography() throws RemoteException {
037    long bobId = 56l;
038    long billId = 57l;
039
040    String title1 = "Bob's Movie 1";
041    int year1 = 2007;
042    String title2 = "Bob's Movie 2";
043    int year2 = 2008;
044
045    MovieDatabase db = getMovieDatabase();
046    long id1 = db.createMovie(title1, year1);
047    long id2 = db.createMovie(title2, year2);
048    db.noteCharacter(id1, "Joe", bobId);
049    db.noteCharacter(id2, "Frank", bobId);
050    db.noteCharacter(id2, "Henry", billId);
051
052    assertEquals(0, db.getFilmography(78L).size());
053
054    SortedSet<Movie> bobMovies = db.getFilmography(bobId);
055    assertEquals(2, bobMovies.size());
056    Iterator<Movie> bobIter = bobMovies.iterator();
057    Movie bobMovie1 = bobIter.next();
058    assertEquals(title1, bobMovie1.getTitle());
059    assertEquals(year1, bobMovie1.getYear());
060    Movie bobMovie2 = bobIter.next();
061    assertEquals(title2, bobMovie2.getTitle());
062    assertEquals(year2, bobMovie2.getYear());
063
064    SortedSet<Movie> billMovies = db.getFilmography(billId);
065    assertEquals(1, billMovies.size());
066    Movie billMovie = billMovies.iterator().next();
067    assertEquals(title2, billMovie.getTitle());
068    assertEquals(year2, billMovie.getYear());
069  }
070
071  @Test
072  public void deletingAnNonExistentMovieThrowsIllegalArgumentException() throws RemoteException {
073    MovieDatabase db = getMovieDatabase();
074    assertThrows(IllegalArgumentException.class, () -> db.deleteMovie(-1));
075  }
076
077  @Test
078  public void testDeleteMovie() throws RemoteException {
079    MovieDatabase db = getMovieDatabase();
080    long movieId = db.createMovie("Movie 1", 2015);
081    db.deleteMovie(movieId);
082    assertThat(db.getMovie(movieId), nullValue());
083  }
084}