package org.quickat.web;

import com.jayway.restassured.RestAssured;
import org.apache.http.HttpStatus;
import org.hamcrest.Matchers;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.quickat.QuickatApplication;
import org.quickat.da.Quickie;
import org.quickat.da.builder.QuickieBuilder;
import org.quickat.repository.QuickiesRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Date;
import java.util.Properties;

import static com.jayway.restassured.RestAssured.when;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = TestConfiguration.class)
@WebAppConfiguration
@IntegrationTest("server.port:0")
public class QuickieControllerTest {

    @Value("${local.server.port}")
    int port;

    @Autowired
    public QuickiesRepository quickiesRepository;

    @Before
    public void setUp() throws Exception {
        quickiesRepository.deleteAll();
        Quickie springBootQuickie = QuickieBuilder.aQuickie().withDate(new Date()).
                withTitle("Spring Boot").
                build();
        Quickie scalaQuickie = QuickieBuilder.aQuickie().withDate(new Date()).
                withTitle("Scala").
                build();
        Quickie vagrantQuickie = QuickieBuilder.aQuickie().withDate(new Date()).
                withTitle("Vagrant").
                build();
        quickiesRepository.save(Arrays.asList(scalaQuickie,springBootQuickie,vagrantQuickie));

        RestAssured.port = port;
    }

    @After
    public void tearDown() throws Exception {

    }

    @Test
    public void testGetQuickies() throws Exception {

        when().get("/quickies").
                then().
                statusCode(HttpStatus.SC_OK).
                body("title", Matchers.hasItems("Vagrant", "Scala", "Spring Boot"));

    }

    @Test
    public void testGetQuickie() throws Exception {

    }

    @Test
    public void testCreateQuickie() throws Exception {

    }

    /*@Autowired   // 5
            CharacterRepository repository;

    Character mickey;
    Character minnie;
    Character pluto;

    @Value("${local.server.port}")   // 6
            int port;

    @Before
    public void setUp() {
        // 7
        mickey = new Character("Mickey Mouse");
        minnie = new Character("Minnie Mouse");
        pluto = new Character("Pluto");

        // 8
        repository.deleteAll();
        repository.save(Arrays.asList(mickey, minnie, pluto));

        // 9
        RestAssured.port = port;
    }

    // 10
    @Test
    public void canFetchMickey() {
        Integer mickeyId = mickey.getId();

        when().
                get("/characters/{id}", mickeyId).
                then().
                statusCode(HttpStatus.SC_OK).
                body("name", Matchers.is("Mickey Mouse")).
                body("id", Matchers.is(mickeyId));
    }

    @Test
    public void canFetchAll() {
        when().
                get("/characters").
                then().
                statusCode(HttpStatus.SC_OK).
                body("name", Matchers.hasItems("Mickey Mouse", "Minnie Mouse", "Pluto"));
    }

    @Test
    public void canDeletePluto() {
        Integer plutoId = pluto.getId();

        when()
                .delete("/characters/{id}", plutoId).
                then().
                statusCode(HttpStatus.SC_NO_CONTENT);
    }*/
}

@Configuration
@Import(QuickatApplication.class)
class TestConfiguration {

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource testDataSource = new DriverManagerDataSource();
        testDataSource.setDriverClassName("org.hsqldb.jdbcDriver");
        testDataSource.setUrl("jdbc:hsqldb:file:hsqldb");
        testDataSource.setUsername("sa");
        testDataSource.setPassword("sa");
        Properties connectionProperties = new Properties();
        connectionProperties.setProperty("hibernate.hbm2ddl.auto","create-drop");
        testDataSource.setConnectionProperties(connectionProperties);
        return testDataSource;

    }
    @Bean
    public HibernateJpaVendorAdapter hibernateJpaVendorAdapter(){
        HibernateJpaVendorAdapter hibernateJpaVendorAdapter=new HibernateJpaVendorAdapter();
        hibernateJpaVendorAdapter.setDatabasePlatform("org.hibernate.dialect.HSQLDialect");
        hibernateJpaVendorAdapter.setShowSql(true);
        return hibernateJpaVendorAdapter;
    }

}
@@ -1247,16 +1247,56 @@
 d.when;%0A
+import static org.hamcrest.Matchers.is;%0A
 %0A%0A@RunWi
@@ -1535,24 +1535,56 @@
  int port;%0A%0A
+    Quickie springBootQuickie;%0A%0A
     @Autowir
@@ -1736,32 +1736,24 @@
 ();%0A        
-Quickie 
 springBootQu
@@ -2218,16 +2218,17 @@
 Quickie,
+ 
 springBo
@@ -2237,16 +2237,17 @@
 Quickie,
+ 
 vagrantQ
@@ -2624,32 +2624,32 @@
    %7D%0A%0A    @Test%0A
-
     public void 
@@ -2677,32 +2677,333 @@
 ws Exception %7B%0A%0A
+        Integer springBootId = Integer.valueOf(springBootQuickie.getId().toString());%0A        when().get(%22/quickies/%7Bid%7D%22, springBootQuickie.getId()).%0A                then().%0A                statusCode(HttpStatus.SC_OK).%0A                body(%22title%22, is(%22Spring Boot%22)).body(%22id%22, is(springBootId));%0A%0A
     %7D%0A%0A    @Test
@@ -5014,16 +5014,17 @@
 l.auto%22,
+ 
 %22create-
@@ -5027,24 +5027,24 @@
 ate-drop%22);%0A
-
         test
@@ -5139,16 +5139,17 @@
 %0A%0A    %7D%0A
+%0A
     @Bea
@@ -5214,16 +5214,17 @@
 dapter()
+ 
 %7B%0A      
@@ -5276,17 +5276,19 @@
 rAdapter
-=
+ = 
 new Hibe

Test improved