1 package net.secodo.jcircuitbreaker.breaker.execution.impl;
2
3 import static org.hamcrest.CoreMatchers.equalTo;
4
5 import static org.hamcrest.core.IsNull.nullValue;
6
7 import static org.junit.Assert.assertThat;
8
9 import java.util.concurrent.ConcurrentHashMap;
10
11 import net.secodo.jcircuitbreaker.breaker.execution.ExecutedTask;
12
13 import org.junit.Test;
14
15
16 @SuppressWarnings("unchecked")
17 public class DefaultExecutionContextImplTest {
18 @Test
19 public void shouldReturnGivenDataToConsumer() throws Exception {
20
21
22 ConcurrentHashMap<String, ExecutedTask<Long>> tasksInProgress = new ConcurrentHashMap<>();
23 final ExecutedTask<Long> task = new ExecutedTask<>(() -> new Long(1L), System.currentTimeMillis());
24 tasksInProgress.put("asdsd", task);
25
26 Object userData = new Object();
27
28 DefaultExecutionContextImpl<Long> context = new DefaultExecutionContextImpl<>(tasksInProgress, userData);
29
30
31
32 assertThat(context.getExecutionsInProgress().size(), equalTo(1));
33 assertThat(context.getExecutionsInProgress().iterator().next(), equalTo(task));
34 assertThat(context.getUserData(), equalTo(userData));
35
36 }
37
38 @Test
39 public void shouldReturnAddedContextAttributes() throws Exception {
40
41
42 ConcurrentHashMap<String, ExecutedTask<Long>> tasksInProgress = new ConcurrentHashMap<>();
43 final ExecutedTask<Long> task = new ExecutedTask<>(() -> new Long(1L), System.currentTimeMillis());
44 tasksInProgress.put("asdsd", task);
45
46 Long userData = 1L;
47
48 DefaultExecutionContextImpl<Long> context = new DefaultExecutionContextImpl<>(tasksInProgress, userData);
49
50
51
52 context.setContextAttribute("testAtt1", 1L);
53 context.setContextAttribute("testAtt2", "tests");
54
55
56
57 assertThat(context.hasContextAttribute("testAtt1"), equalTo(true));
58 assertThat(context.hasContextAttribute("testAtt2"), equalTo(true));
59 assertThat(context.hasContextAttribute("testAtt3"), equalTo(false));
60 assertThat(context.hasContextAttribute("as"), equalTo(false));
61 assertThat(context.getContextAttribute("testAtt1"), equalTo(1L));
62 assertThat(context.getContextAttribute("testAtt2"), equalTo("tests"));
63 assertThat(context.getContextAttribute("asdasds"), equalTo(null));
64
65 }
66
67 @Test
68 public void shouldReturnFalseOrNullWhenAccessingAttributesWithoutAddingAnyAttributeFirst() throws Exception {
69
70 ConcurrentHashMap<String, ExecutedTask<Long>> tasksInProgress = new ConcurrentHashMap<>();
71 final ExecutedTask<Long> task = new ExecutedTask<>(() -> new Long(1L), System.currentTimeMillis());
72 tasksInProgress.put("asdsd", task);
73
74 Long userData = 1L;
75
76 DefaultExecutionContextImpl<Long> context = new DefaultExecutionContextImpl<>(tasksInProgress, userData);
77
78
79
80 assertThat(context.hasContextAttribute("testAtt1"), equalTo(false));
81 assertThat(context.hasContextAttribute("testAtt2"), equalTo(false));
82 assertThat(context.hasContextAttribute("testAtt3"), equalTo(false));
83 assertThat(context.hasContextAttribute("as"), equalTo(false));
84 assertThat(context.getContextAttribute("testAtt1"), nullValue());
85 assertThat(context.getContextAttribute("testAtt2"), nullValue());
86 assertThat(context.getContextAttribute("asdasds"), nullValue());
87
88 }
89
90
91 }