diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f65786..43ba49d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ Proteus Changelog. ## Unreleased ### No issue +**Replace whenComplete in async.** + + +[96534322bf6613a](https://github.com/noboomu/proteus/commit/96534322bf6613a) Joshua Bauer *2020-09-15 18:33:35* + **Ensure worker is used for async dispatch.** diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java b/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java index 5d70ee4..110c698 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/Extractors.java @@ -30,8 +30,10 @@ import java.util.Arrays; import java.util.Date; import java.util.Deque; +import java.util.List; import java.util.Objects; import java.util.function.Function; +import java.util.stream.Collectors; /** * @author jbauer @@ -344,6 +346,28 @@ public static Path filePath(final HttpServerExchange exchange, final String name } } + public static List pathList(final HttpServerExchange exchange, final String name) throws IllegalArgumentException + { + try + { + return exchange.getAttachment(FormDataParser.FORM_DATA).get(name).stream().map( i -> i.getFileItem().getFile()).collect(Collectors.toList()); + } catch( Exception e ) + { + throw new IllegalArgumentException("Missing parameter " + name, e); + } + } + + public static List fileList(final HttpServerExchange exchange, final String name) throws IllegalArgumentException + { + try + { + return exchange.getAttachment(FormDataParser.FORM_DATA).get(name).stream().map( i -> i.getFileItem().getFile().toFile()).collect(Collectors.toList()); + } catch( Exception e ) + { + throw new IllegalArgumentException("Missing parameter " + name, e); + } + } + public static File file(final HttpServerExchange exchange, final String name) throws IllegalArgumentException { try { diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java index 2969cd1..7c0115b 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/ServerDefaultResponseListener.java @@ -84,7 +84,7 @@ public boolean handleDefaultResponse(HttpServerExchange exchange) exchange.setStatusCode(StatusCodes.BAD_REQUEST); } - + statusCode = exchange.getStatusCode(); errorMap.put("exceptionClass", throwable.getClass().getName()); errorMap.put("message", throwable.getMessage()); diff --git a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java index 9258bca..6e3d8ed 100644 --- a/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java +++ b/proteus-core/src/main/java/io/sinistral/proteus/server/handlers/TypeHandler.java @@ -21,9 +21,7 @@ * Enum that assists in code generation for different method parameter types */ - -public enum TypeHandler -{ +public enum TypeHandler { LongType("Long $L = $T.longValue(exchange,$S)", false, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING), IntegerType("Integer $L = $T.integerValue(exchange,$S)", false, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.class, StatementParameterType.STRING), @@ -71,6 +69,10 @@ public enum TypeHandler BeanListValueOfType("$T $L = io.sinistral.proteus.server.Extractors.model(exchange,$L)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.LITERAL), BeanListFromStringType("$T $L = io.sinistral.proteus.server.Extractors.model(exchange,$L)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.LITERAL), + FileListType("$T $L = io.sinistral.proteus.server.Extractors.fileList(exchange,$S)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.STRING), + + PathListType("$T $L = io.sinistral.proteus.server.Extractors.pathList(exchange,$S)", true, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.STRING), + HeaderValueOfType("$T $L = $T.valueOf($T.string(exchange,$S))", false, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.TYPE, io.sinistral.proteus.server.Extractors.Header.class, StatementParameterType.STRING), HeaderFromStringType("$T $L = $T.fromString($T.string(exchange,$S))", false, StatementParameterType.TYPE, StatementParameterType.LITERAL, StatementParameterType.TYPE, io.sinistral.proteus.server.Extractors.Header.class, StatementParameterType.STRING), HeaderStringType("$T $L = $T.string(exchange,$S)", false, String.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Header.class, StatementParameterType.STRING), @@ -104,7 +106,6 @@ public enum TypeHandler OptionalDoubleType("$T $L = $T.doubleValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), OptionalBigDecimalType("$T $L = $T.bigDecimalValue(exchange,$S)", false, Optional.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), - OptionalDateType("$T<$T> $L = $T.date(exchange,$S)", false, Optional.class, java.util.Date.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), OptionalInstantType("$T<$T> $L = $T.instant(exchange,$S)", false, Optional.class, java.time.Instant.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), OptionalZonedDateTimeType("$T<$T> $L = $T.zonedDateTime(exchange,$S)", false, Optional.class, java.time.ZonedDateTime.class, StatementParameterType.LITERAL, io.sinistral.proteus.server.Extractors.Optional.class, StatementParameterType.STRING), @@ -173,7 +174,6 @@ public static void addStatement(MethodSpec.Builder builder, Parameter parameter, for (int i = 0; i < handler.parameterTypes.length; i++) { if (handler.parameterTypes[i] instanceof StatementParameterType) { - if (parameter.isAnnotationPresent(QueryParam.class)) { QueryParam qp = parameter.getAnnotation(QueryParam.class); pName = qp.value(); @@ -229,36 +229,29 @@ public static void addStatement(MethodSpec.Builder builder, Parameter parameter, Min min = parameter.isAnnotationPresent(Min.class) ? parameter.getAnnotationsByType(Min.class)[0] : null; - - if(max != null || min != null) - { - if(max != null && min != null) - { + if (max != null || min != null) { + if (max != null && min != null) { long maxValue = min.value(); long minValue = min.value(); builder.beginControlFlow("if( $L < $L )", pName, minValue); - builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",min.message().equals("{javax.validation.constraints.Min.message}") ? "must be greater than or equal to " + minValue : min.message()); + builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)", min.message().equals("{javax.validation.constraints.Min.message}") ? "must be greater than or equal to " + minValue : min.message()); builder.endControlFlow(); builder.beginControlFlow("else if( $L > $L )", pName, maxValue); - builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",max.message().equals("{javax.validation.constraints.Max.message}") ? "must be less than or equal to " + maxValue : max.message()); + builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)", max.message().equals("{javax.validation.constraints.Max.message}") ? "must be less than or equal to " + maxValue : max.message()); builder.endControlFlow(); - } - else if(max != null) - { + } else if (max != null) { long maxValue = max.value(); builder.beginControlFlow("if( $L > $L )", pName, maxValue); - builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",max.message().equals("{javax.validation.constraints.Max.message}") ? "must be less than or equal to " + maxValue : max.message()); + builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)", max.message().equals("{javax.validation.constraints.Max.message}") ? "must be less than or equal to " + maxValue : max.message()); builder.endControlFlow(); - } - else - { + } else { long minValue = min.value(); builder.beginControlFlow("if( $L < $L )", pName, minValue); - builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)",min.message().equals("{javax.validation.constraints.Min.message}") ? "must be greater than or equal to " + minValue : min.message()); + builder.addStatement("throw new io.sinistral.proteus.server.exceptions.ServerException($S,javax.ws.rs.core.Response.Status.BAD_REQUEST)", min.message().equals("{javax.validation.constraints.Min.message}") ? "must be greater than or equal to " + minValue : min.message()); builder.endControlFlow(); } } @@ -326,9 +319,10 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) if (HandlerGenerator.hasValueOfMethod(erasedType)) { if (!isBeanParam) { return QueryListValueOfType; - } else { + return BeanListValueOfType; + } } else if (HandlerGenerator.hasFromStringMethod(erasedType)) { if (!isBeanParam) { @@ -337,22 +331,24 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) } else { return BeanListFromStringType; } + } else if (erasedType.getTypeName().contains("java.nio.file.Path")) { + return PathListType; + } else if (erasedType.getTypeName().contains("java.io.File")) { + return FileListType; } else { return ModelType; } } catch (Exception e) { HandlerGenerator.log.error(e.getMessage(), e); - throw e; + throw e; } } if (isMap && !isOptional) { try { - - return ModelType; - + return ModelType; } catch (Exception e) { HandlerGenerator.log.error(e.getMessage(), e); @@ -387,7 +383,7 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) throw e; } - } else if (isArray ) { + } else if (isArray) { try { if (type instanceof ParameterizedType) { @@ -420,21 +416,16 @@ public static TypeHandler forType(Type type, Boolean isBeanParam) HandlerGenerator.log.error(e.getMessage(), e); throw e; } - } - - else if (isMap ) { + } else if (isMap) { try { - - return ModelType; + return ModelType; } catch (Exception e) { HandlerGenerator.log.error(e.getMessage(), e); throw e; } - } - - else if (isSet ) { + } else if (isSet) { try { if (type instanceof ParameterizedType) { @@ -481,9 +472,9 @@ else if (isSet ) { return FloatType; } else if (type.equals(Double.class)) { return DoubleType; - }else if (type.equals(BigDecimal.class)) { + } else if (type.equals(BigDecimal.class)) { return BigDecimalType; - } else if (type.equals(java.nio.ByteBuffer.class)) { + } else if (type.equals(java.nio.ByteBuffer.class)) { return ByteBufferType; } else if (type.equals(Boolean.class)) { return BooleanType; @@ -524,9 +515,9 @@ else if (isSet ) { return OptionalFloatType; } else if (type.getTypeName().contains("java.lang.Double")) { return OptionalDoubleType; - } else if (type.getTypeName().contains("java.math.BigDecimal")) { + } else if (type.getTypeName().contains("java.math.BigDecimal")) { return OptionalBigDecimalType; - }else if (type.getTypeName().contains("java.lang.Integer")) { + } else if (type.getTypeName().contains("java.lang.Integer")) { return OptionalIntegerType; } else if (type.getTypeName().contains("java.nio.file.Path")) { return OptionalFilePathType; diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java index 70ce9f6..cc7d3fe 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/controllers/Tests.java @@ -379,8 +379,48 @@ public ServerResponse responseUploadFile(ServerRequest request, @For } + @POST + @Path("list/file") + @Produces(MediaType.APPLICATION_JSON) + @Consumes("*/*") + @Blocking + public ServerResponse> uploadMultipleFiles(ServerRequest request, @FormParam("files") List files, @FormParam("names") List names ) throws Exception + { + + Map map = new HashMap<>(); + + for(int i = 0; i < files.size(); i++) + { + map.put(names.get(i),files.get(i).getTotalSpace()+""); + } + + + return response(map).applicationJson(); + + + } + + @POST + @Path("list/path") + @Produces(MediaType.APPLICATION_JSON) + @Consumes("*/*") + @Blocking + public ServerResponse> uploadMultiplePaths(ServerRequest request, @FormParam("files") List files, @FormParam("names") List names ) throws Exception + { + + Map map = new HashMap<>(); + + for(int i = 0; i < files.size(); i++) + { + map.put(names.get(i),files.get(i).toFile().getTotalSpace()+""); + } + return response(map).applicationJson(); + + + } + @GET @@ -479,6 +519,105 @@ public ServerResponse> debugBlockingEndpoint(ServerRequest re return response().badRequest(e); } } + + @GET + @Path("response/badrequest/blocking") + @Blocking + public ServerResponse> badRequestBlocking(ServerRequest request) + { + + return response().badRequest(new ServerException("Bad Request", Response.Status.BAD_REQUEST)); + + } + + @GET + @Path("response/badrequest") + public ServerResponse> badRequest(ServerRequest request) + { + + return response().badRequest(new ServerException("Bad Request", Response.Status.BAD_REQUEST)); + + } + + @GET + @Path("future/badrequest") + @Produces((MediaType.APPLICATION_JSON)) + public CompletableFuture>> responseFutureBadRequest(ServerRequest request) + { + + CompletableFuture>> future = new CompletableFuture<>(); + + request.getWorker().execute( () -> { + + try + { + + Thread.sleep(2000L); + + future.complete(response().badRequest(new ServerException("Bad request", Response.Status.BAD_REQUEST))); + + } catch( Exception e ) + { + future.completeExceptionally(e); + } + }); + + return future; + } + + @GET + @Path("future/badrequest/blocking") + @Produces((MediaType.APPLICATION_JSON)) + @Blocking + public CompletableFuture>> responseFutureBadRequestBlocking(ServerRequest request) + { + + CompletableFuture>> future = new CompletableFuture<>(); + + request.getWorker().execute( () -> { + + try + { + + Thread.sleep(2000L); + + future.complete(response().badRequest(new ServerException("Bad request", Response.Status.BAD_REQUEST))); + + } catch( Exception e ) + { + future.completeExceptionally(e); + } + }); + + return future; + } + + @GET + @Path("future/notfound/blocking") + @Produces((MediaType.APPLICATION_JSON)) + @Blocking + public CompletableFuture>> responseFutureNotFoundBlocking(ServerRequest request) + { + + CompletableFuture>> future = new CompletableFuture<>(); + + request.getWorker().execute( () -> { + + try + { + + Thread.sleep(2000L); + + future.complete(response().notFound()); + + } catch( Exception e ) + { + future.completeExceptionally(e); + } + }); + + return future; + } @GET @Path("response/future/user") @@ -514,6 +653,33 @@ public CompletableFuture>> responseFutureUser( return future; } + @GET + @Path("response/future/worker/blocking") + @Produces((MediaType.APPLICATION_JSON)) + @Blocking + public CompletableFuture>> responseFutureUserBlocking(ServerRequest request) + { + + CompletableFuture>> future = new CompletableFuture<>(); + + request.getWorker().execute( () -> { + + try + { + + Thread.sleep(2000L); + + future.complete(response(Map.of("status","OK")).applicationJson().ok()); + + } catch( Exception e ) + { + future.completeExceptionally(e); + } + }); + + return future; + } + @GET @Path("response/parameters/complex/{pathLong}") @Produces((MediaType.APPLICATION_JSON)) diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/server/DefaultServer.java b/proteus-core/src/test/java/io/sinistral/proteus/test/server/DefaultServer.java index 4f651d7..787725b 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/server/DefaultServer.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/server/DefaultServer.java @@ -97,7 +97,7 @@ private static void runInternal(final RunNotifier notifier) - RestAssured.baseURI = String.format("http://localhost:%d/v1",port); + RestAssured.baseURI = String.format("http://localhost:%d/",port); RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); diff --git a/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java b/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java index 873aacc..5492501 100644 --- a/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java +++ b/proteus-core/src/test/java/io/sinistral/proteus/test/server/TestControllerEndpoints.java @@ -52,6 +52,8 @@ public class TestControllerEndpoints { private File file = null; + + private List files = new ArrayList<>(); private Set idSet = new HashSet<>(); @@ -62,17 +64,21 @@ public void setUp() try { - byte[] bytes = new byte[8388608]; - Random random = new Random(); + for(int i = 0; i < 4; i++) + { + byte[] bytes = new byte[8388608]; + Random random = new Random(); random.nextBytes(bytes); - file = Files.createTempFile("test-asset", ".mp4").toFile(); - + files.add(Files.createTempFile("test-asset", ".mp4").toFile()); + LongStream.range(1L,10L).forEach( l -> { - + idSet.add(l); }); - + } + + file = files.get(0); } catch (Exception e) { @@ -85,27 +91,27 @@ public void setUp() @Test public void testDebugEndpoint() { - given().accept(ContentType.JSON).when().get("tests/response/debug").then().statusCode(200).body(containsString("testValue")); + given().accept(ContentType.JSON).when().get("v1/tests/response/debug").then().statusCode(200).body(containsString("testValue")); } @Test public void testDebugBlockingEndpoint() { - given().accept(ContentType.JSON).when().get("tests/response/debug/blocking").then().statusCode(200); + given().accept(ContentType.JSON).when().get("v1/tests/response/debug/blocking").then().statusCode(200); } @Test public void exchangeUserJson() { - User user = given().accept(ContentType.JSON).when().get("tests/exchange/user/json").as(User.class); + User user = given().accept(ContentType.JSON).when().get("v1/tests/exchange/user/json").as(User.class); assertThat(user.getId(), CoreMatchers.is(123L)); } @Test public void genericSet() { - given().accept(ContentType.JSON).when().queryParam("ids", idSet).get("tests/generic/set").then().statusCode(200).body(containsString("1")); + given().accept(ContentType.JSON).when().queryParam("ids", idSet).get("v1/tests/generic/set").then().statusCode(200).body(containsString("1")); } @Test @@ -137,7 +143,7 @@ public void genericBeanSet() String body = mapper.writeValueAsString(randomLongs); - given().contentType(ContentType.JSON).accept(ContentType.JSON).body(body).post("tests/generic/set/bean").then().statusCode(200).body(containsString(firstNumber.toString())); + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(body).post("v1/tests/generic/set/bean").then().statusCode(200).body(containsString(firstNumber.toString())); } catch (Exception e) { @@ -174,7 +180,7 @@ public void genericBeanList() String body = mapper.writeValueAsString(randomLongs); - given().contentType(ContentType.JSON).accept(ContentType.JSON).body(body).post("tests/generic/list/bean").then().statusCode(200).body(containsString(firstNumber.toString())); + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(body).post("v1/tests/generic/list/bean").then().statusCode(200).body(containsString(firstNumber.toString())); } catch (Exception e) { @@ -211,7 +217,7 @@ public void genericBeanMap() String body = mapper.writeValueAsString(randomLongs); - given().contentType(ContentType.JSON).accept(ContentType.JSON).body(body).post("tests/generic/map/bean").then().statusCode(200).body(containsString(firstNumber.toString())); + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(body).post("v1/tests/generic/map/bean").then().statusCode(200).body(containsString(firstNumber.toString())); } catch (Exception e) { @@ -222,53 +228,96 @@ public void genericBeanMap() @Test public void optionalGenericSet() { - given().accept(ContentType.JSON).when().queryParam("ids",idSet).get("tests/optional/set").then().statusCode(200).body(containsString("1")); + given().accept(ContentType.JSON).when().queryParam("ids",idSet).get("v1/tests/optional/set").then().statusCode(200).body(containsString("1")); } @Test public void exchangeUserXml() { - User user = given().accept(ContentType.XML).when().get("tests/exchange/user/xml").as(User.class); + User user = given().accept(ContentType.XML).when().get("v1/tests/exchange/user/xml").as(User.class); assertThat(user.getId(), CoreMatchers.is(123L)); } @Test public void responseUserJson() { - User user = given().accept(ContentType.JSON).when().get("tests/response/user/json").as(User.class); + User user = given().accept(ContentType.JSON).when().get("v1/tests/response/user/json").as(User.class); assertThat(user.getId(), CoreMatchers.is(123L)); } @Test public void responseWorkerFuture() { - Map response = given().accept(ContentType.JSON).when().get("tests/response/future/worker").as(Map.class); + Map response = given().accept(ContentType.JSON).when().get("v1/tests/response/future/worker").as(Map.class); assertThat(response.get("status").toString(), CoreMatchers.is("OK")); } + @Test + public void responseWorkerFutureBlocking() + { + Map response = given().accept(ContentType.JSON).when().get("v1/tests/response/future/worker/blocking").as(Map.class); + assertThat(response.get("status").toString(), CoreMatchers.is("OK")); + } + + @Test + public void healthCheck() + { + given().accept(ContentType.TEXT).when().get("health").then().statusCode(200).and().body(containsString("OK"));; + } + @Test public void responseUserXml() { - User user = given().accept(ContentType.XML).when().get("tests/response/user/xml").as(User.class); + User user = given().accept(ContentType.XML).when().get("v1/tests/response/user/xml").as(User.class); assertThat(user.getId(), CoreMatchers.is(123L)); } + @Test + public void badRequest() + { + given().accept(ContentType.TEXT).when().get("v1/tests/response/badrequest").then().statusCode(400); + } + + @Test + public void badRequestBlocking() + { + given().accept(ContentType.TEXT).when().get("v1/tests/response/badrequest/blocking").then().statusCode(400); + } + + @Test + public void badRequestFuture() + { + given().accept(ContentType.TEXT).when().get("v1/tests/future/badrequest").then().statusCode(400); + } + + @Test + public void notFoundFuture() + { + given().accept(ContentType.TEXT).when().get("v1/tests/future/notfound/blocking").then().statusCode(404); + } + + @Test + public void badRequestFutureBlocking() + { + given().accept(ContentType.TEXT).when().get("v1/tests/future/badrequest/blocking").then().statusCode(400); + } + @Test public void exchangePlaintext() { - given().accept(ContentType.TEXT).when().get("tests/exchange/plaintext").then().statusCode(200).and().body(containsString("Hello, World!")); + given().accept(ContentType.TEXT).when().get("v1/tests/exchange/plaintext").then().statusCode(200).and().body(containsString("Hello, World!")); } @Test public void exchangePlaintext2() { - given().accept(ContentType.TEXT).when().get("tests/exchange/plaintext2").then().statusCode(200).and().body(containsString("Hello, World!")); + given().accept(ContentType.TEXT).when().get("v1/tests/exchange/plaintext2").then().statusCode(200).and().body(containsString("Hello, World!")); } @Test public void responsePlaintext() { - given().accept(ContentType.TEXT).when().get("tests/response/plaintext").then().statusCode(200).and().body(containsString("Hello, World!")); + given().accept(ContentType.TEXT).when().get("v1/tests/response/plaintext").then().statusCode(200).and().body(containsString("Hello, World!")); } @Test @@ -276,7 +325,7 @@ public void responseEchoModel() { User model = new User(101L,UserType.ADMIN); - given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).when().post("tests/response/json/echo").then().statusCode(200).and().body(containsString("101")); + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).when().post("v1/tests/response/json/echo").then().statusCode(200).and().body(containsString("101")); } @@ -286,7 +335,7 @@ public void responseBeanParam() User model = new User(); model.setId(101L); - given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).when().post("tests/response/json/beanparam").then().statusCode(200).and().body(containsString("101")); + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(model).when().post("v1/tests/response/json/beanparam").then().statusCode(200).and().body(containsString("101")); } @@ -294,66 +343,66 @@ public void responseBeanParam() @Test public void responseFutureUser() { - given().accept(ContentType.JSON).when().get("tests/response/future/user").then().statusCode(200).and().body(containsString("123")); + given().accept(ContentType.JSON).when().get("v1/tests/response/future/user").then().statusCode(200).and().body(containsString("123")); } @Test public void responseMap() { - given().accept(ContentType.JSON).when().get("tests/response/map").then().statusCode(200).and().body("message", is("success")); + given().accept(ContentType.JSON).when().get("v1/tests/response/map").then().statusCode(200).and().body("message", is("success")); } @Test public void responseFutureMap() { - given().accept(ContentType.JSON).when().get("tests/response/future/map").then().statusCode(200).and().body("message", is("success")); + given().accept(ContentType.JSON).when().get("v1/tests/response/future/map").then().statusCode(200).and().body("message", is("success")); } @Test public void responseFutureResponseMap() { - given().accept(ContentType.JSON).when().get("tests/response/future/response").then().statusCode(200).and().body("message", is("success")); + given().accept(ContentType.JSON).when().get("v1/tests/response/future/response").then().statusCode(200).and().body("message", is("success")); } @Test public void testRedirect() { - given().when().get("tests/redirect").then().statusCode(200).and().header("Server", "gws"); + given().when().get("v1/tests/redirect").then().statusCode(200).and().header("Server", "gws"); } @Test public void testRedirectFoundCode() { - given().when().redirects().follow(false).get("tests/redirect").then().statusCode(302); + given().when().redirects().follow(false).get("v1/tests/redirect").then().statusCode(302); } @Test public void testRedirectMovedPermanentlyCode() { - given().when().redirects().follow(false).get("tests/redirect/permanent").then().statusCode(301); + given().when().redirects().follow(false).get("v1/tests/redirect/permanent").then().statusCode(301); } @Test public void pathParam() { - given().accept(ContentType.TEXT).when().get("tests/response/params/path/foobar").then().statusCode(200).and().body(containsString("foobar")); + given().accept(ContentType.TEXT).when().get("v1/tests/response/params/path/foobar").then().statusCode(200).and().body(containsString("foobar")); } // @Test // public void regexPathParam() // { -// given().accept(ContentType.TEXT).when().get("tests/response/params/regexpath/fooBar").then().statusCode(200).and().body(containsString("fooBar")); +// given().accept(ContentType.TEXT).when().get("v1/tests/response/params/regexpath/fooBar").then().statusCode(200).and().body(containsString("fooBar")); // // } // // @Test // public void invalidRegexPathParam() // { -// given().accept(ContentType.TEXT).when().get("tests/response/params/regexpath/fooBar101").then().statusCode(400); +// given().accept(ContentType.TEXT).when().get("v1/tests/response/params/regexpath/fooBar101").then().statusCode(400); // // } @@ -364,7 +413,7 @@ public void responseUploadFilePathParameter() try { - final InputStream is = given().multiPart("file", file).accept(ContentType.ANY).when().post("tests/response/file/path").asInputStream(); + final InputStream is = given().multiPart("file", file).accept(ContentType.ANY).when().post("v1/tests/response/file/path").asInputStream(); try(final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { @@ -380,6 +429,62 @@ public void responseUploadFilePathParameter() } } + + @Test + public void uploadMultipleFiles() + { + + try + { + + + Map map = given().multiPart("files", files.get(0)).multiPart("files",files.get(1)).multiPart("files",files.get(2)).multiPart("files",files.get(3)) + .multiPart("names",files.get(0).getName()) + .multiPart("names",files.get(1).getName()) + .multiPart("names",files.get(2).getName()) + .multiPart("names",files.get(3).getName()) + .accept(ContentType.JSON).when().post("v1/tests/list/file").as(Map.class); + + + assertThat(map.size(), equalTo(4)); + + assertThat(map.get(files.get(0).getName()), equalTo(files.get(0).getTotalSpace()+"")); + + } catch (Exception e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Test + public void uploadMultiplePaths() + { + + try + { + + + Map map = given().multiPart("files", files.get(0)).multiPart("files",files.get(1)).multiPart("files",files.get(2)).multiPart("files",files.get(3)) + .multiPart("names",files.get(0).getName()) + .multiPart("names",files.get(1).getName()) + .multiPart("names",files.get(2).getName()) + .multiPart("names",files.get(3).getName()) + .accept(ContentType.JSON).when().post("v1/tests/list/file").as(Map.class); + + + assertThat(map.size(), equalTo(4)); + + assertThat(map.get(files.get(0).getName()), equalTo(files.get(0).getTotalSpace()+"")); + + } catch (Exception e) + { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } @Test public void responseUploadOptionalFilePathParameter() @@ -388,7 +493,7 @@ public void responseUploadOptionalFilePathParameter() try { - final InputStream is = given().multiPart("file", file).accept(ContentType.ANY).when().post("tests/response/file/path/optional").asInputStream(); + final InputStream is = given().multiPart("file", file).accept(ContentType.ANY).when().post("v1/tests/response/file/path/optional").asInputStream(); try(final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()) { @@ -412,7 +517,7 @@ public void responseParseListParameter() { List values = new Random().longs(10, 0L, 20L).boxed().collect(Collectors.toList()); - given().contentType(ContentType.JSON).accept(ContentType.JSON).body(values).when().post("tests/response/parse/ids").then().statusCode(200); + given().contentType(ContentType.JSON).accept(ContentType.JSON).body(values).when().post("v1/tests/response/parse/ids").then().statusCode(200); } catch (Exception e) @@ -431,7 +536,7 @@ public void responseUploadByteBufferParameter() try { - final InputStream is = given().multiPart("file", file).accept(ContentType.ANY).when().post("tests/response/bytebuffer").asInputStream(); + final InputStream is = given().multiPart("file", file).accept(ContentType.ANY).when().post("v1/tests/response/bytebuffer").asInputStream(); final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); IOUtils.copy(is, byteArrayOutputStream); @@ -456,7 +561,7 @@ public void responseUploadFileParameter() try { - final InputStream is = given().multiPart("file", file).accept(ContentType.ANY).when().post("tests/response/file").asInputStream(); + final InputStream is = given().multiPart("file", file).accept(ContentType.ANY).when().post("v1/tests/response/file").asInputStream(); final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); IOUtils.copy(is, byteArrayOutputStream); @@ -488,7 +593,7 @@ public void responseParseInstant() .when() - .get("tests/response/parse/instant"). + .get("v1/tests/response/parse/instant"). then().statusCode(200).and().body(containsString(instant.toString())); @@ -507,7 +612,7 @@ public void responseParseTimestamp() .queryParam("timestamp", ts.toString()) .when() - .get("tests/response/parse/timestamp"). + .get("v1/tests/response/parse/timestamp"). then().statusCode(200).and().body(containsString(ts.toString())); @@ -528,7 +633,7 @@ public void responseParseBigDecimal() .when() - .get("tests/response/parse/big-decimal"). + .get("v1/tests/response/parse/big-decimal"). then().statusCode(200).and().body(containsString(value.toString())); } @@ -548,7 +653,7 @@ public void responseParseDouble() .when() - .get("tests/response/parse/double"). + .get("v1/tests/response/parse/double"). then().statusCode(200).and().body(containsString(value.toString())); } @@ -556,7 +661,7 @@ public void responseParseDouble() @Test public void notFound() { - given().accept(ContentType.JSON).when().get("tests/response/error/404").then().statusCode(404).log().body().content(containsString("No entity found")); + given().accept(ContentType.JSON).when().get("v1/tests/response/error/404").then().statusCode(404).log().body().content(containsString("No entity found")); } @@ -564,34 +669,34 @@ public void notFound() @Test public void unauthorized() { - given().accept(ContentType.JSON).when().get("tests/response/error/401").then().statusCode(401).log().body().content(containsString("Unauthorized")); + given().accept(ContentType.JSON).when().get("v1/tests/response/error/401").then().statusCode(401).log().body().content(containsString("Unauthorized")); } @Test public void maxValueError() { - given().queryParam("param",105).when().get("tests/response/max").then().statusCode(400).log(); + given().queryParam("param",105).when().get("v1/tests/response/max").then().statusCode(400).log(); } @Test public void minValueError() { - given().queryParam("param",5).when().get("tests/response/min").then().statusCode(400).log(); + given().queryParam("param",5).when().get("v1/tests/response/min").then().statusCode(400).log(); } @Test public void maxValue() { - given().queryParam("param",50).when().get("tests/response/max").then().statusCode(200).log(); + given().queryParam("param",50).when().get("v1/tests/response/max").then().statusCode(200).log(); } @Test public void minValue() { - given().queryParam("param",15).when().get("tests/response/min").then().statusCode(200).log(); + given().queryParam("param",15).when().get("v1/tests/response/min").then().statusCode(200).log(); } @@ -603,7 +708,7 @@ public void responseComplexParameters() UUID randomUUID = UUID.randomUUID(); Long longValue = 123456789L; List integerList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9); - String stringValue = "TESTSTRING123!#$"; + String stringValue = "v1/testsTRING123!#$"; Map map = given() @@ -639,7 +744,7 @@ public void responseComplexParameters() .when() - .get("tests/response/parameters/complex/" + longValue.toString()) + .get("v1/tests/response/parameters/complex/" + longValue.toString()) .as(Map.class);