router tests
~1.25h work
This commit is contained in:
parent
52f7866707
commit
9b1448edd8
17
.idea/libraries/junit_jupiter.xml
generated
Normal file
17
.idea/libraries/junit_jupiter.xml
generated
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<component name="libraryTable">
|
||||||
|
<library name="junit.jupiter" type="repository">
|
||||||
|
<properties maven-id="org.junit.jupiter:junit-jupiter:5.9.0" />
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/junit-jupiter-5.9.0.jar!/" />
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/junit-jupiter-api-5.9.0.jar!/" />
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/opentest4j-1.2.0.jar!/" />
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/junit-platform-commons-1.9.0.jar!/" />
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/apiguardian-api-1.1.2.jar!/" />
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/junit-jupiter-params-5.9.0.jar!/" />
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/junit-jupiter-engine-5.9.0.jar!/" />
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/junit-platform-engine-1.9.0.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</component>
|
||||||
13
.idea/libraries/mockito_core.xml
generated
Normal file
13
.idea/libraries/mockito_core.xml
generated
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
<component name="libraryTable">
|
||||||
|
<library name="mockito.core" type="repository">
|
||||||
|
<properties maven-id="org.mockito:mockito-core:5.8.0" />
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/mockito-core-5.8.0.jar!/" />
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/byte-buddy-1.14.10.jar!/" />
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/byte-buddy-agent-1.14.10.jar!/" />
|
||||||
|
<root url="jar://$PROJECT_DIR$/lib/objenesis-3.3.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES />
|
||||||
|
</library>
|
||||||
|
</component>
|
||||||
BIN
lib/apiguardian-api-1.1.2.jar
Normal file
BIN
lib/apiguardian-api-1.1.2.jar
Normal file
Binary file not shown.
BIN
lib/junit-jupiter-5.9.0.jar
Normal file
BIN
lib/junit-jupiter-5.9.0.jar
Normal file
Binary file not shown.
BIN
lib/junit-jupiter-api-5.9.0.jar
Normal file
BIN
lib/junit-jupiter-api-5.9.0.jar
Normal file
Binary file not shown.
BIN
lib/junit-jupiter-engine-5.9.0.jar
Normal file
BIN
lib/junit-jupiter-engine-5.9.0.jar
Normal file
Binary file not shown.
BIN
lib/junit-jupiter-params-5.9.0.jar
Normal file
BIN
lib/junit-jupiter-params-5.9.0.jar
Normal file
Binary file not shown.
BIN
lib/junit-platform-commons-1.9.0.jar
Normal file
BIN
lib/junit-platform-commons-1.9.0.jar
Normal file
Binary file not shown.
BIN
lib/junit-platform-engine-1.9.0.jar
Normal file
BIN
lib/junit-platform-engine-1.9.0.jar
Normal file
Binary file not shown.
BIN
lib/opentest4j-1.2.0.jar
Normal file
BIN
lib/opentest4j-1.2.0.jar
Normal file
Binary file not shown.
@ -1,6 +1,7 @@
|
|||||||
package at.nanopenguin.mtcg.http;
|
package at.nanopenguin.mtcg.http;
|
||||||
|
|
||||||
import at.nanopenguin.mtcg.application.service.Service;
|
import at.nanopenguin.mtcg.application.service.Service;
|
||||||
|
import lombok.NonNull;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
@ -8,11 +9,14 @@ import java.util.stream.IntStream;
|
|||||||
public class Router {
|
public class Router {
|
||||||
private final Map<HttpMethod, Map<String, Route>> routeMap = new HashMap<>();
|
private final Map<HttpMethod, Map<String, Route>> routeMap = new HashMap<>();
|
||||||
|
|
||||||
public void addRoute(final HttpMethod method, final String route, final Service service, final int[] pathVars) {
|
public Router() {
|
||||||
Map<String, Route> map = this.routeMap.get(method);
|
for (HttpMethod httpMethod : HttpMethod.values()) {
|
||||||
if (method != null && map == null) {
|
this.routeMap.put(httpMethod, new HashMap<>());
|
||||||
this.routeMap.put(method, (map = new HashMap<>()));
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addRoute(@NonNull final HttpMethod method, final String route, final Service service, final int[] pathVars) {
|
||||||
|
Map<String, Route> map = this.routeMap.get(method);
|
||||||
|
|
||||||
List<String> routeComponents = new ArrayList<>(Arrays.asList(route.split("/")));
|
List<String> routeComponents = new ArrayList<>(Arrays.asList(route.split("/")));
|
||||||
for ( Integer pathVarPos : pathVars) {
|
for ( Integer pathVarPos : pathVars) {
|
||||||
|
|||||||
162
src/test/java/at/nanopenguin/mtcg/http/RouterTest.java
Normal file
162
src/test/java/at/nanopenguin/mtcg/http/RouterTest.java
Normal file
@ -0,0 +1,162 @@
|
|||||||
|
package at.nanopenguin.mtcg.http;
|
||||||
|
|
||||||
|
import at.nanopenguin.mtcg.application.service.Service;
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import org.junit.jupiter.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.mockito.Mockito;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class RouterTest {
|
||||||
|
private static Router router;
|
||||||
|
@BeforeEach
|
||||||
|
void setup() {
|
||||||
|
router = new Router();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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");
|
||||||
|
Assertions.assertNotNull(result);
|
||||||
|
result.handleRequest(null);
|
||||||
|
Mockito.verify(mockedService, Mockito.times(1)).handleRequest(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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");
|
||||||
|
Assertions.assertNotNull(result);
|
||||||
|
result.handleRequest(null);
|
||||||
|
|
||||||
|
Assertions.assertNull(router.resolveRoute(HttpMethod.POST, "/get"));
|
||||||
|
Assertions.assertNull(router.resolveRoute(HttpMethod.PUT, "/get"));
|
||||||
|
Assertions.assertNull(router.resolveRoute(HttpMethod.DELETE, "/get"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@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[]{});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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[]{});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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");
|
||||||
|
Assertions.assertNotNull(result);
|
||||||
|
result.handleRequest(null);
|
||||||
|
Mockito.verify(mockedService, Mockito.times(1)).handleRequest(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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[]{});
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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");
|
||||||
|
Assertions.assertNotNull(result);
|
||||||
|
result.handleRequest(null);
|
||||||
|
Mockito.verify(mockedService, Mockito.times(1)).handleRequest(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@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");
|
||||||
|
Assertions.assertNotNull(result);
|
||||||
|
result.handleRequest(null);
|
||||||
|
Mockito.verify(mockedService, Mockito.times(1)).handleRequest(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user