package net.contargo.iris.address.staticsearch.service;

import net.contargo.iris.address.staticsearch.StaticAddress;
import net.contargo.iris.address.staticsearch.persistence.StaticAddressRepository;
import net.contargo.iris.normalizer.NormalizerServiceImpl;
import net.contargo.iris.sequence.UniqueIdSequence;
import net.contargo.iris.sequence.service.UniqueIdSequenceServiceImpl;

import org.junit.Before;
import org.junit.Test;

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 org.springframework.test.context.transaction.TransactionConfiguration;

import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.BigInteger;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import static org.hamcrest.CoreMatchers.is;

import static org.hamcrest.MatcherAssert.assertThat;


/**
 * @author  Arnold Franke - franke@synyx.de
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:application-context.xml")
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class StaticAddressServiceImplIntegrationTest {

    @PersistenceContext
    private EntityManager em;
    @Autowired
    private StaticAddressRepository staticAddressRepository;
    @Autowired
    private UniqueIdSequenceServiceImpl uniqueIdSequenceService;
    @Autowired
    private NormalizerServiceImpl normalizerService;

    private StaticAddressServiceImpl sut;

    private StaticAddress staticAddress;

    @Before
    public void setUp() {

        sut = new StaticAddressServiceImpl(staticAddressRepository, uniqueIdSequenceService, normalizerService);
        staticAddress = new StaticAddress();
        staticAddress.setLatitude(BigDecimal.ONE);
        staticAddress.setLongitude(BigDecimal.ONE);
        staticAddress.setCity("City");
        staticAddress.setCityNormalized("CITY");
        staticAddress.setPostalcode("12345");
        staticAddress.setCountry("DE");

        em.createQuery("DELETE FROM StaticAddress s").executeUpdate();
    }


    @Test
    public void testSaveWithoutUniqueId() {

        UniqueIdSequence nextSequenceObject = getNextSequenceObjectFromDb();

        BigInteger nextSequence = nextSequenceObject.getNextId();
        StaticAddress savedStaticAddress = sut.saveStaticAddress(staticAddress);
        assertThat(savedStaticAddress.getUniqueId(), is(nextSequence));

        assertThat(getNextSequenceObjectFromDb().getNextId(), is(nextSequence.add(BigInteger.ONE)));
    }


    private UniqueIdSequence getNextSequenceObjectFromDb() {

        String queryString = "SELECT o FROM UniqueIdSequence o WHERE o.entityName = 'StaticAddress'";

        return em.createQuery(queryString, UniqueIdSequence.class).getSingleResult();
    }
}

@@ -807,37 +807,8 @@
 l;%0A%0A
-import java.math.BigDecimal;%0A
 impo
@@ -1022,47 +1022,192 @@
 t;%0A%0A
-%0A/**%0A * @author  Arnold Franke - franke
+import static java.math.BigDecimal.ONE;%0A%0A%0A/**%0A * Unit test of %7B@link StaticAddressService Impl%7D.%0A *%0A * @author  Arnold Franke - franke@synyx.de%0A * @author  Tobias Schneider - schneider
 @syn
@@ -1965,16 +1965,17 @@
 rvice);%0A
+%0A
         
@@ -2041,35 +2041,24 @@
 setLatitude(
-BigDecimal.
 ONE);%0A      
@@ -2086,27 +2086,16 @@
 ngitude(
-BigDecimal.
 ONE);%0A  
@@ -2123,20 +2123,27 @@
 etCity(%22
-City
+Entenhausen
 %22);%0A    
@@ -2183,12 +2183,19 @@
 ed(%22
-CITY
+ENTENHAUSEN
 %22);%0A
@@ -2235,13 +2235,14 @@
 de(%22
-12345
+999999
 %22);%0A
@@ -2284,80 +2284,8 @@
 E%22);
-%0A%0A        em.createQuery(%22DELETE FROM StaticAddress s%22).executeUpdate();
 %0A   
@@ -2319,13 +2319,9 @@
 oid 
-testS
+s
 aveW
@@ -2417,17 +2417,16 @@
 omDb();%0A
-%0A
         
@@ -2479,24 +2479,25 @@
 etNextId();%0A
+%0A
         Stat
@@ -2637,17 +2637,16 @@
 ence));%0A
-%0A
         
@@ -2820,29 +2820,30 @@
     
-String queryString = 
+return em.createQuery(
 %22SEL
@@ -2909,18 +2909,17 @@
 ddress'%22
-;%0A
+,
 %0A       
@@ -2923,42 +2923,15 @@
     
-return em.createQuery(queryString,
+       
  Uni
@@ -2950,16 +2950,29 @@
 e.class)
+%0A            
 .getSing

make static address service integration test independent