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.EventBuilder;
import com.zsc.edu.gateway.modules.iot.tsl.controller.EventController;
import com.zsc.edu.gateway.modules.iot.tsl.dto.EventDto;
import com.zsc.edu.gateway.modules.iot.tsl.entity.Event;
import com.zsc.edu.gateway.modules.iot.tsl.service.EventService;
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(EventController.class)
public class EventControllerTest extends MockMvcConfigBase {
    @Spy
    private static Event event1;
    private static Event event2;
    @MockBean
    private EventService service;

    @Autowired
    private ObjectMapper objectMapper;

    @BeforeAll
    static void beforeAll() {
        event1 = EventBuilder.builder().setName("Event 1").setIdentifier("Description 1").build();
        event1.setId(1L);
        event2 = EventBuilder.builder().setName("Event 2").setIdentifier("Description 2").build();
        event2.setId(2L);
    }

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

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

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