Project

General

Profile

1 26600 sandro.lab
package eu.dnetlib.enabling.is.registry; // NOPMD
2
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertNull;
5
import static org.junit.Assert.fail;
6
import static org.mockito.Matchers.anyString;
7
import static org.mockito.Matchers.argThat;
8
import static org.mockito.Matchers.eq;
9
import static org.mockito.Mockito.verify;
10
import static org.mockito.Mockito.when;
11
12
import java.io.IOException;
13
import java.io.StringWriter;
14
15
import javax.xml.bind.JAXBException;
16
import javax.xml.transform.dom.DOMSource;
17
import javax.xml.xpath.XPathConstants;
18
import javax.xml.xpath.XPathFactory;
19
20
import org.apache.commons.io.IOUtils;
21
import org.apache.commons.logging.Log;
22
import org.apache.commons.logging.LogFactory;
23
import org.junit.Before;
24
import org.junit.Test;
25
import org.junit.runner.RunWith;
26
import org.mockito.ArgumentMatcher;
27
import org.mockito.Mock;
28 45126 claudio.at
import org.mockito.junit.MockitoJUnitRunner;
29 26600 sandro.lab
import org.w3c.dom.Element;
30
import org.w3c.dom.Node;
31
32
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
33
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
34
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
35
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryService;
36
import eu.dnetlib.enabling.tools.OpaqueResource;
37
import eu.dnetlib.enabling.tools.StringOpaqueResource;
38
import eu.dnetlib.enabling.tools.blackboard.ActionStatus;
39
import eu.dnetlib.enabling.tools.blackboard.BlackboardMessage;
40
import eu.dnetlib.enabling.tools.blackboard.BlackboardMessageImpl;
41
import eu.dnetlib.enabling.tools.blackboard.BlackboardParameter;
42
import eu.dnetlib.enabling.tools.blackboard.BlackboardParameterImpl;
43 32742 michele.ar
import eu.dnetlib.miscutils.jaxb.JaxbFactory;
44 26600 sandro.lab
45
/**
46
 * RegistryBlackboardManagerImpl test.
47
 *
48
 * @author marko
49
 *
50
 */
51 45126 claudio.at
@RunWith(MockitoJUnitRunner.class)
52 26600 sandro.lab
public class RegistryBlackboardManagerImplTest {
53
54
	/**
55
	 * logger.
56
	 */
57
	private static final Log log = LogFactory.getLog(RegistryBlackboardManagerImplTest.class); // NOPMD by marko on 11/24/08 5:02 PM
58
59
	/**
60
	 * test message id.
61
	 */
62
	private static final String MSG_ID = "xxx";
63
64
	/**
65
	 * fake message date.
66
	 */
67
	private static final String MSG_DATE = "2009-03-19T17:55:45+01:00";
68
69
	/**
70
	 * service profile ID.
71
	 */
72
	private static final String PROF_ID = "123";
73
74
	/**
75
	 * lookup service mock.
76
	 */
77
	@Mock
78
	private transient ISLookUpService lookupService;
79
80
	/**
81
	 * registry service mock.
82
	 */
83
	@Mock
84
	private transient ISRegistryService registryService;
85
86
	/**
87
	 * instance under test.
88
	 */
89
	private transient RegistryBlackboardManagerImpl manager;
90
91
	/**
92
	 * common setup.
93
	 *
94
	 * @throws JAXBException
95
	 *             could happen
96
	 * @throws IOException
97
	 *             shouldn't happen
98
	 * @throws ISLookUpException
99
	 *             mock
100
	 */
101
	@Before
102
	public void setUp() throws JAXBException, IOException, ISLookUpException {
103
		manager = new RegistryBlackboardManagerImpl();
104 32742 michele.ar
		manager.setIsLookup(lookupService);
105 26600 sandro.lab
		manager.setRegistryService(registryService);
106
107
		manager.setMessageFactory(new JaxbFactory<BlackboardMessage>(BlackboardMessageImpl.class));
108
109
		manager.setMessageDater(new RegistryBlackboardManagerImpl.MessageDater() {
110 32742 michele.ar
111 26600 sandro.lab
			@Override
112
			public String getCurrentDate() {
113
				return MSG_DATE;
114
			}
115
116
			@Override
117
			public String getNumericStamp() {
118
				return "1237483712.666000";
119
			}
120
		});
121
	}
122
123
	/**
124
	 * add message.
125
	 *
126
	 * @throws JAXBException
127
	 *             could happen
128
	 * @throws ISRegistryException
129
	 *             could happen
130
	 * @throws IOException
131
	 *             could happen
132
	 * @throws ISLookUpException
133
	 *             mock
134
	 */
135
	@Test
136
	public void testAddMessage() throws JAXBException, ISRegistryException, IOException, ISLookUpException {
137
		final StringWriter serviceProfile = new StringWriter();
138
		IOUtils.copy(getClass().getResourceAsStream("serviceProfile.xml"), serviceProfile);
139
		when(lookupService.getResourceProfile(PROF_ID)).thenReturn(serviceProfile.toString());
140
141
		final BlackboardMessage message = manager.getMessageFactory().newInstance();
142
		message.setId(MSG_ID);
143
		message.setDate(MSG_DATE);
144
		message.setAction("CREATE");
145
		message.setActionStatus(ActionStatus.ASSIGNED);
146
147
		final BlackboardParameter param1 = new BlackboardParameterImpl();
148
		param1.setName("format");
149
		param1.setValue("DMF");
150
		message.getParameters().add(param1);
151
152
		final BlackboardParameter param2 = new BlackboardParameterImpl();
153
		param1.setName("id");
154
		param1.setValue("");
155
		message.getParameters().add(param2);
156
157
		manager.addMessage(PROF_ID, MSG_ID, manager.getMessageFactory().serialize(message));
158
159
		verify(registryService).updateProfile(eq(PROF_ID), argThat(new ArgumentMatcher<String>() {
160
161
			@Override
162 45126 claudio.at
			public boolean matches(final String argument) {
163 26600 sandro.lab
				log.info("arg: " + argument);
164
165
				try {
166
					final OpaqueResource updatedResource = new StringOpaqueResource((String) argument);
167
					final Node messageNode = (Node) XPathFactory.newInstance().newXPath().evaluate("//BLACKBOARD/MESSAGE[last()]",
168
							updatedResource.asDom(), XPathConstants.NODE);
169
170
					final BlackboardMessage updatedMessage = manager.getMessageFactory().parse(new DOMSource(messageNode));
171
172
					assertEquals("messages should match", message, updatedMessage);
173 32742 michele.ar
174 26600 sandro.lab
					final Element lastUpdated = (Element) XPathFactory.newInstance().newXPath().evaluate("//BLACKBOARD/LAST_REQUEST",
175
							updatedResource.asDom(), XPathConstants.NODE);
176
					assertEquals("stamp date", manager.getMessageDater().getNumericStamp(), lastUpdated.getAttribute("date"));
177
					assertEquals("stamp message", MSG_ID, lastUpdated.getTextContent());
178
				} catch (Exception e) {
179
					fail("got exception" + e);
180
				}
181
				return true;
182
			}
183
		}), anyString());
184
	}
185
186
	/**
187
	 * delete message.
188
	 *
189
	 * @throws ISLookUpException
190
	 *             mock
191
	 * @throws IOException
192
	 *             could happen
193
	 * @throws ISRegistryException
194
	 *             mock
195
	 */
196
	@Test
197
	public void testDeleteMessage() throws ISLookUpException, IOException, ISRegistryException {
198
		final StringWriter serviceProfile = new StringWriter();
199
200
		IOUtils.copy(getClass().getResourceAsStream("serviceProfileWithMessage.xml"), serviceProfile);
201
		when(lookupService.getResourceProfile(PROF_ID)).thenReturn(serviceProfile.toString());
202
203
		manager.deleteMessage(PROF_ID, MSG_ID);
204
205
		verify(registryService).updateProfile(eq(PROF_ID), argThat(new ArgumentMatcher<String>() {
206
207
			@Override
208 45126 claudio.at
			public boolean matches(final String argument) {
209 26600 sandro.lab
				log.debug("arg: " + argument);
210
211
				try {
212
					final OpaqueResource updatedResource = new StringOpaqueResource((String) argument);
213
214
					final Node messageNode = (Node) XPathFactory.newInstance().newXPath().evaluate("//BLACKBOARD/MESSAGE[last()]",
215
							updatedResource.asDom(), XPathConstants.NODE);
216
217
					assertNull("message should be deleted", messageNode);
218
				} catch (Exception e) {
219
					fail("got exception" + e);
220
				}
221
				return true;
222
			}
223
		}), anyString());
224
225 32742 michele.ar
		// assertNotNull("dummy", manager);
226 26600 sandro.lab
	}
227
228
	/**
229
	 * replay message.
230
	 *
231
	 * @throws ISLookUpException
232
	 *             mock
233
	 * @throws IOException
234
	 *             shouldn't happen
235
	 * @throws ISRegistryException
236
	 *             mock
237
	 * @throws JAXBException
238
	 *             could happen
239
	 */
240
	@Test
241
	public void testReplyMessage() throws ISLookUpException, IOException, ISRegistryException, JAXBException {
242
		final StringWriter serviceProfile = new StringWriter();
243
244
		IOUtils.copy(getClass().getResourceAsStream("serviceProfileWithMessage.xml"), serviceProfile);
245
		when(lookupService.getResourceProfile(PROF_ID)).thenReturn(serviceProfile.toString());
246
247
		final BlackboardMessage message = manager.getMessageFactory().newInstance();
248
		message.setId(MSG_ID);
249
		message.setDate(MSG_DATE);
250
		message.setAction("CREATE");
251
		message.setActionStatus(ActionStatus.ONGOING);
252
253
		final BlackboardParameter param1 = new BlackboardParameterImpl();
254
		param1.setName("format");
255
		param1.setValue("DMF");
256
		message.getParameters().add(param1);
257
258
		final BlackboardParameter param2 = new BlackboardParameterImpl();
259
		param1.setName("id");
260
		param1.setValue("");
261
		message.getParameters().add(param2);
262
263
		manager.replyMessage(PROF_ID, manager.getMessageFactory().serialize(message));
264
265
		verify(registryService).updateProfile(eq(PROF_ID), argThat(new ArgumentMatcher<String>() {
266
267
			@Override
268 45126 claudio.at
			public boolean matches(final String argument) {
269 26600 sandro.lab
				log.info("arg: " + argument);
270
271
				try {
272
					final OpaqueResource updatedResource = new StringOpaqueResource((String) argument);
273
274
					final Node messageNode = (Node) XPathFactory.newInstance().newXPath().evaluate("//BLACKBOARD/MESSAGE[last()]",
275
							updatedResource.asDom(), XPathConstants.NODE);
276
277
					final BlackboardMessage updatedMessage = manager.getMessageFactory().parse(new DOMSource(messageNode));
278
279
					assertEquals("messages should match", message, updatedMessage);
280 32742 michele.ar
281 26600 sandro.lab
					final Element lastUpdated = (Element) XPathFactory.newInstance().newXPath().evaluate("//BLACKBOARD/LAST_RESPONSE",
282
							updatedResource.asDom(), XPathConstants.NODE);
283
					assertEquals("stamp date", manager.getMessageDater().getNumericStamp(), lastUpdated.getAttribute("date"));
284
					assertEquals("stamp message", MSG_ID, lastUpdated.getTextContent());
285
				} catch (Exception e) {
286
					fail("got exception" + e);
287
				}
288
				return true;
289
			}
290
		}), anyString());
291
	}
292
293
}