com.siriusit.spezg.multilib.storage.jpa.JpaLoanRepositoryTest.java Source code

Java tutorial

Introduction

Here is the source code for com.siriusit.spezg.multilib.storage.jpa.JpaLoanRepositoryTest.java

Source

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.siriusit.spezg.multilib.storage.jpa;

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

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.unitils.UnitilsJUnit4TestClassRunner;
import org.unitils.dbunit.annotation.DataSet;
import org.unitils.spring.annotation.SpringApplicationContext;
import org.unitils.spring.annotation.SpringBeanByType;

import com.siriusit.spezg.multilib.domain.Loan;
import com.siriusit.spezg.multilib.domain.Person;
import com.siriusit.spezg.multilib.domain.Unit;
import com.siriusit.spezg.multilib.storage.LoanRepository;
import com.siriusit.spezg.multilib.storage.PersonRepository;
import com.siriusit.spezg.multilib.storage.UnitRepository;

/**
 * 
 * @author Thao Nguyen <thao.nguyen@SiriusIT.com>
 */

@DataSet({ "/com/siriusit/spezg/multilib/jpa/Person.db.xml", "/com/siriusit/spezg/multilib/jpa/Unit.db.xml",
        "/com/siriusit/spezg/multilib/jpa/Loan.db.xml" })
@RunWith(UnitilsJUnit4TestClassRunner.class)
public class JpaLoanRepositoryTest {

    @SpringApplicationContext("classpath:com/siriusit/spezg/multilib/jpa/spring-config-domain-test.xml")
    private ApplicationContext applicationContext;
    @SpringBeanByType
    private LoanRepository loanRepository;
    @SpringBeanByType
    private PersonRepository personRepository;
    @SpringBeanByType
    private UnitRepository unitRepository;
    private static final Logger logger = LoggerFactory.getLogger(JpaLoanRepositoryTest.class);

    @Test
    public void testStoreLoanToLoanDB() {
        Person owner = personRepository.findPersonByUsername("JoFerraro");
        Unit film = unitRepository.findUnitsByTitle("Title2").get(0);
        Date dateOut = new Date();
        Calendar deadline = Calendar.getInstance();
        deadline.roll(Calendar.MONTH, 1);
        Loan result = loanRepository.createLoan(owner, film, null, dateOut, deadline.getTime());
        assertTrue(result instanceof Loan);
        assertEquals(film.getDescription(), result.getLoanObject().getDescription());
        assertEquals(owner.getMail(), result.getLoaner().getMail());
    }

    @Test
    public void testFindNumberOfUser() {
        Person owner = personRepository.findPersonByUsername("LatinKing");
        List<? extends Loan> resultList = loanRepository.findLoanByOwner(owner);
        Loan result = resultList.get(0);
        String expectedTitle = "Title5";
        String actualTitle = result.getLoanObject().getTitle();
        assertTrue(resultList.size() == 1);
        assertTrue(result instanceof Loan);
        assertEquals(expectedTitle, actualTitle);
    }

}