package com.arielslf.belajar.java.pelatihan.dao;

import com.arielslf.belajar.java.pelatihan.PelatihanApplication;
import com.arielslf.belajar.java.pelatihan.entity.Materi;
import com.arielslf.belajar.java.pelatihan.entity.Peserta;
import com.arielslf.belajar.java.pelatihan.entity.Sesi;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.sql.DataSource;
import org.junit.After;
import org.junit.Assert;

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.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = PelatihanApplication.class)
@Sql(
        executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD,
        scripts = {"/data/peserta.sql", "/data/materi.sql", "/data/sesi.sql"}
)
public class SesiDaoTest {
    
    @Autowired
    private SesiDao sd;
    
    @Test
    public void testCariByMateri(){
        Materi m = new Materi();
        m.setId("aa6");
        
        PageRequest page = new PageRequest(0, 5);
        
        Page<Sesi> hasilQuery = sd.findByMateri(m, page);
        Assert.assertEquals(2L, hasilQuery.getTotalElements());
        
        Assert.assertFalse(hasilQuery.getContent().isEmpty());
        Sesi s = hasilQuery.getContent().get(0);
        Assert.assertNotNull(s);
        Assert.assertEquals("Java Fundamental", s.getMateri().getNama());
        
        
    }

    @Test
    public void testCariBerdasarkanTanggalMulaiDanKodeMateri() throws Exception {
        PageRequest page = new PageRequest(0, 5);
        
        SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
        Date sejak = formater.parse("2015-01-01");
        Date sampai = formater.parse("2015-01-03");
        
        Page<Sesi> hasil = sd.cariBerdasarkanTanggalMulaiDanKodeMateri(sejak, sampai, "JF-002", page);
        Assert.assertEquals(1L, hasil.getTotalElements());
        Assert.assertFalse(hasil.getContent().isEmpty());
        
        Sesi s = hasil.getContent().get(0);
        Assert.assertEquals("Java Web", s.getMateri().getNama());
        
    }
    


    
    

    
    
    
}

@@ -281,16 +281,79 @@
 y.Sesi;%0A
+import java.sql.Connection;%0Aimport java.sql.PreparedStatement;%0A
 import j
@@ -371,16 +371,16 @@
 ultSet;%0A
-
 import j
@@ -1311,24 +1311,71 @@
 ao sd;%0A    %0A
+    @Autowired%0A    private DataSource ds;%0A    %0A
     @Test%0A  
@@ -2607,33 +2607,1618 @@
   %0A%0A
-%0A    %0A    %0A%0A    
+    @Test%0A    public void testSaveSesi() throws Exception%7B%0A        Peserta p1 = new Peserta();%0A        p1.setId(%22aa1%22);%0A        %0A        Peserta p2 = new Peserta();%0A        p2.setId(%22aa3%22);%0A        %0A        Materi m = new Materi();%0A        m.setId(%22aa8%22);%0A        %0A        SimpleDateFormat formater = new SimpleDateFormat(%22yyyy-MM-dd%22);%0A        Date sejak = formater.parse(%222015-02-01%22);%0A        Date sampai = formater.parse(%222015-02-03%22);%0A        %0A        Sesi s = new Sesi();%0A        s.setMateri(m);%0A        s.setMulai(sejak);%0A        s.setSampai(sampai);  %0A        s.getDaftarPeserta().add(p1);%0A        s.getDaftarPeserta().add(p2);%0A        %0A        sd.save(s);%0A        String idSesiBaru = s.getId();%0A        Assert.assertNotNull(idSesiBaru);%0A        System.out.println(%22ID Baru : %22+s.getId());%0A        %0A        String sql = %22select count(*) from sesi where id_materi='aa8'%22;%0A        String sqlManyToMany = %22select count(*) from peserta_pelatihan %22%0A                + %22 where id_sesi=?%22;%0A        %0A        try(Connection c = ds.getConnection())%7B%0A            // cek table sesi%0A            ResultSet rs = c.createStatement().executeQuery(sql);%0A            Assert.assertNotNull(rs.next());%0A            Assert.assertEquals(1L, rs.getLong(1));%0A            %0A            // cek tabel relasi many to many dengan peserta%0A            PreparedStatement ps = c.prepareStatement(sqlManyToMany);%0A            ps.setString(1, idSesiBaru);%0A            ResultSet rs2 = ps.executeQuery();%0A            %0A            Assert.assertTrue(rs2.next());%0A            Assert.assertEquals(2L, rs2.getLong(1));%0A        %7D%0A        %0A    %7D
 %0A    %0A 
-   
 %0A%7D%0A

add ManyToMany insert