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 {
|
public class RouterTest {
|
||||||
private static Router router;
|
private static Router router;
|
||||||
|
private static Service mockedService;
|
||||||
|
private static Service mockedServiceUnwanted;
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setup() {
|
void setup() {
|
||||||
router = new Router();
|
router = new Router();
|
||||||
|
mockedService = Mockito.mock(Service.class);
|
||||||
|
mockedServiceUnwanted = Mockito.mock(Service.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void simpleRoute() throws SQLException, JsonProcessingException {
|
void simpleRoute() throws SQLException, JsonProcessingException {
|
||||||
Service mockedService = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||||
|
|
||||||
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
||||||
@ -32,7 +35,6 @@ public class RouterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void wrongPath() throws SQLException, JsonProcessingException {
|
void wrongPath() throws SQLException, JsonProcessingException {
|
||||||
Service mockedService = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||||
|
|
||||||
Assertions.assertNull(router.resolveRoute(HttpMethod.GET, "/different"));
|
Assertions.assertNull(router.resolveRoute(HttpMethod.GET, "/different"));
|
||||||
@ -40,7 +42,6 @@ public class RouterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void wrongMethod() throws SQLException, JsonProcessingException {
|
void wrongMethod() throws SQLException, JsonProcessingException {
|
||||||
Service mockedService = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||||
|
|
||||||
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
||||||
@ -54,52 +55,42 @@ public class RouterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void differentHttpExists() throws SQLException, JsonProcessingException {
|
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[]{});
|
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||||
|
router.addRoute(HttpMethod.POST, "/get", mockedServiceUnwanted, new int[]{});
|
||||||
Service mockedServiceLong = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get/long", mockedServiceLong, new int[]{});
|
|
||||||
|
|
||||||
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
||||||
Assertions.assertNotNull(result);
|
Assertions.assertNotNull(result);
|
||||||
result.handleRequest(null);
|
result.handleRequest(null);
|
||||||
Mockito.verify(mockedService, Mockito.times(1)).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
|
@Test
|
||||||
void shorterPathExists() throws SQLException, JsonProcessingException {
|
void shorterPathExists() throws SQLException, JsonProcessingException {
|
||||||
Service mockedService = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get/path", mockedService, new int[]{});
|
router.addRoute(HttpMethod.GET, "/get/path", mockedService, new int[]{});
|
||||||
|
router.addRoute(HttpMethod.GET, "/get", mockedServiceUnwanted, new int[]{});
|
||||||
Service mockedServiceShort = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get", mockedServiceShort, new int[]{});
|
|
||||||
|
|
||||||
Service result = router.resolveRoute(HttpMethod.GET, "/get/path");
|
Service result = router.resolveRoute(HttpMethod.GET, "/get/path");
|
||||||
Assertions.assertNotNull(result);
|
Assertions.assertNotNull(result);
|
||||||
result.handleRequest(null);
|
result.handleRequest(null);
|
||||||
Mockito.verify(mockedService, Mockito.times(1)).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
|
@Test
|
||||||
void pathWithVar() throws SQLException, JsonProcessingException {
|
void pathWithVar() throws SQLException, JsonProcessingException {
|
||||||
Service mockedService = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get/{var}", mockedService, new int[]{2});
|
router.addRoute(HttpMethod.GET, "/get/{var}", mockedService, new int[]{2});
|
||||||
|
|
||||||
Service result = router.resolveRoute(HttpMethod.GET, "/get/value");
|
Service result = router.resolveRoute(HttpMethod.GET, "/get/value");
|
||||||
@ -110,37 +101,30 @@ public class RouterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void longerPathWithVarExists() throws SQLException, JsonProcessingException {
|
void longerPathWithVarExists() throws SQLException, JsonProcessingException {
|
||||||
Service mockedService = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
router.addRoute(HttpMethod.GET, "/get", mockedService, new int[]{});
|
||||||
|
router.addRoute(HttpMethod.GET, "/get/{var}", mockedServiceUnwanted, new int[]{2});
|
||||||
Service mockedServiceVar = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get/{var}", mockedServiceVar, new int[]{2});
|
|
||||||
|
|
||||||
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
Service result = router.resolveRoute(HttpMethod.GET, "/get");
|
||||||
Assertions.assertNotNull(result);
|
Assertions.assertNotNull(result);
|
||||||
result.handleRequest(null);
|
result.handleRequest(null);
|
||||||
Mockito.verify(mockedService, Mockito.times(1)).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
|
@Test
|
||||||
void pathBeforeVarIsValid() throws SQLException, JsonProcessingException {
|
void pathBeforeVarIsValid() throws SQLException, JsonProcessingException {
|
||||||
Service mockedService = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get/{var}", mockedService, new int[]{2});
|
router.addRoute(HttpMethod.GET, "/get/{var}", mockedService, new int[]{2});
|
||||||
|
router.addRoute(HttpMethod.GET, "/get", mockedServiceUnwanted, new int[]{});
|
||||||
Service mockedServiceNoVar = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get", mockedServiceNoVar, new int[]{});
|
|
||||||
|
|
||||||
Service result = router.resolveRoute(HttpMethod.GET, "/get/value");
|
Service result = router.resolveRoute(HttpMethod.GET, "/get/value");
|
||||||
Assertions.assertNotNull(result);
|
Assertions.assertNotNull(result);
|
||||||
result.handleRequest(null);
|
result.handleRequest(null);
|
||||||
Mockito.verify(mockedService, Mockito.times(1)).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
|
@Test
|
||||||
void pathContinuesAfterVar() throws SQLException, JsonProcessingException {
|
void pathContinuesAfterVar() throws SQLException, JsonProcessingException {
|
||||||
Service mockedService = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get/{var}/path", mockedService, new int[]{2});
|
router.addRoute(HttpMethod.GET, "/get/{var}/path", mockedService, new int[]{2});
|
||||||
|
|
||||||
Service result = router.resolveRoute(HttpMethod.GET, "/get/value/path");
|
Service result = router.resolveRoute(HttpMethod.GET, "/get/value/path");
|
||||||
@ -151,7 +135,6 @@ public class RouterTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void pathWithTwoVars() throws SQLException, JsonProcessingException {
|
void pathWithTwoVars() throws SQLException, JsonProcessingException {
|
||||||
Service mockedService = Mockito.mock(Service.class);
|
|
||||||
router.addRoute(HttpMethod.GET, "/get/{var}/path/{var}", mockedService, new int[]{2, 4});
|
router.addRoute(HttpMethod.GET, "/get/{var}/path/{var}", mockedService, new int[]{2, 4});
|
||||||
|
|
||||||
Service result = router.resolveRoute(HttpMethod.GET, "/get/value/path/other_value");
|
Service result = router.resolveRoute(HttpMethod.GET, "/get/value/path/other_value");
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user