package com.zsc.edu.gateway.rest.iot;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zsc.edu.gateway.MockMvcConfigBase;
import com.zsc.edu.gateway.domain.iot.ProductBuilder;
import com.zsc.edu.gateway.modules.iot.product.controller.ProductController;
import com.zsc.edu.gateway.modules.iot.product.dto.ProductDto;
import com.zsc.edu.gateway.modules.iot.product.entity.Product;
import com.zsc.edu.gateway.modules.iot.product.service.ProductService;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;

import java.util.List;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(ProductController.class)
public class ProductControllerTest extends MockMvcConfigBase {
    @Spy
    private static Product product1;
    private static Product product2;
    @MockBean
    private ProductService service;

    @Autowired
    private ObjectMapper objectMapper;

    @BeforeAll
    static void beforeAll() {
        product1 = ProductBuilder.bProduct().name("Product 1").productType("Description 1").build();
        product1.setId(1L);
        product2 = ProductBuilder.bProduct().name("Product 2").productType("Description 2").build();
        product2.setId(2L);
    }

    @Test
    void create() throws Exception {
        ProductDto dto = new ProductDto();
        dto.setProductType(product1.getProductType());
        dto.setName(product1.getName());
        dto.setParams(null);
        when(service.create(any(ProductDto.class))).thenReturn(product1);
        mockMvc.perform(post("/api/rest/product")
                        .with(csrf().asHeader())
                        .with(user(userDetails))
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(dto))
                )
                .andExpect(status().isOk())
                .andDo(print());
        verify(service).create(any(ProductDto.class));
    }

    @Test
    void list() throws Exception {
        List<Product> products = Lists.newArrayList(product1, product2);
        when(service.page(any(Page.class), any(LambdaQueryWrapper.class))).thenReturn(new Page<Product>().setRecords(products).setTotal((long) products.size()));
        mockMvc.perform(get("/api/rest/product").with(user(userDetails)))
                .andExpect(status().isOk())
                .andDo(print());
        verify(service).page(any(Page.class), any(LambdaQueryWrapper.class));
    }

    @Test
    void update() throws Exception {
        ProductDto dto = new ProductDto();
        dto.setProductType(product1.getProductType());
        dto.setName(product1.getName());
        dto.setParams(null);
        when(service.update(any(ProductDto.class), anyLong())).thenReturn(product1);
        mockMvc.perform(patch("/api/rest/product/{id}", product1.getId())
                        .with(csrf().asHeader())
                        .with(user(userDetails))
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(dto))
                )
                .andExpect(status().isOk())
                .andDo(print());
        verify(service).update(any(ProductDto.class), anyLong());
    }
}
