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 TBProductBlockDetailsLogResource REST controller.
 *
 * @see TBProductBlockDetailsLogResource
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TpfApp.class)
public class TBProductBlockDetailsLogResourceIntTest {

    @Autowired
    private TBProductBlockDetailsLogRepository tBProductBlockDetailsLogRepository;

    @Autowired
    private TBProductBlockDetailsLogMapper tBProductBlockDetailsLogMapper;

    @Autowired
    private TBProductBlockDetailsLogService tBProductBlockDetailsLogService;

    /**
     * This repository is mocked in the vn.azteam.tpf.repository.search test package.
     *
     * @see vn.azteam.tpf.repository.search.TBProductBlockDetailsLogSearchRepositoryMockConfiguration
     */
    @Autowired
    private TBProductBlockDetailsLogSearchRepository mockTBProductBlockDetailsLogSearchRepository;

    @Autowired
    private TBProductBlockDetailsLogQueryService tBProductBlockDetailsLogQueryService;

    @Autowired
    private MappingJackson2HttpMessageConverter jacksonMessageConverter;

    @Autowired
    private PageableHandlerMethodArgumentResolver pageableArgumentResolver;

    @Autowired
    private ExceptionTranslator exceptionTranslator;

    @Autowired
    private EntityManager em;

    @Autowired
    private Validator validator;

    private MockMvc restTBProductBlockDetailsLogMockMvc;

    private TBProductBlockDetailsLog tBProductBlockDetailsLog;

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        final TBProductBlockDetailsLogResource tBProductBlockDetailsLogResource = new TBProductBlockDetailsLogResource(tBProductBlockDetailsLogService, tBProductBlockDetailsLogQueryService);
        this.restTBProductBlockDetailsLogMockMvc = MockMvcBuilders.standaloneSetup(tBProductBlockDetailsLogResource)
            .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 TBProductBlockDetailsLog createEntity(EntityManager em) {
        TBProductBlockDetailsLog tBProductBlockDetailsLog = new TBProductBlockDetailsLog();
        return tBProductBlockDetailsLog;
    }

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

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

        // Create the TBProductBlockDetailsLog
        TBProductBlockDetailsLogDTO tBProductBlockDetailsLogDTO = tBProductBlockDetailsLogMapper.toDto(tBProductBlockDetailsLog);
        restTBProductBlockDetailsLogMockMvc.perform(post("/api/tb-product-block-details-logs")
            .contentType(TestUtil.APPLICATION_JSON_UTF8)
            .content(TestUtil.convertObjectToJsonBytes(tBProductBlockDetailsLogDTO)))
            .andExpect(status().isCreated());

        // Validate the TBProductBlockDetailsLog in the database
        List<TBProductBlockDetailsLog> tBProductBlockDetailsLogList = tBProductBlockDetailsLogRepository.findAll();
        assertThat(tBProductBlockDetailsLogList).hasSize(databaseSizeBeforeCreate + 1);
        TBProductBlockDetailsLog testTBProductBlockDetailsLog = tBProductBlockDetailsLogList.get(tBProductBlockDetailsLogList.size() - 1);

