forked from wildfly/wildfly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AffinityIdentifierFactoryTestCase.java
118 lines (94 loc) · 4.12 KB
/
AffinityIdentifierFactoryTestCase.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* JBoss, Home of Professional Open Source.
* Copyright 2014, Red Hat, Inc., and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.wildfly.clustering.ee.infinispan.affinity;
import static org.junit.Assert.assertSame;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import java.util.UUID;
import java.util.function.Supplier;
import org.infinispan.Cache;
import org.infinispan.affinity.KeyAffinityService;
import org.infinispan.affinity.KeyGenerator;
import org.infinispan.manager.EmbeddedCacheManager;
import org.infinispan.remoting.transport.Address;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.MockitoAnnotations;
import org.wildfly.clustering.ee.Key;
import org.wildfly.clustering.ee.cache.IdentifierFactory;
import org.wildfly.clustering.ee.infinispan.GroupedKey;
import org.wildfly.clustering.infinispan.affinity.KeyAffinityServiceFactory;
/**
* Unit test for {@link AffinityIdentifierFactory}
*
* @author Paul Ferraro
*/
public class AffinityIdentifierFactoryTestCase {
private final Supplier<UUID> factory = mock(Supplier.class);
private final KeyAffinityServiceFactory affinityFactory = mock(KeyAffinityServiceFactory.class);
private final KeyAffinityService<Key<UUID>> affinity = mock(KeyAffinityService.class);
private final Cache<Key<UUID>, ?> cache = mock(Cache.class);
private final Address localAddress = mock(Address.class);
private IdentifierFactory<UUID> subject;
@Captor
private ArgumentCaptor<KeyGenerator<Key<UUID>>> capturedGenerator;
@Before
public void init() throws Exception {
EmbeddedCacheManager manager = mock(EmbeddedCacheManager.class);
try (AutoCloseable test = MockitoAnnotations.openMocks(this)) {
when(this.affinityFactory.createService(same(this.cache), this.capturedGenerator.capture())).thenReturn(this.affinity);
when(this.cache.getCacheManager()).thenReturn(manager);
when(manager.getAddress()).thenReturn(this.localAddress);
this.subject = new AffinityIdentifierFactory<>(this.factory, this.cache, this.affinityFactory);
KeyGenerator<Key<UUID>> generator = this.capturedGenerator.getValue();
assertSame(generator, this.subject);
UUID expected = UUID.randomUUID();
when(this.factory.get()).thenReturn(expected);
Key<UUID> result = generator.getKey();
assertSame(expected, result.getId());
}
}
@Test
public void start() {
this.subject.start();
verify(this.affinity).start();
verifyNoMoreInteractions(this.affinity);
}
@Test
public void stop() {
this.subject.stop();
verify(this.affinity).stop();
verifyNoMoreInteractions(this.affinity);
}
@Test
public void createIdentifier() {
UUID expected = UUID.randomUUID();
when(this.affinity.getKeyForAddress(this.localAddress)).thenReturn(new GroupedKey<>(expected));
UUID result = this.subject.get();
assertSame(expected, result);
}
}