Turned on checkstyle

This commit is contained in:
Marcin Grzejszczak
2019-02-01 15:48:32 +01:00
parent 94e9b8f2f8
commit e4b08a083c
268 changed files with 5114 additions and 3993 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,10 +21,17 @@ import org.springframework.cloud.function.context.catalog.FunctionInspector;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
/**
* Simple implementation of a {@link StringConverter}.
*
* @author Dave Syer
*/
public class BasicStringConverter implements StringConverter {
private ConversionService conversionService;
private ConfigurableListableBeanFactory registry;
private FunctionInspector inspector;
public BasicStringConverter(FunctionInspector inspector,
@@ -35,16 +42,14 @@ public class BasicStringConverter implements StringConverter {
@Override
public Object convert(Object function, String value) {
if (conversionService == null && registry != null) {
ConversionService conversionService = this.registry
.getConversionService();
if (this.conversionService == null && this.registry != null) {
ConversionService conversionService = this.registry.getConversionService();
this.conversionService = conversionService != null ? conversionService
: new DefaultConversionService();
}
Class<?> type = inspector.getInputType(function);
return conversionService.canConvert(String.class, type)
? conversionService.convert(value, type)
: value;
Class<?> type = this.inspector.getInputType(function);
return this.conversionService.canConvert(String.class, type)
? this.conversionService.convert(value, type) : value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,6 +34,8 @@ import java.util.stream.Stream;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.cloud.function.context.catalog.FunctionInspector;
@@ -65,9 +67,6 @@ import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.ServerWebInputException;
import org.springframework.web.server.UnsupportedMediaTypeStatusException;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
* @author Oleg Zhurakousky
@@ -123,23 +122,27 @@ public class RequestProcessor {
.flatMap(body -> response(wrapper, body, false));
}
public Mono<ResponseEntity<?>> post(FunctionWrapper wrapper, String body, boolean stream) {
public Mono<ResponseEntity<?>> post(FunctionWrapper wrapper, String body,
boolean stream) {
Object function = wrapper.handler();
Class<?> inputType = inspector.getInputType(function);
Class<?> inputType = this.inspector.getInputType(function);
Type itemType = getItemType(function);
Object input = body;
if (StringUtils.hasText(body)) {
if (this.shouldUseJsonConversion(body, wrapper.headers.getContentType())) {
Type jsonType = body.startsWith("[") && Collection.class.isAssignableFrom(inputType) || body.startsWith("{") ? inputType : Collection.class;
Type jsonType = body.startsWith("[")
&& Collection.class.isAssignableFrom(inputType)
|| body.startsWith("{") ? inputType : Collection.class;
if (body.startsWith("[")) {
jsonType = ResolvableType.forClassWithGenerics((Class<?>)jsonType, (Class<?>) itemType).getType();
jsonType = ResolvableType.forClassWithGenerics((Class<?>) jsonType,
(Class<?>) itemType).getType();
}
input = mapper.toObject(body, jsonType);
input = this.mapper.toObject(body, jsonType);
}
else {
input = converter.convert(function, body);
input = this.converter.convert(function, body);
}
}
@@ -148,7 +151,8 @@ public class RequestProcessor {
private boolean shouldUseJsonConversion(String body, MediaType contentType) {
return (body.startsWith("[") || body.startsWith("{"))
&& (contentType == null || (contentType != null && !"text".equalsIgnoreCase(contentType.getType())));
&& (contentType == null || (contentType != null
&& !"text".equalsIgnoreCase(contentType.getType())));
}
public Mono<ResponseEntity<?>> stream(FunctionWrapper request) {
@@ -171,7 +175,7 @@ public class RequestProcessor {
Flux<?> flux;
if (body != null) {
if (Collection.class
.isAssignableFrom(inspector.getInputType(wrapper.handler()))) {
.isAssignableFrom(this.inspector.getInputType(wrapper.handler()))) {
flux = Flux.just(body);
}
else {
@@ -181,15 +185,18 @@ public class RequestProcessor {
flux = Flux.fromIterable(iterable);
}
}
else if (MultiValueMap.class.isAssignableFrom(inspector.getInputType(wrapper.handler()))) {
else if (MultiValueMap.class
.isAssignableFrom(this.inspector.getInputType(wrapper.handler()))) {
flux = Flux.just(wrapper.params());
}
else {
throw new IllegalStateException("Failed to determine input for function call with parameters: '" + wrapper.params
+ "' and headers: `" + wrapper.headers + "`");
throw new IllegalStateException(
"Failed to determine input for function call with parameters: '"
+ wrapper.params + "' and headers: `" + wrapper.headers
+ "`");
}
if (inspector.isMessage(function)) {
if (this.inspector.isMessage(function)) {
flux = messages(wrapper, function == null ? consumer : function, flux);
}
Mono<ResponseEntity<?>> responseEntityMono = null;
@@ -224,7 +231,7 @@ public class RequestProcessor {
private Mono<ResponseEntity<?>> stream(FunctionWrapper request, Publisher<?> result) {
BodyBuilder builder = ResponseEntity.ok();
if (inspector.isMessage(request.handler())) {
if (this.inspector.isMessage(request.handler())) {
result = Flux.from(result)
.doOnNext(value -> addHeaders(builder, (Message<?>) value))
.map(message -> MessageUtils.unpack(request.handler(), message)
@@ -242,7 +249,7 @@ public class RequestProcessor {
Publisher<?> result, Boolean single, boolean getter) {
BodyBuilder builder = ResponseEntity.ok();
if (inspector.isMessage(handler)) {
if (this.inspector.isMessage(handler)) {
result = Flux.from(result)
.map(message -> MessageUtils.unpack(handler, message))
.doOnNext(value -> addHeaders(builder, value))
@@ -267,8 +274,8 @@ public class RequestProcessor {
if (handler instanceof FluxWrapper) {
handler = ((FluxWrapper<?>) handler).getTarget();
}
Class<?> type = inspector.getInputType(handler);
Class<?> wrapper = inspector.getInputWrapper(handler);
Class<?> type = this.inspector.getInputType(handler);
Class<?> wrapper = this.inspector.getInputWrapper(handler);
return Collection.class.isAssignableFrom(type) || Flux.class.equals(wrapper);
}
@@ -276,8 +283,8 @@ public class RequestProcessor {
if (handler instanceof FluxWrapper) {
handler = ((FluxWrapper<?>) handler).getTarget();
}
Class<?> type = inspector.getOutputType(handler);
Class<?> wrapper = inspector.getOutputWrapper(handler);
Class<?> type = this.inspector.getOutputType(handler);
Class<?> wrapper = this.inspector.getOutputWrapper(handler);
if (Stream.class.isAssignableFrom(type)) {
return false;
}
@@ -294,8 +301,7 @@ public class RequestProcessor {
ResolvableType actualType = elementType;
Class<?> resolvedType = elementType.resolve();
ReactiveAdapter adapter = (resolvedType != null
? getAdapterRegistry().getAdapter(resolvedType)
: null);
? getAdapterRegistry().getAdapter(resolvedType) : null);
ServerHttpRequest request = exchange.getRequest();
ServerHttpResponse response = exchange.getResponse();
@@ -360,10 +366,8 @@ public class RequestProcessor {
}
private Throwable handleReadError(MethodParameter parameter, Throwable ex) {
return (ex instanceof DecodingException
? new ServerWebInputException("Failed to read HTTP message", parameter,
ex)
: ex);
return (ex instanceof DecodingException ? new ServerWebInputException(
"Failed to read HTTP message", parameter, ex) : ex);
}
private ServerWebInputException handleMissingBody(MethodParameter param) {
@@ -377,13 +381,14 @@ public class RequestProcessor {
private Publisher<?> value(Function<Publisher<?>, Publisher<?>> function,
Publisher<String> value) {
Flux<?> input = Flux.from(value).map(body -> converter.convert(function, body));
Flux<?> input = Flux.from(value)
.map(body -> this.converter.convert(function, body));
return Mono.from(function.apply(input));
}
private Object getTargetFunction(Object function) {
// we need to get the actual un-fluxed function so we can interrogate for types
Object target = inspector.getRegistration(function).getTarget();
Object target = this.inspector.getRegistration(function).getTarget();
if (target instanceof FluxWrapper) {
target = ((FluxWrapper<?>) target).getTarget();
}
@@ -391,12 +396,12 @@ public class RequestProcessor {
}
private Type getItemType(Object function) {
Class<?> inputType = inspector.getInputType(function);
Class<?> inputType = this.inspector.getInputType(function);
if (!Collection.class.isAssignableFrom(inputType)) {
return inputType;
}
Type type = inspector.getRegistration(this.getTargetFunction(function)).getType()
.getType();
Type type = this.inspector.getRegistration(this.getTargetFunction(function))
.getType().getType();
if (type instanceof ParameterizedType) {
type = ((ParameterizedType) type).getActualTypeArguments()[0];
}
@@ -417,6 +422,9 @@ public class RequestProcessor {
return type;
}
/**
* Wrapper for functions.
*/
public static class FunctionWrapper {
private final Function<Publisher<?>, Publisher<?>> function;
@@ -442,7 +450,8 @@ public class RequestProcessor {
}
public Object handler() {
return function != null ? function : consumer != null ? consumer : supplier;
return this.function != null ? this.function
: this.consumer != null ? this.consumer : this.supplier;
}
public Function<Publisher<?>, Publisher<?>> function() {
@@ -458,7 +467,7 @@ public class RequestProcessor {
}
public MultiValueMap<String, String> params() {
return params;
return this.params;
}
public HttpHeaders headers() {
@@ -488,5 +497,7 @@ public class RequestProcessor {
public Publisher<String> argument() {
return this.argument;
}
}
}

View File

@@ -1,11 +1,11 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
@@ -23,6 +23,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
/**
* @author Mark Fisher
*/
// @checkstyle:off
@SpringBootConfiguration
@EnableAutoConfiguration
public class RestApplication {
@@ -30,5 +31,6 @@ public class RestApplication {
public static void main(String[] args) {
SpringApplication.run(RestApplication.class, args);
}
}
}
// @checkstyle:on

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.web;
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,15 +23,33 @@ package org.springframework.cloud.function.web.constants;
*/
public abstract class WebRequestConstants {
/**
* Function attribute name.
*/
public static final String FUNCTION = WebRequestConstants.class.getName()
+ ".function";
/**
* Consumer attribute name.
*/
public static final String CONSUMER = WebRequestConstants.class.getName()
+ ".consumer";
/**
* Supplier attribute name.
*/
public static final String SUPPLIER = WebRequestConstants.class.getName()
+ ".supplier";
/**
* Argument attribute name.
*/
public static final String ARGUMENT = WebRequestConstants.class.getName()
+ ".argument";
public static final String HANDLER = WebRequestConstants.class.getName()
+ ".handler";
/**
* Handler attribute name.
*/
public static final String HANDLER = WebRequestConstants.class.getName() + ".handler";
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import org.springframework.cloud.function.web.RequestProcessor;
import org.springframework.cloud.function.web.RequestProcessor.FunctionWrapper;
@@ -38,8 +39,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
* @author Mark Fisher
@@ -58,7 +57,7 @@ public class FunctionController {
public Mono<ResponseEntity<?>> form(ServerWebExchange request) {
FunctionWrapper wrapper = wrapper(request);
return request.getFormData().doOnSuccess(params -> wrapper.params(params))
.then(Mono.defer(() -> processor.post(wrapper, null, false)));
.then(Mono.defer(() -> this.processor.post(wrapper, null, false)));
}
@PostMapping(path = "/**", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@@ -67,7 +66,7 @@ public class FunctionController {
FunctionWrapper wrapper = wrapper(request);
return request.getMultipartData()
.doOnSuccess(params -> wrapper.params(multi(params)))
.then(Mono.defer(() -> processor.post(wrapper, null, false)));
.then(Mono.defer(() -> this.processor.post(wrapper, null, false)));
}
private MultiValueMap<String, String> multi(MultiValueMap<String, Part> body) {
@@ -87,7 +86,7 @@ public class FunctionController {
@ResponseBody
public Mono<ResponseEntity<?>> post(ServerWebExchange request) {
FunctionWrapper wrapper = wrapper(request);
return processor.post(wrapper, request);
return this.processor.post(wrapper, request);
}
@PostMapping(path = "/**")
@@ -95,7 +94,7 @@ public class FunctionController {
public Mono<ResponseEntity<?>> post(ServerWebExchange request,
@RequestBody(required = false) String body) {
FunctionWrapper wrapper = wrapper(request);
return processor.post(wrapper, body, false);
return this.processor.post(wrapper, body, false);
}
@PostMapping(path = "/**", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@@ -103,21 +102,21 @@ public class FunctionController {
public Mono<ResponseEntity<?>> postStream(ServerWebExchange request,
@RequestBody(required = false) String body) {
FunctionWrapper wrapper = wrapper(request);
return processor.post(wrapper, body, true);
return this.processor.post(wrapper, body, true);
}
@GetMapping(path = "/**")
@ResponseBody
public Mono<ResponseEntity<?>> get(ServerWebExchange request) {
FunctionWrapper wrapper = wrapper(request);
return processor.get(wrapper);
return this.processor.get(wrapper);
}
@GetMapping(path = "/**", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public Mono<ResponseEntity<?>> getStream(ServerWebExchange request) {
FunctionWrapper wrapper = wrapper(request);
return processor.stream(wrapper);
return this.processor.stream(wrapper);
}
private FunctionWrapper wrapper(ServerWebExchange request) {
@@ -139,4 +138,5 @@ public class FunctionController {
}
return wrapper;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
@@ -35,8 +36,6 @@ import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.result.method.annotation.RequestMappingHandlerMapping;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
*
@@ -57,7 +56,7 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
public FunctionHandlerMapping(FunctionCatalog catalog,
FunctionController controller) {
this.functions = catalog;
logger.info("FunctionCatalog: " + catalog);
this.logger.info("FunctionCatalog: " + catalog);
setOrder(super.getOrder() - 5);
this.controller = controller;
}
@@ -65,9 +64,9 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
detectHandlerMethods(controller);
while (prefix.endsWith("/")) {
prefix = prefix.substring(0, prefix.length() - 1);
detectHandlerMethods(this.controller);
while (this.prefix.endsWith("/")) {
this.prefix = this.prefix.substring(0, this.prefix.length() - 1);
}
}
@@ -78,23 +77,23 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
@Override
public Mono<HandlerMethod> getHandlerInternal(ServerWebExchange request) {
String path = request.getRequest().getPath().pathWithinApplication().value();
if (StringUtils.hasText(prefix) && !path.startsWith(prefix)) {
if (StringUtils.hasText(this.prefix) && !path.startsWith(this.prefix)) {
return Mono.empty();
}
Mono<HandlerMethod> handler = super.getHandlerInternal(request);
if (path == null) {
return handler;
}
if (path.startsWith(prefix)) {
path = path.substring(prefix.length());
if (path.startsWith(this.prefix)) {
path = path.substring(this.prefix.length());
}
Object function = findFunctionForGet(request, path);
if (function == null) {
function = findFunctionForPost(request, path);
}
if (function != null) {
if (logger.isDebugEnabled()) {
logger.debug("Found function for POST: " + path);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Found function for POST: " + path);
}
request.getAttributes().put(WebRequestConstants.HANDLER, function);
}
@@ -107,12 +106,12 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
return null;
}
path = path.startsWith("/") ? path.substring(1) : path;
Consumer<Publisher<?>> consumer = functions.lookup(Consumer.class, path);
Consumer<Publisher<?>> consumer = this.functions.lookup(Consumer.class, path);
if (consumer != null) {
request.getAttributes().put(WebRequestConstants.CONSUMER, consumer);
return consumer;
}
Function<Object, Object> function = functions.lookup(Function.class, path);
Function<Object, Object> function = this.functions.lookup(Function.class, path);
if (function != null) {
request.getAttributes().put(WebRequestConstants.FUNCTION, function);
return function;
@@ -127,7 +126,7 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
path = path.startsWith("/") ? path.substring(1) : path;
Object functionForGet = null;
Supplier<Publisher<?>> supplier = functions.lookup(Supplier.class, path);
Supplier<Publisher<?>> supplier = this.functions.lookup(Supplier.class, path);
if (supplier != null) {
request.getAttributes().put(WebRequestConstants.SUPPLIER, supplier);
functionForGet = supplier;
@@ -135,7 +134,7 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
else {
StringBuilder builder = new StringBuilder();
String name = path;
String[] splitPath = path.split("/");
String[] splitPath = path.split("/");
Function<Object, Object> function = null;
for (int i = 0; i < splitPath.length || function != null; i++) {
String element = splitPath[i];
@@ -145,12 +144,11 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
builder.append(element);
name = builder.toString();
function = functions.lookup(Function.class, name);
function = this.functions.lookup(Function.class, name);
if (function != null) {
request.getAttributes().put(WebRequestConstants.FUNCTION, function);
String value = path.length() > name.length()
? path.substring(name.length() + 1)
: null;
? path.substring(name.length() + 1) : null;
request.getAttributes().put(WebRequestConstants.ARGUMENT, value);
functionForGet = function;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.cloud.function.web.flux;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -34,8 +36,6 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
* @author Mark Fisher
@@ -43,8 +43,8 @@ import reactor.core.publisher.Flux;
*/
@Configuration
@ConditionalOnClass({ Flux.class, AsyncHandlerMethodReturnValueHandler.class })
@ConditionalOnWebApplication(type=Type.REACTIVE)
@Import({FunctionController.class, RequestProcessor.class})
@ConditionalOnWebApplication(type = Type.REACTIVE)
@Import({ FunctionController.class, RequestProcessor.class })
@AutoConfigureAfter({ JacksonAutoConfiguration.class, GsonAutoConfiguration.class })
public class ReactorAutoConfiguration {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,10 @@ import java.util.function.Function;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.DisposableServer;
import reactor.netty.http.server.HttpServer;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
@@ -63,13 +67,7 @@ import static org.springframework.web.reactive.function.server.RequestPredicates
import static org.springframework.web.reactive.function.server.RouterFunctions.route;
import static org.springframework.web.reactive.function.server.ServerResponse.status;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.netty.DisposableServer;
import reactor.netty.http.server.HttpServer;
/**
*
* @author Dave Syer
* @since 2.0
*
@@ -148,7 +146,7 @@ class FunctionEndpointInitializer
private GenericApplicationContext context;
public ServerListener(GenericApplicationContext context) {
ServerListener(GenericApplicationContext context) {
this.context = context;
}
@@ -212,7 +210,7 @@ class FunctionEndpointFactory {
private RequestProcessor processor;
public FunctionEndpointFactory(FunctionCatalog catalog, FunctionInspector inspector,
FunctionEndpointFactory(FunctionCatalog catalog, FunctionInspector inspector,
RequestProcessor processor, Environment environment) {
String handler = environment.resolvePlaceholders("${function.handler}");
if (handler.startsWith("$")) {
@@ -242,13 +240,13 @@ class FunctionEndpointFactory {
public <T> RouterFunction<?> functionEndpoints() {
return route(POST("/"), request -> {
Class<T> outputType = (Class<T>) this.inspector.getOutputType(this.function);
FunctionWrapper wrapper = RequestProcessor.wrapper(function, null, null);
FunctionWrapper wrapper = RequestProcessor.wrapper(this.function, null, null);
Mono<ResponseEntity<?>> stream = request.bodyToMono(String.class)
.flatMap(content -> processor.post(wrapper, content, false));
.flatMap(content -> this.processor.post(wrapper, content, false));
return stream.flatMap(entity -> status(entity.getStatusCode())
.headers(headers -> headers.addAll(entity.getHeaders()))
.body(Mono.just((T) entity.getBody()), outputType));
});
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import java.util.function.Function;
import java.util.function.Supplier;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Mono;
import org.springframework.cloud.function.web.RequestProcessor;
import org.springframework.cloud.function.web.RequestProcessor.FunctionWrapper;
@@ -36,8 +37,6 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
* @author Mark Fisher
@@ -56,7 +55,7 @@ public class FunctionController {
@ResponseBody
public Mono<ResponseEntity<?>> form(WebRequest request) {
FunctionWrapper wrapper = wrapper(request);
return processor.post(wrapper, null, false);
return this.processor.post(wrapper, null, false);
}
@PostMapping(path = "/**")
@@ -64,7 +63,7 @@ public class FunctionController {
public Mono<ResponseEntity<?>> post(WebRequest request,
@RequestBody(required = false) String body) {
FunctionWrapper wrapper = wrapper(request);
return processor.post(wrapper, body, false);
return this.processor.post(wrapper, body, false);
}
@PostMapping(path = "/**", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@@ -72,22 +71,23 @@ public class FunctionController {
public Mono<ResponseEntity<Publisher<?>>> postStream(WebRequest request,
@RequestBody(required = false) String body) {
FunctionWrapper wrapper = wrapper(request);
return processor.post(wrapper, body, true).map(response -> ResponseEntity.ok()
.headers(response.getHeaders()).body((Publisher<?>) response.getBody()));
return this.processor.post(wrapper, body, true)
.map(response -> ResponseEntity.ok().headers(response.getHeaders())
.body((Publisher<?>) response.getBody()));
}
@GetMapping(path = "/**")
@ResponseBody
public Mono<ResponseEntity<?>> get(WebRequest request) {
FunctionWrapper wrapper = wrapper(request);
return processor.get(wrapper);
return this.processor.get(wrapper);
}
@GetMapping(path = "/**", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
@ResponseBody
public Mono<ResponseEntity<Publisher<?>>> getStream(WebRequest request) {
FunctionWrapper wrapper = wrapper(request);
return processor.stream(wrapper).map(response -> ResponseEntity.ok()
return this.processor.stream(wrapper).map(response -> ResponseEntity.ok()
.headers(response.getHeaders()).body((Publisher<?>) response.getBody()));
}
@@ -116,4 +116,5 @@ public class FunctionController {
}
return wrapper;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
public FunctionHandlerMapping(FunctionCatalog catalog,
FunctionController controller) {
this.functions = catalog;
logger.info("FunctionCatalog: " + catalog);
this.logger.info("FunctionCatalog: " + catalog);
setOrder(super.getOrder() - 5);
this.controller = controller;
}
@@ -64,9 +64,9 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
@Override
public void afterPropertiesSet() {
super.afterPropertiesSet();
detectHandlerMethods(controller);
while (prefix.endsWith("/")) {
prefix = prefix.substring(0, prefix.length() - 1);
detectHandlerMethods(this.controller);
while (this.prefix.endsWith("/")) {
this.prefix = this.prefix.substring(0, this.prefix.length() - 1);
}
}
@@ -86,24 +86,24 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
if (path == null) {
return handler;
}
if (StringUtils.hasText(prefix) && !path.startsWith(prefix)) {
if (StringUtils.hasText(this.prefix) && !path.startsWith(this.prefix)) {
return null;
}
if (path.startsWith(prefix)) {
path = path.substring(prefix.length());
if (path.startsWith(this.prefix)) {
path = path.substring(this.prefix.length());
}
Object function = findFunctionForGet(request, path);
if (function != null) {
if (logger.isDebugEnabled()) {
logger.debug("Found function for GET: " + path);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Found function for GET: " + path);
}
request.setAttribute(WebRequestConstants.HANDLER, function);
return handler;
}
function = findFunctionForPost(request, path);
if (function != null) {
if (logger.isDebugEnabled()) {
logger.debug("Found function for POST: " + path);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Found function for POST: " + path);
}
request.setAttribute(WebRequestConstants.HANDLER, function);
return handler;
@@ -116,12 +116,12 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
return null;
}
path = path.startsWith("/") ? path.substring(1) : path;
Consumer<Publisher<?>> consumer = functions.lookup(Consumer.class, path);
Consumer<Publisher<?>> consumer = this.functions.lookup(Consumer.class, path);
if (consumer != null) {
request.setAttribute(WebRequestConstants.CONSUMER, consumer);
return consumer;
}
Function<Object, Object> function = functions.lookup(Function.class, path);
Function<Object, Object> function = this.functions.lookup(Function.class, path);
if (function != null) {
request.setAttribute(WebRequestConstants.FUNCTION, function);
return function;
@@ -134,7 +134,7 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
return null;
}
path = path.startsWith("/") ? path.substring(1) : path;
Supplier<Publisher<?>> supplier = functions.lookup(Supplier.class, path);
Supplier<Publisher<?>> supplier = this.functions.lookup(Supplier.class, path);
if (supplier != null) {
request.setAttribute(WebRequestConstants.SUPPLIER, supplier);
return supplier;
@@ -150,7 +150,8 @@ public class FunctionHandlerMapping extends RequestMappingHandlerMapping
name = builder.toString();
value = path.length() > name.length() ? path.substring(name.length() + 1)
: null;
Function<Object, Object> function = functions.lookup(Function.class, name);
Function<Object, Object> function = this.functions.lookup(Function.class,
name);
if (function != null) {
request.setAttribute(WebRequestConstants.FUNCTION, function);
request.setAttribute(WebRequestConstants.ARGUMENT, value);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.cloud.function.web.mvc;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -34,15 +36,13 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
* @author Mark Fisher
* @author Oleg Zhurakousky
*/
@Configuration
@ConditionalOnWebApplication(type=Type.SERVLET)
@ConditionalOnWebApplication(type = Type.SERVLET)
@ConditionalOnClass({ Flux.class, AsyncHandlerMethodReturnValueHandler.class })
@Import({ FunctionController.class, RequestProcessor.class })
@AutoConfigureAfter({ JacksonAutoConfiguration.class, GsonAutoConfiguration.class })
@@ -60,4 +60,5 @@ public class ReactorAutoConfiguration {
ConfigurableListableBeanFactory beanFactory) {
return new BasicStringConverter(inspector, beanFactory);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,4 +25,5 @@ import java.util.function.Supplier;
public interface DestinationResolver {
String destination(Supplier<?> supplier, String name, Object value);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,9 +25,9 @@ import org.springframework.http.HttpHeaders;
*
*/
public interface RequestBuilder {
URI uri(String destination);
HttpHeaders headers(String destination, Object value);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,7 +31,9 @@ import org.springframework.http.HttpHeaders;
class SimpleRequestBuilder implements RequestBuilder {
private String baseUrl = "http://${destination}";
private Map<String, String> headers = new LinkedHashMap<>();
private Environment environment;
SimpleRequestBuilder(Environment environment) {
@@ -42,10 +44,10 @@ class SimpleRequestBuilder implements RequestBuilder {
public HttpHeaders headers(String destination, Object value) {
// TODO: add message headers if any
HttpHeaders result = new HttpHeaders();
for (String key : headers.keySet()) {
String header = headers.get(key);
for (String key : this.headers.keySet()) {
String header = this.headers.get(key);
header = header.replace("${destination}", destination);
header = environment.resolvePlaceholders(header);
header = this.environment.resolvePlaceholders(header);
result.add(key, header);
}
return result;
@@ -54,8 +56,8 @@ class SimpleRequestBuilder implements RequestBuilder {
@Override
public URI uri(String destination) {
try {
return new URI(environment
.resolvePlaceholders(baseUrl.replace("${destination}", destination)));
return new URI(this.environment.resolvePlaceholders(
this.baseUrl.replace("${destination}", destination)));
}
catch (URISyntaxException e) {
throw new IllegalStateException("Cannot create URI", e);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -68,16 +68,20 @@ class SupplierAutoConfiguration {
static class SourceActiveCondition extends AnyNestedCondition {
public SourceActiveCondition() {
SourceActiveCondition() {
super(ConfigurationPhase.PARSE_CONFIGURATION);
}
@ConditionalOnNotWebApplication
static class OnNotWebapp {
}
@ConditionalOnProperty(prefix = "spring.cloud.function.web.supplier", name = "enabled")
static class Enabled {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,11 @@ import java.util.Collections;
import java.util.Set;
import java.util.function.Supplier;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import org.springframework.cloud.function.context.FunctionCatalog;
import org.springframework.context.SmartLifecycle;
import org.springframework.http.HttpHeaders;
@@ -28,11 +33,6 @@ import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
/**
* Forwards items obtained from a {@link Supplier} or set of suppliers to an external HTTP
* endpoint.
@@ -62,7 +62,7 @@ class SupplierExporter implements SmartLifecycle {
private volatile Disposable subscription;
public SupplierExporter(RequestBuilder requestBuilder,
SupplierExporter(RequestBuilder requestBuilder,
DestinationResolver destinationResolver, FunctionCatalog catalog,
WebClient client, SupplierProperties props) {
this.requestBuilder = requestBuilder;
@@ -84,10 +84,10 @@ class SupplierExporter implements SmartLifecycle {
this.ok = true;
Flux<Object> streams = Flux.empty();
Set<String> names = this.supplier == null ? catalog.getNames(Supplier.class)
Set<String> names = this.supplier == null ? this.catalog.getNames(Supplier.class)
: Collections.singleton(this.supplier);
for (String name : names) {
Supplier<Flux<Object>> supplier = catalog.lookup(Supplier.class, name);
Supplier<Flux<Object>> supplier = this.catalog.lookup(Supplier.class, name);
streams = streams.mergeWith(forward(supplier, name));
}
@@ -105,13 +105,14 @@ class SupplierExporter implements SmartLifecycle {
private Flux<ClientResponse> forward(Supplier<Flux<Object>> supplier, String name) {
return supplier.get().publishOn(Schedulers.parallel()).flatMap(value -> {
String destination = destinationResolver.destination(supplier, name, value);
String destination = this.destinationResolver.destination(supplier, name,
value);
return post(uri(destination), destination, value);
});
}
private Mono<ClientResponse> post(URI uri, String destination, Object value) {
Mono<ClientResponse> result = client.post().uri(uri)
Mono<ClientResponse> result = this.client.post().uri(uri)
.headers(headers -> headers(headers, destination, value))
.body(BodyInserters.fromObject(value)).exchange();
if (this.debug) {
@@ -121,11 +122,11 @@ class SupplierExporter implements SmartLifecycle {
}
private void headers(HttpHeaders headers, String destination, Object value) {
headers.putAll(requestBuilder.headers(destination, value));
headers.putAll(this.requestBuilder.headers(destination, value));
}
private URI uri(String destination) {
return requestBuilder.uri(destination);
return this.requestBuilder.uri(destination);
}
public boolean isOk() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,10 +29,17 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
public class SupplierProperties {
private boolean autoStartup = true;
private boolean debug = true;
private String name;
private String templateUrl;
private boolean enabled;
private Map<String, String> headers = new LinkedHashMap<>();
public boolean isEnabled() {
return this.enabled;
}
@@ -41,8 +48,6 @@ public class SupplierProperties {
this.enabled = enabled;
}
private Map<String, String> headers = new LinkedHashMap<>();
public boolean isAutoStartup() {
return this.autoStartup;
}
@@ -59,23 +64,24 @@ public class SupplierProperties {
this.debug = debug;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
public String getTemplateUrl() {
return this.templateUrl;
}
public void setTemplateUrl(String templateUrl) {
this.templateUrl = templateUrl;
}
public String getTemplateUrl() {
return this.templateUrl;
}
public Map<String, String> getHeaders() {
return this.headers;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.function.web.util;
import java.util.Arrays;
@@ -28,12 +29,16 @@ import org.springframework.messaging.MessageHeaders;
* @author Dave Syer
* @author Oleg Zhurakousky
*/
public class HeaderUtils {
public final class HeaderUtils {
private static HttpHeaders IGNORED = new HttpHeaders();
private static HttpHeaders REQUEST_ONLY = new HttpHeaders();
private HeaderUtils() {
throw new IllegalStateException("Can't instantiate a utility class");
}
static {
IGNORED.add(MessageHeaders.ID, "");
IGNORED.add(HttpHeaders.CONTENT_LENGTH, "0");
@@ -86,4 +91,5 @@ public class HeaderUtils {
private static Collection<?> multi(Object value) {
return value instanceof Collection ? (Collection<?>) value : Arrays.asList(value);
}
}

View File

@@ -1,9 +1,11 @@
{"properties": [
{
"name": "spring.cloud.function.web.path",
"type": "java.lang.String",
"description": "Path to web resources for functions (should start with / if not empty).",
"defaultValue": ""
}]
{
"properties": [
{
"name": "spring.cloud.function.web.path",
"type": "java.lang.String",
"description": "Path to web resources for functions (should start with / if not empty).",
"defaultValue": ""
}
]
}

View File

@@ -2,10 +2,8 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.springframework.cloud.function.web.flux.ReactorAutoConfiguration,\
org.springframework.cloud.function.web.mvc.ReactorAutoConfiguration,\
org.springframework.cloud.function.web.source.SupplierAutoConfiguration
org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc=\
org.springframework.cloud.function.web.flux.ReactorAutoConfiguration,\
org.springframework.cloud.function.context.config.ContextFunctionCatalogAutoConfiguration
org.springframework.context.ApplicationContextInitializer=\
org.springframework.cloud.function.web.function.FunctionEndpointInitializer
org.springframework.cloud.function.web.function.FunctionEndpointInitializer

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -28,6 +28,8 @@ import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.reactivestreams.Publisher;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -52,13 +54,10 @@ import org.springframework.web.bind.annotation.RestController;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* Tests for vanilla MVC handling (no function layer). Validates the MVC customizations
* that are added in this project independently of the specific concerns of function.
*
*
* @author Dave Syer
*
*/
@@ -67,28 +66,31 @@ import reactor.core.publisher.Mono;
public class FluxRestApplicationTests {
private static final MediaType EVENT_STREAM = MediaType.valueOf("text/event-stream");
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Autowired
private TestConfiguration test;
@Before
public void init() {
test.list.clear();
this.test.list.clear();
}
@Test
public void wordsSSE() throws Exception {
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.get(new URI("/words")).accept(EVENT_STREAM).build(),
String.class).getBody()).isEqualTo(sse("foo", "bar"));
}
@Test
public void wordsJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/words"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -97,7 +99,7 @@ public class FluxRestApplicationTests {
@Test
@Ignore("Fix error handling")
public void errorJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/bang"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[\"foo\"]");
@@ -105,7 +107,7 @@ public class FluxRestApplicationTests {
@Test
public void words() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -113,7 +115,7 @@ public class FluxRestApplicationTests {
@Test
public void foos() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/foos")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody())
@@ -122,7 +124,7 @@ public class FluxRestApplicationTests {
@Test
public void getMore() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/get/more")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -131,44 +133,44 @@ public class FluxRestApplicationTests {
@Test
@Ignore("Should this even work? Or do we need to be explicit about the JSON?")
public void updates() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.post(new URI("/updates")).body("one\ntwo"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isEqualTo("onetwo");
}
@Test
public void updatesJson() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/updates")).contentType(MediaType.APPLICATION_JSON)
.body("[\"one\",\"two\"]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isEqualTo("[\"one\",\"two\"]");
}
@Test
public void addFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/addFoos")).contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody())
.isEqualTo("[{\"value\":\"foo\"},{\"value\":\"bar\"}]");
}
@Test
public void timeout() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/timeout")).build(), String.class)
.getBody()).isEqualTo("[\"foo\"]");
}
@Test
public void emptyJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/empty"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[]");
@@ -176,22 +178,23 @@ public class FluxRestApplicationTests {
@Test
public void sentences() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/sentences")).build(), String.class)
.getBody()).isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
}
@Test
public void sentencesAcceptAny() throws Exception {
assertThat(rest.exchange(
RequestEntity.get(new URI("/sentences")).accept(MediaType.ALL).build(),
String.class).getBody())
.isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
assertThat(
this.rest
.exchange(RequestEntity.get(new URI("/sentences"))
.accept(MediaType.ALL).build(), String.class)
.getBody()).isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
}
@Test
public void sentencesAcceptJson() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.get(new URI("/sentences"))
.accept(MediaType.APPLICATION_JSON).build(),
@@ -203,7 +206,7 @@ public class FluxRestApplicationTests {
@Test
public void uppercase() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/uppercase")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"[FOO]\",\"[BAR]\"]");
@@ -211,7 +214,7 @@ public class FluxRestApplicationTests {
@Test
public void uppercaseFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/upFoos")).contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
assertThat(result.getBody())
@@ -220,7 +223,7 @@ public class FluxRestApplicationTests {
@Test
public void transform() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/transform")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"[FOO]\",\"[BAR]\"]");
@@ -228,7 +231,7 @@ public class FluxRestApplicationTests {
@Test
public void postMore() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/post/more")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"[FOO]\",\"[BAR]\"]");
@@ -236,21 +239,21 @@ public class FluxRestApplicationTests {
@Test
public void uppercaseGet() throws Exception {
assertThat(rest.exchange(RequestEntity.get(new URI("/uppercase/foo"))
assertThat(this.rest.exchange(RequestEntity.get(new URI("/uppercase/foo"))
.accept(MediaType.TEXT_PLAIN).build(), String.class).getBody())
.isEqualTo("[FOO]");
}
@Test
public void convertGet() throws Exception {
assertThat(rest.exchange(RequestEntity.get(new URI("/wrap/123"))
assertThat(this.rest.exchange(RequestEntity.get(new URI("/wrap/123"))
.accept(MediaType.TEXT_PLAIN).build(), String.class).getBody())
.isEqualTo("..123..");
}
@Test
public void convertGetJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/entity/321"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("{\"value\":321}");
@@ -259,7 +262,7 @@ public class FluxRestApplicationTests {
@Test
public void uppercaseJsonStream() throws Exception {
assertThat(
rest.exchange(
this.rest.exchange(
RequestEntity.post(new URI("/maps"))
.contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"),
@@ -269,7 +272,7 @@ public class FluxRestApplicationTests {
@Test
public void uppercaseSSE() throws Exception {
assertThat(rest.exchange(RequestEntity.post(new URI("/uppercase"))
assertThat(this.rest.exchange(RequestEntity.post(new URI("/uppercase"))
.accept(EVENT_STREAM).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class).getBody())
.isEqualTo(sse("[FOO]", "[BAR]"));
@@ -277,9 +280,10 @@ public class FluxRestApplicationTests {
@Test
public void altSSE() throws Exception {
assertThat(rest.exchange(RequestEntity.post(new URI("/alt")).accept(EVENT_STREAM)
.contentType(MediaType.APPLICATION_JSON).body("[\"foo\",\"bar\"]"),
String.class).getBody()).isEqualTo(sse("[FOO]", "[BAR]"));
assertThat(this.rest.exchange(RequestEntity.post(new URI("/alt"))
.accept(EVENT_STREAM).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class).getBody())
.isEqualTo(sse("[FOO]", "[BAR]"));
}
private String sse(String... values) {
@@ -396,6 +400,7 @@ public class FluxRestApplicationTests {
}
public static class Foo {
private String value;
public Foo(String value) {
@@ -406,11 +411,13 @@ public class FluxRestApplicationTests {
}
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,6 +27,8 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -51,13 +53,10 @@ import org.springframework.web.bind.annotation.RestController;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* Tests for vanilla MVC handling (no function layer). Validates the MVC customizations
* that are added in this project independently of the specific concerns of function.
*
*
* @author Dave Syer
*
*/
@@ -66,28 +65,31 @@ import reactor.core.publisher.Mono;
public class MvcRestApplicationTests {
private static final MediaType EVENT_STREAM = MediaType.valueOf("text/event-stream");
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Autowired
private TestConfiguration test;
@Before
public void init() {
test.list.clear();
this.test.list.clear();
}
@Test
public void wordsSSE() throws Exception {
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.get(new URI("/words")).accept(EVENT_STREAM).build(),
String.class).getBody()).isEqualTo(sse("foo", "bar"));
}
@Test
public void wordsJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/words"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -96,7 +98,7 @@ public class MvcRestApplicationTests {
@Test
@Ignore("Fix error handling")
public void errorJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/bang"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[\"foo\"]");
@@ -104,7 +106,7 @@ public class MvcRestApplicationTests {
@Test
public void words() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -112,7 +114,7 @@ public class MvcRestApplicationTests {
@Test
public void foos() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/foos")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody())
@@ -121,7 +123,7 @@ public class MvcRestApplicationTests {
@Test
public void getMore() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/get/more")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -130,44 +132,44 @@ public class MvcRestApplicationTests {
@Test
@Ignore("Should this even work? Or do we need to be explicit about the JSON?")
public void updates() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.post(new URI("/updates")).body("one\ntwo"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isEqualTo("onetwo");
}
@Test
public void updatesJson() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/updates")).contentType(MediaType.APPLICATION_JSON)
.body("[\"one\",\"two\"]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isEqualTo("[\"one\",\"two\"]");
}
@Test
public void addFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/addFoos")).contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody())
.isEqualTo("[{\"value\":\"foo\"},{\"value\":\"bar\"}]");
}
@Test
public void timeout() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/timeout")).build(), String.class)
.getBody()).isEqualTo("[\"foo\"]");
}
@Test
public void emptyJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/empty"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[]");
@@ -175,22 +177,23 @@ public class MvcRestApplicationTests {
@Test
public void sentences() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/sentences")).build(), String.class)
.getBody()).isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
}
@Test
public void sentencesAcceptAny() throws Exception {
assertThat(rest.exchange(
RequestEntity.get(new URI("/sentences")).accept(MediaType.ALL).build(),
String.class).getBody())
.isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
assertThat(
this.rest
.exchange(RequestEntity.get(new URI("/sentences"))
.accept(MediaType.ALL).build(), String.class)
.getBody()).isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
}
@Test
public void sentencesAcceptJson() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.get(new URI("/sentences"))
.accept(MediaType.APPLICATION_JSON).build(),
@@ -202,7 +205,7 @@ public class MvcRestApplicationTests {
@Test
public void uppercase() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/uppercase")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"[FOO]\",\"[BAR]\"]");
@@ -210,7 +213,7 @@ public class MvcRestApplicationTests {
@Test
public void uppercaseFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/upFoos")).contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
assertThat(result.getBody())
@@ -219,7 +222,7 @@ public class MvcRestApplicationTests {
@Test
public void transform() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/transform")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"[FOO]\",\"[BAR]\"]");
@@ -227,7 +230,7 @@ public class MvcRestApplicationTests {
@Test
public void postMore() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/post/more")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"[FOO]\",\"[BAR]\"]");
@@ -235,17 +238,19 @@ public class MvcRestApplicationTests {
@Test
public void uppercaseGet() {
assertThat(rest.getForObject("/uppercase/foo", String.class)).isEqualTo("[FOO]");
assertThat(this.rest.getForObject("/uppercase/foo", String.class))
.isEqualTo("[FOO]");
}
@Test
public void convertGet() {
assertThat(rest.getForObject("/wrap/123", String.class)).isEqualTo("..123..");
assertThat(this.rest.getForObject("/wrap/123", String.class))
.isEqualTo("..123..");
}
@Test
public void convertGetJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/entity/321"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("{\"value\":321}");
@@ -254,7 +259,7 @@ public class MvcRestApplicationTests {
@Test
public void uppercaseJsonStream() throws Exception {
assertThat(
rest.exchange(
this.rest.exchange(
RequestEntity.post(new URI("/maps"))
.contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"),
@@ -264,7 +269,7 @@ public class MvcRestApplicationTests {
@Test
public void uppercaseSSE() throws Exception {
assertThat(rest.exchange(RequestEntity.post(new URI("/uppercase"))
assertThat(this.rest.exchange(RequestEntity.post(new URI("/uppercase"))
.accept(EVENT_STREAM).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class).getBody())
.isEqualTo(sse("[FOO]", "[BAR]"));
@@ -374,6 +379,7 @@ public class MvcRestApplicationTests {
}
public static class Foo {
private String value;
public Foo(String value) {
@@ -384,11 +390,13 @@ public class MvcRestApplicationTests {
}
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import java.util.function.Function;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@@ -39,8 +40,6 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
@@ -51,20 +50,22 @@ public class ComponentTests {
@LocalServerPort
private int port;
@Autowired
private Greeter greeter;
@Autowired
private TestRestTemplate rest;
@Test
public void contextLoads() throws Exception {
assertThat(greeter).isNotNull();
assertThat(this.greeter).isNotNull();
}
@Test
@Ignore("FIXME")
public void greeter() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.post(new URI("/greeter"))
.contentType(MediaType.TEXT_PLAIN).body("World"),
@@ -76,14 +77,17 @@ public class ComponentTests {
@SpringBootApplication
@ComponentScan
protected static class TestConfiguration {
}
@Component("greeter")
protected static class Greeter implements Function<Flux<String>, Flux<String>> {
@Override
public Flux<String> apply(Flux<String> flux) {
return flux.map(name -> "Hello " + name);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.util.function.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
@@ -30,8 +31,6 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
*
@@ -48,16 +47,19 @@ public class ExplicitNonFunctionalTests {
@Test
public void words() throws Exception {
client.post().uri("/").body(Mono.just("foo"), String.class).exchange()
this.client.post().uri("/").body(Mono.just("foo"), String.class).exchange()
.expectStatus().isOk().expectBody(String.class).isEqualTo("FOO");
}
@SpringBootConfiguration
@EnableAutoConfiguration
protected static class TestConfiguration implements Function<String, String> {
@Override
public String apply(String value) {
return value.toUpperCase();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.util.function.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
@@ -28,14 +29,12 @@ import org.springframework.cloud.function.context.test.FunctionalSpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
*
*/
@RunWith(SpringRunner.class)
//Only need web-application-type because MVC is on the classpath
// Only need web-application-type because MVC is on the classpath
@FunctionalSpringBootTest("spring.main.web-application-type=reactive")
@AutoConfigureWebTestClient
public class FunctionalTests {
@@ -45,15 +44,18 @@ public class FunctionalTests {
@Test
public void words() throws Exception {
client.post().uri("/").body(Mono.just("foo"), String.class).exchange()
this.client.post().uri("/").body(Mono.just("foo"), String.class).exchange()
.expectStatus().isOk().expectBody(String.class).isEqualTo("FOO");
}
@SpringBootConfiguration
protected static class TestConfiguration implements Function<String, String> {
@Override
public String apply(String value) {
return value.toUpperCase();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
@@ -30,8 +31,6 @@ import org.springframework.cloud.function.context.test.FunctionalSpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
*
@@ -46,18 +45,22 @@ public class FunctionalWithInputListTests {
@Test
public void words() throws Exception {
client.post().uri("/").body(Mono.just("[{\"value\":\"foo\"}, {\"value\":\"bar\"}]"), String.class)
this.client.post().uri("/")
.body(Mono.just("[{\"value\":\"foo\"}, {\"value\":\"bar\"}]"),
String.class)
.exchange().expectStatus().isOk().expectBody(String.class)
.isEqualTo("{\"value\":\"FOOBAR\"}");
}
@SpringBootConfiguration
protected static class TestConfiguration implements Function<List<Foo>, Foo> {
@Override
public Foo apply(List<Foo> value) {
return new Foo(value.stream().map(foo -> foo.getValue().toUpperCase())
.collect(Collectors.joining()));
}
}
public static class Foo {
@@ -85,4 +88,5 @@ public class FunctionalWithInputListTests {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,14 +16,13 @@
package org.springframework.cloud.function.test;
import static org.junit.Assert.assertTrue;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
@@ -32,7 +31,7 @@ import org.springframework.cloud.function.context.test.FunctionalSpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
@@ -48,8 +47,11 @@ public class FunctionalWithInputSetTests {
@Test
public void words() throws Exception {
String reply = client.post().uri("/").body(Mono.just("[{\"value\":\"foo\"}, {\"value\":\"bar\"}]"), String.class)
.exchange().expectStatus().isOk().expectBody(String.class).returnResult().getResponseBody();
String reply = this.client.post().uri("/")
.body(Mono.just("[{\"value\":\"foo\"}, {\"value\":\"bar\"}]"),
String.class)
.exchange().expectStatus().isOk().expectBody(String.class).returnResult()
.getResponseBody();
assertTrue(reply.contains("FOO"));
assertTrue(reply.contains("BAR"));
assertTrue(reply.contains("{\"value\":\""));
@@ -57,11 +59,13 @@ public class FunctionalWithInputSetTests {
@SpringBootConfiguration
protected static class TestConfiguration implements Function<Set<Foo>, Foo> {
@Override
public Foo apply(Set<Foo> value) {
return new Foo(value.stream().map(foo -> foo.getValue().toUpperCase())
.collect(Collectors.joining()));
}
}
public static class Foo {
@@ -89,4 +93,5 @@ public class FunctionalWithInputSetTests {
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ import java.util.function.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
@@ -29,10 +30,7 @@ import org.springframework.messaging.support.MessageBuilder;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
/**
*
* @author Oleg Zhurakousky
*
*/
@@ -47,7 +45,7 @@ public class HeadersToMessageTests {
@Test
public void testBodyAndCustomHeaderFromMessagePropagation() throws Exception {
client.post().uri("/").body(Mono.just("foo"), String.class).exchange()
this.client.post().uri("/").body(Mono.just("foo"), String.class).exchange()
.expectStatus().isOk().expectHeader()
.valueEquals("x-content-type", "application/xml").expectHeader()
.valueEquals("foo", "bar").expectBody(String.class).isEqualTo("FOO");
@@ -56,11 +54,15 @@ public class HeadersToMessageTests {
@SpringBootConfiguration
protected static class TestConfiguration
implements Function<Message<String>, Message<String>> {
public Message<String> apply(Message<String> request) {
Message<String> message = MessageBuilder.withPayload(request.getPayload().toUpperCase())
Message<String> message = MessageBuilder
.withPayload(request.getPayload().toUpperCase())
.setHeader("X-Content-Type", "application/xml")
.setHeader("foo", "bar").build();
return message;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.util.function.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
@@ -31,8 +32,6 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
*
@@ -48,16 +47,19 @@ public class ImplicitNonFunctionalTests {
@Test
public void words() throws Exception {
client.post().uri("/").body(Mono.just("foo"), String.class).exchange()
this.client.post().uri("/").body(Mono.just("foo"), String.class).exchange()
.expectStatus().isOk().expectBody(String.class).isEqualTo("FOO");
}
@SpringBootConfiguration
@EnableAutoConfiguration
protected static class TestConfiguration {
@Bean
public Function<String, String> uppercase() {
return value -> value.toUpperCase();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,8 @@ import java.util.function.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -30,8 +32,6 @@ import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
/**
* @author Oleg Zhurakousky
*
@@ -39,7 +39,7 @@ import reactor.core.publisher.Mono;
@RunWith(SpringRunner.class)
@SpringBootTest({ "spring.main.web-application-type=REACTIVE",
"spring.functional.enabled=false",
"spring.cloud.function.definition=uppercase|reverse"})
"spring.cloud.function.definition=uppercase|reverse" })
@AutoConfigureWebTestClient
@DirtiesContext
public class MoreThenOneFunctionRootMappingTests {
@@ -49,13 +49,14 @@ public class MoreThenOneFunctionRootMappingTests {
@Test
public void words() throws Exception {
client.post().uri("/").body(Mono.just("star"), String.class).exchange()
this.client.post().uri("/").body(Mono.just("star"), String.class).exchange()
.expectStatus().isOk().expectBody(String.class).isEqualTo("RATS");
}
@SpringBootConfiguration
@EnableAutoConfiguration
protected static class TestConfiguration {
@Bean
public Function<String, String> uppercase() {
return v -> v.toUpperCase();
@@ -65,5 +66,7 @@ public class MoreThenOneFunctionRootMappingTests {
public Function<String, String> reverse() {
return v -> new StringBuilder(v).reverse().toString();
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@ import java.util.function.Function;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
@@ -28,14 +29,12 @@ import org.springframework.cloud.function.context.test.FunctionalSpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
*
*/
@RunWith(SpringRunner.class)
//Only need web-application-type because MVC is on the classpath
// Only need web-application-type because MVC is on the classpath
@FunctionalSpringBootTest("spring.main.web-application-type=reactive")
@AutoConfigureWebTestClient
public class PojoTests {
@@ -45,23 +44,30 @@ public class PojoTests {
@Test
public void single() throws Exception {
client.post().uri("/").body(Mono.just("{\"value\":\"foo\"}"), String.class).exchange()
.expectStatus().isOk().expectBody(String.class).isEqualTo("{\"value\":\"FOO\"}");
this.client.post().uri("/").body(Mono.just("{\"value\":\"foo\"}"), String.class)
.exchange().expectStatus().isOk().expectBody(String.class)
.isEqualTo("{\"value\":\"FOO\"}");
}
@Test
public void multiple() throws Exception {
client.post().uri("/").body(Mono.just("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class).exchange()
.expectStatus().isOk().expectBody(String.class).isEqualTo("[{\"value\":\"FOO\"},{\"value\":\"BAR\"}]");
this.client.post().uri("/")
.body(Mono.just("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"),
String.class)
.exchange().expectStatus().isOk().expectBody(String.class)
.isEqualTo("[{\"value\":\"FOO\"},{\"value\":\"BAR\"}]");
}
@SpringBootConfiguration
protected static class TestConfiguration implements Function<Foo, Foo> {
@Override
public Foo apply(Foo value) {
return new Foo(value.getValue().toUpperCase());
}
}
}
class Foo {
@@ -88,4 +94,4 @@ class Foo {
return "Foo [value=" + this.value + "]";
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018-2019 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,7 +41,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
*
* @author Dave Syer
* @author Oleg Zhurakousky
*
@@ -50,7 +49,7 @@ import static org.junit.Assert.assertTrue;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"spring.cloud.function.web.path=/functions",
"spring.main.web-application-type=reactive" })
@ContextConfiguration(classes= {RestApplication.class, TestConfiguration.class})
@ContextConfiguration(classes = { RestApplication.class, TestConfiguration.class })
public class HeadersToMessageTests {
@Autowired
@@ -59,9 +58,10 @@ public class HeadersToMessageTests {
@Test
public void testBodyAndCustomHeaderFromMessagePropagation() throws Exception {
// test POJO paylod
ResponseEntity<String> postForEntity = rest.exchange(RequestEntity
.post(new URI("/functions/employee")).contentType(MediaType.APPLICATION_JSON)
.body("{\"name\":\"Bob\",\"age\":25}"), String.class);
ResponseEntity<String> postForEntity = this.rest
.exchange(RequestEntity.post(new URI("/functions/employee"))
.contentType(MediaType.APPLICATION_JSON)
.body("{\"name\":\"Bob\",\"age\":25}"), String.class);
assertEquals("{\"name\":\"Bob\",\"age\":25}", postForEntity.getBody());
assertTrue(postForEntity.getHeaders().containsKey("x-content-type"));
assertEquals("application/xml",
@@ -69,9 +69,8 @@ public class HeadersToMessageTests {
assertEquals("bar", postForEntity.getHeaders().get("foo").get(0));
// test simple type payload
postForEntity = rest.postForEntity(
new URI("/functions/string"), "{\"name\":\"Bob\",\"age\":25}",
String.class);
postForEntity = this.rest.postForEntity(new URI("/functions/string"),
"{\"name\":\"Bob\",\"age\":25}", String.class);
assertEquals("{\"name\":\"Bob\",\"age\":25}", postForEntity.getBody());
assertTrue(postForEntity.getHeaders().containsKey("x-content-type"));
assertEquals("application/xml",
@@ -82,6 +81,7 @@ public class HeadersToMessageTests {
@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
protected static class TestConfiguration {
@Bean({ "string" })
public Function<Message<String>, Message<String>> functiono() {
return request -> {
@@ -95,29 +95,39 @@ public class HeadersToMessageTests {
@Bean({ "employee" })
public Function<Message<Employee>, Message<Employee>> function1() {
return request -> {
Message<Employee> message = MessageBuilder.withPayload(request.getPayload())
Message<Employee> message = MessageBuilder
.withPayload(request.getPayload())
.setHeader("X-Content-Type", "application/xml")
.setHeader("foo", "bar").build();
return message;
};
}
}
@SuppressWarnings("unused") // used by json converter
private static class Employee {
private String name;
private int age;
public String getName() {
return name;
return this.name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
return this.age;
}
public void setAge(int age) {
this.age = age;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
@@ -53,44 +54,46 @@ import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.main.web-application-type=reactive")
@ContextConfiguration(classes= {RestApplication.class, ApplicationConfiguration.class})
@ContextConfiguration(classes = { RestApplication.class, ApplicationConfiguration.class })
public class HttpGetIntegrationTests {
private static final MediaType EVENT_STREAM = MediaType.TEXT_EVENT_STREAM;
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Autowired
private ApplicationConfiguration test;
@Before
public void init() {
test.list.clear();
this.test.list.clear();
}
@Test
public void staticResource() {
assertThat(rest.getForObject("/test.html", String.class)).contains("<body>Test");
assertThat(this.rest.getForObject("/test.html", String.class))
.contains("<body>Test");
}
@Test
public void wordsSSE() throws Exception {
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.get(new URI("/words")).accept(EVENT_STREAM).build(),
String.class).getBody()).isEqualTo(sse("foo", "bar"));
}
@Test
public void wordsJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/words"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -99,7 +102,7 @@ public class HttpGetIntegrationTests {
@Test
@Ignore("Fix error handling")
public void errorJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/bang"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[\"foo\"]");
@@ -107,7 +110,7 @@ public class HttpGetIntegrationTests {
@Test
public void words() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -115,7 +118,7 @@ public class HttpGetIntegrationTests {
@Test
public void word() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.get(new URI("/word")).accept(MediaType.TEXT_PLAIN).build(),
String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
@@ -124,7 +127,7 @@ public class HttpGetIntegrationTests {
@Test
public void foos() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/foos")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody())
@@ -133,7 +136,7 @@ public class HttpGetIntegrationTests {
@Test
public void getMore() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/get/more")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -141,7 +144,7 @@ public class HttpGetIntegrationTests {
@Test
public void bareWords() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/bareWords")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -149,7 +152,7 @@ public class HttpGetIntegrationTests {
@Test
public void timeoutJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/timeout"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[\"foo\"]");
@@ -157,7 +160,7 @@ public class HttpGetIntegrationTests {
@Test
public void emptyJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/empty"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[]");
@@ -165,22 +168,23 @@ public class HttpGetIntegrationTests {
@Test
public void sentences() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/sentences")).build(), String.class)
.getBody()).isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
}
@Test
public void sentencesAcceptAny() throws Exception {
assertThat(rest.exchange(
RequestEntity.get(new URI("/sentences")).accept(MediaType.ALL).build(),
String.class).getBody())
.isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
assertThat(
this.rest
.exchange(RequestEntity.get(new URI("/sentences"))
.accept(MediaType.ALL).build(), String.class)
.getBody()).isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
}
@Test
public void sentencesAcceptJson() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.get(new URI("/sentences"))
.accept(MediaType.APPLICATION_JSON).build(),
@@ -192,7 +196,7 @@ public class HttpGetIntegrationTests {
@Test
public void sentencesAcceptSse() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.get(new URI("/sentences")).accept(EVENT_STREAM).build(),
String.class);
assertThat(result.getBody())
@@ -203,7 +207,7 @@ public class HttpGetIntegrationTests {
@Test
public void postMoreFoo() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.get(new URI("/post/more/foo")).accept(MediaType.TEXT_PLAIN).build(),
String.class);
assertThat(result.getBody()).isEqualTo("(FOO)");
@@ -211,7 +215,7 @@ public class HttpGetIntegrationTests {
@Test
public void uppercaseGet() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.get(new URI("/uppercase/foo")).accept(MediaType.TEXT_PLAIN).build(),
String.class);
assertThat(result.getBody()).isEqualTo("(FOO)");
@@ -219,7 +223,7 @@ public class HttpGetIntegrationTests {
@Test
public void convertGet() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.get(new URI("/wrap/123")).accept(MediaType.TEXT_PLAIN).build(),
String.class);
assertThat(result.getBody()).isEqualTo("..123..");
@@ -227,13 +231,13 @@ public class HttpGetIntegrationTests {
@Test
public void supplierFirst() {
assertThat(rest.getForObject("/not/a/function", String.class))
assertThat(this.rest.getForObject("/not/a/function", String.class))
.isEqualTo("[\"hello\"]");
}
@Test
public void convertGetJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/entity/321"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("{\"value\":321}");
@@ -339,6 +343,7 @@ public class HttpGetIntegrationTests {
}
public static class Foo {
private String value;
public Foo(String value) {
@@ -349,12 +354,13 @@ public class HttpGetIntegrationTests {
}
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -57,9 +59,6 @@ import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
*/
@@ -69,23 +68,26 @@ import reactor.core.publisher.Mono;
public class HttpPostIntegrationTests {
private static final MediaType EVENT_STREAM = MediaType.TEXT_EVENT_STREAM;
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Autowired
private ApplicationConfiguration test;
@Before
public void init() {
test.list.clear();
this.test.list.clear();
}
@Test
public void qualifierFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity.post(new URI("/foos"))
.contentType(MediaType.APPLICATION_JSON).body("[\"foo\",\"bar\"]"),
String.class);
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/foos")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody())
.isEqualTo("[{\"value\":\"[FOO]\"},{\"value\":\"[BAR]\"}]");
@@ -93,47 +95,47 @@ public class HttpPostIntegrationTests {
@Test
public void updates() throws Exception {
ResponseEntity<String> result = rest.exchange(
RequestEntity.post(new URI("/updates")).contentType(MediaType.APPLICATION_JSON).body("[\"one\", \"two\"]"),
String.class);
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/updates")).contentType(MediaType.APPLICATION_JSON)
.body("[\"one\", \"two\"]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isNull();
}
@Test
public void updatesJson() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/updates")).contentType(MediaType.APPLICATION_JSON)
.body("[\"one\",\"two\"]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isEqualTo(null);
}
@Test
public void addFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/addFoos")).contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isEqualTo(null);
}
@Test
public void bareUpdates() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/bareUpdates")).contentType(MediaType.APPLICATION_JSON)
.body("[\"one\",\"two\"]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isEqualTo("[]");
}
@Test
public void uppercase() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/uppercase")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
@@ -141,7 +143,7 @@ public class HttpPostIntegrationTests {
@Test
public void messages() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/messages")).contentType(MediaType.APPLICATION_JSON)
.header("x-foo", "bar").body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getHeaders().getFirst("x-foo")).isEqualTo("bar");
@@ -151,7 +153,7 @@ public class HttpPostIntegrationTests {
@Test
public void headers() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/headers")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getHeaders().getFirst("foo")).isEqualTo("bar");
@@ -161,7 +163,7 @@ public class HttpPostIntegrationTests {
@Test
public void uppercaseSingleValue() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.post(new URI("/uppercase"))
.contentType(MediaType.TEXT_PLAIN).body("foo"),
@@ -172,7 +174,7 @@ public class HttpPostIntegrationTests {
@Test
@Ignore("WebFlux would split the request body into lines: TODO make this work the same")
public void uppercasePlainText() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.post(new URI("/uppercase"))
.contentType(MediaType.TEXT_PLAIN).body("foo\nbar"),
String.class);
@@ -181,7 +183,7 @@ public class HttpPostIntegrationTests {
@Test
public void uppercaseFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/upFoos")).contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
assertThat(result.getBody())
@@ -191,7 +193,7 @@ public class HttpPostIntegrationTests {
@Test
public void uppercaseFoo() throws Exception {
// Single Foo can be parsed
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/upFoos")).contentType(MediaType.APPLICATION_JSON)
.body("{\"value\":\"foo\"}"), String.class);
assertThat(result.getBody()).isEqualTo("[{\"value\":\"FOO\"}]");
@@ -199,7 +201,7 @@ public class HttpPostIntegrationTests {
@Test
public void bareUppercaseFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/bareUpFoos")).contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
assertThat(result.getBody())
@@ -208,18 +210,18 @@ public class HttpPostIntegrationTests {
@Test
public void typelessFunctionPassingArray() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
.post(new URI("/typelessFunctionExpectingText")).contentType(MediaType.TEXT_PLAIN)
.body("[{\"value\":\"foo\"}]"), String.class);
assertThat(result.getBody())
.isEqualTo("[{\"value\":\"foo\"}]");
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.post(new URI("/typelessFunctionExpectingText"))
.contentType(MediaType.TEXT_PLAIN).body("[{\"value\":\"foo\"}]"),
String.class);
assertThat(result.getBody()).isEqualTo("[{\"value\":\"foo\"}]");
}
@Test
public void bareUppercaseFoo() throws Exception {
// Single Foo can be parsed and returns a single value if the function is defined
// that way
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/bareUpFoos")).contentType(MediaType.APPLICATION_JSON)
.body("{\"value\":\"foo\"}"), String.class);
assertThat(result.getBody()).isEqualTo("{\"value\":\"FOO\"}");
@@ -227,7 +229,7 @@ public class HttpPostIntegrationTests {
@Test
public void bareUppercase() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/bareUppercase")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
@@ -235,7 +237,7 @@ public class HttpPostIntegrationTests {
@Test
public void singleValuedText() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.post(new URI("/bareUppercase")).accept(MediaType.TEXT_PLAIN)
.contentType(MediaType.TEXT_PLAIN).body("foo"),
String.class);
@@ -244,7 +246,7 @@ public class HttpPostIntegrationTests {
@Test
public void transform() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/transform")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
@@ -252,7 +254,7 @@ public class HttpPostIntegrationTests {
@Test
public void postMore() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/post/more")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
@@ -260,8 +262,11 @@ public class HttpPostIntegrationTests {
@Test
public void convertPost() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity.post(new URI("/wrap"))
.contentType(MediaType.TEXT_PLAIN).body("123"), String.class);
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.post(new URI("/wrap"))
.contentType(MediaType.TEXT_PLAIN).body("123"),
String.class);
// Result is multi-valued so it has to come out as an array
assertThat(result.getBody()).isEqualTo("[\"..123..\"]");
}
@@ -270,7 +275,7 @@ public class HttpPostIntegrationTests {
public void convertPostJson() throws Exception {
// If you POST a single value to a Function<Flux<Integer>,Flux<Integer>> it can't
// determine if the output is single valued, so it has to send an array back
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.post(new URI("/doubler"))
.contentType(MediaType.TEXT_PLAIN).body("123"),
@@ -280,7 +285,7 @@ public class HttpPostIntegrationTests {
@Test
public void uppercaseJsonArray() throws Exception {
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.post(new URI("/maps"))
.contentType(MediaType.APPLICATION_JSON)
// The new line in the middle is optional
@@ -291,7 +296,7 @@ public class HttpPostIntegrationTests {
@Test
public void uppercaseSSE() throws Exception {
assertThat(rest.exchange(RequestEntity.post(new URI("/uppercase"))
assertThat(this.rest.exchange(RequestEntity.post(new URI("/uppercase"))
.accept(EVENT_STREAM).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class).getBody())
.isEqualTo(sse("(FOO)", "(BAR)"));
@@ -305,7 +310,7 @@ public class HttpPostIntegrationTests {
map.put("A", Arrays.asList("1", "2", "3"));
map.put("B", Arrays.asList("5", "6"));
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.post(new URI("/sum")).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_FORM_URLENCODED).body(map),
String.class).getBody()).isEqualTo("[{\"A\":6,\"B\":11}]");
@@ -319,7 +324,7 @@ public class HttpPostIntegrationTests {
map.put("A", Arrays.asList("1", "2", "3"));
map.put("B", Arrays.asList("5", "6"));
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.post(new URI("/sum")).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.MULTIPART_FORM_DATA).body(map),
String.class).getBody()).isEqualTo("[{\"A\":6,\"B\":11}]");
@@ -328,7 +333,7 @@ public class HttpPostIntegrationTests {
@Test
public void count() throws Exception {
List<String> list = Arrays.asList("A", "B", "A");
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.post(new URI("/count")).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON).body(list),
String.class).getBody()).isEqualTo("{\"A\":2,\"B\":1}");
@@ -390,14 +395,14 @@ public class HttpPostIntegrationTests {
return value -> {
Assert.isInstanceOf(String.class, value);
return value;
};
};
}
// @Bean
// public Function<byte[],?> byteArrayInputFunction() {
//// return value -> new Foo(value.getValue().trim().toUpperCase());
// throw new UnsupportedOperationException("boom?");
// }
// @Bean
// public Function<byte[],?> byteArrayInputFunction() {
//// return value -> new Foo(value.getValue().trim().toUpperCase());
// throw new UnsupportedOperationException("boom?");
// }
@Bean
public Function<Flux<Integer>, Flux<String>> wrap() {
@@ -422,22 +427,22 @@ public class HttpPostIntegrationTests {
public Function<String, Foo> qualifier() {
return value -> {
return new Foo("[" + value.trim().toUpperCase() + "]");
};
};
}
@Bean
public Consumer<Flux<String>> updates() {
return flux -> flux.subscribe(value -> list.add(value));
return flux -> flux.subscribe(value -> this.list.add(value));
}
@Bean
public Consumer<Flux<Foo>> addFoos() {
return flux -> flux.subscribe(value -> list.add(value.getValue()));
return flux -> flux.subscribe(value -> this.list.add(value.getValue()));
}
@Bean
public Consumer<String> bareUpdates() {
return value -> list.add(value);
return value -> this.list.add(value);
}
@Bean("not/a")
@@ -457,9 +462,11 @@ public class HttpPostIntegrationTests {
return flux -> flux.collect(HashMap::new,
(map, word) -> map.merge(word, 1, Integer::sum));
}
}
public static class Foo {
private String value;
public Foo(String value) {
@@ -470,12 +477,13 @@ public class HttpPostIntegrationTests {
}
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.function.Supplier;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -39,8 +40,6 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
@@ -49,17 +48,18 @@ import reactor.core.publisher.Flux;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"spring.main.web-application-type=reactive",
"spring.cloud.function.web.path=/functions", "debug" })
@ContextConfiguration(classes= {RestApplication.class, TestConfiguration.class})
@ContextConfiguration(classes = { RestApplication.class, TestConfiguration.class })
public class PrefixTests {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Test
public void words() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.get(new URI("/functions/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -67,7 +67,7 @@ public class PrefixTests {
@Test
public void missing() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
@@ -75,9 +75,12 @@ public class PrefixTests {
@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
protected static class TestConfiguration {
@Bean({ "words", "get/more" })
public Supplier<Flux<String>> words() {
return () -> Flux.fromArray(new String[] { "foo", "bar" });
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.function.Supplier;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
@@ -44,25 +45,24 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.main.web-application-type=reactive")
@ContextConfiguration(classes= {RestApplication.class, TestConfiguration.class})
@ContextConfiguration(classes = { RestApplication.class, TestConfiguration.class })
public class SingletonTests {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Test
public void words() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -71,6 +71,7 @@ public class SingletonTests {
@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
protected static class TestConfiguration {
@Bean
public static BeanDefinitionRegistryPostProcessor processor() {
return new BeanDefinitionRegistryPostProcessor() {
@@ -91,12 +92,16 @@ public class SingletonTests {
}
};
}
}
static class MySupplier implements Supplier<Flux<String>> {
@Override
public Flux<String> get() {
return Flux.just("foo", "bar");
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@ import java.util.function.Function;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -40,26 +41,25 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "")
@ContextConfiguration(classes= {RestApplication.class, TestConfiguration.class})
@ContextConfiguration(classes = { RestApplication.class, TestConfiguration.class })
public class DefaultRouteTests {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Test
@Ignore("FIXME")
public void explicit() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.post(new URI("/uppercase")).body("foo"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("FOO");
@@ -68,8 +68,8 @@ public class DefaultRouteTests {
@Test
@Ignore("FIXME")
public void implicit() throws Exception {
ResponseEntity<String> result = rest.exchange(
RequestEntity.post(new URI("/")).body("foo"), String.class);
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.post(new URI("/")).body("foo"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("FOO");
}
@@ -77,9 +77,12 @@ public class DefaultRouteTests {
@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
protected static class TestConfiguration {
@Bean
public Function<Flux<String>, Flux<String>> uppercase() {
return flux -> flux.map(value -> value.toUpperCase());
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,7 +43,6 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
*
* @author Oleg Zhurakousky
*
*/
@@ -59,9 +58,10 @@ public class HeadersToMessageTests {
@Test
public void testBodyAndCustomHeaderFromMessagePropagation() throws Exception {
HttpEntity<String> postForEntity = rest.exchange(RequestEntity
.post(new URI("/functions/employee")).contentType(MediaType.APPLICATION_JSON)
.body("{\"name\":\"Bob\",\"age\":25}"), String.class);
HttpEntity<String> postForEntity = this.rest
.exchange(RequestEntity.post(new URI("/functions/employee"))
.contentType(MediaType.APPLICATION_JSON)
.body("{\"name\":\"Bob\",\"age\":25}"), String.class);
assertEquals("{\"name\":\"Bob\",\"age\":25}", postForEntity.getBody());
assertTrue(postForEntity.getHeaders().containsKey("x-content-type"));
assertEquals("application/xml",
@@ -71,9 +71,11 @@ public class HeadersToMessageTests {
@Test
public void testHeadersPropagatedByDefault() throws Exception {
HttpEntity<String> postForEntity = rest.exchange(RequestEntity
.post(new URI("/functions/vanilla")).contentType(MediaType.APPLICATION_JSON).header("x-context-type", "rubbish")
.body("{\"name\":\"Bob\",\"age\":25}"), String.class);
HttpEntity<String> postForEntity = this.rest
.exchange(RequestEntity.post(new URI("/functions/vanilla"))
.contentType(MediaType.APPLICATION_JSON)
.header("x-context-type", "rubbish")
.body("{\"name\":\"Bob\",\"age\":25}"), String.class);
assertEquals("{\"name\":\"Bob\",\"age\":25,\"foo\":\"bar\"}",
postForEntity.getBody());
assertTrue(postForEntity.getHeaders().containsKey("x-context-type"));
@@ -83,6 +85,7 @@ public class HeadersToMessageTests {
@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
protected static class TestConfiguration {
@Bean({ "employee" })
public Function<Message<Map<String, Object>>, Message<Map<String, Object>>> function() {
return request -> {
@@ -102,5 +105,7 @@ public class HeadersToMessageTests {
return message;
};
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,6 +30,7 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
@@ -53,44 +54,46 @@ import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties="spring.main.web-application-type=servlet")
@ContextConfiguration(classes= {RestApplication.class, ApplicationConfiguration.class})
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.main.web-application-type=servlet")
@ContextConfiguration(classes = { RestApplication.class, ApplicationConfiguration.class })
public class HttpGetIntegrationTests {
private static final MediaType EVENT_STREAM = MediaType.TEXT_EVENT_STREAM;
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Autowired
private ApplicationConfiguration test;
@Before
public void init() {
test.list.clear();
this.test.list.clear();
}
@Test
public void staticResource() {
assertThat(rest.getForObject("/test.html", String.class)).contains("<body>Test");
assertThat(this.rest.getForObject("/test.html", String.class))
.contains("<body>Test");
}
@Test
public void wordsSSE() throws Exception {
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.get(new URI("/words")).accept(EVENT_STREAM).build(),
String.class).getBody()).isEqualTo(sse("foo", "bar"));
}
@Test
public void wordsJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/words"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -99,7 +102,7 @@ public class HttpGetIntegrationTests {
@Test
@Ignore("Fix error handling")
public void errorJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/bang"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[\"foo\"]");
@@ -107,7 +110,7 @@ public class HttpGetIntegrationTests {
@Test
public void words() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -115,7 +118,7 @@ public class HttpGetIntegrationTests {
@Test
public void word() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/word")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("foo");
@@ -123,7 +126,7 @@ public class HttpGetIntegrationTests {
@Test
public void foos() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/foos")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody())
@@ -132,7 +135,7 @@ public class HttpGetIntegrationTests {
@Test
public void getMore() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/get/more")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -140,7 +143,7 @@ public class HttpGetIntegrationTests {
@Test
public void bareWords() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/bareWords")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -148,7 +151,7 @@ public class HttpGetIntegrationTests {
@Test
public void timeoutJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/timeout"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[\"foo\"]");
@@ -156,7 +159,7 @@ public class HttpGetIntegrationTests {
@Test
public void emptyJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/empty"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("[]");
@@ -164,22 +167,23 @@ public class HttpGetIntegrationTests {
@Test
public void sentences() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/sentences")).build(), String.class)
.getBody()).isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
}
@Test
public void sentencesAcceptAny() throws Exception {
assertThat(rest.exchange(
RequestEntity.get(new URI("/sentences")).accept(MediaType.ALL).build(),
String.class).getBody())
.isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
assertThat(
this.rest
.exchange(RequestEntity.get(new URI("/sentences"))
.accept(MediaType.ALL).build(), String.class)
.getBody()).isEqualTo("[[\"go\",\"home\"],[\"come\",\"back\"]]");
}
@Test
public void sentencesAcceptJson() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.get(new URI("/sentences"))
.accept(MediaType.APPLICATION_JSON).build(),
@@ -191,7 +195,7 @@ public class HttpGetIntegrationTests {
@Test
public void sentencesAcceptSse() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.get(new URI("/sentences")).accept(EVENT_STREAM).build(),
String.class);
assertThat(result.getBody())
@@ -202,28 +206,31 @@ public class HttpGetIntegrationTests {
@Test
public void postMoreFoo() {
assertThat(rest.getForObject("/post/more/foo", String.class)).isEqualTo("(FOO)");
assertThat(this.rest.getForObject("/post/more/foo", String.class))
.isEqualTo("(FOO)");
}
@Test
public void uppercaseGet() {
assertThat(rest.getForObject("/uppercase/foo", String.class)).isEqualTo("(FOO)");
assertThat(this.rest.getForObject("/uppercase/foo", String.class))
.isEqualTo("(FOO)");
}
@Test
public void convertGet() {
assertThat(rest.getForObject("/wrap/123", String.class)).isEqualTo("..123..");
assertThat(this.rest.getForObject("/wrap/123", String.class))
.isEqualTo("..123..");
}
@Test
public void supplierFirst() {
assertThat(rest.getForObject("/not/a/function", String.class))
assertThat(this.rest.getForObject("/not/a/function", String.class))
.isEqualTo("[\"hello\"]");
}
@Test
public void convertGetJson() throws Exception {
assertThat(rest
assertThat(this.rest
.exchange(RequestEntity.get(new URI("/entity/321"))
.accept(MediaType.APPLICATION_JSON).build(), String.class)
.getBody()).isEqualTo("{\"value\":321}");
@@ -329,6 +336,7 @@ public class HttpGetIntegrationTests {
}
public static class Foo {
private String value;
public Foo(String value) {
@@ -339,12 +347,13 @@ public class HttpGetIntegrationTests {
}
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -29,6 +29,8 @@ import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -56,34 +58,35 @@ import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
/**
* @author Dave Syer
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties="spring.main.web-application-type=servlet")
@ContextConfiguration(classes= {RestApplication.class, ApplicationConfiguration.class})
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.main.web-application-type=servlet")
@ContextConfiguration(classes = { RestApplication.class, ApplicationConfiguration.class })
public class HttpPostIntegrationTests {
private static final MediaType EVENT_STREAM = MediaType.TEXT_EVENT_STREAM;
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Autowired
private ApplicationConfiguration test;
@Before
public void init() {
test.list.clear();
this.test.list.clear();
}
@Test
public void qualifierFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity.post(new URI("/foos"))
.contentType(MediaType.APPLICATION_JSON).body("[\"foo\",\"bar\"]"),
String.class);
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/foos")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody())
.isEqualTo("[{\"value\":\"[FOO]\"},{\"value\":\"[BAR]\"}]");
@@ -91,48 +94,47 @@ public class HttpPostIntegrationTests {
@Test
public void updates() throws Exception {
ResponseEntity<String> result = rest.exchange(
RequestEntity.post(new URI("/updates")).contentType(MediaType.APPLICATION_JSON).body("[\"one\", \"two\"]"),
String.class);
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/updates")).contentType(MediaType.APPLICATION_JSON)
.body("[\"one\", \"two\"]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isNull();
}
@Test
public void updatesJson() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/updates")).contentType(MediaType.APPLICATION_JSON)
.body("[\"one\",\"two\"]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isEqualTo(null);
}
@Test
public void addFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/addFoos")).contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.ACCEPTED);
assertThat(test.list).hasSize(2);
assertThat(result.getBody())
.isEqualTo(null);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isEqualTo(null);
}
@Test
public void bareUpdates() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/bareUpdates")).contentType(MediaType.APPLICATION_JSON)
.body("[\"one\",\"two\"]"), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(test.list).hasSize(2);
assertThat(this.test.list).hasSize(2);
assertThat(result.getBody()).isEqualTo("[]");
}
@Test
public void uppercase() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/uppercase")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
@@ -140,7 +142,7 @@ public class HttpPostIntegrationTests {
@Test
public void messages() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/messages")).contentType(MediaType.APPLICATION_JSON)
// Remove this when Spring 5.0.8 is used
.accept(MediaType.valueOf("application/stream+json"))
@@ -152,7 +154,7 @@ public class HttpPostIntegrationTests {
@Test
public void headers() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/headers")).contentType(MediaType.APPLICATION_JSON)
// Remove this when Spring 5.0.8 is used
.accept(MediaType.valueOf("application/stream+json"))
@@ -164,7 +166,7 @@ public class HttpPostIntegrationTests {
@Test
public void uppercaseSingleValue() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.post(new URI("/uppercase"))
.contentType(MediaType.TEXT_PLAIN).body("foo"),
@@ -176,7 +178,7 @@ public class HttpPostIntegrationTests {
@Test
@Ignore("WebFlux would split the request body into lines: TODO make this work the same")
public void uppercasePlainText() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.post(new URI("/uppercase"))
.contentType(MediaType.TEXT_PLAIN).body("foo\nbar"),
String.class);
@@ -185,7 +187,7 @@ public class HttpPostIntegrationTests {
@Test
public void uppercaseFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/upFoos")).contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
assertThat(result.getBody())
@@ -195,7 +197,7 @@ public class HttpPostIntegrationTests {
@Test
public void uppercaseFoo() throws Exception {
// Single Foo can be parsed
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/upFoos")).contentType(MediaType.APPLICATION_JSON)
.body("{\"value\":\"foo\"}"), String.class);
assertThat(result.getBody()).isEqualTo("[{\"value\":\"FOO\"}]");
@@ -203,7 +205,7 @@ public class HttpPostIntegrationTests {
@Test
public void bareUppercaseFoos() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/bareUpFoos")).contentType(MediaType.APPLICATION_JSON)
.body("[{\"value\":\"foo\"},{\"value\":\"bar\"}]"), String.class);
assertThat(result.getBody())
@@ -214,7 +216,7 @@ public class HttpPostIntegrationTests {
public void bareUppercaseFoo() throws Exception {
// Single Foo can be parsed and returns a single value if the function is defined
// that way
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/bareUpFoos")).contentType(MediaType.APPLICATION_JSON)
.body("{\"value\":\"foo\"}"), String.class);
assertThat(result.getBody()).isEqualTo("{\"value\":\"FOO\"}");
@@ -222,7 +224,7 @@ public class HttpPostIntegrationTests {
@Test
public void bareUppercase() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/bareUppercase")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
@@ -230,7 +232,7 @@ public class HttpPostIntegrationTests {
@Test
public void singleValuedText() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.post(new URI("/bareUppercase"))
.contentType(MediaType.TEXT_PLAIN).body("foo"),
@@ -240,7 +242,7 @@ public class HttpPostIntegrationTests {
@Test
public void transform() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/transform")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
@@ -248,7 +250,7 @@ public class HttpPostIntegrationTests {
@Test
public void postMore() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity
ResponseEntity<String> result = this.rest.exchange(RequestEntity
.post(new URI("/post/more")).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class);
assertThat(result.getBody()).isEqualTo("[\"(FOO)\",\"(BAR)\"]");
@@ -256,8 +258,11 @@ public class HttpPostIntegrationTests {
@Test
public void convertPost() throws Exception {
ResponseEntity<String> result = rest.exchange(RequestEntity.post(new URI("/wrap"))
.contentType(MediaType.TEXT_PLAIN).body("123"), String.class);
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.post(new URI("/wrap"))
.contentType(MediaType.TEXT_PLAIN).body("123"),
String.class);
// Result is multi-valued so it has to come out as an array
assertThat(result.getBody()).isEqualTo("[\"..123..\"]");
}
@@ -266,7 +271,7 @@ public class HttpPostIntegrationTests {
public void convertPostJson() throws Exception {
// If you POST a single value to a Function<Flux<Integer>,Flux<Integer>> it can't
// determine if the output is single valued, so it has to send an array back
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(
RequestEntity.post(new URI("/doubler"))
.contentType(MediaType.TEXT_PLAIN).body("123"),
@@ -276,7 +281,7 @@ public class HttpPostIntegrationTests {
@Test
public void uppercaseJsonArray() throws Exception {
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.post(new URI("/maps"))
.contentType(MediaType.APPLICATION_JSON)
// The new line in the middle is optional
@@ -287,7 +292,7 @@ public class HttpPostIntegrationTests {
@Test
public void uppercaseSSE() throws Exception {
assertThat(rest.exchange(RequestEntity.post(new URI("/uppercase"))
assertThat(this.rest.exchange(RequestEntity.post(new URI("/uppercase"))
.accept(EVENT_STREAM).contentType(MediaType.APPLICATION_JSON)
.body("[\"foo\",\"bar\"]"), String.class).getBody())
.isEqualTo(sse("(FOO)", "(BAR)"));
@@ -301,7 +306,7 @@ public class HttpPostIntegrationTests {
map.put("A", Arrays.asList("1", "2", "3"));
map.put("B", Arrays.asList("5", "6"));
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.post(new URI("/sum")).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.MULTIPART_FORM_DATA).body(map),
String.class).getBody()).isEqualTo("[{\"A\":6,\"B\":11}]");
@@ -315,7 +320,7 @@ public class HttpPostIntegrationTests {
map.put("A", Arrays.asList("1", "2", "3"));
map.put("B", Arrays.asList("5", "6"));
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.post(new URI("/sum")).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.MULTIPART_FORM_DATA).body(map),
String.class).getBody()).isEqualTo("[{\"A\":6,\"B\":11}]");
@@ -324,7 +329,7 @@ public class HttpPostIntegrationTests {
@Test
public void count() throws Exception {
List<String> list = Arrays.asList("A", "B", "A");
assertThat(rest.exchange(
assertThat(this.rest.exchange(
RequestEntity.post(new URI("/count")).accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON).body(list),
String.class).getBody()).isEqualTo("{\"A\":2,\"B\":1}");
@@ -407,17 +412,17 @@ public class HttpPostIntegrationTests {
@Bean
public Consumer<Flux<String>> updates() {
return flux -> flux.subscribe(value -> list.add(value));
return flux -> flux.subscribe(value -> this.list.add(value));
}
@Bean
public Consumer<Flux<Foo>> addFoos() {
return flux -> flux.subscribe(value -> list.add(value.getValue()));
return flux -> flux.subscribe(value -> this.list.add(value.getValue()));
}
@Bean
public Consumer<String> bareUpdates() {
return value -> list.add(value);
return value -> this.list.add(value);
}
@Bean("not/a")
@@ -437,9 +442,11 @@ public class HttpPostIntegrationTests {
return flux -> flux.collect(HashMap::new,
(map, word) -> map.merge(word, 1, Integer::sum));
}
}
public static class Foo {
private String value;
public Foo(String value) {
@@ -450,12 +457,13 @@ public class HttpPostIntegrationTests {
}
public String getValue() {
return value;
return this.value;
}
public void setValue(String value) {
this.value = value;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.function.Supplier;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -39,8 +40,6 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
@@ -49,17 +48,18 @@ import reactor.core.publisher.Flux;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = {
"spring.main.web-application-type=servlet",
"spring.cloud.function.web.path=/functions" })
@ContextConfiguration(classes= {RestApplication.class, TestConfiguration.class})
@ContextConfiguration(classes = { RestApplication.class, TestConfiguration.class })
public class PrefixTests {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Test
public void words() throws Exception {
ResponseEntity<String> result = rest.exchange(
ResponseEntity<String> result = this.rest.exchange(
RequestEntity.get(new URI("/functions/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
@@ -67,7 +67,7 @@ public class PrefixTests {
@Test
public void missing() throws Exception {
ResponseEntity<String> result = rest
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
}
@@ -75,9 +75,12 @@ public class PrefixTests {
@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
protected static class TestConfiguration {
@Bean({ "words", "get/more" })
public Supplier<Flux<String>> words() {
return () -> Flux.fromArray(new String[] { "foo", "bar" });
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2015 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import java.util.function.Supplier;
import org.junit.Test;
import org.junit.runner.RunWith;
import reactor.core.publisher.Flux;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
@@ -44,26 +45,25 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat;
import reactor.core.publisher.Flux;
/**
* @author Dave Syer
*
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes= {RestApplication.class, TestConfiguration.class})
@ContextConfiguration(classes = { RestApplication.class, TestConfiguration.class })
public class SingletonTests {
@LocalServerPort
private int port;
@Autowired
private TestRestTemplate rest;
@Test
public void words() throws Exception {
ResponseEntity<String> result = rest.exchange(
RequestEntity.get(new URI("/words")).build(), String.class);
ResponseEntity<String> result = this.rest
.exchange(RequestEntity.get(new URI("/words")).build(), String.class);
assertThat(result.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(result.getBody()).isEqualTo("[\"foo\",\"bar\"]");
}
@@ -71,6 +71,7 @@ public class SingletonTests {
@EnableAutoConfiguration
@org.springframework.boot.test.context.TestConfiguration
protected static class TestConfiguration {
@Bean
public static BeanDefinitionRegistryPostProcessor processor() {
return new BeanDefinitionRegistryPostProcessor() {
@@ -85,17 +86,22 @@ public class SingletonTests {
public void postProcessBeanDefinitionRegistry(
BeanDefinitionRegistry registry) throws BeansException {
// Simulates what happens when you add a compiled function
RootBeanDefinition beanDefinition = new RootBeanDefinition(MySupplier.class);
RootBeanDefinition beanDefinition = new RootBeanDefinition(
MySupplier.class);
registry.registerBeanDefinition("words", beanDefinition);
}
};
}
}
static class MySupplier implements Supplier<Flux<String>> {
@Override
public Flux<String> get() {
return Flux.just("foo", "bar");
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -49,21 +49,24 @@ public class SourceAutoConfigurationIntegrationTests {
@Test
public void fails() throws Exception {
int count = 0;
while(forwarder.isRunning() && count++<100) {
while (this.forwarder.isRunning() && count++ < 100) {
Thread.sleep(20);
}
// It completed
assertThat(forwarder.isRunning()).isFalse();
assertThat(this.forwarder.isRunning()).isFalse();
// But failed
assertThat(forwarder.isOk()).isFalse();
assertThat(this.forwarder.isOk()).isFalse();
}
@EnableAutoConfiguration
@TestConfiguration
public static class ApplicationConfiguration {
@Bean
public Supplier<String> word() {
return () -> "foo";
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -54,12 +54,12 @@ import static org.assertj.core.api.Assertions.assertThat;
// in a webapp we have to explicitly enable the export
"spring.cloud.function.web.supplier.enabled=true",
// manually so we know the webapp is listening when we start
"spring.cloud.function.web.supplier.autoStartup=false"})
"spring.cloud.function.web.supplier.autoStartup=false" })
@ContextConfiguration(classes = { RestApplication.class, ApplicationConfiguration.class })
public class WebAppIntegrationTests {
private static Log logger = LogFactory.getLog(WebAppIntegrationTests.class);
@Autowired
private SupplierExporter forwarder;
@@ -68,16 +68,18 @@ public class WebAppIntegrationTests {
@Test
public void posts() throws Exception {
forwarder.start();
app.latch.await(10, TimeUnit.SECONDS);
assertThat(app.values).hasSize(1);
this.forwarder.start();
this.app.latch.await(10, TimeUnit.SECONDS);
assertThat(this.app.values).hasSize(1);
}
@EnableAutoConfiguration
@TestConfiguration
@RestController
public static class ApplicationConfiguration {
private List<String> values = new ArrayList<>();
private CountDownLatch latch = new CountDownLatch(1);
@Bean
@@ -89,9 +91,11 @@ public class WebAppIntegrationTests {
@PostMapping("/values")
public String value(@RequestBody String body) {
logger.info("Body: " + body);
values.add(body);
latch.countDown();
this.values.add(body);
this.latch.countDown();
return "ok";
}
}
}

View File

@@ -1 +1,3 @@
<html><body>Test</body></html>
<html>
<body>Test</body>
</html>