Project

General

Profile

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
import org.mockito.runners.MockitoJUnit44Runner;
18

    
19
import eu.dnetlib.enabling.is.sn.resourcestate.ResourceStateSubscription;
20
import eu.dnetlib.enabling.is.sn.resourcestate.ResourceStateSubscriptionRegistry;
21
import eu.dnetlib.enabling.tools.OpaqueResource;
22

    
23
/**
24
 * Test the default notification detector implementation.
25
 *
26
 * @author marko
27
 *
28
 */
29
@RunWith(MockitoJUnit44Runner.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 extends 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 Object argument) {
71
			final NotificationMessage message = (NotificationMessage) argument;
72
			return message.getSubscriptionId() == subscription.getSubscriptionId() && message.getResourceId().equals("123")
73
					&& message.getTopic().equals(prefix + ".*.*");
74
		}
75
	}
76

    
77
	/**
78
	 * instance under test.
79
	 */
80
	private transient NotificationDetectorImpl detector;
81

    
82
	/**
83
	 * registry mock.
84
	 */
85
	@Mock
86
	private transient ResourceStateSubscriptionRegistry registry;
87

    
88
	/**
89
	 * sender mock.
90
	 */
91
	@Mock
92
	private transient NotificationSender sender;
93

    
94
	/**
95
	 * resource mock.
96
	 */
97
	@Mock
98
	private transient OpaqueResource resource;
99

    
100
	/**
101
	 * resource subscription.
102
	 */
103
	@Mock
104
	private transient ResourceStateSubscription subscription;
105

    
106
	/**
107
	 * mock subscriber.
108
	 */
109
	private transient W3CEndpointReference subscriber;
110

    
111
	/**
112
	 * common.
113
	 */
114
	@Before
115
	public void setUp() {
116
		detector = new NotificationDetectorImpl();
117
		final List<ResourceStateSubscriptionRegistry> registries = new ArrayList<ResourceStateSubscriptionRegistry>();
118
		registries.add(registry);
119
		detector.setRegistries(registries);
120

    
121
		detector.setSender(sender);
122

    
123
		final W3CEndpointReferenceBuilder builder = new W3CEndpointReferenceBuilder();
124
		builder.address("http://test.com/test/test");
125
		subscriber = builder.build();
126

    
127
		final List<ResourceStateSubscription> subscriptions = new ArrayList<ResourceStateSubscription>();
128
		subscriptions.add(subscription);
129
		when(registry.listMatchingSubscriptions(ResourceStateSubscription.PREFIX_CREATE, SOME_TYPE, SOME_ID)).thenReturn(subscriptions);
130
		when(registry.listMatchingSubscriptions(ResourceStateSubscription.PREFIX_DELETE, SOME_TYPE, SOME_ID)).thenReturn(subscriptions);
131
		when(registry.listMatchingSubscriptions(ResourceStateSubscription.PREFIX_UPDATE, SOME_TYPE, SOME_ID)).thenReturn(subscriptions);
132

    
133
		when(resource.getResourceId()).thenReturn(SOME_ID);
134
		when(resource.getResourceType()).thenReturn(SOME_TYPE);
135

    
136
		when(subscription.getSubscriptionId()).thenReturn("sn123");
137
		when(subscription.getSubscriberAsEpr()).thenReturn(subscriber);
138
		when(subscription.getXpath()).thenReturn("");
139
		when(subscription.getType()).thenReturn("*");
140
		when(subscription.getResourceId()).thenReturn("*");
141
	}
142

    
143
	/**
144
	 * test 'create'.
145
	 */
146
	@Test
147
	public void testResourceCreated() {
148
		when(subscription.getPrefix()).thenReturn(ResourceStateSubscription.PREFIX_CREATE);
149

    
150
		detector.resourceCreated(resource);
151

    
152
		verify(sender).send(eq(subscriber), argThat(new CheckMessage(ResourceStateSubscription.PREFIX_CREATE)));
153

    
154
		assertNotNull("dummy", sender);
155
	}
156

    
157
	/**
158
	 * test 'delete'.
159
	 */
160
	@Test
161
	public void testResourceDeleted() {
162
		when(subscription.getPrefix()).thenReturn(ResourceStateSubscription.PREFIX_DELETE);
163

    
164
		detector.resourceDeleted(resource);
165

    
166
		verify(sender).send(eq(subscriber), argThat(new CheckMessage(ResourceStateSubscription.PREFIX_DELETE)));
167

    
168
		assertNotNull("dummy", sender);
169
	}
170

    
171
	/**
172
	 * test 'update'.
173
	 */
174
	@Test
175
	public void testResourceUpdated() {
176
		when(subscription.getXpath()).thenReturn(null);
177
		when(subscription.getPrefix()).thenReturn(ResourceStateSubscription.PREFIX_UPDATE);
178

    
179
		detector.resourceUpdated(resource, resource);
180

    
181
		verify(sender).send(eq(subscriber), argThat(new CheckMessage(ResourceStateSubscription.PREFIX_UPDATE)));
182

    
183
		assertNotNull("dummy", sender);
184

    
185
	}
186

    
187
}
(3-3/4)