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