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