1
|
package eu.dnetlib.enabling.resultset; // NOPMD
|
2
|
|
3
|
import static org.junit.Assert.assertFalse;
|
4
|
import static org.junit.Assert.assertTrue;
|
5
|
import static org.junit.Assert.assertNotNull;
|
6
|
import static org.mockito.Matchers.anyObject;
|
7
|
import static org.mockito.Matchers.anyString;
|
8
|
import static org.mockito.Matchers.argThat;
|
9
|
import static org.mockito.Matchers.eq;
|
10
|
import static org.mockito.Mockito.verify;
|
11
|
import static org.mockito.Mockito.times;
|
12
|
import static org.mockito.Mockito.when;
|
13
|
|
14
|
import java.io.IOException;
|
15
|
import java.io.StringWriter;
|
16
|
import java.util.List;
|
17
|
|
18
|
import javax.xml.transform.TransformerConfigurationException;
|
19
|
import javax.xml.ws.wsaddressing.W3CEndpointReference;
|
20
|
|
21
|
import org.apache.commons.io.IOUtils;
|
22
|
import org.junit.Before;
|
23
|
import org.junit.Test;
|
24
|
import org.junit.runner.RunWith;
|
25
|
import org.mockito.ArgumentMatcher;
|
26
|
import org.mockito.Mock;
|
27
|
import org.mockito.junit.MockitoJUnitRunner;
|
28
|
|
29
|
import com.google.common.collect.Lists;
|
30
|
|
31
|
import eu.dnetlib.enabling.resultset.rmi.ResultSetException;
|
32
|
import eu.dnetlib.enabling.resultset.rmi.ResultSetService;
|
33
|
import eu.dnetlib.enabling.tools.ServiceResolver;
|
34
|
|
35
|
/**
|
36
|
* test xslt resultset.
|
37
|
*
|
38
|
* @author marko
|
39
|
*
|
40
|
*/
|
41
|
@RunWith(MockitoJUnitRunner.class)
|
42
|
public class XSLTMappedResultSetFactoryTest {
|
43
|
|
44
|
/**
|
45
|
* instance under test.
|
46
|
*/
|
47
|
private transient XSLTMappedResultSetFactory factory;
|
48
|
|
49
|
/**
|
50
|
* resultset factory mock.
|
51
|
*/
|
52
|
@Mock
|
53
|
private transient ResultSetFactory resultSetFactory;
|
54
|
|
55
|
/**
|
56
|
* service resolver.
|
57
|
*/
|
58
|
@Mock
|
59
|
private transient ServiceResolver serviceResolver;
|
60
|
|
61
|
/**
|
62
|
* resultset service mock.
|
63
|
*/
|
64
|
@Mock
|
65
|
private transient ResultSetService resultSetService;
|
66
|
|
67
|
/**
|
68
|
* underlying resultSet mock
|
69
|
*/
|
70
|
@Mock
|
71
|
private transient ResultSet resultSet;
|
72
|
|
73
|
/**
|
74
|
* Common setup.
|
75
|
*
|
76
|
*/
|
77
|
@Before
|
78
|
public void setUp() {
|
79
|
factory = new XSLTMappedResultSetFactory();
|
80
|
factory.setResultSetFactory(resultSetFactory);
|
81
|
factory.setServiceResolver(serviceResolver);
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* test invalid xslt.
|
86
|
*
|
87
|
* @throws TransformerConfigurationException
|
88
|
* could happen
|
89
|
*/
|
90
|
@Test(expected = IllegalStateException.class)
|
91
|
public void testInvalidXslt() throws TransformerConfigurationException {
|
92
|
factory.createMappedResultSet(null, "<bla/>");
|
93
|
}
|
94
|
|
95
|
/**
|
96
|
* test xslt.
|
97
|
*
|
98
|
* @throws TransformerConfigurationException
|
99
|
* could happen
|
100
|
* @throws IOException
|
101
|
* could happen
|
102
|
* @throws ResultSetException mock
|
103
|
*/
|
104
|
@Test
|
105
|
public void testXslt() throws TransformerConfigurationException, IOException, ResultSetException {
|
106
|
when(serviceResolver.getService(eq(ResultSetService.class), (W3CEndpointReference) anyObject())).thenReturn(resultSetService);
|
107
|
when(serviceResolver.getResourceIdentifier((W3CEndpointReference) anyObject())).thenReturn("123");
|
108
|
|
109
|
//when(resultSetService.getNumberOfElements(anyString())).thenReturn(1);
|
110
|
when(resultSetService.getResult("123", 1, 1, "waiting")).thenReturn(Lists.newArrayList("<first>something</first>"));
|
111
|
when(resultSetService.getRSStatus("123")).thenReturn("closed");
|
112
|
|
113
|
when(resultSet.isOpen()).thenReturn(true);
|
114
|
|
115
|
final StringWriter xsltSource = new StringWriter();
|
116
|
IOUtils.copy(getClass().getResourceAsStream("test.xsl"), xsltSource);
|
117
|
factory.createMappedResultSet(null, xsltSource.toString());
|
118
|
|
119
|
verify(resultSetFactory, times(1)).createResultSet(argThat(new ArgumentMatcher<ResultSetListener>() {
|
120
|
|
121
|
@Override
|
122
|
public boolean matches(final ResultSetListener argument) {
|
123
|
final MappedResultSet listener = (MappedResultSet) argument;
|
124
|
listener.setResultSet(resultSet);
|
125
|
final List<String> res = listener.getResult(1, 1);
|
126
|
|
127
|
assertNotNull("null result", res);
|
128
|
assertFalse("empty result", res.isEmpty());
|
129
|
|
130
|
assertNotNull("null element", res.get(0));
|
131
|
|
132
|
assertTrue("transformed correctly", res.get(0).contains("<second>something</second>"));
|
133
|
|
134
|
return true;
|
135
|
}
|
136
|
}));
|
137
|
}
|
138
|
|
139
|
}
|