From 031355790faf00c8f800f71ba7b599a5dab8634c Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Mon, 25 Sep 2017 17:23:02 -0400 Subject: [PATCH] Remove rx package and classes. These things are now much better dealt with in Spring MVC and WebFlux --- .../netflix/rx/DeferredResultSubscriber.java | 68 ------- .../netflix/rx/ObservableSseEmitter.java | 45 ----- .../rx/ResponseBodyEmitterSubscriber.java | 90 --------- .../netflix/rx/RxJavaAutoConfiguration.java | 64 ------- .../cloud/netflix/rx/RxResponse.java | 58 ------ .../netflix/rx/SingleDeferredResult.java | 50 ----- .../netflix/rx/SingleReturnValueHandler.java | 129 ------------- .../main/resources/META-INF/spring.factories | 1 - .../rx/ObservableReturnValueHandlerTests.java | 171 ------------------ .../netflix/rx/ObservableSseEmitterTests.java | 168 ----------------- .../netflix/rx/SingleDeferredResultTests.java | 73 -------- .../rx/SingleReturnValueHandlerTests.java | 139 -------------- 12 files changed, 1056 deletions(-) delete mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/DeferredResultSubscriber.java delete mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/ObservableSseEmitter.java delete mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/ResponseBodyEmitterSubscriber.java delete mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/RxJavaAutoConfiguration.java delete mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/RxResponse.java delete mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleDeferredResult.java delete mode 100644 spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandler.java delete mode 100644 spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/ObservableReturnValueHandlerTests.java delete mode 100644 spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/ObservableSseEmitterTests.java delete mode 100644 spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleDeferredResultTests.java delete mode 100644 spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandlerTests.java diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/DeferredResultSubscriber.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/DeferredResultSubscriber.java deleted file mode 100644 index e84e6742..00000000 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/DeferredResultSubscriber.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright 2013-2016 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.netflix.rx; - -import org.springframework.web.context.request.async.DeferredResult; - -import rx.Observable; -import rx.Subscriber; -import rx.Subscription; - -/** - * A subscriber that sets the single value produced by the {@link Observable} on the {@link DeferredResult}. - * - * @author Jakub Narloch - * @see DeferredResult - */ -class DeferredResultSubscriber extends Subscriber implements Runnable { - - private final DeferredResult deferredResult; - - private final Subscription subscription; - - private boolean completed; - - public DeferredResultSubscriber(Observable observable, DeferredResult deferredResult) { - - this.deferredResult = deferredResult; - this.deferredResult.onTimeout(this); - this.deferredResult.onCompletion(this); - this.subscription = observable.subscribe(this); - } - - @Override - public void onNext(T value) { - if (!completed) { - deferredResult.setResult(value); - } - } - - @Override - public void onError(Throwable e) { - deferredResult.setErrorResult(e); - } - - @Override - public void onCompleted() { - completed = true; - } - - @Override - public void run() { - this.subscription.unsubscribe(); - } -} diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/ObservableSseEmitter.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/ObservableSseEmitter.java deleted file mode 100644 index a267c54c..00000000 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/ObservableSseEmitter.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright 2013-2016 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.netflix.rx; - -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; - -import rx.Observable; - -/** - * A specialized {@link SseEmitter} that handles {@link Observable} return types. - * - * @author Jakub Narloch - * @see SseEmitter - */ -class ObservableSseEmitter extends SseEmitter { - - public ObservableSseEmitter(Observable observable) { - this(null, observable); - } - - public ObservableSseEmitter(MediaType mediaType, Observable observable) { - this(null, mediaType, observable); - } - - public ObservableSseEmitter(Long timeout, MediaType mediaType, - Observable observable) { - super(timeout); - new ResponseBodyEmitterSubscriber<>(mediaType, observable, this); - } -} diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/ResponseBodyEmitterSubscriber.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/ResponseBodyEmitterSubscriber.java deleted file mode 100644 index d00eb8c9..00000000 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/ResponseBodyEmitterSubscriber.java +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright 2013-2016 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.netflix.rx; - -import java.io.IOException; - -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyEmitter; - -import rx.Observable; -import rx.Subscriber; -import rx.Subscription; - - -/** - * Subscriber that emits any value produced by the {@link Observable} into the delegated {@link ResponseBodyEmitter}. - * - * @author Jakub Narloch - */ -class ResponseBodyEmitterSubscriber extends Subscriber implements Runnable { - - private final MediaType mediaType; - - private final Subscription subscription; - - private final ResponseBodyEmitter responseBodyEmitter; - - private boolean completed; - - /** - * Creates new instance of {@link ResponseBodyEmitterSubscriber} with response media type, observable and response - * emitter. - * - * @param mediaType the marshaled object media type - * @param observable the observable - * @param responseBodyEmitter the response emitter - */ - public ResponseBodyEmitterSubscriber(MediaType mediaType, Observable observable, ResponseBodyEmitter responseBodyEmitter) { - - this.mediaType = mediaType; - this.responseBodyEmitter = responseBodyEmitter; - this.responseBodyEmitter.onTimeout(this); - this.responseBodyEmitter.onCompletion(this); - this.subscription = observable.subscribe(this); - } - - @Override - public void onNext(T value) { - - try { - if(!completed) { - responseBodyEmitter.send(value, mediaType); - } - } catch (IOException e) { - throw new RuntimeException(e.getMessage(), e); - } - } - - @Override - public void onError(Throwable e) { - responseBodyEmitter.completeWithError(e); - } - - @Override - public void onCompleted() { - if(!completed) { - completed = true; - responseBodyEmitter.complete(); - } - } - - @Override - public void run() { - subscription.unsubscribe(); - } -} diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/RxJavaAutoConfiguration.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/RxJavaAutoConfiguration.java deleted file mode 100644 index 2391a0bf..00000000 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/RxJavaAutoConfiguration.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright 2013-2016 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.netflix.rx; - -import java.util.List; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; -import org.springframework.cloud.client.actuator.HasFeatures; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler; -import org.springframework.web.method.support.HandlerMethodReturnValueHandler; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; - -import rx.Observable; -import rx.Single; - -/** - * @author Spencer Gibb - */ -@Configuration -@ConditionalOnWebApplication -@ConditionalOnClass(Observable.class) -public class RxJavaAutoConfiguration { - - @Configuration - @ConditionalOnClass({ AsyncHandlerMethodReturnValueHandler.class, WebMvcConfigurerAdapter.class }) - protected static class RxJavaReturnValueHandlerConfig { - @Bean - public SingleReturnValueHandler singleReturnValueHandler() { - return new SingleReturnValueHandler(); - } - - @Bean - public WebMvcConfigurerAdapter observableMVCConfiguration(final SingleReturnValueHandler singleReturnValueHandler) { - return new WebMvcConfigurerAdapter() { - @Override - public void addReturnValueHandlers(List returnValueHandlers) { - returnValueHandlers.add(singleReturnValueHandler); - } - }; - } - - @Bean - public HasFeatures rxFeature() { - return HasFeatures.namedFeatures("MVC Observable", Observable.class, "MVC Single", Single.class); - } - } -} diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/RxResponse.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/RxResponse.java deleted file mode 100644 index 48a966a3..00000000 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/RxResponse.java +++ /dev/null @@ -1,58 +0,0 @@ -package org.springframework.cloud.netflix.rx; - -import org.springframework.http.MediaType; -import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; - -import rx.Observable; - -/** - * A convenient class allowing to wrap either the {@link Observable} into a response supported by the - * Spring MVC. - * - * @author Jakub Narloch - */ -public final class RxResponse { - - private RxResponse() { - - } - - /** - * Wraps the {@link Observable} into a {@link SseEmitter}. Every value produced by the observable will be emitted - * as server side event. - * - * @param observable the observable instance - * @param the result type - * @return the sse emitter - */ - public static SseEmitter sse(Observable observable) { - return new ObservableSseEmitter<>(observable); - } - - /** - * Wraps the {@link Observable} into a {@link SseEmitter}. Every value produced by the observable will be emitted - * as server side event. - * - * @param mediaType the media type of produced entry - * @param observable the observable instance - * @param the result type - * @return the sse emitter - */ - public static SseEmitter sse(MediaType mediaType, Observable observable) { - return new ObservableSseEmitter<>(mediaType, observable); - } - - /** - * Wraps the {@link Observable} into a {@link SseEmitter}. Every value produced by the observable will be emitted - * as server side event. - * - * @param timeout the response timeout - * @param mediaType the media type of produced entry - * @param observable the observable instance - * @param the result type - * @return the sse emitter - */ - public static SseEmitter sse(long timeout, MediaType mediaType, Observable observable) { - return new ObservableSseEmitter<>(timeout, mediaType, observable); - } -} diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleDeferredResult.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleDeferredResult.java deleted file mode 100644 index 6a538252..00000000 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleDeferredResult.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright 2013-2016 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.netflix.rx; - -import org.springframework.util.Assert; -import org.springframework.web.context.request.async.DeferredResult; - -import rx.Single; - -/** - * A specialized {@link DeferredResult} that handles {@link Single} return type. - * - * @author Jakub Narloch - * @see DeferredResult - */ -class SingleDeferredResult extends DeferredResult { - - public SingleDeferredResult(Single single) { - initSingle(single); - } - - public SingleDeferredResult(long timeout, Single single) { - super(timeout); - initSingle(single); - } - - public SingleDeferredResult(Long timeout, Object timeoutResult, Single single) { - super(timeout, timeoutResult); - initSingle(single); - } - - private void initSingle(Single single) { - Assert.notNull(single, "single can not be null"); - new DeferredResultSubscriber<>(single.toObservable(), this); - } -} diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandler.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandler.java deleted file mode 100644 index fd214162..00000000 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandler.java +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright 2013-2016 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.netflix.rx; - -import org.springframework.core.MethodParameter; -import org.springframework.core.ResolvableType; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.context.request.NativeWebRequest; -import org.springframework.web.context.request.async.DeferredResult; -import org.springframework.web.context.request.async.WebAsyncUtils; -import org.springframework.web.method.support.AsyncHandlerMethodReturnValueHandler; -import org.springframework.web.method.support.ModelAndViewContainer; - -import rx.Single; -import rx.functions.Func1; - -/** - * A specialized {@link AsyncHandlerMethodReturnValueHandler} that handles {@link Single} - * return types. - * - * @author Spencer Gibb - * @author Jakub Narloch - */ -public class SingleReturnValueHandler implements AsyncHandlerMethodReturnValueHandler { - - @Override - public boolean isAsyncReturnValue(Object returnValue, MethodParameter returnType) { - return returnValue != null && supportsReturnType(returnType); - } - - @Override - public boolean supportsReturnType(MethodParameter returnType) { - return Single.class.isAssignableFrom(returnType.getParameterType()) - || isResponseEntity(returnType); - } - - private boolean isResponseEntity(MethodParameter returnType) { - if (ResponseEntity.class.isAssignableFrom(returnType.getParameterType())) { - Class bodyType = ResolvableType.forMethodParameter(returnType) - .getGeneric(0).resolve(); - return bodyType != null && Single.class.isAssignableFrom(bodyType); - } - return false; - } - - @Override - public void handleReturnValue(Object returnValue, MethodParameter returnType, - ModelAndViewContainer mavContainer, NativeWebRequest webRequest) - throws Exception { - - if (returnValue == null) { - mavContainer.setRequestHandled(true); - return; - } - - ResponseEntity> responseEntity = getResponseEntity(returnValue); - if (responseEntity != null) { - returnValue = responseEntity.getBody(); - if (returnValue == null) { - mavContainer.setRequestHandled(true); - return; - } - } - - final Single single = Single.class.cast(returnValue); - WebAsyncUtils.getAsyncManager(webRequest).startDeferredResultProcessing( - convertToDeferredResult(responseEntity, single), mavContainer); - } - - @SuppressWarnings("unchecked") - private ResponseEntity> getResponseEntity(Object returnValue) { - if (ResponseEntity.class.isAssignableFrom(returnValue.getClass())) { - return (ResponseEntity>) returnValue; - - } - return null; - } - - protected DeferredResult convertToDeferredResult( - final ResponseEntity> responseEntity, Single single) { - - // TODO: use lambda when java8 :-) - Single> singleResponse = single - .map(new Func1>() { - @Override - public ResponseEntity call(Object object) { - if (object instanceof ResponseEntity) { - return (ResponseEntity) object; - } - - return new ResponseEntity(object, - getHttpHeaders(responseEntity), - getHttpStatus(responseEntity)); - } - }); - - return new SingleDeferredResult<>(singleResponse); - } - - private HttpStatus getHttpStatus(ResponseEntity responseEntity) { - if (responseEntity == null) { - return HttpStatus.OK; - } - return responseEntity.getStatusCode(); - } - - private HttpHeaders getHttpHeaders(ResponseEntity responseEntity) { - if (responseEntity == null) { - return new HttpHeaders(); - } - return responseEntity.getHeaders(); - } -} diff --git a/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories b/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories index 20c3e165..99f994ab 100644 --- a/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-netflix-core/src/main/resources/META-INF/spring.factories @@ -7,7 +7,6 @@ org.springframework.cloud.netflix.feign.encoding.FeignContentGzipEncodingAutoCon org.springframework.cloud.netflix.hystrix.HystrixAutoConfiguration,\ org.springframework.cloud.netflix.hystrix.security.HystrixSecurityAutoConfiguration,\ org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration,\ -org.springframework.cloud.netflix.rx.RxJavaAutoConfiguration,\ org.springframework.cloud.netflix.zuul.ZuulServerAutoConfiguration,\ org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/ObservableReturnValueHandlerTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/ObservableReturnValueHandlerTests.java deleted file mode 100644 index b98559cc..00000000 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/ObservableReturnValueHandlerTests.java +++ /dev/null @@ -1,171 +0,0 @@ -/* - * Copyright 2013-2016 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.netflix.rx; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8; - -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.TimeUnit; - -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -import rx.Observable; -import rx.Single; -import rx.functions.Func1; - -/** - * Tests the demonstrate using {@link Observable} with {@link SingleReturnValueHandler} class. - * - * @author Spencer Gibb - * @author Jakub Narloch - */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = ObservableReturnValueHandlerTests.Application.class, webEnvironment = WebEnvironment.RANDOM_PORT) -@DirtiesContext -public class ObservableReturnValueHandlerTests { - - @Value("${local.server.port}") - private int port = 0; - - private TestRestTemplate restTemplate = new TestRestTemplate(); - - @Configuration - @EnableAutoConfiguration - @RestController - protected static class Application { - - // tag::rx_observable[] - @RequestMapping(method = RequestMethod.GET, value = "/single") - public Single single() { - return Observable.just("single value").toSingle(); - } - - @RequestMapping(method = RequestMethod.GET, value = "/multiple") - public Single> multiple() { - return Observable.just("multiple", "values").toList().toSingle(); - } - - @RequestMapping(method = RequestMethod.GET, value = "/responseWithObservable") - public ResponseEntity> responseWithObservable() { - - Observable observable = Observable.just("single value"); - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(APPLICATION_JSON_UTF8); - return new ResponseEntity<>(observable.toSingle(), headers, HttpStatus.CREATED); - } - - @RequestMapping(method = RequestMethod.GET, value = "/timeout") - public Observable timeout() { - return Observable.timer(1, TimeUnit.MINUTES).map(new Func1() { - @Override - public String call(Long aLong) { - return "single value"; - } - }); - } - // end::rx_observable[] - - @RequestMapping(method = RequestMethod.GET, value = "/throw") - public Single error() { - return Observable.error(new RuntimeException("Unexpected")).toSingle(); - } - } - - @Test - public void shouldRetrieveSingleValue() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/single"), String.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals("single value", response.getBody()); - } - - @Test - public void shouldRetrieveMultipleValues() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/multiple"), List.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals(Arrays.asList("multiple", "values"), response.getBody()); - } - - @Test - public void shouldRetrieveSingleValueWithStatusCodeAndCustomHeader() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/responseWithObservable"), String.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.CREATED, response.getStatusCode()); - assertEquals(MediaType.APPLICATION_JSON_UTF8, response.getHeaders().getContentType()); - assertEquals("single value", response.getBody()); - } - - @Test - public void shouldRetrieveErrorResponse() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/throw"), Object.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); - } - - @Test - @Ignore("adds 30s to build") - public void shouldTimeoutOnConnection() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/timeout"), Object.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); - } - - private String path(String context) { - return String.format("http://localhost:%d%s", port, context); - } -} \ No newline at end of file diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/ObservableSseEmitterTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/ObservableSseEmitterTests.java deleted file mode 100644 index 79f60e31..00000000 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/ObservableSseEmitterTests.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright 2013-2016 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.netflix.rx; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8; - -import java.util.Date; -import java.util.GregorianCalendar; -import java.util.TimeZone; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; - -import com.fasterxml.jackson.annotation.JsonCreator; -import com.fasterxml.jackson.annotation.JsonProperty; - -import rx.Observable; - -/** - * Tests the {@link ObservableSseEmitter} class. - * - * @author Spencer Gibb - * @author Jakub Narloch - */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = ObservableSseEmitterTests.Application.class, webEnvironment = WebEnvironment.RANDOM_PORT) -@DirtiesContext -public class ObservableSseEmitterTests { - - @Value("${local.server.port}") - private int port = 0; - - private TestRestTemplate restTemplate = new TestRestTemplate(); - - @Configuration - @EnableAutoConfiguration - @RestController - protected static class Application { - - // tag::rx_observable_sse[] - @RequestMapping(method = RequestMethod.GET, value = "/sse") - public SseEmitter single() { - return RxResponse.sse(Observable.just("single value")); - } - - @RequestMapping(method = RequestMethod.GET, value = "/messages") - public SseEmitter messages() { - return RxResponse.sse(Observable.just("message 1", "message 2", "message 3")); - } - - @RequestMapping(method = RequestMethod.GET, value = "/events") - public SseEmitter event() { - return RxResponse.sse(APPLICATION_JSON_UTF8, - Observable.just(new EventDto("Spring io", getDate(2016, 5, 19)), - new EventDto("SpringOnePlatform", getDate(2016, 8, 1)))); - } - // end::rx_observable_sse[] - } - - @Test - public void shouldRetrieveSse() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/sse"), - String.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals("data:single value\n\n", response.getBody()); - } - - @Test - public void shouldRetrieveSseWithMultipleMessages() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/messages"), - String.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals("data:message 1\n\ndata:message 2\n\ndata:message 3\n\n", - response.getBody()); - } - - @Test - public void shouldRetrieveJsonOverSseWithMultipleMessages() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/events"), - String.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals( - "data:{\"name\":\"Spring io\",\"date\":1466337600000}\n\ndata:{\"name\":\"SpringOnePlatform\",\"date\":1472731200000}\n\n", - response.getBody()); - } - - private String path(String context) { - return String.format("http://localhost:%d%s", port, context); - } - - private static Date getDate(int year, int month, int day) { - GregorianCalendar calendar = new GregorianCalendar(year, month, day, 12, 0, 0); - calendar.setTimeZone(TimeZone.getTimeZone("UTC")); - return calendar.getTime(); - } - - /** - * A simple DTO used for testing purpose. - * - * @author Jakub Narloch - */ - static class EventDto { - - private final String name; - - private final Date date; - - @JsonCreator - public EventDto(@JsonProperty("name") String name, - @JsonProperty("date") Date date) { - this.name = name; - this.date = date; - } - - public String getName() { - return name; - } - - public Date getDate() { - return date; - } - } -} \ No newline at end of file diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleDeferredResultTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleDeferredResultTests.java deleted file mode 100644 index 4f4db036..00000000 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleDeferredResultTests.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright 2013-2017 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ - -package org.springframework.cloud.netflix.rx; - -import java.lang.reflect.Field; - -import org.assertj.core.api.Assertions; -import org.junit.Before; -import org.junit.Test; -import org.springframework.web.context.request.async.DeferredResult; - -import static org.springframework.util.ReflectionUtils.findField; -import static org.springframework.util.ReflectionUtils.getField; -import static org.springframework.util.ReflectionUtils.makeAccessible; - -import rx.Single; - -/** - * @author Spencer Gibb - */ -public class SingleDeferredResultTests { - - private Object resultNone; - private Field timeoutResultField; - - @Before - public void init() { - Field resultNoneField = findField(DeferredResult.class, "RESULT_NONE"); - makeAccessible(resultNoneField); - resultNone = getField(resultNoneField, null); - - timeoutResultField = findField(DeferredResult.class, "timeoutResult"); - makeAccessible(timeoutResultField); - } - - @Test - public void testDefaultTimeoutResult() { - Object timeoutResult = getField(timeoutResultField, new SingleDeferredResult<>(Single.just(""))); - - Assertions.assertThat(timeoutResult).as("timeoutResult was not the default").isSameAs(resultNone); - } - - @Test - public void testDefaultTimeoutResultWithTimeout() { - Object timeoutResult = getField(timeoutResultField, new SingleDeferredResult<>(1L, Single.just(""))); - - Assertions.assertThat(timeoutResult).as("timeoutResult was not the default").isSameAs(resultNone); - } - - @Test - public void testCustomTimeoutResultWithTimeout() { - Object customTimeoutResult = new Object(); - Object timeoutResult = getField(timeoutResultField, new SingleDeferredResult<>(1L, customTimeoutResult, Single.just(""))); - - Assertions.assertThat(timeoutResult).as("timeoutResult was not the custom one").isSameAs(customTimeoutResult); - } - -} diff --git a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandlerTests.java b/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandlerTests.java deleted file mode 100644 index 4aef0fbe..00000000 --- a/spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/rx/SingleReturnValueHandlerTests.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright 2013-2016 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 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.netflix.rx; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.EnableAutoConfiguration; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.context.annotation.Configuration; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -import rx.Single; - -/** - * Tests the {@link SingleReturnValueHandler} class. - * - * @author Spencer Gibb - * @author Jakub Narloch - */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringBootTest(classes = SingleReturnValueHandlerTests.Application.class, webEnvironment = WebEnvironment.RANDOM_PORT) -@DirtiesContext -public class SingleReturnValueHandlerTests { - - @Value("${local.server.port}") - private int port = 0; - - private TestRestTemplate restTemplate = new TestRestTemplate(); - - @Configuration - @EnableAutoConfiguration - @RestController - protected static class Application { - - // tag::rx_single[] - @RequestMapping(method = RequestMethod.GET, value = "/single") - public Single single() { - return Single.just("single value"); - } - - @RequestMapping(method = RequestMethod.GET, value = "/singleWithResponse") - public ResponseEntity> singleWithResponse() { - return new ResponseEntity<>(Single.just("single value"), - HttpStatus.NOT_FOUND); - } - - @RequestMapping(method = RequestMethod.GET, value = "/singleCreatedWithResponse") - public Single> singleOuterWithResponse() { - return Single.just(new ResponseEntity<>("single value", HttpStatus.CREATED)); - } - - @RequestMapping(method = RequestMethod.GET, value = "/throw") - public Single error() { - return Single.error(new RuntimeException("Unexpected")); - } - // end::rx_single[] - } - - @Test - public void shouldRetrieveSingleValue() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/single"), - String.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertEquals("single value", response.getBody()); - } - - @Test - public void shouldRetrieveSingleValueWithStatusCode() { - - // when - ResponseEntity response = restTemplate - .getForEntity(path("/singleWithResponse"), String.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); - assertEquals("single value", response.getBody()); - } - - @Test - public void shouldRetrieveErrorResponse() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/throw"), - Object.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.getStatusCode()); - } - - @Test - public void shouldRetrieveSingleValueWithCreatedCode() { - - // when - ResponseEntity response = restTemplate.getForEntity(path("/singleCreatedWithResponse"), - String.class); - - // then - assertNotNull(response); - assertEquals(HttpStatus.CREATED, response.getStatusCode()); - assertEquals("single value", response.getBody()); - } - - private String path(String context) { - return String.format("http://localhost:%d%s", port, context); - } -} \ No newline at end of file