com.zergiu.tvman.jobs.TestTVRageShowLoader.java Source code

Java tutorial

Introduction

Here is the source code for com.zergiu.tvman.jobs.TestTVRageShowLoader.java

Source

/*
 * Copyright (c) Sergiu Giurgiu 2012.
 *
 * This file is part of TVMan.
 *
 * TVMan is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * TVMan is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with TVMan.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.zergiu.tvman.jobs;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;

import org.jmock.Expectations;
import org.jmock.Mockery;
import org.jmock.integration.junit4.JMock;
import org.jmock.integration.junit4.JUnit4Mockery;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.oxm.Unmarshaller;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;

import com.zergiu.tvman.entities.TVShow;
import com.zergiu.tvman.entities.TVShowEpisode;
import com.zergiu.tvman.entities.TVShowSeason;
import com.zergiu.tvman.entities.TVShowStatus;
import com.zergiu.tvman.shows.tvrage.TVRageShowLoaderJob;

/**
 * @author sergiu
 *
 */
@RunWith(JMock.class)
public class TestTVRageShowLoader {

    private TVRageShowLoaderJob showLoader;
    private JobExecutionContext jobContext;
    private JobDetail jobDetail;
    private Mockery context = new JUnit4Mockery();
    private TVShow show;

    /**
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception {
        jobContext = context.mock(JobExecutionContext.class);
        jobDetail = context.mock(JobDetail.class);
        show = new TVShow();
        show.setId(1L);
        show.setLanguage("en");
    }

    /**
     * @throws java.lang.Exception
     */
    @After
    public void tearDown() throws Exception {
    }

    /**
     * Test method for {@link com.zergiu.tvman.shows.tvrage.TVRageShowLoaderJob#execute(org.quartz.JobExecutionContext)}.
     * @throws JobExecutionException 
     */
    @Test
    public void testExecuteBuffyTheVampireSlayer() throws JobExecutionException {
        context.checking(new Expectations() {
            {
                oneOf(jobContext).getJobDetail();
                will(returnValue(jobDetail));
            }
        });

        context.checking(new Expectations() {
            {
                oneOf(jobDetail).getJobBuilder();
                will(returnValue(JobBuilder.newJob()));
            }
        });

        show.setProviderSeriesId("2930");
        showLoader = createShowLoader("resources/tvrage/buffy_full_show_info.xml");

        showLoader.execute(jobContext);
        context.assertIsSatisfied();
        assertNotNull(show.getSeasons());
        assertEquals(7, show.getSeasons().size());
        assertEquals(144, getEpisodeCount());
        assertEquals(TVShowStatus.Ended, show.getStatus());
        Calendar firstAirDate = Calendar.getInstance();
        firstAirDate.set(Calendar.YEAR, 1997);
        firstAirDate.set(Calendar.MONTH, Calendar.MARCH);
        firstAirDate.set(Calendar.DAY_OF_MONTH, 10);
        firstAirDate.set(Calendar.HOUR_OF_DAY, 0);
        firstAirDate.set(Calendar.MINUTE, 0);
        firstAirDate.set(Calendar.SECOND, 0);
        firstAirDate.set(Calendar.MILLISECOND, 0);
        assertEquals(firstAirDate.getTime(), show.getFirstAirDate());
        for (TVShowSeason season : show.getSeasons()) {
            for (TVShowEpisode ep : season.getEpisodes()) {
                assertNotNull(ep.getAirDate());
            }
        }
    }

    @Test
    public void testExecuteFringe() throws JobExecutionException {
        context.checking(new Expectations() {
            {
                oneOf(jobContext).getJobDetail();
                will(returnValue(jobDetail));
            }
        });

        context.checking(new Expectations() {
            {
                oneOf(jobDetail).getJobBuilder();
                will(returnValue(JobBuilder.newJob()));
            }
        });

        show.setProviderSeriesId("18388");
        showLoader = createShowLoader("resources/tvrage/fringe_full_show_info.xml");

        showLoader.execute(jobContext);
        context.assertIsSatisfied();
        assertNotNull(show.getSeasons());
        assertEquals(4, show.getSeasons().size());
        assertEquals(80, getEpisodeCount());
        assertEquals(TVShowStatus.Continuing, show.getStatus());
        Calendar firstAirDate = Calendar.getInstance();
        firstAirDate.set(Calendar.YEAR, 2008);
        firstAirDate.set(Calendar.MONTH, Calendar.SEPTEMBER);
        firstAirDate.set(Calendar.DAY_OF_MONTH, 9);
        firstAirDate.set(Calendar.HOUR_OF_DAY, 0);
        firstAirDate.set(Calendar.MINUTE, 0);
        firstAirDate.set(Calendar.SECOND, 0);
        firstAirDate.set(Calendar.MILLISECOND, 0);
        assertEquals(firstAirDate.getTime(), show.getFirstAirDate());
        assertEquals("FOX", show.getNetwork());
        for (TVShowSeason season : show.getSeasons()) {
            for (TVShowEpisode ep : season.getEpisodes()) {
                assertNotNull(ep.getAirDate());
            }
        }
    }

    /**
     * @return
     */
    private Object getEpisodeCount() {
        int count = 0;
        for (TVShowSeason season : show.getSeasons()) {
            count += season.getEpisodes().size();
        }
        return count;
    }

    /**
     * @param string
     * @return
     */
    private TVRageShowLoaderJob createShowLoader(final String resourcePath) {
        showLoader = new TVRageShowLoaderJob() {

            /* (non-Javadoc)
             * @see com.zergiu.tvman.shows.tvrage.TVRageShowLoaderJob#getShow(org.quartz.JobExecutionContext)
             */
            @Override
            protected TVShow getShow(JobExecutionContext context) {
                return show;
            }

            /* (non-Javadoc)
             * @see com.zergiu.tvman.shows.tvrage.TVRageShowLoaderJob#getShowDataStream(java.lang.String)
             */
            @Override
            protected InputStream getShowDataStream(String urlString) throws IOException {
                return TestTVRageShowLoader.class.getResourceAsStream(resourcePath);
            }

            @Override
            protected Unmarshaller getUnmarshaller() {
                Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
                marshaller.setPackagesToScan(new String[] { "com.zergiu.tvman.shows.tvrage.entities" });

                return marshaller;
            }

        };
        return showLoader;
    }

}