package com.demo1.unit;

import static org.assertj.core.api.Assertions.*;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Optional;

import org.junit.jupiter.api.Test;

import com.demo1.board.domain.Board;
import com.demo1.board.domain.BoardMapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.jdbc.Sql;

@SpringBootTest
@Sql(scripts = "classpath:sql/test/board.sql")
class BoardMapperTest {

    @Autowired
    private BoardMapper boardMapper;

    @Test
    void testGetBoardById() {
        //act
        Optional<Board> result = boardMapper.getBoardById(1L);

        //assert
        assertThat(result).isPresent();
        assertThat(result.get().getTitle()).isEqualTo("Title 1");
        assertThat(result.get().getContent()).isEqualTo("Content 1");
    }

    @Test
    void testGetAllBoards() {
        //act
        List<Board> result = boardMapper.getAllBoards();

        //assert
        assertThat(result).hasSize(13);
    }

    @Test
    void testInsertBoard() {
        //arrange
        Board board = new Board();
        board.setTitle("new title123");
        board.setContent("new content123");

        //act
        boardMapper.insertBoard(board);

        //assert
        assertThat(board.getId()).isEqualTo(14L);
    }

    @Test
    void testUpdateBoard() {
        //arrange
        Board board = new Board();
        board.setId(1L);
        board.setTitle("new title");
        board.setContent("new content");

        //act
        boardMapper.updateBoard(board);

        Optional<Board> result = boardMapper.getBoardById(board.getId());

        //assert
        assertThat(result).isPresent();
        assertThat(result.get().getTitle()).isEqualTo("new title");
        assertThat(result.get().getContent()).isEqualTo("new content");
    }

    @Test
    void testDeleteBoard() {
        //act
        boardMapper.deleteBoard(1L);

        //assert
        assertThatCode(()->boardMapper.getBoardById(1L).orElseThrow(NoSuchElementException::new))
                .isInstanceOf(NoSuchElementException.class);
    }
}

