|
9 | 9 | *
|
10 | 10 | * Contributors:
|
11 | 11 | * Microsoft Corporation - initial API and implementation
|
| 12 | + * Cofinity-X - prioritized transfer services |
12 | 13 | *
|
13 | 14 | */
|
14 | 15 |
|
|
23 | 24 | import java.util.stream.Stream;
|
24 | 25 |
|
25 | 26 | import static org.assertj.core.api.Assertions.assertThat;
|
26 |
| -import static org.mockito.ArgumentMatchers.any; |
27 | 27 | import static org.mockito.Mockito.eq;
|
28 | 28 | import static org.mockito.Mockito.mock;
|
29 | 29 | import static org.mockito.Mockito.verify;
|
30 | 30 | import static org.mockito.Mockito.when;
|
31 | 31 |
|
32 | 32 | class TransferServiceRegistryImplTest {
|
33 |
| - TransferService transferService = mock(TransferService.class); |
34 |
| - TransferService transferService2 = mock(TransferService.class); |
35 |
| - |
36 |
| - DataFlowStartMessage request = createRequest().build(); |
37 |
| - TransferServiceSelectionStrategy transferServiceSelectionStrategy = mock(TransferServiceSelectionStrategy.class); |
| 33 | + private TransferService transferService = mock(TransferService.class); |
| 34 | + private TransferService transferService2 = mock(TransferService.class); |
| 35 | + |
| 36 | + private DataFlowStartMessage request = createRequest().build(); |
| 37 | + private TransferServiceSelectionStrategy transferServiceSelectionStrategy = mock(TransferServiceSelectionStrategy.class); |
38 | 38 | @SuppressWarnings("unchecked")
|
39 |
| - ArgumentCaptor<Stream<TransferService>> streamCaptor = ArgumentCaptor.forClass(Stream.class); |
40 |
| - |
| 39 | + private ArgumentCaptor<Stream<TransferService>> streamCaptor = ArgumentCaptor.forClass(Stream.class); |
| 40 | + |
| 41 | + private TransferServiceRegistryImpl registry = new TransferServiceRegistryImpl(transferServiceSelectionStrategy); |
| 42 | + |
41 | 43 | @Test
|
42 |
| - void resolveTransferService_filters_matches() { |
| 44 | + void resolveTransferService_noServicesRegistered_shouldReturnNull() { |
| 45 | + var service = registry.resolveTransferService(request); |
| 46 | + |
| 47 | + assertThat(service).isNull(); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void resolveTransferService_noServiceCanHandle_shouldReturnNull() { |
| 52 | + registry.registerTransferService(transferService); |
43 | 53 | when(transferService.canHandle(request)).thenReturn(false);
|
| 54 | + |
| 55 | + var service = registry.resolveTransferService(request); |
| 56 | + |
| 57 | + assertThat(service).isNull(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void resolveTransferService_withPriorities_shouldReturnHighestPriorityService() { |
| 62 | + registry.registerTransferService(transferService); |
| 63 | + registry.registerTransferService(1, transferService2); |
| 64 | + when(transferService.canHandle(request)).thenReturn(true); |
44 | 65 | when(transferService2.canHandle(request)).thenReturn(true);
|
45 |
| - |
46 |
| - createRegistryAndResolveForRequest(); |
47 |
| - |
48 |
| - assertThat(streamCaptor.getValue()).containsExactly(transferService2); |
| 66 | + |
| 67 | + var service = registry.resolveTransferService(request); |
| 68 | + |
| 69 | + assertThat(service).isEqualTo(transferService2); |
49 | 70 | }
|
50 |
| - |
| 71 | + |
51 | 72 | @Test
|
52 |
| - void resolveTransferService_handles_multipleMatches() { |
| 73 | + void resolveTransferService_withSamePriority_shouldReturnFirstWithHighestPriority() { |
| 74 | + registry.registerTransferService(1, transferService); |
| 75 | + registry.registerTransferService(1, transferService2); |
53 | 76 | when(transferService.canHandle(request)).thenReturn(true);
|
54 | 77 | when(transferService2.canHandle(request)).thenReturn(true);
|
55 |
| - |
56 |
| - createRegistryAndResolveForRequest(); |
57 |
| - |
58 |
| - assertThat(streamCaptor.getValue()).containsExactly(transferService, transferService2); |
| 78 | + |
| 79 | + var service = registry.resolveTransferService(request); |
| 80 | + |
| 81 | + assertThat(service).isEqualTo(transferService); |
59 | 82 | }
|
60 |
| - |
| 83 | + |
61 | 84 | @Test
|
62 |
| - void resolveTransferService_handles_noMatch() { |
63 |
| - var registry = new TransferServiceRegistryImpl(transferServiceSelectionStrategy); |
64 |
| - |
| 85 | + void resolveTransferService_noPriorityAndNoneCanHandle_shouldApplyStrategyWithEmptyStream() { |
| 86 | + registry.registerTransferService(transferService); |
| 87 | + registry.registerTransferService(transferService2); |
| 88 | + when(transferService.canHandle(request)).thenReturn(false); |
| 89 | + when(transferService2.canHandle(request)).thenReturn(false); |
| 90 | + |
65 | 91 | registry.resolveTransferService(request);
|
66 |
| - |
| 92 | + |
67 | 93 | verify(transferServiceSelectionStrategy).chooseTransferService(eq(request), streamCaptor.capture());
|
68 | 94 | assertThat(streamCaptor.getValue()).isEmpty();
|
69 | 95 | }
|
70 |
| - |
| 96 | + |
71 | 97 | @Test
|
72 |
| - void resolveTransferService_returns_strategyResult() { |
73 |
| - var registry = new TransferServiceRegistryImpl(transferServiceSelectionStrategy); |
74 |
| - when(transferServiceSelectionStrategy.chooseTransferService(eq(request), any())) |
75 |
| - .thenReturn(transferService); |
76 |
| - |
77 |
| - var resolved = registry.resolveTransferService(request); |
78 |
| - |
79 |
| - assertThat(resolved).isSameAs(transferService); |
| 98 | + void resolveTransferService_noPriorityAndOneCanHandle_shouldApplyStrategyWithOneService() { |
| 99 | + registry.registerTransferService(transferService); |
| 100 | + registry.registerTransferService(transferService2); |
| 101 | + when(transferService.canHandle(request)).thenReturn(true); |
| 102 | + when(transferService2.canHandle(request)).thenReturn(false); |
| 103 | + |
| 104 | + registry.resolveTransferService(request); |
| 105 | + |
| 106 | + verify(transferServiceSelectionStrategy).chooseTransferService(eq(request), streamCaptor.capture()); |
| 107 | + assertThat(streamCaptor.getValue()).containsExactly(transferService); |
80 | 108 | }
|
81 |
| - |
82 |
| - private void createRegistryAndResolveForRequest() { |
83 |
| - var registry = new TransferServiceRegistryImpl(transferServiceSelectionStrategy); |
| 109 | + |
| 110 | + @Test |
| 111 | + void resolveTransferService_noPriorityAndAllCanHandle_shouldApplyStrategyWithAllServices() { |
84 | 112 | registry.registerTransferService(transferService);
|
85 | 113 | registry.registerTransferService(transferService2);
|
86 |
| - |
| 114 | + when(transferService.canHandle(request)).thenReturn(true); |
| 115 | + when(transferService2.canHandle(request)).thenReturn(true); |
| 116 | + |
87 | 117 | registry.resolveTransferService(request);
|
88 |
| - |
| 118 | + |
89 | 119 | verify(transferServiceSelectionStrategy).chooseTransferService(eq(request), streamCaptor.capture());
|
| 120 | + assertThat(streamCaptor.getValue()).containsExactly(transferService, transferService2); |
90 | 121 | }
|
91 | 122 |
|
92 | 123 | private DataFlowStartMessage.Builder createRequest() {
|
|
0 commit comments