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.ServeBuilder;
import com.zsc.edu.gateway.modules.iot.tsl.controller.ServeController;
import com.zsc.edu.gateway.modules.iot.tsl.dto.ServeDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Serve;
import com.zsc.edu.gateway.modules.iot.tsl.service.ServeService;
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(ServeController.class)
public class ServeControllerTest extends MockMvcConfigBase {
    @Spy
    private static Serve serve1;
    private static Serve serve2;
    @MockBean
    private ServeService service;

    @Autowired
    private ObjectMapper objectMapper;

    @BeforeAll
    static void beforeAll() {
        serve1 = ServeBuilder.aServe().name("Serve 1").identifier("Description 1").build();
        serve1.setId(1L);
        serve2 = ServeBuilder.aServe().name("Serve 2").identifier("Description 2").build();
        serve2.setId(2L);
    }

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

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

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