1
|
package eu.dnetlib.enabling.is.sn;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringReader;
|
5
|
import javax.xml.parsers.DocumentBuilder;
|
6
|
import javax.xml.parsers.DocumentBuilderFactory;
|
7
|
import javax.xml.parsers.ParserConfigurationException;
|
8
|
|
9
|
import eu.dnetlib.enabling.is.sn.resourcestate.ResourceStateNotificationDetector;
|
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
|
import org.w3c.dom.Document;
|
18
|
import org.xml.sax.InputSource;
|
19
|
import org.xml.sax.SAXException;
|
20
|
|
21
|
import static org.junit.Assert.assertNotNull;
|
22
|
import static org.mockito.Matchers.argThat;
|
23
|
import static org.mockito.Mockito.verify;
|
24
|
|
25
|
/**
|
26
|
* Test NotificationTriggerImpl, a simple forwarder.
|
27
|
*
|
28
|
* @author marko
|
29
|
*
|
30
|
*/
|
31
|
@RunWith(MockitoJUnitRunner.class)
|
32
|
public class NotificationTriggerImplTest {
|
33
|
|
34
|
/**
|
35
|
* Check that the resource has the expected properties.
|
36
|
*
|
37
|
* @author marko
|
38
|
*
|
39
|
*/
|
40
|
private static final class MatchResource implements ArgumentMatcher<OpaqueResource> {
|
41
|
/**
|
42
|
* {@inheritDoc}
|
43
|
*
|
44
|
* @see org.mockito.ArgumentMatcher#matches(java.lang.Object)
|
45
|
*/
|
46
|
@Override
|
47
|
public boolean matches(final OpaqueResource resource) {
|
48
|
return resource.getResourceId().equals("123") && resource.getResourceType().equals("type") && resource.getResourceKind().equals("kind");
|
49
|
}
|
50
|
}
|
51
|
|
52
|
/**
|
53
|
* test old document version.
|
54
|
*/
|
55
|
private static final String OLD_DOC = "<RESOURCE_PROFILE><HEADER><RESOURCE_KIND value=\"kind\"/><RESOURCE_TYPE value=\"type\"/><RESOURCE_IDENTIFIER value=\"123\"/></HEADER><BODY>old</BODY></RESOURCE_PROFILE>";
|
56
|
|
57
|
/**
|
58
|
* test old document version.
|
59
|
*/
|
60
|
private static final String NEW_DOC = "<RESOURCE_PROFILE><HEADER><RESOURCE_KIND value=\"kind\"/><RESOURCE_TYPE value=\"type\"/><RESOURCE_IDENTIFIER value=\"123\"/></HEADER><BODY>new</BODY></RESOURCE_PROFILE>";
|
61
|
|
62
|
/**
|
63
|
* class under test.
|
64
|
*/
|
65
|
private transient NotificationTriggerImpl trigger;
|
66
|
|
67
|
/**
|
68
|
* detector mock.
|
69
|
*/
|
70
|
@Mock
|
71
|
private transient ResourceStateNotificationDetector<OpaqueResource> detector;
|
72
|
|
73
|
/**
|
74
|
* common preparation.
|
75
|
*
|
76
|
*/
|
77
|
@Before
|
78
|
public void setUp() {
|
79
|
trigger = new NotificationTriggerImpl();
|
80
|
trigger.setName("test trigger");
|
81
|
trigger.setDetector(detector);
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* test created.
|
86
|
*
|
87
|
* @throws IOException
|
88
|
* never
|
89
|
* @throws SAXException
|
90
|
* never
|
91
|
* @throws ParserConfigurationException
|
92
|
* never
|
93
|
*/
|
94
|
|
95
|
@Test
|
96
|
public void testCreated() throws ParserConfigurationException, SAXException, IOException {
|
97
|
trigger.created("someFile", "someCollection", parse(NEW_DOC));
|
98
|
verify(detector).resourceCreated(argThat(new MatchResource()));
|
99
|
|
100
|
assertNotNull("dummy", detector);
|
101
|
}
|
102
|
|
103
|
/**
|
104
|
* test deleted.
|
105
|
*
|
106
|
* @throws IOException
|
107
|
* never
|
108
|
* @throws SAXException
|
109
|
* never
|
110
|
* @throws ParserConfigurationException
|
111
|
* never
|
112
|
*/
|
113
|
@Test
|
114
|
public void testDeleted() throws ParserConfigurationException, SAXException, IOException {
|
115
|
trigger.deleted("someFile", "someCollection", parse(OLD_DOC));
|
116
|
verify(detector).resourceDeleted(argThat(new MatchResource()));
|
117
|
|
118
|
assertNotNull("dummy", detector);
|
119
|
}
|
120
|
|
121
|
/**
|
122
|
* test updated.
|
123
|
*
|
124
|
* @throws IOException
|
125
|
* never
|
126
|
* @throws SAXException
|
127
|
* never
|
128
|
* @throws ParserConfigurationException
|
129
|
* never
|
130
|
*/
|
131
|
@Test
|
132
|
public void testUpdated() throws ParserConfigurationException, SAXException, IOException {
|
133
|
trigger.updated("someFile", "someCollection", parse(OLD_DOC), parse(NEW_DOC));
|
134
|
verify(detector).resourceUpdated(argThat(new MatchResource()), argThat(new MatchResource()));
|
135
|
|
136
|
assertNotNull("dummy", detector);
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* helper method.
|
141
|
*
|
142
|
* @param source
|
143
|
* xml source
|
144
|
* @return DOM object
|
145
|
* @throws ParserConfigurationException
|
146
|
* shouldn't happen
|
147
|
* @throws SAXException
|
148
|
* shouldn't happen
|
149
|
* @throws IOException
|
150
|
* shouldn't happen
|
151
|
*/
|
152
|
private Document parse(final String source) throws ParserConfigurationException, SAXException, IOException {
|
153
|
final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
154
|
return builder.parse(new InputSource(new StringReader(source)));
|
155
|
}
|
156
|
|
157
|
}
|