        // Validate the TBProductBlockDetailsLog in Elasticsearch
        verify(mockTBProductBlockDetailsLogSearchRepository, times(1)).save(testTBProductBlockDetailsLog);
    }

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

        // Create the TBProductBlockDetailsLog with an existing ID
        tBProductBlockDetailsLog.setId(1L);
        TBProductBlockDetailsLogDTO tBProductBlockDetailsLogDTO = tBProductBlockDetailsLogMapper.toDto(tBProductBlockDetailsLog);

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

        // Validate the TBProductBlockDetailsLog in the database
        List<TBProductBlockDetailsLog> tBProductBlockDetailsLogList = tBProductBlockDetailsLogRepository.findAll();
        assertThat(tBProductBlockDetailsLogList).hasSize(databaseSizeBeforeCreate);

        // Validate the TBProductBlockDetailsLog in Elasticsearch
        verify(mockTBProductBlockDetailsLogSearchRepository, times(0)).save(tBProductBlockDetailsLog);
    }

    @Test
    @Transactional
    public void getAllTBProductBlockDetailsLogs() throws Exception {
        // Initialize the database
        tBProductBlockDetailsLogRepository.saveAndFlush(tBProductBlockDetailsLog);

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

    @Test
    @Transactional
    public void getTBProductBlockDetailsLog() throws Exception {
        // Initialize the database
        tBProductBlockDetailsLogRepository.saveAndFlush(tBProductBlockDetailsLog);

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

        // Check, that the count call also returns 1
        restTBProductBlockDetailsLogMockMvc.perform(get("/api/tb-product-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 defaultTBProductBlockDetailsLogShouldNotBeFound(String filter) throws Exception {
        restTBProductBlockDetailsLogMockMvc.perform(get("/api/tb-product-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
        restTBProductBlockDetailsLogMockMvc.perform(get("/api/tb-product-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 getNonExistingTBProductBlockDetailsLog() throws Exception {
        // Get the tBProductBlockDetailsLog
        restTBProductBlockDetailsLogMockMvc.perform(get("/api/tb-product-block-details-logs/{id}", Long.MAX_VALUE))
            .andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    public void updateTBProductBlockDetailsLog() throws Exception {
        // Initialize the database
        tBProductBlockDetailsLogRepository.saveAndFlush(tBProductBlockDetailsLog);

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

        // Update the tBProductBlockDetailsLog
        TBProductBlockDetailsLog updatedTBProductBlockDetailsLog = tBProductBlockDetailsLogRepository.findById(tBProductBlockDetailsLog.getId()).get();
        // Disconnect from session so that the updates on updatedTBProductBlockDetailsLog are not directly saved in db
        em.detach(updatedTBProductBlockDetailsLog);
        TBProductBlockDetailsLogDTO tBProductBlockDetailsLogDTO = tBProductBlockDetailsLogMapper.toDto(updatedTBProductBlockDetailsLog);

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

        // Validate the TBProductBlockDetailsLog in the database
        List<TBProductBlockDetailsLog> tBProductBlockDetailsLogList = tBProductBlockDetailsLogRepository.findAll();
        assertThat(tBProductBlockDetailsLogList).hasSize(databaseSizeBeforeUpdate);
        TBProductBlockDetailsLog testTBProductBlockDetailsLog = tBProductBlockDetailsLogList.get(tBProductBlockDetailsLogList.size() - 1);

        // Validate the TBProductBlockDetailsLog in Elasticsearch
        verify(mockTBProductBlockDetailsLogSearchRepository, times(1)).save(testTBProductBlockDetailsLog);
    }

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

        // Create the TBProductBlockDetailsLog
        TBProductBlockDetailsLogDTO tBProductBlockDetailsLogDTO = tBProductBlockDetailsLogMapper.toDto(tBProductBlockDetailsLog);

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

        // Validate the TBProductBlockDetailsLog in the database
        List<TBProductBlockDetailsLog> tBProductBlockDetailsLogList = tBProductBlockDetailsLogRepository.findAll();
        assertThat(tBProductBlockDetailsLogList).hasSize(databaseSizeBeforeUpdate);

        // Validate the TBProductBlockDetailsLog in Elasticsearch
        verify(mockTBProductBlockDetailsLogSearchRepository, times(0)).save(tBProductBlockDetailsLog);
    }

    @Test
    @Transactional
    public void deleteTBProductBlockDetailsLog() throws Exception {
        // Initialize the database
        tBProductBlockDetailsLogRepository.saveAndFlush(tBProductBlockDetailsLog);

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

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

        // Validate the database is empty
        List<TBProductBlockDetailsLog> tBProductBlockDetailsLogList = tBProductBlockDetailsLogRepository.findAll();
        assertThat(tBProductBlockDetailsLogList).hasSize(databaseSizeBeforeDelete - 1);

        // Validate the TBProductBlockDetailsLog in Elasticsearch
        verify(mockTBProductBlockDetailsLogSearchRepository, times(1)).deleteById(tBProductBlockDetailsLog.getId());
    }

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

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

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

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