de.msg.repository.RouteRepositoryImplJpaTest.java Source code

Java tutorial

Introduction

Here is the source code for de.msg.repository.RouteRepositoryImplJpaTest.java

Source

/*
 * Copyright (C) 2016 msg-systems ag
 *
 * Licensed 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.
 *
 *
 * @author Rafael Kansy
 * @author Michael Schaefer
 */

package de.msg.repository;

import static org.springframework.test.annotation.DirtiesContext.ClassMode.BEFORE_EACH_TEST_METHOD;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;

import de.msg.domain.route.Route;

@DataJpaTest
@ComponentScan("de.msg.domain")
@RunWith(SpringRunner.class)
@DirtiesContext(classMode = BEFORE_EACH_TEST_METHOD)
public class RouteRepositoryImplJpaTest {

    // ID is generated by database so it is complicated to hard code it in integration tests.
    private static long ID = 40L;

    @Autowired
    private RouteRepository repository;

    @Before
    public void getId() {
        ID = repository.findAll().iterator().next().getId();
    }

    @Test
    public void create() throws Exception {
        long expected = repository.count() + 1;

        Route entity = new Route("LH400", "MUC", "NYC");
        repository.save(entity);

        long actual = repository.count();
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void createIterable() throws Exception {
        long expected = repository.count() + 2;

        List<Route> entities = new ArrayList<>();
        entities.add(new Route("LH400", "MUC", "NYC"));
        entities.add(new Route("LH450", "NYC", "MUC"));

        repository.save(entities);

        long actual = repository.count();
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void exists() throws Exception {
        boolean actual = repository.exists(ID);

        Assert.assertEquals(true, actual);
    }

    @Test
    public void findOne() throws Exception {
        Route expected = new Route("LH7902", "MUC", "IAH");

        Route actual = repository.findOne(ID);

        Assert.assertEquals(expected.getFlightNumber(), actual.getFlightNumber());
        Assert.assertEquals(expected.getDeparture(), actual.getDeparture());
        Assert.assertEquals(expected.getDestination(), actual.getDestination());
    }

    @Test
    public void findAll() throws Exception {
        Iterable<Route> actual = repository.findAll();
        Assert.assertNotNull(actual.iterator().hasNext());
    }

    @Test
    public void findByDeparture() throws Exception {
        Iterable<Route> actual = repository.findByDeparture("MUC");
        Assert.assertEquals("MUC", actual.iterator().next().getDeparture());
    }

    @Test
    public void queryFindByDeparture() throws Exception {
        Iterable<Route> actual = repository.queryFindByDeparture("MUC");
        Assert.assertEquals("MUC", actual.iterator().next().getDeparture());
    }

    @Test
    public void findByDestination() throws Exception {
        Iterable<Route> actual = repository.findByDestination("IAH");
        Assert.assertEquals("IAH", actual.iterator().next().getDestination());
    }

    @Test
    public void update() throws Exception {
        Route expected = new Route("LH7902", "MUC", "BAR");

        Route entity = repository.findOne(ID);
        entity.setDestination("BAR");
        repository.save(entity);

        Route actual = repository.findOne(ID);
        Assert.assertEquals(expected.getFlightNumber(), actual.getFlightNumber());
        Assert.assertEquals(expected.getDeparture(), actual.getDeparture());
        Assert.assertEquals(expected.getDestination(), actual.getDestination());
    }

    @Test
    public void delete() throws Exception {
        long expected = repository.count() - 1;

        repository.delete(ID);

        long actual = repository.count();
        Assert.assertEquals(expected, actual);
    }

    @Test
    public void deleteAll() throws Exception {
        long expected = 0;

        repository.deleteAll();

        long actual = repository.count();
        Assert.assertEquals(expected, actual);
    }
}