View Javadoc
1   package net.secodo.jcircuitbreaker.breakhandler.impl;
2   
3   import net.secodo.jcircuitbreaker.breaker.ContextAwareCircuitBreaker;
4   import net.secodo.jcircuitbreaker.breaker.execution.ExecutionContext;
5   import net.secodo.jcircuitbreaker.breakhandler.BreakHandler;
6   import net.secodo.jcircuitbreaker.breakhandler.exception.BreakHandlerException;
7   import net.secodo.jcircuitbreaker.breakstrategy.BreakStrategy;
8   import net.secodo.jcircuitbreaker.exception.CircuitBreakerException;
9   import net.secodo.jcircuitbreaker.exception.TaskExecutionException;
10  import net.secodo.jcircuitbreaker.task.Task;
11  import org.hamcrest.core.IsEqual;
12  import org.junit.Test;
13  
14  import java.util.concurrent.atomic.AtomicLong;
15  
16  import static org.hamcrest.CoreMatchers.equalTo;
17  import static org.hamcrest.CoreMatchers.not;
18  import static org.junit.Assert.*;
19  import static org.mockito.Mockito.mock;
20  
21  public class SimpleHandlerFactoryTest {
22  
23    @Test
24    public void shouldCreateNewInstanceOfHandlerForEachInvocation() {
25      // given
26      SimpleHandlerFactory<Long> counterNextFactory = new SimpleHandlerFactory<>(CounterNextHandler.class);
27  
28      // when - then
29      CounterNextHandler breakHandler1 = (CounterNextHandler) counterNextFactory.makeHandler(mock(Task.class), mock(ExecutionContext.class));
30      assertThat(breakHandler1.getValue(), equalTo(1l));
31  
32      // when - then
33      CounterNextHandler breakHandler2 = (CounterNextHandler) counterNextFactory.makeHandler(mock(Task.class), mock(ExecutionContext.class));
34      assertThat(breakHandler2, not(equalTo(breakHandler1)));
35      assertThat(breakHandler2.getValue(), equalTo(2l));
36  
37      // when - then
38      CounterNextHandler breakHandler3 = (CounterNextHandler) counterNextFactory.makeHandler(mock(Task.class), mock(ExecutionContext.class));
39      assertThat(breakHandler3, not(equalTo(breakHandler2)));
40      assertThat(breakHandler3, not(equalTo(breakHandler1)));
41      assertThat(breakHandler3.getValue(), equalTo(3l));
42    }
43    
44    @Test
45    public void shouldThrowBreakHandlerExceptionWhenPassedBreakHandlerClassDoesNotHaveAccessibleArgumentConstructor() {
46      // given
47      SimpleHandlerFactory<Long> counterNextFactory = new SimpleHandlerFactory<>(BreakHandlerWithoutDefaultConstructor.class);
48  
49      // when
50      try {
51        counterNextFactory.makeHandler(mock(Task.class), mock(ExecutionContext.class));
52        fail(BreakHandlerException.class.getSimpleName() + " was expected");
53      } catch (BreakHandlerException e) {
54        // ok
55      }
56  
57    }
58    
59    
60  
61  
62  }
63  
64  class CounterNextHandler implements BreakHandler<Long> {
65    private long value;
66  
67    private static AtomicLong counter = new AtomicLong(0);
68  
69    public CounterNextHandler() {
70      this.value = counter.incrementAndGet();;
71    }
72  
73    @Override
74    public Long onBreak(ContextAwareCircuitBreaker<Long> circuitBreaker, Task<Long> task, BreakStrategy<Long> breakStrategy, ExecutionContext<Long> executionContext) throws TaskExecutionException, CircuitBreakerException, BreakHandlerException {
75      return value;
76    }
77  
78    public long getValue() {
79      return value;
80    }
81  
82    @Override
83    public boolean equals(Object o) {
84      if (this == o) return true;
85      if (o == null || getClass() != o.getClass()) return false;
86  
87      CounterNextHandler that = (CounterNextHandler) o;
88  
89      return value == that.value;
90    }
91  
92    @Override
93    public int hashCode() {
94      return (int) (value ^ (value >>> 32));
95    }
96    
97    
98  };
99  class BreakHandlerWithoutDefaultConstructor implements BreakHandler<Long> {
100   public BreakHandlerWithoutDefaultConstructor(int A) {
101   }
102 
103   @Override
104   public Long onBreak(ContextAwareCircuitBreaker<Long> circuitBreaker, Task<Long> task, BreakStrategy<Long> breakStrategy, ExecutionContext<Long> executionContext) throws TaskExecutionException, CircuitBreakerException, BreakHandlerException {
105     return 1L;
106   }
107   
108 };