streaming.test.StreamingTest.java Source code

Java tutorial

Introduction

Here is the source code for streaming.test.StreamingTest.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package streaming.test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.Persistence;
import junit.framework.Assert;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import streaming.entity.Episode;
import streaming.entity.Film;
import static streaming.entity.Film_.real;
import streaming.entity.Genre;
import streaming.entity.Lien;
import streaming.entity.Pays;
import streaming.entity.Realisateur;
import streaming.entity.Saison;
import streaming.entity.Serie;
import streaming.exception.SynopsisException;
import streaming.service.DBService;
import streaming.service.FilmService;
import streaming.service.GenreService;
import streaming.service.PaysService;
import streaming.service.RealisateurService;
import streaming.service.SerieService;

/**
 *
 * @author ajc
 */

@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(locations = "file:/C:\\Users\\ajc\\Documents\\NetBeansProjects\\Streaming\\application-context.xml")
@ContextConfiguration(locations = "file:/C:\\Users\\admin\\Desktop\\Projets\\Streaming\\application-context.xml")
public class StreamingTest {

    @Autowired
    private FilmService filmService;

    @Autowired
    private PaysService paysService;

    // @Before
    public void suppressionTest() throws SynopsisException {
        DBService db = new DBService();
        db.deleteAll();

        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();

        //Genre
        GenreService gs = new GenreService();
        Genre[] genre = { new Genre("Action"), new Genre("Fantastique"), new Genre("Policier"),
                new Genre("Romance"), new Genre("Animation"), new Genre("Comedie") };
        for (Genre g : genre) {
            gs.ajouter(g);
        }

        //Pays
        PaysService ps = new PaysService();
        Pays[] pays = { new Pays("France"), new Pays("USA"), new Pays("UK"), new Pays("Espagne"),
                new Pays("Nouvelle-Zlande") };
        for (Pays p : pays) {
            ps.ajouter(p);
        }

        //Realisateur
        RealisateurService rs = new RealisateurService();
        Realisateur[] real = { new Realisateur("Coen", "Joel"), new Realisateur("Coen", "Ethan"),
                new Realisateur("Jackson", "Peter"), new Realisateur("Tarantino", "Quentin"),
                new Realisateur("Mawenn", "") };
        for (Realisateur r : real) {
            rs.ajouter(r);
        }

        //Film
        FilmService fs = new FilmService();
        Film[] film = { new Film("Django Unchained", 2012L, "Synopsis"), new Film("Kill Bill", 2003L, "Synopsis"),
                new Film("Kill Bill 2", 2004L, "Synopsis"), new Film("Jackie Brown", 1997L, "Synopsis"),
                new Film("Sin City", 2005L, "Synopsis"), new Film("Bad Taste", 1987L, "Synopsis"),
                new Film("Braindead", 1992L, "Synopsis"), new Film("Le Hobbit", 2015L, "Synopsis"),
                new Film("Polisse", 2011L, "Synopsis"), new Film("Big Lebowski", 1998L, "Synopsis"),
                new Film("Ave Cesar", 2016L, "Synopsis") };
        for (Film f : film) {
            fs.ajouter(f);

        }
        //set Genre Action
        List<Film> action = new ArrayList<>();
        for (int i = 0; i <= 3; i++) {
            action.add(film[i]);
            film[i].setGenre(genre[0]);
            fs.modifier(film[i]);
        }
        genre[0].setFilm(action);
        gs.modifier(genre[0]);

        //set Genre Fantastique
        List<Film> fantastique = new ArrayList<>();
        for (int i = 4; i <= 7; i++) {
            fantastique.add(film[i]);
            film[i].setGenre(genre[1]);
            fs.modifier(film[i]);
        }
        genre[1].setFilm(fantastique);
        gs.modifier(genre[1]);

        //set Genre Fantastique
        List<Film> policier = new ArrayList<>();
        for (int i = 8; i <= 8; i++) {
            policier.add(film[i]);
            film[i].setGenre(genre[2]);
            fs.modifier(film[i]);
        }
        genre[2].setFilm(policier);
        gs.modifier(genre[2]);

        //set Genre Comedie
        List<Film> comedie = new ArrayList<>();
        for (int i = 9; i <= 10; i++) {
            comedie.add(film[i]);
            film[i].setGenre(genre[5]);
            fs.modifier(film[i]);
        }
        genre[3].setFilm(comedie);
        gs.modifier(genre[3]);

        //Set Pays USA
        List<Film> usa = new ArrayList<>();
        for (int i = 0; i <= 4; i++) {
            usa.add(film[i]);
            film[i].setPays(pays[1]);
            fs.modifier(film[i]);
        }
        for (int i = 9; i <= 10; i++) {
            usa.add(film[i]);
            film[i].setPays(pays[1]);
            fs.modifier(film[i]);
        }
        pays[1].setFilm(usa);
        ps.modifier(pays[1]);

        //Set Pays Nouvelle-Zelande
        List<Film> nz = new ArrayList<>();
        for (int i = 5; i <= 7; i++) {
            nz.add(film[i]);
            film[i].setPays(pays[4]);
            fs.modifier(film[i]);
        }
        pays[4].setFilm(nz);
        ps.modifier(pays[4]);

        //Set Pays France
        List<Film> fr = new ArrayList<>();
        for (int i = 8; i <= 8; i++) {
            fr.add(film[i]);
            film[i].setPays(pays[0]);
            fs.modifier(film[i]);
        }
        pays[0].setFilm(fr);
        ps.modifier(pays[0]);

        //Set Real Tarantino
        List<Film> tar = new ArrayList<>();
        for (int i = 0; i <= 4; i++) {
            tar.add(film[i]);
            film[i].getReal().add(real[3]);
            fs.modifier(film[i]);
        }
        real[3].setFilm(tar);
        rs.modifier(real[3]);

        //Set Real Jackson
        List<Film> jac = new ArrayList<>();
        for (int i = 5; i <= 7; i++) {
            jac.add(film[i]);
            film[i].getReal().add(real[2]);
            fs.modifier(film[i]);
        }
        real[2].setFilm(jac);
        rs.modifier(real[2]);

        //Set Real Mainwenn
        List<Film> mai = new ArrayList<>();
        for (int i = 8; i <= 8; i++) {
            mai.add(film[i]);
            film[i].getReal().add(real[4]);
            fs.modifier(film[i]);
        }
        real[4].setFilm(mai);
        rs.modifier(real[4]);

        //Set Real Coen
        List<Film> coen = new ArrayList<>();
        for (int i = 9; i <= 10; i++) {
            coen.add(film[i]);
            film[i].getReal().add(real[0]);
            film[i].getReal().add(real[1]);
            fs.modifier(film[i]);
        }
        real[0].setFilm(coen);
        rs.modifier(real[0]);
        real[1].setFilm(coen);
        rs.modifier(real[1]);
    }

