chore: Remove redundant codes
This commit is contained in:
parent
0310a4b191
commit
48541a08e7
1 changed files with 149 additions and 93 deletions
|
|
@ -4,28 +4,23 @@ import static org.mockito.ArgumentMatchers.*;
|
|||
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.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonDeserializer;
|
||||
import com.fasterxml.jackson.databind.JsonSerializer;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.js0ny.ilp_coursework.data.*;
|
||||
import io.github.js0ny.ilp_coursework.service.GpsCalculationService;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryDependsOnPostProcessor;
|
||||
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.bean.override.mockito.MockitoBean;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
|
||||
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@WebMvcTest(ApiController.class)
|
||||
public class ApiControllerTest {
|
||||
|
||||
|
|
@ -41,6 +36,7 @@ public class ApiControllerTest {
|
|||
@Nested
|
||||
@DisplayName("GET /uid")
|
||||
class GetUidTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("GET /uid -> 200 OK")
|
||||
void getUid_shouldReturn200AndStudentIdFromService() throws Exception {
|
||||
|
|
@ -55,20 +51,27 @@ public class ApiControllerTest {
|
|||
@Nested
|
||||
@DisplayName("POST /distanceTo")
|
||||
class GetDistanceTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("POST /distanceTo -> 200 OK")
|
||||
void getDistance_shouldReturn200AndDistance_whenCorrectInput() throws Exception {
|
||||
void getDistance_shouldReturn200AndDistance_whenCorrectInput()
|
||||
throws Exception {
|
||||
double expected = 5.0;
|
||||
String endpoint = "/api/v1/distanceTo";
|
||||
LngLatDto p1 = new LngLatDto(0, 4.0);
|
||||
LngLatDto p2 = new LngLatDto(3.0, 0);
|
||||
var req = new DistanceRequestDto(p1, p2);
|
||||
when(service.calculateDistance(any(LngLatDto.class), any(LngLatDto.class))).thenReturn(expected);
|
||||
when(
|
||||
service.calculateDistance(
|
||||
any(LngLatDto.class),
|
||||
any(LngLatDto.class)
|
||||
)
|
||||
).thenReturn(expected);
|
||||
var mock = mockMvc.perform(
|
||||
post(endpoint)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(req)));
|
||||
|
||||
.content(objectMapper.writeValueAsString(req))
|
||||
);
|
||||
|
||||
mock.andExpect(status().isOk());
|
||||
mock.andExpect(content().string(String.valueOf(expected)));
|
||||
|
|
@ -77,7 +80,6 @@ public class ApiControllerTest {
|
|||
@Test
|
||||
@DisplayName("POST /distanceTo -> 400 Bad Request: Missing Field")
|
||||
void getDistance_shouldReturn400_whenMissingField() throws Exception {
|
||||
double expected = 5.0;
|
||||
String endpoint = "/api/v1/distanceTo";
|
||||
String req = """
|
||||
{
|
||||
|
|
@ -87,33 +89,40 @@ public class ApiControllerTest {
|
|||
}
|
||||
}
|
||||
""";
|
||||
when(service.calculateDistance(any(LngLatDto.class), isNull())).thenThrow(new NullPointerException());
|
||||
var mock = mockMvc.perform(post(endpoint)
|
||||
when(
|
||||
service.calculateDistance(any(LngLatDto.class), isNull())
|
||||
).thenThrow(new NullPointerException());
|
||||
mockMvc
|
||||
.perform(
|
||||
post(endpoint)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(req))
|
||||
.content(req)
|
||||
)
|
||||
.andExpect(status().isBadRequest());
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("POST /isCloseTo")
|
||||
class IsCloseToTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("POST /isCloseTo -> 200 OK")
|
||||
void getIsCloseTo_shouldReturn200AndBoolean_whenCorrectInput() throws Exception {
|
||||
void getIsCloseTo_shouldReturn200AndBoolean_whenCorrectInput()
|
||||
throws Exception {
|
||||
boolean expected = false;
|
||||
String endpoint = "/api/v1/isCloseTo";
|
||||
LngLatDto p1 = new LngLatDto(0, 4.0);
|
||||
LngLatDto p2 = new LngLatDto(3.0, 0);
|
||||
var req = new DistanceRequestDto(p1, p2);
|
||||
when(service.isCloseTo(any(LngLatDto.class), any(LngLatDto.class))).thenReturn(expected);
|
||||
when(
|
||||
service.isCloseTo(any(LngLatDto.class), any(LngLatDto.class))
|
||||
).thenReturn(expected);
|
||||
var mock = mockMvc.perform(
|
||||
post(endpoint)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(req)));
|
||||
|
||||
.content(objectMapper.writeValueAsString(req))
|
||||
);
|
||||
|
||||
mock.andExpect(status().isOk());
|
||||
mock.andExpect(content().string(String.valueOf(expected)));
|
||||
|
|
@ -121,46 +130,55 @@ public class ApiControllerTest {
|
|||
|
||||
@Test
|
||||
@DisplayName("POST /isCloseTo -> 400 Bad Request: Malformed JSON ")
|
||||
void getIsCloseTo_shouldReturn400_whenJsonIsMalformed() throws Exception {
|
||||
void getIsCloseTo_shouldReturn400_whenJsonIsMalformed()
|
||||
throws Exception {
|
||||
// json without a bracket
|
||||
String malformedJson = """
|
||||
{
|
||||
"position1": { "lng": 0.0, "lat": 3.0 }
|
||||
""";
|
||||
mockMvc.perform(post("/api/v1/isCloseTo")
|
||||
mockMvc
|
||||
.perform(
|
||||
post("/api/v1/isCloseTo")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(malformedJson))
|
||||
.content(malformedJson)
|
||||
)
|
||||
.andExpect(status().isBadRequest());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
@DisplayName("POST /nextPosition")
|
||||
class GetNextPositionTests {
|
||||
|
||||
String endpoint = "/api/v1/nextPosition";
|
||||
|
||||
@Test
|
||||
@DisplayName("POST /nextPosition -> 200 OK")
|
||||
void getNextPosition_shouldReturn200AndCoordinate_whenCorrectInput() throws Exception {
|
||||
void getNextPosition_shouldReturn200AndCoordinate_whenCorrectInput()
|
||||
throws Exception {
|
||||
LngLatDto expected = new LngLatDto(0.00015, 0.0);
|
||||
LngLatDto p = new LngLatDto(0, 0);
|
||||
var req = new MovementRequestDto(p, 0);
|
||||
when(service.nextPosition(any(LngLatDto.class), anyDouble())).thenReturn(expected);
|
||||
when(
|
||||
service.nextPosition(any(LngLatDto.class), anyDouble())
|
||||
).thenReturn(expected);
|
||||
var mock = mockMvc.perform(
|
||||
post(endpoint)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(req)));
|
||||
|
||||
.content(objectMapper.writeValueAsString(req))
|
||||
);
|
||||
|
||||
mock.andExpect(status().isOk());
|
||||
mock.andExpect(content().json(objectMapper.writeValueAsString(expected)));
|
||||
|
||||
mock.andExpect(
|
||||
content().json(objectMapper.writeValueAsString(expected))
|
||||
);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("POST /nextPosition -> 400 Bad Request: Missing Field")
|
||||
void getNextPosition_shouldReturn400_whenKeyNameError() throws Exception {
|
||||
void getNextPosition_shouldReturn400_whenKeyNameError()
|
||||
throws Exception {
|
||||
// "position" should be "start"
|
||||
String malformedJson = """
|
||||
{
|
||||
|
|
@ -168,74 +186,112 @@ public class ApiControllerTest {
|
|||
"angle": 180
|
||||
}
|
||||
""";
|
||||
when(service.nextPosition(isNull(), anyDouble())).thenThrow(new NullPointerException());
|
||||
mockMvc.perform(post("/api/v1/nextPosition")
|
||||
when(service.nextPosition(isNull(), anyDouble())).thenThrow(
|
||||
new NullPointerException()
|
||||
);
|
||||
mockMvc
|
||||
.perform(
|
||||
post("/api/v1/nextPosition")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(malformedJson))
|
||||
.content(malformedJson)
|
||||
)
|
||||
.andExpect(MockMvcResultMatchers.status().isBadRequest());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Nested
|
||||
@DisplayName("POST /isInRegion")
|
||||
class GetIsInRegionTests {
|
||||
|
||||
@Test
|
||||
@DisplayName("POST /isInRegion -> 200 OK")
|
||||
void getIsInRegion_shouldReturn200AndBoolean_whenCorrectInput() throws Exception {
|
||||
void getIsInRegion_shouldReturn200AndBoolean_whenCorrectInput()
|
||||
throws Exception {
|
||||
boolean expected = false;
|
||||
String endpoint = "/api/v1/isInRegion";
|
||||
var position = new LngLatDto(1.234, 1.222);
|
||||
var region = new RegionDto("central",
|
||||
List.of(new LngLatDto(-3.192473, 55.946233), new LngLatDto(-3.192473, 55.942617),
|
||||
new LngLatDto(-3.184319, 55.942617), new LngLatDto(-3.184319, 55.946233),
|
||||
new LngLatDto(-3.192473, 55.946233)));
|
||||
var region = new RegionDto(
|
||||
"central",
|
||||
List.of(
|
||||
new LngLatDto(-3.192473, 55.946233),
|
||||
new LngLatDto(-3.192473, 55.942617),
|
||||
new LngLatDto(-3.184319, 55.942617),
|
||||
new LngLatDto(-3.184319, 55.946233),
|
||||
new LngLatDto(-3.192473, 55.946233)
|
||||
)
|
||||
);
|
||||
var req = new RegionCheckRequestDto(position, region);
|
||||
when(service.checkIsInRegion(any(LngLatDto.class), any(RegionDto.class))).thenReturn(expected);
|
||||
when(
|
||||
service.checkIsInRegion(
|
||||
any(LngLatDto.class),
|
||||
any(RegionDto.class)
|
||||
)
|
||||
).thenReturn(expected);
|
||||
var mock = mockMvc.perform(
|
||||
post(endpoint)
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(req)));
|
||||
|
||||
.content(objectMapper.writeValueAsString(req))
|
||||
);
|
||||
|
||||
mock.andExpect(status().isOk());
|
||||
mock.andExpect(content().string(String.valueOf(expected)));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("POST /isInRegion -> 400 Bad Request: Passing a list of empty vertices to isInRegion")
|
||||
void getIsInRegion_shouldReturn400_whenPassingIllegalArguments() throws Exception {
|
||||
@DisplayName(
|
||||
"POST /isInRegion -> 400 Bad Request: Passing a list of empty vertices to isInRegion"
|
||||
)
|
||||
void getIsInRegion_shouldReturn400_whenPassingIllegalArguments()
|
||||
throws Exception {
|
||||
var position = new LngLatDto(1, 1);
|
||||
var region = new RegionDto("illegal", List.of());
|
||||
var request = new RegionCheckRequestDto(position, region);
|
||||
when(service.checkIsInRegion(any(LngLatDto.class), any(RegionDto.class)))
|
||||
.thenThrow(new IllegalArgumentException("Region is not closed."));
|
||||
mockMvc.perform(post("/api/v1/isInRegion")
|
||||
when(
|
||||
service.checkIsInRegion(
|
||||
any(LngLatDto.class),
|
||||
any(RegionDto.class)
|
||||
)
|
||||
).thenThrow(new IllegalArgumentException("Region is not closed."));
|
||||
mockMvc
|
||||
.perform(
|
||||
post("/api/v1/isInRegion")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(request)))
|
||||
.content(objectMapper.writeValueAsString(request))
|
||||
)
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("POST /isInRegion -> 400 Bad Request: Passing a list of not-closing vertices to isInRegion")
|
||||
void getIsInRegion_shouldReturn400_whenPassingNotClosingVertices() throws Exception {
|
||||
@DisplayName(
|
||||
"POST /isInRegion -> 400 Bad Request: Passing a list of not-closing vertices to isInRegion"
|
||||
)
|
||||
void getIsInRegion_shouldReturn400_whenPassingNotClosingVertices()
|
||||
throws Exception {
|
||||
var position = new LngLatDto(1, 1);
|
||||
var region = new RegionDto("illegal", List.of(
|
||||
var region = new RegionDto(
|
||||
"illegal",
|
||||
List.of(
|
||||
new LngLatDto(1, 2),
|
||||
new LngLatDto(3, 4),
|
||||
new LngLatDto(5, 6),
|
||||
new LngLatDto(7, 8),
|
||||
new LngLatDto(9, 10)
|
||||
));
|
||||
)
|
||||
);
|
||||
var request = new RegionCheckRequestDto(position, region);
|
||||
when(service.checkIsInRegion(any(LngLatDto.class), any(RegionDto.class)))
|
||||
.thenThrow(new IllegalArgumentException("Region is not closed."));
|
||||
mockMvc.perform(post("/api/v1/isInRegion")
|
||||
when(
|
||||
service.checkIsInRegion(
|
||||
any(LngLatDto.class),
|
||||
any(RegionDto.class)
|
||||
)
|
||||
).thenThrow(new IllegalArgumentException("Region is not closed."));
|
||||
mockMvc
|
||||
.perform(
|
||||
post("/api/v1/isInRegion")
|
||||
.contentType(MediaType.APPLICATION_JSON)
|
||||
.content(objectMapper.writeValueAsString(request)))
|
||||
.content(objectMapper.writeValueAsString(request))
|
||||
)
|
||||
.andExpect(status().isBadRequest());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue