package benjamin.groehbiel.ch;

import benjamin.groehbiel.ch.shortener.DictionaryHash;
import benjamin.groehbiel.ch.shortener.DictionaryRepository;
import org.junit.After;
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.SpringApplicationConfiguration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.sql.DataSource;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ApplicationTest.class, initializers = PersistenceInitializer.class)
public class PostgresTest {

    JdbcTemplate jdbcTemplate;

    @Autowired
    DictionaryRepository dictionaryRepository;

    @Autowired
    public void setDataSource(DataSource dataSource) {
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    @After
    @Before
    public void clearDb() {
        dictionaryRepository.deleteAll();
        jdbcTemplate.execute("ALTER SEQUENCE dictionary_hash_id_seq RESTART WITH 1;");
    }

    @Test
    public void isZeroCountWhenNoInserts() {
        assertThat(dictionaryRepository.count(), equalTo(0L));
    }

    @Test
    public void insertHash() {
        DictionaryHash dictionaryItem = new DictionaryHash("water", "en", "blablabla", false);
        DictionaryHash newDictionaryItem = dictionaryRepository.save(dictionaryItem);
        assertThat(dictionaryRepository.count(), equalTo(1L));
        assertThat(newDictionaryItem.getHashId(), equalTo(1L));
    }

}

@@ -68,83 +68,38 @@
 ner.
-DictionaryHash;%0Aimport benjamin.groehbiel.ch.shortener.DictionaryRepository
+*;%0Aimport org.hamcrest.Matcher
 ;%0Aim
@@ -333,16 +333,70 @@
 ration;%0A
+import org.springframework.core.io.ClassPathResource;%0A
 import o
@@ -541,16 +541,139 @@
 ource;%0A%0A
+import java.io.File;%0Aimport java.io.IOException;%0Aimport java.util.List;%0A%0Aimport static java.util.stream.Collectors.toList;%0A
 import s
@@ -1840,10 +1840,561 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void addAllHashesToDatabase() throws IOException %7B%0A        File wordNet = new ClassPathResource(%22WordNet%22).getFile();%0A        List%3CWordDefinition%3E englishWords = WordNetHelper.load(wordNet.getPath());%0A%0A        List%3CDictionaryHash%3E dictionaryHashes = englishWords.stream().map(w -%3E %7B%0A            return new DictionaryHash(w.getWord(), %22en%22, w.getDescription(), false);%0A        %7D).collect(toList());%0A%0A        dictionaryRepository.save(dictionaryHashes);%0A%0A        assertThat(dictionaryRepository.count(), equalTo((long) 21));%0A    %7D%0A%0A
 %7D%0A

Add all wordnet entries to database