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