spring-react-kubernetes-api/src/test/java/tk/antoine/roux/infrastructure/in/NodeControllerTest.java

110 lines
4.0 KiB
Java

package tk.antoine.roux.infrastructure.in;
import io.vavr.collection.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import tk.antoine.roux.domain.model.Node;
import tk.antoine.roux.domain.usecases.Command;
import tk.antoine.roux.domain.usecases.Command.EmptyCommand;
import tk.antoine.roux.domain.usecases.CriteriaCommand;
import tk.antoine.roux.domain.usecases.operation.GetNodesUseCase;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@WebMvcTest(NodeController.class)
@Import(WebConfiguration.class)
class NodeControllerTest {
@Autowired
MockMvc mockMvc;
@Value("${backoffice.configuration.api-prefix}")
private String backendPrefix;
@MockBean
private GetNodesUseCase getNodesUseCase;
private static final EmptyCommand EMPTY_COMMAND = new EmptyCommand();
@Test
void listNode() throws Exception {
// Given
MockHttpServletRequestBuilder request = get(backendPrefix + "/v1/nodes");
Mockito.when(getNodesUseCase.invoke(any(Command.class)))
.thenReturn(List.of(Node.builder().build(), Node.builder().build(), Node.builder().build()));
// When
ResultActions requestResult = mockMvc.perform(request);
// Then
requestResult
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$[0]").exists())
.andExpect(jsonPath("$[1]").exists())
.andExpect(jsonPath("$[2]").exists())
;
Mockito.verify(getNodesUseCase).invoke(eq(EMPTY_COMMAND));
}
@Test
void listNodeWithCriteria() throws Exception {
// Given
MockHttpServletRequestBuilder request = get(backendPrefix + "/v1/nodes")
.param("criteria", "worker");
Mockito.when(getNodesUseCase.invoke(any(Command.class)))
.thenReturn(List.of(Node.builder().build(), Node.builder().build(), Node.builder().build()));
// When
ResultActions requestResult = mockMvc.perform(request);
// Then
requestResult
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray());
Mockito.verify(getNodesUseCase).invoke(isA(CriteriaCommand.class));
}
@Test
@DisplayName("Call nodes endpoint with criteria causing bad request")
void listNodeWithInvalidCriteria() throws Exception {
// Given
MockHttpServletRequestBuilder request = get(backendPrefix + "/v1/nodes")
.param("criteria", "worker [ltl");
Mockito.when(getNodesUseCase.invoke(any(Command.class)))
.thenReturn(List.of(Node.builder().build(), Node.builder().build(), Node.builder().build()));
// When
ResultActions requestResult = mockMvc.perform(request);
// Then
requestResult
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$").isMap())
.andExpect(jsonPath("$.title").exists())
.andExpect(jsonPath("$.detail").exists())
.andExpect(jsonPath("$.status").value(400))
;
Mockito.verifyNoInteractions(getNodesUseCase);
}
}