package vn.azteam.tpf.web.rest;

import vn.azteam.tpf.TpfApp;

import vn.azteam.tpf.web.rest.errors.ExceptionTranslator;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.Validator;

import javax.persistence.EntityManager;
import java.util.Collections;
import java.util.List;


import static vn.azteam.tpf.web.rest.TestUtil.createFormattingConversionService;
import static org.assertj.core.api.Assertions.assertThat;
import static org.elasticsearch.index.query.QueryBuilders.queryStringQuery;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

/**
 * Test class for the TBBlockDetailsLogResource REST controller.
 *
 * @see TBBlockDetailsLogResource
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TpfApp.class)
public class TBBlockDetailsLogResourceIntTest {

    @Autowired
    private TBBlockDetailsLogRepository tBBlockDetailsLogRepository;

    @Autowired
    private TBBlockDetailsLogMapper tBBlockDetailsLogMapper;

    @Autowired
    private TBBlockDetailsLogService tBBlockDetailsLogService;

    /**
     * This repository is mocked in the vn.azteam.tpf.repository.search test package.
     *
     * @see vn.azteam.tpf.repository.search.TBBlockDetailsLogSearchRepositoryMockConfiguration
     */
    @Autowired
    private TBBlockDetailsLogSearchRepository mockTBBlockDetailsLogSearchRepository;

    @Autowired
    private TBBlockDetailsLogQueryService tBBlockDetailsLogQueryService;

    @Autowired
    private MappingJackson2HttpMessageConverter jacksonMessageConverter;

    @Autowired
    private PageableHandlerMethodArgumentResolver pageableArgumentResolver;

    @Autowired
    private ExceptionTranslator exceptionTranslator;

    @Autowired
    private EntityManager em;

    @Autowired
    private Validator validator;

    private MockMvc restTBBlockDetailsLogMockMvc;

    private TBBlockDetailsLog tBBlockDetailsLog;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        final TBBlockDetailsLogResource tBBlockDetailsLogResource = new TBBlockDetailsLogResource(tBBlockDetailsLogService, tBBlockDetailsLogQueryService);
        this.restTBBlockDetailsLogMockMvc = MockMvcBuilders.standaloneSetup(tBBlockDetailsLogResource)
            .setCustomArgumentResolvers(pageableArgumentResolver)
            .setControllerAdvice(exceptionTranslator)
            .setConversionService(createFormattingConversionService())
            .setMessageConverters(jacksonMessageConverter)
            .setValidator(validator).build();
    }

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static TBBlockDetailsLog createEntity(EntityManager em) {
        TBBlockDetailsLog tBBlockDetailsLog = new TBBlockDetailsLog();
        return tBBlockDetailsLog;
    }

    @Before
    public void initTest() {
        tBBlockDetailsLog = createEntity(em);
    }

    @Test
    @Transactional
    public void createTBBlockDetailsLog() throws Exception {
        int databaseSizeBeforeCreate = tBBlockDetailsLogRepository.findAll().size();

        // Create the TBBlockDetailsLog
        TBBlockDetailsLogDTO tBBlockDetailsLogDTO = tBBlockDetailsLogMapper.toDto(tBBlockDetailsLog);
        restTBBlockDetailsLogMockMvc.perform(post("/api/tb-block-details-logs")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(tBBlockDetailsLogDTO)))
            .andExpect(status().isCreated());

        // Validate the TBBlockDetailsLog in the database
        List<TBBlockDetailsLog> tBBlockDetailsLogList = tBBlockDetailsLogRepository.findAll();
        assertThat(tBBlockDetailsLogList).hasSize(databaseSizeBeforeCreate + 1);
        TBBlockDetailsLog testTBBlockDetailsLog = tBBlockDetailsLogList.get(tBBlockDetailsLogList.size() - 1);

        // Validate the TBBlockDetailsLog in Elasticsearch
        verify(mockTBBlockDetailsLogSearchRepository, times(1)).save(testTBBlockDetailsLog);
    }

    @Test
    @Transactional
    public void createTBBlockDetailsLogWithExistingId() throws Exception {
        int databaseSizeBeforeCreate = tBBlockDetailsLogRepository.findAll().size();

        // Create the TBBlockDetailsLog with an existing ID
        tBBlockDetailsLog.setId(1L);
        TBBlockDetailsLogDTO tBBlockDetailsLogDTO = tBBlockDetailsLogMapper.toDto(tBBlockDetailsLog);

        // An entity with an existing ID cannot be created, so this API call must fail
        restTBBlockDetailsLogMockMvc.perform(post("/api/tb-block-details-logs")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(tBBlockDetailsLogDTO)))
            .andExpect(status().isBadRequest());

        // Validate the TBBlockDetailsLog in the database
        List<TBBlockDetailsLog> tBBlockDetailsLogList = tBBlockDetailsLogRepository.findAll();
        assertThat(tBBlockDetailsLogList).hasSize(databaseSizeBeforeCreate);

        // Validate the TBBlockDetailsLog in Elasticsearch
        verify(mockTBBlockDetailsLogSearchRepository, times(0)).save(tBBlockDetailsLog);
    }

    @Test
    @Transactional
    public void getAllTBBlockDetailsLogs() throws Exception {
        // Initialize the database
        tBBlockDetailsLogRepository.saveAndFlush(tBBlockDetailsLog);

        // Get all the tBBlockDetailsLogList
        restTBBlockDetailsLogMockMvc.perform(get("/api/tb-block-details-logs?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(tBBlockDetailsLog.getId().intValue())));
    }

    @Test
    @Transactional
    public void getTBBlockDetailsLog() throws Exception {
        // Initialize the database
        tBBlockDetailsLogRepository.saveAndFlush(tBBlockDetailsLog);

        // Get the tBBlockDetailsLog
        restTBBlockDetailsLogMockMvc.perform(get("/api/tb-block-details-logs/{id}", tBBlockDetailsLog.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(jsonPath("$.id").value(tBBlockDetailsLog.getId().intValue()));
    }
    /**
     * Executes the search, and checks that the default entity is returned
     */
    private void defaultTBBlockDetailsLogShouldBeFound(String filter) throws Exception {
        restTBBlockDetailsLogMockMvc.perform(get("/api/tb-block-details-logs?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(tBBlockDetailsLog.getId().intValue())));

        // Check, that the count call also returns 1
        restTBBlockDetailsLogMockMvc.perform(get("/api/tb-block-details-logs/count?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(content().string("1"));
    }

    /**
     * Executes the search, and checks that the default entity is not returned
     */
    private void defaultTBBlockDetailsLogShouldNotBeFound(String filter) throws Exception {
        restTBBlockDetailsLogMockMvc.perform(get("/api/tb-block-details-logs?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(jsonPath("$").isArray())
            .andExpect(jsonPath("$").isEmpty());

        // Check, that the count call also returns 0
        restTBBlockDetailsLogMockMvc.perform(get("/api/tb-block-details-logs/count?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(content().string("0"));
    }


    @Test
    @Transactional
    public void getNonExistingTBBlockDetailsLog() throws Exception {
        // Get the tBBlockDetailsLog
        restTBBlockDetailsLogMockMvc.perform(get("/api/tb-block-details-logs/{id}", Long.MAX_VALUE))
            .andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    public void updateTBBlockDetailsLog() throws Exception {
        // Initialize the database
        tBBlockDetailsLogRepository.saveAndFlush(tBBlockDetailsLog);

        int databaseSizeBeforeUpdate = tBBlockDetailsLogRepository.findAll().size();

        // Update the tBBlockDetailsLog
        TBBlockDetailsLog updatedTBBlockDetailsLog = tBBlockDetailsLogRepository.findById(tBBlockDetailsLog.getId()).get();
        // Disconnect from session so that the updates on updatedTBBlockDetailsLog are not directly saved in db
        em.detach(updatedTBBlockDetailsLog);
        TBBlockDetailsLogDTO tBBlockDetailsLogDTO = tBBlockDetailsLogMapper.toDto(updatedTBBlockDetailsLog);

        restTBBlockDetailsLogMockMvc.perform(put("/api/tb-block-details-logs")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(tBBlockDetailsLogDTO)))
            .andExpect(status().isOk());

        // Validate the TBBlockDetailsLog in the database
        List<TBBlockDetailsLog> tBBlockDetailsLogList = tBBlockDetailsLogRepository.findAll();
        assertThat(tBBlockDetailsLogList).hasSize(databaseSizeBeforeUpdate);
        TBBlockDetailsLog testTBBlockDetailsLog = tBBlockDetailsLogList.get(tBBlockDetailsLogList.size() - 1);

        // Validate the TBBlockDetailsLog in Elasticsearch
        verify(mockTBBlockDetailsLogSearchRepository, times(1)).save(testTBBlockDetailsLog);
    }

    @Test
    @Transactional
    public void updateNonExistingTBBlockDetailsLog() throws Exception {
        int databaseSizeBeforeUpdate = tBBlockDetailsLogRepository.findAll().size();

        // Create the TBBlockDetailsLog
        TBBlockDetailsLogDTO tBBlockDetailsLogDTO = tBBlockDetailsLogMapper.toDto(tBBlockDetailsLog);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restTBBlockDetailsLogMockMvc.perform(put("/api/tb-block-details-logs")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(tBBlockDetailsLogDTO)))
            .andExpect(status().isBadRequest());

        // Validate the TBBlockDetailsLog in the database
        List<TBBlockDetailsLog> tBBlockDetailsLogList = tBBlockDetailsLogRepository.findAll();
        assertThat(tBBlockDetailsLogList).hasSize(databaseSizeBeforeUpdate);

        // Validate the TBBlockDetailsLog in Elasticsearch
        verify(mockTBBlockDetailsLogSearchRepository, times(0)).save(tBBlockDetailsLog);
    }

    @Test
    @Transactional
    public void deleteTBBlockDetailsLog() throws Exception {
        // Initialize the database
        tBBlockDetailsLogRepository.saveAndFlush(tBBlockDetailsLog);

        int databaseSizeBeforeDelete = tBBlockDetailsLogRepository.findAll().size();

        // Get the tBBlockDetailsLog
        restTBBlockDetailsLogMockMvc.perform(delete("/api/tb-block-details-logs/{id}", tBBlockDetailsLog.getId())
            .accept(TestUtil.APPLICATION_JSON_UTF8))
            .andExpect(status().isOk());

        // Validate the database is empty
        List<TBBlockDetailsLog> tBBlockDetailsLogList = tBBlockDetailsLogRepository.findAll();
        assertThat(tBBlockDetailsLogList).hasSize(databaseSizeBeforeDelete - 1);

        // Validate the TBBlockDetailsLog in Elasticsearch
        verify(mockTBBlockDetailsLogSearchRepository, times(1)).deleteById(tBBlockDetailsLog.getId());
    }

    @Test
    @Transactional
    public void searchTBBlockDetailsLog() throws Exception {
        // Initialize the database
        tBBlockDetailsLogRepository.saveAndFlush(tBBlockDetailsLog);
        when(mockTBBlockDetailsLogSearchRepository.search(queryStringQuery("id:" + tBBlockDetailsLog.getId()), PageRequest.of(0, 20)))
            .thenReturn(new PageImpl<>(Collections.singletonList(tBBlockDetailsLog), PageRequest.of(0, 1), 1));
        // Search the tBBlockDetailsLog
        restTBBlockDetailsLogMockMvc.perform(get("/api/_search/tb-block-details-logs?query=id:" + tBBlockDetailsLog.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(tBBlockDetailsLog.getId().intValue())));
    }

    @Test
    @Transactional
    public void equalsVerifier() throws Exception {
        TestUtil.equalsVerifier(TBBlockDetailsLog.class);
        TBBlockDetailsLog tBBlockDetailsLog1 = new TBBlockDetailsLog();
        tBBlockDetailsLog1.setId(1L);
        TBBlockDetailsLog tBBlockDetailsLog2 = new TBBlockDetailsLog();
        tBBlockDetailsLog2.setId(tBBlockDetailsLog1.getId());
        assertThat(tBBlockDetailsLog1).isEqualTo(tBBlockDetailsLog2);
        tBBlockDetailsLog2.setId(2L);
        assertThat(tBBlockDetailsLog1).isNotEqualTo(tBBlockDetailsLog2);
        tBBlockDetailsLog1.setId(null);
        assertThat(tBBlockDetailsLog1).isNotEqualTo(tBBlockDetailsLog2);
    }

    @Test
    @Transactional
    public void dtoEqualsVerifier() throws Exception {
        TestUtil.equalsVerifier(TBBlockDetailsLogDTO.class);
        TBBlockDetailsLogDTO tBBlockDetailsLogDTO1 = new TBBlockDetailsLogDTO();
        tBBlockDetailsLogDTO1.setId(1L);
        TBBlockDetailsLogDTO tBBlockDetailsLogDTO2 = new TBBlockDetailsLogDTO();
        assertThat(tBBlockDetailsLogDTO1).isNotEqualTo(tBBlockDetailsLogDTO2);
        tBBlockDetailsLogDTO2.setId(tBBlockDetailsLogDTO1.getId());
        assertThat(tBBlockDetailsLogDTO1).isEqualTo(tBBlockDetailsLogDTO2);
        tBBlockDetailsLogDTO2.setId(2L);
        assertThat(tBBlockDetailsLogDTO1).isNotEqualTo(tBBlockDetailsLogDTO2);
        tBBlockDetailsLogDTO1.setId(null);
        assertThat(tBBlockDetailsLogDTO1).isNotEqualTo(tBBlockDetailsLogDTO2);
    }

    @Test
    @Transactional
    public void testEntityFromId() {
        assertThat(tBBlockDetailsLogMapper.fromId(42L).getId()).isEqualTo(42);
        assertThat(tBBlockDetailsLogMapper.fromId(null)).isNull();
    }
}