    //  @Test
    public void creationSerie() {
        Serie serie = new Serie("Dexter", "Synopsis");

        for (Long i = 1L; i <= 8L; i++) {
            Saison saison = new Saison(i, 2005L + i);
            serie.getSaison().add(saison);
            saison.setSerie(serie);
            for (Long j = 1L; j <= i; j++) {
                Episode episode = new Episode(j, "E" + j);
                saison.getEpisode().add(episode);
                episode.setSaison(saison);
                for (Long k = 1L; k <= i; k++) {
                    Lien lien = new Lien("URL" + k);
                    episode.getLiens().add(lien);
                    lien.setEpisode(episode);
                }
            }
        }
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        em.getTransaction().begin();
        em.persist(serie);
        em.getTransaction().commit();

    }

    //@Test
    public void requete0() {
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out.println("Requete 0");
        System.out.println(em.createQuery("SELECT g FROM Genre g").getResultList());

    }
    // @Test

    public void requete1() {
        System.out.println("Requete 1");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out.println(em.createQuery("SELECT f FROM Film f ORDER BY f.anneeProd ASC ").getResultList());
    }

    // @Test
    public void requete2() {
        System.out.println("Requete 2");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        Assert.assertEquals(6, em.createQuery("SELECT g FROM Genre g").getResultList().size());
        Assert.assertEquals(6L, em.createQuery("SELECT count(g) FROM Genre g").getSingleResult());

    }

    //@Test
    public void requete3() {
        System.out.println("Requete 3");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        Assert.assertEquals(1,
                em.createQuery("SELECT f FROM Film f JOIN f.pays p WHERE p.nom='France'").getResultList().size());
    }

