package org.itevents.dao.mybatis.mapper;

import com.github.springtestdbunit.annotation.*;
import com.github.springtestdbunit.assertion.DatabaseAssertionMode;
import org.itevents.AbstractDbTest;
import org.itevents.model.City;
import org.itevents_utils.BuilderUtil;
import org.itevents_utils.dbunit.dataset_loader.CityReplacementDataSetLoader;
import org.junit.Test;
import org.springframework.dao.DuplicateKeyException;

import javax.inject.Inject;

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

/**
 * Created by vaa25 on 22.07.2015.
 */

@DatabaseSetup(value = "file:src/test/resources/dbunit/CityMapperTest/CityMapperTest_initial.xml",
        type = DatabaseOperation.REFRESH)
@DatabaseTearDown(value = "file:src/test/resources/dbunit/CityMapperTest/CityMapperTest_initial.xml",
        type = DatabaseOperation.DELETE_ALL)
@DbUnitConfiguration(databaseConnection = "dbUnitDatabaseConnection", dataSetLoader = CityReplacementDataSetLoader.class)
public class CityMapperTest extends AbstractDbTest {

    private final String TEST_PATH = PATH + "CityMapperTest/";
    @Inject
    private CityMapper cityMapper;

    @Test
    public void testGetCitySuccess() throws Exception {
        City expectedCity = BuilderUtil.buildCityKyiv();
        City returnedCity = cityMapper.getCity(ID_1);
        assertEquals(expectedCity, returnedCity);
    }

    @Test
    public void testGetCityFail() throws Exception {
        City returnedCity = cityMapper.getCity(ID_0);
        assertNull(returnedCity);
    }

    @Test
    @ExpectedDatabase(value = TEST_PATH + "testAddCity_expected.xml",
            assertionMode = DatabaseAssertionMode.NON_STRICT_UNORDERED)
    public void testAddCity() throws Exception {
        City testCity = BuilderUtil.buildCityTest();
        cityMapper.addCity(testCity);
    }

    @Test(expected = DuplicateKeyException.class)
    @DatabaseSetup(value = TEST_PATH + "testAddExistingCity_Initial.xml", type = DatabaseOperation.REFRESH)
    @ExpectedDatabase(value = TEST_PATH + "testAddCity_expected.xml",
            assertionMode = DatabaseAssertionMode.NON_STRICT_UNORDERED)
    public void testAddExistingCity() throws Exception {
        City testCity = BuilderUtil.buildCityTest();
        cityMapper.addCity(testCity);
    }

    @Test
    public void testGetAllCities() {
        int expectedSize = 4;
        int returnedSize = cityMapper.getAllCities().size();
        assertEquals(expectedSize, returnedSize);
    }

    @Test
    @DatabaseSetup(value = TEST_PATH + "testAddExistingCity_Initial.xml", type = DatabaseOperation.REFRESH)
    @ExpectedDatabase(value = TEST_PATH + "CityMapperTest_initial.xml",
            assertionMode = DatabaseAssertionMode.NON_STRICT_UNORDERED)
    public void testRemoveCitySuccess() {
        City testCity = BuilderUtil.buildCityTest();
        cityMapper.removeCity(testCity);
    }

    @Test
    @ExpectedDatabase(value = TEST_PATH + "CityMapperTest_initial.xml",
            assertionMode = DatabaseAssertionMode.NON_STRICT_UNORDERED)
    public void testRemoveCityFail() {
        City testCity = BuilderUtil.buildCityTest();
        cityMapper.removeCity(testCity);
    }
}
@@ -1182,26 +1182,26 @@
 oid 
-testGetCitySuccess
+shouldFindCityById
 () t
@@ -1416,23 +1416,34 @@
 oid 
-testGetCityFail
+exceptNullWhenCityIsAbsent
 () t
@@ -1723,20 +1723,22 @@
 ic void 
-test
+should
 AddCity(
@@ -2172,20 +2172,52 @@
 ic void 
-test
+shouldThrowDuplicateKeyExceptionWhen
 AddExist
@@ -2369,20 +2369,22 @@
 ic void 
-test
+should
 GetAllCi
@@ -2810,36 +2810,38 @@
     public void 
-test
+should
 RemoveCitySucces
@@ -2838,15 +2838,8 @@
 City
-Success
 () %7B
@@ -3114,26 +3114,33 @@
 oid 
-testRemoveCityFail
+shouldNotRemoveAbsentCity
 () %7B

rename CityMapperTest methods