improvements to router tests
~15 min work
This commit is contained in:
parent
6da608f90b
commit
4874f110c1
@ -14,14 +14,17 @@ import java.util.Map;
|
||||
|
||||
public class RouterTest {
|
||||
private static Router router;
|
||||
private static Service mockedService;
|
||||
private static Service mockedServiceUnwanted;
|
||||
@BeforeEach
|
||||
void setup() {
|
||||
router = new Router();
|
||||
mockedService = Mockito.mock(Service.class);
|
||||
mockedServiceUnwanted = Mockito.mock(Service.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void simpleRoute() throws SQLException, JsonProcessingException {
|
||||
Service mockedService = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
||||
@ -32,7 +35,6 @@ public class RouterTest {
|
||||
|
||||
@Test
|
||||
void wrongPath() throws SQLException, JsonProcessingException {
|
||||
Service mockedService = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||
|
||||
Assertions.assertNull(router.resolveRoute(HttpMethod.GET, "/different"));
|
||||
@ -40,7 +42,6 @@ public class RouterTest {
|
||||
|
||||
@Test
|
||||
void wrongMethod() throws SQLException, JsonProcessingException {
|
||||
Service mockedService = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
||||
@ -54,52 +55,42 @@ public class RouterTest {
|
||||
|
||||
@Test
|
||||
void differentHttpExists() throws SQLException, JsonProcessingException {
|
||||
Service mockedServiceGet = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedServiceGet, new int[]{});
|
||||
|
||||
Service mockedServicePost = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.POST, "/get", mockedServicePost, new int[]{});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
||||
Assertions.assertNotNull(result);
|
||||
result.handleRequest(null);
|
||||
Mockito.verify(mockedServiceGet, Mockito.times(1)).handleRequest(null);
|
||||
Mockito.verify(mockedServicePost, Mockito.times(0)).handleRequest(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void longerPathExists() throws SQLException, JsonProcessingException {
|
||||
Service mockedService = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||
|
||||
Service mockedServiceLong = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get/long", mockedServiceLong, new int[]{});
|
||||
router.addRoute(HttpMethod.POST, "/get", mockedServiceUnwanted, new int[]{});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
||||
Assertions.assertNotNull(result);
|
||||
result.handleRequest(null);
|
||||
Mockito.verify(mockedService, Mockito.times(1)).handleRequest(null);
|
||||
Mockito.verify(mockedServiceLong, Mockito.times(0)).handleRequest(null);
|
||||
Mockito.verify(mockedServiceUnwanted, Mockito.times(0)).handleRequest(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void longerPathExists() throws SQLException, JsonProcessingException {
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||
router.addRoute(HttpMethod.GET, "/get/long", mockedServiceUnwanted, new int[]{});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
||||
Assertions.assertNotNull(result);
|
||||
result.handleRequest(null);
|
||||
Mockito.verify(mockedService, Mockito.times(1)).handleRequest(null);
|
||||
Mockito.verify(mockedServiceUnwanted, Mockito.times(0)).handleRequest(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shorterPathExists() throws SQLException, JsonProcessingException {
|
||||
Service mockedService = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get/path", mockedService, new int[]{});
|
||||
|
||||
Service mockedServiceShort = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedServiceShort, new int[]{});
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedServiceUnwanted, new int[]{});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get/path");
|
||||
Assertions.assertNotNull(result);
|
||||
result.handleRequest(null);
|
||||
Mockito.verify(mockedService, Mockito.times(1)).handleRequest(null);
|
||||
Mockito.verify(mockedServiceShort, Mockito.times(0)).handleRequest(null);
|
||||
Mockito.verify(mockedServiceUnwanted, Mockito.times(0)).handleRequest(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathWithVar() throws SQLException, JsonProcessingException {
|
||||
Service mockedService = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get/{var}", mockedService, new int[]{2});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get/value");
|
||||
@ -110,37 +101,30 @@ public class RouterTest {
|
||||
|
||||
@Test
|
||||
void longerPathWithVarExists() throws SQLException, JsonProcessingException {
|
||||
Service mockedService = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||
|
||||
Service mockedServiceVar = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get/{var}", mockedServiceVar, new int[]{2});
|
||||
router.addRoute(HttpMethod.GET, "/get/{var}", mockedServiceUnwanted, new int[]{2});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
||||
Assertions.assertNotNull(result);
|
||||
result.handleRequest(null);
|
||||
Mockito.verify(mockedService, Mockito.times(1)).handleRequest(null);
|
||||
Mockito.verify(mockedServiceVar, Mockito.times(0)).handleRequest(null);
|
||||
Mockito.verify(mockedServiceUnwanted, Mockito.times(0)).handleRequest(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathBeforeVarIsValid() throws SQLException, JsonProcessingException {
|
||||
Service mockedService = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get/{var}", mockedService, new int[]{2});
|
||||
|
||||
Service mockedServiceNoVar = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedServiceNoVar, new int[]{});
|
||||
router.addRoute(HttpMethod.GET, "/get", mockedServiceUnwanted, new int[]{});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get/value");
|
||||
Assertions.assertNotNull(result);
|
||||
result.handleRequest(null);
|
||||
Mockito.verify(mockedService, Mockito.times(1)).handleRequest(null);
|
||||
Mockito.verify(mockedServiceNoVar, Mockito.times(0)).handleRequest(null);
|
||||
Mockito.verify(mockedServiceUnwanted, Mockito.times(0)).handleRequest(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
void pathContinuesAfterVar() throws SQLException, JsonProcessingException {
|
||||
Service mockedService = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get/{var}/path", mockedService, new int[]{2});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get/value/path");
|
||||
@ -151,7 +135,6 @@ public class RouterTest {
|
||||
|
||||
@Test
|
||||
void pathWithTwoVars() throws SQLException, JsonProcessingException {
|
||||
Service mockedService = Mockito.mock(Service.class);
|
||||
router.addRoute(HttpMethod.GET, "/get/{var}/path/{var}", mockedService, new int[]{2, 4});
|
||||
|
||||
Service result = router.resolveRoute(HttpMethod.GET, "/get/value/path/other_value");
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user