    // @Test
    public void requete4() {
        System.out.println("Requete 4");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out.println(
                em.createQuery("SELECT f FROM Film f JOIN f.genre g WHERE g.nom='Fantastique' ORDER BY f.titre ASC")
                        .getResultList());

    }

    // @Test
    public void requete5() {
        System.out.println("Requete 5");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out
                .println(em.createQuery("SELECT f FROM Film f WHERE f.genre.nom = 'Fantastique'").getResultList());
    }

    // @Test
    public void requete6() {
        System.out.println("Requete 6");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out.println(em.createQuery(
                "SELECT DISTINCT f FROM Film f Join f.genre g Join f.real r WHERE g.nom = 'Romance' AND r.nom='Coen' ORDER BY f.anneeProd")
                .getResultList());
    }
    //  @Test

    public void requete7() {
        System.out.println("Requete 7");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out.println(em.createQuery("SELECT f FROM Film f WHERE f.genre.nom <> 'Action'").getResultList());
    }

    //   @Test
    public void requete8() {
        System.out.println("Requete 8");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out.println(em.createQuery(
                "SELECT count(f) FROM Film f Join f.pays p Join f.genre g WHERE g.nom <> 'Fantastique' AND p.nom='Nouvelle-Zlande'")
                .getResultList());
    }

    //  @Test
    public void requete9() {
        System.out.println("Requete 9");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out.println(em.createQuery(
                "SELECT count(f) FROM Film f Join f.pays p Join f.real r WHERE p.nom='Nouvelle-Zlande' AND r.nom='Jackson'")
                .getResultList());
    }

    //  @Test
    public void requete10() {
        System.out.println("Requete 10");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out.println(em.createQuery(
                "SELECT f1 FROM Film f1 JOIN f1.real r WHERE r.prenom ='Joel' INTERSECT SELECT f2 FROM Film f2 JOIN f2.real r1 WHERE r1.prenom='Ethan'")
                .getResultList());
    }

    //   @Test
    public void requete11() {
        System.out.println("Requete 11");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out.println(em.createQuery(
                "SELECT f FROM Film f WHERE f.anneeProd=(SELECT MIN(f1.anneeProd) FROM Film f1 JOIN f1.real r1 WHERE r1.prenom ='Peter')")
                .getResultList());
    }

    //  @Test
    public void requete12() {
        System.out.println("Requete 12");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        System.out.println(em.createQuery("SELECT AVG(f.anneeProd) FROM Film f").getSingleResult());
    }

    //  @Test
    public void requete13() {
        System.out.println("Requete 13");
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
        Collection<Object[]> col = em
                .createQuery("SELECT COUNT(f.id), r FROM Film f JOIN f.real r GROUP BY r ORDER BY COUNT(f.id)")
                .getResultList();

        for (Object o[] : col) {
            System.out.println(o[0] + " " + ((Realisateur) o[1]).getNom() + " " + ((Realisateur) o[1]).getPrenom());
        }
    }

    //   @Test
    public void creationDBTest() {
        EntityManager em = Persistence.createEntityManagerFactory("StreamingPU").createEntityManager();
    }

    //@Test
    public void ajouterModifierOK() throws SynopsisException {
        Film f = new Film("ZUT", 1996L);
        f.setSynopsis("Synopsis");
        FilmService film = new FilmService();
        film.ajouter(f);
        System.out.println(film.rechercheGenre(1L));
        System.out.println(film.rechercheTitreReal("Coen"));
        System.out.println(film.rechercheTitreReal("Bill"));
    }

    //@Test(expected = SynopsisException.class)
    public void ajouterModifierKO() throws SynopsisException {
        Film f = new Film("ZUT", 1996L);
        FilmService film = new FilmService();
        film.ajouter(f);
    }

    //@Test
    public void testRequeteSerie() {
        SerieService ss = new SerieService();
        System.out.println(ss.recherchePays(1L));
        System.out.println(ss.rechercheTitre("ext"));
    }

    @Test
    public void testSpring() throws SynopsisException {

        filmService.ajouter(new Film("Origin master", 2016L, "Vous etes bons"));
        System.out.println(filmService.rechercheTitre("Origin master"));
        paysService.ajouter(new Pays("Canada"));
        System.out.println(paysService.rechercheNom("Canada"));

    }
}