Untitled

 avatar
unknown
plain_text
4 months ago
3.6 kB
4
Indexable
package com.santander.scib.dlx.web;

import com.santander.scib.dlx.model.web.CreateCalendarRequest;
import com.santander.scib.dlx.model.web.GenericResponse;
import com.santander.scib.dlx.model.web.UpdateCalendarRequest;
import com.santander.scib.dlx.service.CalendarService;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.ResponseEntity;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.http.MediaType.APPLICATION_JSON;

@ExtendWith(MockitoExtension.class)
class CalendarControllerTest {

    @Mock
    private CalendarService calendarService;

    @InjectMocks
    private CalendarController calendarController;

    private MockMvc mockMvc;

    @Test
    void createCalendar_Success() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(calendarController).build();
        CreateCalendarRequest request = new CreateCalendarRequest();
        request.setCalendar("test-calendar");
        GenericResponse response = new GenericResponse("success");

        when(calendarService.createCalendar(anyString(), any(CreateCalendarRequest.class)))
                .thenReturn(ResponseEntity.status(201).body(response));

        mockMvc.perform(post("/nat/calendar")
                        .header("token", "test-token")
                        .contentType(APPLICATION_JSON)
                        .content("{\"calendar\":\"test-calendar\"}"))
                .andExpect(status().isCreated())
                .andExpect(jsonPath("$.result").value("success"));
    }

    @Test
    void deleteCalendar_Success() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(calendarController).build();
        GenericResponse response = new GenericResponse("deleted");

        when(calendarService.deleteCalendar(anyString(), anyString()))
                .thenReturn(ResponseEntity.ok(response));

        mockMvc.perform(delete("/nat/calendar/1234")
                        .header("token", "test-token"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.result").value("deleted"));
    }

    @Test
    void updateCalendar_Success() throws Exception {
        mockMvc = MockMvcBuilders.standaloneSetup(calendarController).build();
        UpdateCalendarRequest request = new UpdateCalendarRequest();
        request.setCalendarUpdate("updated-calendar");
        GenericResponse response = new GenericResponse("updated");

        when(calendarService.updateCalendar(anyString(), anyString(), any(UpdateCalendarRequest.class)))
                .thenReturn(ResponseEntity.ok(response));

        mockMvc.perform(patch("/nat/calendar/1234")
                        .header("token", "test-token")
                        .contentType(APPLICATION_JSON)
                        .content("{\"calendarUpdate\":\"updated-calendar\"}"))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.result").value("updated"));
    }
}
Editor is loading...
Leave a Comment