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.DeviceBuilder;
import com.zsc.edu.gateway.framework.security.UserDetailsImpl;
import com.zsc.edu.gateway.modules.iot.device.controller.DeviceController;
import com.zsc.edu.gateway.modules.iot.device.dto.DeviceDto;
import com.zsc.edu.gateway.modules.iot.device.entity.Device;
import com.zsc.edu.gateway.modules.iot.device.service.DeviceService;
import com.zsc.edu.gateway.modules.iot.record.service.RecordDataService;
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(DeviceController.class)
public class DeviceControllerTest extends MockMvcConfigBase {
    @Spy
    private static Device device1;
    private static Device device2;
    @MockBean
    private DeviceService service;
    @MockBean
    private RecordDataService recordDataService;

    @BeforeAll
    static void beforeAll() {
        device1 = DeviceBuilder.aDevice().online(true).name("Device 1").build();
        device1.setId(1L);
        device2 = DeviceBuilder.aDevice().online(false).name("Device 2").build();
        device2.setId(2L);
    }

    @Test
    void create() throws Exception {
        DeviceDto dto = new DeviceDto();
        dto.setProductId(121L);
        dto.setName(device1.getName());
        when(service.create(any(DeviceDto.class))).thenReturn(device1);
        mockMvc.perform(post("/api/rest/device")
                        .with(csrf().asHeader())
                        .with(user(userDetails))
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(dto))
                )
                .andExpect(status().isOk())
                .andDo(print());
        verify(service).create(any(DeviceDto.class));
    }

    @Test
    void list() throws Exception {
        List<Device> devices = Lists.newArrayList(device1, device2);
        when(service.page(any(Page.class), any(LambdaQueryWrapper.class))).thenReturn(new Page<Device>().setRecords(devices).setTotal((long) devices.size()));

        mockMvc.perform(get("/api/rest/device").with(user(userDetails)))
                .andExpect(status().isOk())
                .andDo(print());

        verify(service).page(any(Page.class), any(LambdaQueryWrapper.class));
    }

    @Test
    void update() throws Exception {
        DeviceDto dto = new DeviceDto();
        dto.setProductId(121L);
        dto.setName(device1.getName());
        when(service.update(any(DeviceDto.class), anyLong())).thenReturn(device1);
        mockMvc.perform(patch("/api/rest/device/{id}", device1.getId())
                        .with(csrf().asHeader())
                        .with(user(userDetails))
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(objectMapper.writeValueAsString(dto))
                )
                .andExpect(status().isOk())
                .andDo(print());
        verify(service).update(any(DeviceDto.class), anyLong());
    }
}
