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