|
| 1 | +package com.netflix.hystrix.contrib.javanica.test.common.error; |
| 2 | + |
| 3 | +import org.junit.Before; |
| 4 | +import org.junit.Ignore; |
| 5 | +import org.junit.Test; |
| 6 | + |
| 7 | +import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties; |
| 8 | +import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; |
| 9 | +import com.netflix.hystrix.contrib.javanica.annotation.HystrixException; |
| 10 | +import com.netflix.hystrix.exception.HystrixRuntimeException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Created by Mike Cowan |
| 14 | + */ |
| 15 | +public abstract class BasicDefaultRaiseHystrixExceptionsTest { |
| 16 | + |
| 17 | + private Service service; |
| 18 | + |
| 19 | + @Before |
| 20 | + public void setUp() throws Exception { |
| 21 | + service = createService(); |
| 22 | + } |
| 23 | + |
| 24 | + protected abstract Service createService(); |
| 25 | + |
| 26 | + @Test(expected = BadRequestException.class) |
| 27 | + public void testDefaultIgnoreException() { |
| 28 | + service.commandInheritsDefaultIgnoreExceptions(); |
| 29 | + } |
| 30 | + |
| 31 | + @Test(expected = SpecificException.class) |
| 32 | + public void testCommandOverridesDefaultIgnoreExceptions() { |
| 33 | + service.commandOverridesDefaultIgnoreExceptions(SpecificException.class); |
| 34 | + } |
| 35 | + |
| 36 | + @Test(expected = HystrixRuntimeException.class) |
| 37 | + public void testCommandOverridesDefaultIgnoreExceptions_nonIgnoreExceptionShouldBePropagated() { |
| 38 | + // method throws BadRequestException that isn't ignored |
| 39 | + service.commandOverridesDefaultIgnoreExceptions(BadRequestException.class); |
| 40 | + } |
| 41 | + |
| 42 | + @Ignore // https://github.com/Netflix/Hystrix/issues/993#issuecomment-229542203 |
| 43 | + @Test(expected = BadRequestException.class) |
| 44 | + public void testFallbackCommandInheritsDefaultIgnoreException() { |
| 45 | + service.commandWithFallbackInheritsDefaultIgnoreExceptions(); |
| 46 | + } |
| 47 | + |
| 48 | + @Ignore // https://github.com/Netflix/Hystrix/issues/993#issuecomment-229542203 |
| 49 | + @Test(expected = SpecificException.class) |
| 50 | + public void testFallbackCommandOverridesDefaultIgnoreExceptions() { |
| 51 | + service.commandWithFallbackOverridesDefaultIgnoreExceptions(SpecificException.class); |
| 52 | + } |
| 53 | + |
| 54 | + @Test(expected = HystrixRuntimeException.class) |
| 55 | + public void testFallbackCommandOverridesDefaultIgnoreExceptions_nonIgnoreExceptionShouldBePropagated() { |
| 56 | + service.commandWithFallbackOverridesDefaultIgnoreExceptions(BadRequestException.class); |
| 57 | + } |
| 58 | + |
| 59 | + @DefaultProperties(ignoreExceptions = BadRequestException.class, raiseHystrixExceptions = {HystrixException.RUNTIME_EXCEPTION}) |
| 60 | + public static class Service { |
| 61 | + @HystrixCommand |
| 62 | + public Object commandInheritsDefaultIgnoreExceptions() throws BadRequestException { |
| 63 | + // this exception will be ignored (wrapped in HystrixBadRequestException) because specified in default ignore exceptions |
| 64 | + throw new BadRequestException("from 'commandInheritsIgnoreExceptionsFromDefault'"); |
| 65 | + } |
| 66 | + |
| 67 | + @HystrixCommand(ignoreExceptions = SpecificException.class) |
| 68 | + public Object commandOverridesDefaultIgnoreExceptions(Class<? extends Throwable> errorType) throws BadRequestException, SpecificException { |
| 69 | + if(errorType.equals(BadRequestException.class)){ |
| 70 | + // isn't ignored because command doesn't specify this exception type in 'ignoreExceptions' |
| 71 | + throw new BadRequestException("from 'commandOverridesDefaultIgnoreExceptions', cause: " + errorType.getSimpleName()); |
| 72 | + } |
| 73 | + // something went wrong, this error is ignored because specified in the command's ignoreExceptions |
| 74 | + throw new SpecificException("from 'commandOverridesDefaultIgnoreExceptions', cause: " + errorType.getSimpleName()); |
| 75 | + } |
| 76 | + |
| 77 | + @HystrixCommand(fallbackMethod = "fallbackInheritsDefaultIgnoreExceptions") |
| 78 | + public Object commandWithFallbackInheritsDefaultIgnoreExceptions() throws SpecificException { |
| 79 | + // isn't ignored, need to trigger fallback |
| 80 | + throw new SpecificException("from 'commandWithFallbackInheritsDefaultIgnoreExceptions'"); |
| 81 | + } |
| 82 | + |
| 83 | + @HystrixCommand |
| 84 | + private Object fallbackInheritsDefaultIgnoreExceptions() throws BadRequestException { |
| 85 | + // should be ignored because specified in global ignore exception, fallback command inherits default ignore exceptions |
| 86 | + throw new BadRequestException("from 'fallbackInheritsDefaultIgnoreExceptions'"); |
| 87 | + } |
| 88 | + |
| 89 | + @HystrixCommand(fallbackMethod = "fallbackOverridesDefaultIgnoreExceptions") |
| 90 | + public Object commandWithFallbackOverridesDefaultIgnoreExceptions(Class<? extends Throwable> errorType) { |
| 91 | + // isn't ignored, need to trigger fallback |
| 92 | + throw new SpecificException(); |
| 93 | + } |
| 94 | + |
| 95 | + @HystrixCommand(ignoreExceptions = SpecificException.class) |
| 96 | + private Object fallbackOverridesDefaultIgnoreExceptions(Class<? extends Throwable> errorType) { |
| 97 | + if(errorType.equals(BadRequestException.class)){ |
| 98 | + // isn't ignored because fallback doesn't specify this exception type in 'ignoreExceptions' |
| 99 | + throw new BadRequestException("from 'fallbackOverridesDefaultIgnoreExceptions', cause: " + errorType.getSimpleName()); |
| 100 | + } |
| 101 | + // something went wrong, this error is ignored because specified in the fallback's ignoreExceptions |
| 102 | + throw new SpecificException("from 'commandOverridesDefaultIgnoreExceptions', cause: " + errorType.getSimpleName()); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public static final class BadRequestException extends RuntimeException { |
| 107 | + public BadRequestException() { |
| 108 | + } |
| 109 | + |
| 110 | + public BadRequestException(String message) { |
| 111 | + super(message); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + public static final class SpecificException extends RuntimeException { |
| 116 | + public SpecificException() { |
| 117 | + } |
| 118 | + |
| 119 | + public SpecificException(String message) { |
| 120 | + super(message); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments