1
|
package eu.dnetlib.enabling.is.sn;
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.List;
|
5
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
6
|
import javax.xml.ws.wsaddressing.W3CEndpointReferenceBuilder;
|
7
|
|
8
|
import eu.dnetlib.enabling.is.sn.resourcestate.ResourceStateSubscription;
|
9
|
import eu.dnetlib.enabling.is.sn.resourcestate.ResourceStateSubscriptionRegistry;
|
10
|
import eu.dnetlib.enabling.tools.OpaqueResource;
|
11
|
import org.junit.Before;
|
12
|
import org.junit.Test;
|
13
|
import org.junit.runner.RunWith;
|
14
|
import org.mockito.ArgumentMatcher;
|
15
|
import org.mockito.Mock;
|
16
|
import org.mockito.junit.MockitoJUnitRunner;
|
17
|
|
18
|
import static org.junit.Assert.assertNotNull;
|
19
|
import static org.mockito.Mockito.*;
|
20
|
|
21
|
/**
|
22
|
* Test the default notification detector implementation.
|
23
|
*
|
24
|
* @author marko
|
25
|
*
|
26
|
*/
|
27
|
@RunWith(MockitoJUnitRunner.class)
|
28
|
public class NotificationDetectorImplTest {
|
29
|
|
30
|
/**
|
31
|
* some type.
|
32
|
*/
|
33
|
private static final String SOME_TYPE = "SomeType";
|
34
|
|
35
|
/**
|
36
|
* some resource identifier.
|
37
|
*/
|
38
|
private static final String SOME_ID = "123";
|
39
|
|
40
|
/**
|
41
|
* Check if a message has the correct content.
|
42
|
*
|
43
|
* @author marko
|
44
|
*
|
45
|
*/
|
46
|
private final class CheckMessage implements ArgumentMatcher<NotificationMessage> {
|
47
|
|
48
|
/**
|
49
|
* prefix.
|
50
|
*/
|
51
|
private final transient String prefix;
|
52
|
|
53
|
/**
|
54
|
* constructor.
|
55
|
*
|
56
|
* @param prefix topic prefix
|
57
|
*/
|
58
|
CheckMessage(final String prefix) {
|
59
|
super();
|
60
|
this.prefix = prefix;
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* {@inheritDoc}
|
65
|
* @see org.mockito.ArgumentMatcher#matches(java.lang.Object)
|
66
|
*/
|
67
|
@Override
|
68
|
public boolean matches(final NotificationMessage message) {
|
69
|
return message.getSubscriptionId() == subscription.getSubscriptionId() && message.getResourceId().equals("123")
|
70
|
&& message.getTopic().equals(prefix + ".*.*");
|
71
|
}
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* instance under test.
|
76
|
*/
|
77
|
private transient NotificationDetectorImpl detector;
|
78
|
|
79
|
/**
|
80
|
* registry mock.
|
81
|
*/
|
82
|
@Mock
|
83
|
private transient ResourceStateSubscriptionRegistry registry;
|
84
|
|
85
|
/**
|
86
|
* sender mock.
|
87
|
*/
|
88
|
@Mock
|
89
|
private transient NotificationSender sender;
|
90
|
|
91
|
/**
|
92
|
* resource mock.
|
93
|
*/
|
94
|
@Mock
|
95
|
private transient OpaqueResource resource;
|
96
|
|
97
|
/**
|
98
|
* resource subscription.
|
99
|
*/
|
100
|
@Mock
|
101
|
private transient ResourceStateSubscription subscription;
|
102
|
|
103
|
/**
|
104
|
* mock subscriber.
|
105
|
*/
|
106
|
private transient W3CEndpointReference subscriber;
|
107
|
|
108
|
/**
|
109
|
* common.
|
110
|
*/
|
111
|
@Before
|
112
|
public void setUp() {
|
113
|
detector = new NotificationDetectorImpl();
|
114
|
final List<ResourceStateSubscriptionRegistry> registries = new ArrayList<ResourceStateSubscriptionRegistry>();
|
115
|
registries.add(registry);
|
116
|
detector.setRegistries(registries);
|
117
|
|
118
|
detector.setSender(sender);
|
119
|
|
120
|
final W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
|
121
|
builder.address("http://test.com/test/test");
|
122
|
subscriber = builder.build();
|
123
|
|
124
|
final List<ResourceStateSubscription> subscriptions = new ArrayList<ResourceStateSubscription>();
|
125
|
subscriptions.add(subscription);
|
126
|
when(registry.listMatchingSubscriptions(ResourceStateSubscription.PREFIX_CREATE, SOME_TYPE, SOME_ID)).thenReturn(subscriptions);
|
127
|
when(registry.listMatchingSubscriptions(ResourceStateSubscription.PREFIX_DELETE, SOME_TYPE, SOME_ID)).thenReturn(subscriptions);
|
128
|
when(registry.listMatchingSubscriptions(ResourceStateSubscription.PREFIX_UPDATE, SOME_TYPE, SOME_ID)).thenReturn(subscriptions);
|
129
|
|
130
|
when(resource.getResourceId()).thenReturn(SOME_ID);
|
131
|
when(resource.getResourceType()).thenReturn(SOME_TYPE);
|
132
|
|
133
|
when(subscription.getSubscriptionId()).thenReturn("sn123");
|
134
|
when(subscription.getSubscriberAsEpr()).thenReturn(subscriber);
|
135
|
when(subscription.getXpath()).thenReturn("");
|
136
|
when(subscription.getType()).thenReturn("*");
|
137
|
when(subscription.getResourceId()).thenReturn("*");
|
138
|
}
|
139
|
|
140
|
/**
|
141
|
* test 'create'.
|
142
|
*/
|
143
|
@Test
|
144
|
public void testResourceCreated() {
|
145
|
when(subscription.getPrefix()).thenReturn(ResourceStateSubscription.PREFIX_CREATE);
|
146
|
|
147
|
detector.resourceCreated(resource);
|
148
|
|
149
|
verify(sender).send(eq(subscriber), argThat(new CheckMessage(ResourceStateSubscription.PREFIX_CREATE)));
|
150
|
|
151
|
assertNotNull("dummy", sender);
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* test 'delete'.
|
156
|
*/
|
157
|
@Test
|
158
|
public void testResourceDeleted() {
|
159
|
when(subscription.getPrefix()).thenReturn(ResourceStateSubscription.PREFIX_DELETE);
|
160
|
|
161
|
detector.resourceDeleted(resource);
|
162
|
|
163
|
verify(sender).send(eq(subscriber), argThat(new CheckMessage(ResourceStateSubscription.PREFIX_DELETE)));
|
164
|
|
165
|
assertNotNull("dummy", sender);
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* test 'update'.
|
170
|
*/
|
171
|
@Test
|
172
|
public void testResourceUpdated() {
|
173
|
when(subscription.getXpath()).thenReturn(null);
|
174
|
when(subscription.getPrefix()).thenReturn(ResourceStateSubscription.PREFIX_UPDATE);
|
175
|
|
176
|
detector.resourceUpdated(resource, resource);
|
177
|
|
178
|
verify(sender).send(eq(subscriber), argThat(new CheckMessage(ResourceStateSubscription.PREFIX_UPDATE)));
|
179
|
|
180
|
assertNotNull("dummy", sender);
|
181
|
|
182
|
}
|
183
|
|
184
|
}
|