View Javadoc

1   package com.atlassian.messagequeue.internal.core;
2   
3   import com.atlassian.messagequeue.MessageRunnerKey;
4   import com.atlassian.messagequeue.registry.MessageRunner;
5   import org.junit.Before;
6   import org.junit.Test;
7   import org.junit.runner.RunWith;
8   import org.mockito.Mock;
9   import org.mockito.runners.MockitoJUnitRunner;
10  
11  import java.util.Optional;
12  
13  import static org.hamcrest.core.Is.is;
14  import static org.junit.Assert.assertThat;
15  import static org.junit.Assert.fail;
16  
17  @RunWith(MockitoJUnitRunner.class)
18  public class DefaultMessageRunnerRegistryServiceTest {
19  
20      private DefaultMessageRunnerRegistryService registryService;
21  
22      @Mock
23      private MessageRunner messageRunnerA;
24      @Mock
25      private MessageRunner messageRunnerB;
26      private MessageRunnerKey key;
27  
28      @Before
29      public void setUp() throws Exception {
30          registryService = new DefaultMessageRunnerRegistryService();
31          key = MessageRunnerKey.of("foobar");
32      }
33  
34      @Test
35      public void getMessageRunnerWhenNoMessageRunnerExists() throws Exception {
36          assertThat(registryService.getMessageRunner(MessageRunnerKey.of("foobar")), is(Optional.empty()));
37      }
38  
39      @Test
40      public void registeredMessageRunnerIsReturned() throws Exception {
41          registryService.registerMessageRunner(key, messageRunnerA);
42  
43          assertThat(registryService.getMessageRunner(key), is(Optional.of(messageRunnerA)));
44      }
45  
46      @Test
47      public void registeringKeyAgainOverridesPreviousRegistration() throws Exception {
48          registryService.registerMessageRunner(key, messageRunnerA);
49          registryService.registerMessageRunner(key, messageRunnerB);
50  
51          assertThat(registryService.getMessageRunner(key), is(Optional.of(messageRunnerB)));
52      }
53  
54      @Test
55      public void removeMessageRunner() throws Exception {
56          registryService.registerMessageRunner(key, messageRunnerA);
57          registryService.unregisterMessageRunner(key);
58  
59          assertThat(registryService.getMessageRunner(key), is(Optional.empty()));
60      }
61  
62      @Test
63      public void unregisterIsNoOpForKeyWithoutAnAssociatedRunner() throws Exception {
64          try {
65              registryService.unregisterMessageRunner(key);
66          } catch (Exception e) {
67              fail();
68          }
69      }
70  }