|
| 1 | +package feign.hystrix; |
| 2 | + |
| 3 | +import static feign.assertj.MockWebServerAssertions.assertThat; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.junit.Rule; |
| 8 | +import org.junit.Test; |
| 9 | +import org.junit.rules.ExpectedException; |
| 10 | + |
| 11 | +import com.netflix.hystrix.HystrixCommand; |
| 12 | +import com.squareup.okhttp.mockwebserver.MockResponse; |
| 13 | +import com.squareup.okhttp.mockwebserver.MockWebServer; |
| 14 | + |
| 15 | +import feign.Headers; |
| 16 | +import feign.RequestLine; |
| 17 | +import feign.gson.GsonDecoder; |
| 18 | + |
| 19 | +public class HystrixBuilderTest { |
| 20 | + |
| 21 | + @Rule |
| 22 | + public final ExpectedException thrown = ExpectedException.none(); |
| 23 | + @Rule |
| 24 | + public final MockWebServer server = new MockWebServer(); |
| 25 | + |
| 26 | + @Test |
| 27 | + public void hystrixCommand() { |
| 28 | + server.enqueue(new MockResponse().setBody("\"foo\"")); |
| 29 | + |
| 30 | + TestInterface api = target(); |
| 31 | + |
| 32 | + HystrixCommand<String> command = api.command(); |
| 33 | + |
| 34 | + assertThat(command).isNotNull(); |
| 35 | + assertThat(command.execute()).isEqualTo("foo"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void hystrixCommandInt() { |
| 40 | + server.enqueue(new MockResponse().setBody("1")); |
| 41 | + |
| 42 | + TestInterface api = target(); |
| 43 | + |
| 44 | + HystrixCommand<Integer> command = api.intCommand(); |
| 45 | + |
| 46 | + assertThat(command).isNotNull(); |
| 47 | + assertThat(command.execute()).isEqualTo(new Integer(1)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void hystrixCommandList() { |
| 52 | + server.enqueue(new MockResponse().setBody("[\"foo\",\"bar\"]")); |
| 53 | + |
| 54 | + TestInterface api = target(); |
| 55 | + |
| 56 | + HystrixCommand<List<String>> command = api.listCommand(); |
| 57 | + |
| 58 | + assertThat(command).isNotNull(); |
| 59 | + assertThat(command.execute()).hasSize(2).contains("foo", "bar"); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void plainString() { |
| 64 | + server.enqueue(new MockResponse().setBody("\"foo\"")); |
| 65 | + |
| 66 | + TestInterface api = target(); |
| 67 | + |
| 68 | + String string = api.get(); |
| 69 | + |
| 70 | + assertThat(string).isEqualTo("foo"); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void plainList() { |
| 75 | + server.enqueue(new MockResponse().setBody("[\"foo\",\"bar\"]")); |
| 76 | + |
| 77 | + TestInterface api = target(); |
| 78 | + |
| 79 | + List<String> list = api.getList(); |
| 80 | + |
| 81 | + assertThat(list).isNotNull().hasSize(2).contains("foo", "bar"); |
| 82 | + } |
| 83 | + |
| 84 | + private TestInterface target() { |
| 85 | + return HystrixFeign.builder() |
| 86 | + .decoder(new GsonDecoder()) |
| 87 | + .target(TestInterface.class, "http://localhost:" + server.getPort()); |
| 88 | + } |
| 89 | + |
| 90 | + interface TestInterface { |
| 91 | + |
| 92 | + @RequestLine("GET /") |
| 93 | + @Headers("Accept: application/json") |
| 94 | + HystrixCommand<List<String>> listCommand(); |
| 95 | + |
| 96 | + @RequestLine("GET /") |
| 97 | + @Headers("Accept: application/json") |
| 98 | + HystrixCommand<String> command(); |
| 99 | + |
| 100 | + @RequestLine("GET /") |
| 101 | + @Headers("Accept: application/json") |
| 102 | + HystrixCommand<Integer> intCommand(); |
| 103 | + |
| 104 | + @RequestLine("GET /") |
| 105 | + @Headers("Accept: application/json") |
| 106 | + String get(); |
| 107 | + |
| 108 | + @RequestLine("GET /") |
| 109 | + @Headers("Accept: application/json") |
| 110 | + List<String> getList(); |
| 111 | + } |
| 112 | +} |
0 commit comments