1
|
package eu.dnetlib.enabling.is.sn.resourcestate;
|
2
|
|
3
|
import static org.junit.Assert.assertEquals;
|
4
|
import static org.junit.Assert.assertNotNull; // NOPMD
|
5
|
import static org.mockito.Matchers.argThat;
|
6
|
import static org.mockito.Mockito.verify; // NOPMD
|
7
|
|
8
|
import org.junit.Before;
|
9
|
import org.junit.Test;
|
10
|
import org.junit.runner.RunWith;
|
11
|
import org.mockito.ArgumentMatcher;
|
12
|
import org.mockito.Mock;
|
13
|
import org.mockito.junit.MockitoJUnitRunner;
|
14
|
|
15
|
import eu.dnetlib.enabling.is.sn.SubscriptionRequest;
|
16
|
import eu.dnetlib.enabling.is.sn.rmi.SubscriptionRequestRejectedException;
|
17
|
|
18
|
/**
|
19
|
* test ResourceStateSubscriptionRegistryTest.
|
20
|
*
|
21
|
* @author marko
|
22
|
*
|
23
|
*/
|
24
|
@RunWith(MockitoJUnitRunner.class)
|
25
|
public class ResourceStateSubscriptionRegistryTest {
|
26
|
|
27
|
/**
|
28
|
* matches.
|
29
|
*
|
30
|
* @author marko
|
31
|
*
|
32
|
*/
|
33
|
public static class MatchSubscription implements ArgumentMatcher<ResourceStateSubscription> {
|
34
|
|
35
|
/**
|
36
|
* prefix.
|
37
|
*/
|
38
|
private final transient String prefix;
|
39
|
|
40
|
/**
|
41
|
* type.
|
42
|
*/
|
43
|
private final transient String type;
|
44
|
|
45
|
/**
|
46
|
* resource id.
|
47
|
*/
|
48
|
private final transient String resId;
|
49
|
|
50
|
/**
|
51
|
* create a new match subscription checker for a given prefix and a given type.
|
52
|
*
|
53
|
* @param prefix
|
54
|
* prefix
|
55
|
* @param type
|
56
|
* type
|
57
|
* @param resId
|
58
|
* resource id
|
59
|
*/
|
60
|
MatchSubscription(final String prefix, final String type, final String resId) {
|
61
|
super();
|
62
|
this.prefix = prefix;
|
63
|
this.type = type;
|
64
|
this.resId = resId;
|
65
|
}
|
66
|
|
67
|
/**
|
68
|
* {@inheritDoc}
|
69
|
*
|
70
|
* @see org.mockito.ArgumentMatcher#matches(java.lang.Object)
|
71
|
*/
|
72
|
@Override
|
73
|
public boolean matches(final ResourceStateSubscription sub) {
|
74
|
return sub.getPrefix().equals(this.prefix) && sub.getType().equals(this.type) && sub.getResourceId().equals(this.resId);
|
75
|
}
|
76
|
|
77
|
}
|
78
|
|
79
|
/**
|
80
|
* object under test.
|
81
|
*/
|
82
|
private transient ResourceStateSubscriptionRegistry registry;
|
83
|
|
84
|
/**
|
85
|
* DAO mock.
|
86
|
*/
|
87
|
@Mock
|
88
|
private transient ResourceStateSubscriptionDAO subscriptionDao;
|
89
|
|
90
|
/**
|
91
|
* subscription request.
|
92
|
*/
|
93
|
private transient SubscriptionRequest sub;
|
94
|
|
95
|
/**
|
96
|
* common setup.
|
97
|
*/
|
98
|
@Before
|
99
|
public void setUp() {
|
100
|
registry = new ResourceStateSubscriptionRegistry();
|
101
|
registry.setSubscriptionDao(subscriptionDao);
|
102
|
|
103
|
final SubscriptionRequestFilter filter = new SubscriptionRequestFilter();
|
104
|
filter.setActive(true);
|
105
|
registry.setSubscriptionRequestFilter(filter);
|
106
|
|
107
|
sub = new SubscriptionRequest();
|
108
|
}
|
109
|
|
110
|
/**
|
111
|
* test registration.
|
112
|
*
|
113
|
* @throws SubscriptionRequestRejectedException
|
114
|
*/
|
115
|
@Test
|
116
|
public void testRegisterSubscription() throws SubscriptionRequestRejectedException {
|
117
|
sub.setTopicExpression("CREATE/MyResourceType/123/some/path");
|
118
|
registry.registerSubscription(sub);
|
119
|
verify(subscriptionDao).addSubscription(argThat(new MatchSubscription("CREATE", "MyResourceType", "123")));
|
120
|
|
121
|
assertNotNull("dummy", subscriptionDao);
|
122
|
}
|
123
|
|
124
|
/**
|
125
|
* test registration.
|
126
|
*
|
127
|
* @throws SubscriptionRequestRejectedException
|
128
|
*/
|
129
|
@Test(expected = SubscriptionRequestRejectedException.class)
|
130
|
public void testRegisterSubscriptionReject1() throws SubscriptionRequestRejectedException {
|
131
|
|
132
|
sub.setTopicExpression("UPDATE/OtherResourceType");
|
133
|
registry.registerSubscription(sub);
|
134
|
verify(subscriptionDao).addSubscription(argThat(new MatchSubscription("UPDATE", "OtherResourceType", "*")));
|
135
|
|
136
|
assertNotNull("dummy", subscriptionDao);
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* test registration.
|
141
|
*
|
142
|
* @throws SubscriptionRequestRejectedException
|
143
|
*/
|
144
|
@Test(expected = SubscriptionRequestRejectedException.class)
|
145
|
public void testRegisterSubscriptionReject2() throws SubscriptionRequestRejectedException {
|
146
|
|
147
|
sub.setTopicExpression("DELETE/*");
|
148
|
registry.registerSubscription(sub);
|
149
|
verify(subscriptionDao).addSubscription(argThat(new MatchSubscription("DELETE", "*", "*")));
|
150
|
|
151
|
assertNotNull("dummy", subscriptionDao);
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* test registration.
|
156
|
*
|
157
|
* @throws SubscriptionRequestRejectedException
|
158
|
*/
|
159
|
@Test(expected = SubscriptionRequestRejectedException.class)
|
160
|
public void testRegisterSubscriptionReject3() throws SubscriptionRequestRejectedException {
|
161
|
|
162
|
sub.setTopicExpression("CREATE");
|
163
|
registry.registerSubscription(sub);
|
164
|
verify(subscriptionDao).addSubscription(argThat(new MatchSubscription("CREATE", "*", "*")));
|
165
|
|
166
|
assertNotNull("dummy", subscriptionDao);
|
167
|
}
|
168
|
|
169
|
/**
|
170
|
* test match id.
|
171
|
*/
|
172
|
@Test
|
173
|
public void testMatchId() {
|
174
|
assertEquals("check pure id", "1234123", registry.matchId("1234123").getPrefix());
|
175
|
assertEquals("check with suffix", "1234123", registry.matchId("1234123/blabla/blabla").getPrefix());
|
176
|
assertEquals("check suffix", "/blabla/blabla", registry.matchId("1234123/blabla/blabla").getRest());
|
177
|
}
|
178
|
|
179
|
}
|