changing module settings,
adding unit testing for controllers
This commit is contained in:
@@ -25,7 +25,6 @@ import java.util.UUID;
|
||||
public class EventController {
|
||||
|
||||
private final EtrackService etrackService;
|
||||
private final String sortBy = "messageId";
|
||||
private final List<Event> eventList = new LinkedList<>();
|
||||
|
||||
@GetMapping("/etrack/{uuid}")
|
||||
@@ -44,14 +43,6 @@ public class EventController {
|
||||
return "etrack";
|
||||
}
|
||||
|
||||
@PostMapping(value = "/etrack/{uuid}", params = {"sortBy"})
|
||||
public String sortBy(@Valid EventRequestDto eventRequestDto, final Model model, BindingResult bindingResult,
|
||||
@PathVariable UUID uuid){
|
||||
model.addAttribute("clientUUID", uuid);
|
||||
populateModelWithLists(model, eventRequestDto, false);
|
||||
return "etrack";
|
||||
}
|
||||
|
||||
private void populateModelWithLists(Model model, EventRequestDto eventRequestDto, boolean updateList){
|
||||
if (updateList){
|
||||
eventList.clear();
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
package com.release11.klaus.model;
|
||||
|
||||
import com.release11.klaus.model.constraints.HttpCode;
|
||||
import lombok.*;
|
||||
import org.springframework.data.annotation.Id;
|
||||
import org.springframework.data.redis.core.RedisHash;
|
||||
import org.springframework.data.redis.core.index.Indexed;
|
||||
|
||||
import javax.validation.constraints.Positive;
|
||||
import java.io.Serializable;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
@Data
|
||||
@ToString
|
||||
@RedisHash("MockedResponseDto")
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public final class MockedResponseDto implements Serializable {
|
||||
@Id
|
||||
private String compositePrimaryKey;
|
||||
@Indexed
|
||||
private UUID clientUUID;
|
||||
@Positive
|
||||
private int mockedResponseId;
|
||||
private String mediaType;
|
||||
private String messageBody;
|
||||
private Map<String, String> httpHeaders;
|
||||
@HttpCode
|
||||
private Integer httpStatus;
|
||||
|
||||
public MockedResponseDto(UUID clientUUID, int mockedResponseId, String mediaType,
|
||||
String messageBody, Map<String, String> httpHeaders, Integer httpStatus) {
|
||||
this.compositePrimaryKey = clientUUID.toString() + "_" + mockedResponseId;
|
||||
this.clientUUID = clientUUID;
|
||||
this.mockedResponseId = mockedResponseId;
|
||||
this.mediaType = mediaType;
|
||||
this.messageBody = messageBody;
|
||||
this.httpHeaders = httpHeaders;
|
||||
this.httpStatus = httpStatus;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package com.release11.klaus.controller;
|
||||
|
||||
import com.release11.klaus.model.Event;
|
||||
import com.release11.klaus.model.EventRequestDto;
|
||||
import com.release11.klaus.service.EtrackService;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.validation.BindingResult;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class EventControllerTest {
|
||||
|
||||
@Mock
|
||||
EtrackService etrackService;
|
||||
|
||||
@Mock
|
||||
Model model;
|
||||
|
||||
@Mock
|
||||
BindingResult bindingResult;
|
||||
|
||||
MockMvc mockMvc;
|
||||
EventRequestDto eventRequestDto;
|
||||
UUID uuid;
|
||||
List<Event> eventList = new LinkedList<>();
|
||||
|
||||
@InjectMocks
|
||||
EventController eventController;
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
uuid = UUID.randomUUID();
|
||||
eventRequestDto = EventRequestDto.builder()
|
||||
.clientUUID(uuid)
|
||||
.localDateTimeFrom(LocalDateTime.now().minusDays(5))
|
||||
.localDateTimeTo(LocalDateTime.now())
|
||||
.mockedResponseId(372)
|
||||
.build();
|
||||
eventList.add(Event.builder()
|
||||
.dateTimeStamp(LocalDateTime.now())
|
||||
.interfaceName("deleteMockedResponse")
|
||||
.clientUUID(uuid.toString())
|
||||
.messageId(372)
|
||||
.thread("the best thread")
|
||||
.level("the highest level")
|
||||
.message("Message 372 has been removed.")
|
||||
.build());
|
||||
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(eventController).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void showEtrack() throws Exception {
|
||||
mockMvc.perform(get("/etrack/" + uuid)
|
||||
.param("clientUUID", String.valueOf(uuid))
|
||||
.param("localDateTimeFrom", String.valueOf(LocalDateTime.now().minusDays(6)))
|
||||
.param("localDateTimeTo", String.valueOf(LocalDateTime.now()))
|
||||
.param("mockedResponseId", "332"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("etrack"))
|
||||
.andExpect(model().attribute("clientUUID", uuid))
|
||||
.andExpect(model().attributeExists("eventRequestDto"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
void getLogs() throws Exception {
|
||||
when(etrackService.getEventsByDateTimeAndBusinessKeys(any()))
|
||||
.thenReturn(eventList);
|
||||
|
||||
mockMvc.perform(post("/etrack/" + uuid)
|
||||
.param("clientUUID", String.valueOf(uuid))
|
||||
.param("localDateTimeFrom", String.valueOf(LocalDateTime.now().minusDays(6)))
|
||||
.param("localDateTimeTo", String.valueOf(LocalDateTime.now()))
|
||||
.param("mockedResponseId", "332"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("etrack"))
|
||||
.andExpect(model().attribute("clientUUID", uuid))
|
||||
.andExpect(model().attributeExists("eventRequestDto"))
|
||||
.andExpect(model().attributeExists("eventList"));
|
||||
|
||||
verify(etrackService).getEventsByDateTimeAndBusinessKeys(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void eventRequestDto() {
|
||||
assert eventController.eventRequestDto() != null;
|
||||
}
|
||||
|
||||
@Test
|
||||
void eventList() {
|
||||
assert eventController.eventList() != null;
|
||||
}
|
||||
}
|
||||
@@ -1,19 +1,94 @@
|
||||
package com.release11.klaus.controller;
|
||||
|
||||
import org.apache.tomcat.jni.Local;
|
||||
import com.release11.klaus.model.MockedMessageDto;
|
||||
import com.release11.klaus.service.KlausService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
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.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
import java.time.Period;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyInt;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class KlausControllerTest {
|
||||
|
||||
@Test
|
||||
void getMockedResponse() {
|
||||
@Mock
|
||||
KlausService klausService;
|
||||
|
||||
@InjectMocks
|
||||
KlausController klausController;
|
||||
|
||||
MockMvc mockMvc;
|
||||
UUID uuid;
|
||||
MockedMessageDto mockedMessageDto;
|
||||
List<MockedMessageDto> mockedMessageDtoList = new ArrayList<>();
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
uuid = UUID.randomUUID();
|
||||
mockedMessageDto = MockedMessageDto.builder()
|
||||
.clientUUID(uuid)
|
||||
.mockedResponseId(323)
|
||||
.mediaType(MediaType.APPLICATION_JSON.toString())
|
||||
.messageBody("my message body")
|
||||
.httpHeaders(new HashMap<>())
|
||||
.httpStatus(200)
|
||||
.build();
|
||||
mockedMessageDtoList.add(mockedMessageDto);
|
||||
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(klausController).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void setMockedResponse() {
|
||||
void deleteMockedResponse() throws Exception {
|
||||
mockMvc.perform(delete("/klaus/v1/delete/" + uuid + "/" + mockedMessageDto.getMockedResponseId()))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
verify(klausService).deleteMockedResponse(any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getAllMockedResponses() throws Exception {
|
||||
when(klausService.getAllMockedResponses(uuid))
|
||||
.thenReturn(mockedMessageDtoList);
|
||||
|
||||
mockMvc.perform(get("/klaus/v1/getAll/" + uuid)
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
verify(klausService).getAllMockedResponses(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testGetMockedResponse() throws Exception {
|
||||
when(klausService.getMockedResponse(uuid, mockedMessageDto.getMockedResponseId()))
|
||||
.thenReturn(mockedMessageDto);
|
||||
|
||||
mockMvc.perform(get("/klaus/v1/get/" + uuid + "/" + mockedMessageDto.getMockedResponseId())
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
verify(klausService).getMockedResponse(any(), anyInt());
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() {
|
||||
reset(klausService);
|
||||
}
|
||||
}
|
||||
@@ -1,23 +1,216 @@
|
||||
package com.release11.klaus.controller;
|
||||
|
||||
import com.release11.klaus.model.Event;
|
||||
import com.release11.klaus.model.EventRequestDto;
|
||||
import com.release11.klaus.model.MockedMessageDto;
|
||||
import com.release11.klaus.service.KlausService;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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.MediaType;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Arrays;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class KlausMvcControllerTest {
|
||||
|
||||
@Test
|
||||
void showHome() {
|
||||
@Mock
|
||||
KlausService klausService;
|
||||
|
||||
@InjectMocks
|
||||
KlausMvcController klausMvcController;
|
||||
|
||||
MockMvc mockMvc;
|
||||
UUID uuid;
|
||||
MockedMessageDto mockedMessageDto;
|
||||
List<MockedMessageDto> mockedMessageDtoList = new ArrayList<>();
|
||||
|
||||
@BeforeEach
|
||||
void beforeEach() {
|
||||
uuid = UUID.randomUUID();
|
||||
mockedMessageDto = MockedMessageDto.builder()
|
||||
.clientUUID(uuid)
|
||||
.mockedResponseId(323)
|
||||
.mediaType(MediaType.APPLICATION_JSON.toString())
|
||||
.messageBody("my message body")
|
||||
.httpHeaders(new HashMap<>())
|
||||
.httpStatus(200)
|
||||
.build();
|
||||
mockedMessageDtoList.add(mockedMessageDto);
|
||||
|
||||
mockMvc = MockMvcBuilders.standaloneSetup(klausMvcController).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void showHome() throws Exception {
|
||||
when(klausService.getAllMockedResponses(any()))
|
||||
.thenReturn(mockedMessageDtoList);
|
||||
|
||||
mockMvc.perform(get("/home/" + uuid))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("index"))
|
||||
.andExpect(model().attributeExists("mockedMessageDtoList"));
|
||||
|
||||
verify(klausService).getAllMockedResponses(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void showHomePost() throws Exception {
|
||||
mockMvc.perform(post("/home/e9dc0ad9-acd2-4699-99f7-368d53d7afd6")
|
||||
.param("clientUUID", "e9dc0ad9-acd2-4699-99f7-368d53d7afd6")
|
||||
.param("mockedResponseId", "323")
|
||||
.param("mediaType", "application/json")
|
||||
.param("messageBody", "my message body")
|
||||
.param("httpStatus", "200"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(model().attributeExists("mockedMessageDtoList"))
|
||||
.andExpect(view().name("index"));
|
||||
|
||||
verify(klausService).setMockedResponse(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void showHomePostNotValid() throws Exception {
|
||||
mockMvc.perform(post("/home/e9dc0ad9-acd2-4699-99f7-368d53d7afd6")
|
||||
.param("clientUUID", "e9dc0ad9-acd2-4699-99f7-368d53d7afd6")
|
||||
.param("mockedResponseId", "aaaa")
|
||||
.param("mediaType", "application/json")
|
||||
.param("messageBody", "my message body")
|
||||
.param("httpStatus", "200"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(model().attributeHasErrors("mockedMessageDto"))
|
||||
.andExpect(model().attributeHasFieldErrors("mockedMessageDto", "mockedResponseId"))
|
||||
.andExpect(view().name("index"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void newMessage() throws Exception {
|
||||
when(klausService.getAllMockedResponses(any()))
|
||||
.thenReturn(mockedMessageDtoList);
|
||||
|
||||
mockMvc.perform(post("/home/e9dc0ad9-acd2-4699-99f7-368d53d7afd6")
|
||||
.param("newMessage", "newMessage")
|
||||
.param("clientUUID", "e9dc0ad9-acd2-4699-99f7-368d53d7afd6"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(model().attributeExists("mockedMessageDtoList"))
|
||||
.andExpect(view().name("index"));
|
||||
|
||||
verify(klausService).getAllMockedResponses(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void showGetMockedResponse() throws Exception {
|
||||
when(klausService.getMockedResponse(any(), anyInt()))
|
||||
.thenReturn(mockedMessageDto);
|
||||
|
||||
mockMvc.perform(get("/home/getMockedResponse")
|
||||
.param("clientUUID", "e9dc0ad9-acd2-4699-99f7-368d53d7afd6")
|
||||
.param("mockedResponseId", "323")
|
||||
.param("mediaType", "application/json")
|
||||
.param("messageBody", "my message body")
|
||||
.param("httpStatus", "200"))
|
||||
.andExpect(status().isOk());
|
||||
|
||||
verify(klausService).getMockedResponse(any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void addHeader() throws Exception {
|
||||
when(klausService.getAllMockedResponses(any()))
|
||||
.thenReturn(mockedMessageDtoList);
|
||||
|
||||
mockMvc.perform(post("/home/e9dc0ad9-acd2-4699-99f7-368d53d7afd6")
|
||||
.param("addHeader", "addHeader")
|
||||
.param("headerKey", "myKey")
|
||||
.param("headerValue", "myValue")
|
||||
.param("clientUUID", "e9dc0ad9-acd2-4699-99f7-368d53d7afd6"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("index"))
|
||||
.andExpect(model().attributeExists("mockedMessageDtoList"));
|
||||
|
||||
verify(klausService, times(0)).getAllMockedResponses(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeHeader() throws Exception {
|
||||
when(klausService.getAllMockedResponses(any()))
|
||||
.thenReturn(mockedMessageDtoList);
|
||||
|
||||
mockMvc.perform(post("/home/e9dc0ad9-acd2-4699-99f7-368d53d7afd6")
|
||||
.param("removeHeader", "removeHeader")
|
||||
.param("clientUUID", "e9dc0ad9-acd2-4699-99f7-368d53d7afd6"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("index"))
|
||||
.andExpect(model().attributeExists("mockedMessageDtoList"));
|
||||
|
||||
verify(klausService, times(0)).getAllMockedResponses(any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateMessage() throws Exception {
|
||||
when(klausService.getAllMockedResponses(any()))
|
||||
.thenReturn(mockedMessageDtoList);
|
||||
when(klausService.getMockedResponse(any(), anyInt())).
|
||||
thenReturn(mockedMessageDto);
|
||||
|
||||
mockMvc.perform(post("/home/e9dc0ad9-acd2-4699-99f7-368d53d7afd6")
|
||||
.param("updateMessage", "323"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("index"))
|
||||
.andExpect(model().attributeExists("canTest"))
|
||||
.andExpect(model().attributeExists("mockedMessageDtoList"));
|
||||
|
||||
verify(klausService, times(0)).getAllMockedResponses(any());
|
||||
verify(klausService).getMockedResponse(any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeMessage() throws Exception {
|
||||
when(klausService.getAllMockedResponses(any()))
|
||||
.thenReturn(mockedMessageDtoList);
|
||||
|
||||
mockMvc.perform(post("/home/e9dc0ad9-acd2-4699-99f7-368d53d7afd6")
|
||||
.param("removeMessage", "323"))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(view().name("index"))
|
||||
.andExpect(model().attributeDoesNotExist("canTest"))
|
||||
.andExpect(model().attributeExists("mockedMessageDtoList"));
|
||||
|
||||
verify(klausService).getAllMockedResponses(any());
|
||||
verify(klausService).deleteMockedResponse(any(), anyInt());
|
||||
}
|
||||
|
||||
@Test
|
||||
void localhost() {
|
||||
assert klausMvcController.localhost() != null;
|
||||
}
|
||||
|
||||
@Test
|
||||
void clientUUID() {
|
||||
assert klausMvcController.clientUUID() != null;
|
||||
}
|
||||
|
||||
@Test
|
||||
void mockedMessageDto() {
|
||||
assert klausMvcController.mockedMessageDto() != null;
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void afterEach() {
|
||||
reset(klausService);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user