1
|
package eu.dnetlib.enabling.is.lookup;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.StringReader;
|
5
|
import java.io.StringWriter;
|
6
|
import java.util.Arrays;
|
7
|
import java.util.List;
|
8
|
|
9
|
import eu.dnetlib.enabling.is.store.ISStore;
|
10
|
import eu.dnetlib.enabling.is.store.ISStoreException;
|
11
|
import eu.dnetlib.enabling.tools.XQueryUtilsImpl;
|
12
|
import eu.dnetlib.rmi.enabling.ISLookUpDocumentNotFoundException;
|
13
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
14
|
import org.apache.commons.io.IOUtils;
|
15
|
import org.dom4j.Document;
|
16
|
import org.dom4j.DocumentException;
|
17
|
import org.dom4j.io.SAXReader;
|
18
|
import org.junit.Before;
|
19
|
import org.junit.Test;
|
20
|
import org.junit.runner.RunWith;
|
21
|
import org.mockito.Mock;
|
22
|
import org.mockito.junit.MockitoJUnitRunner;
|
23
|
|
24
|
import static org.junit.Assert.assertEquals;
|
25
|
import static org.junit.Assert.assertNotNull;
|
26
|
import static org.mockito.Matchers.anyString;
|
27
|
import static org.mockito.Mockito.when;
|
28
|
|
29
|
/**
|
30
|
* ISLookUpService test.
|
31
|
*
|
32
|
* @author marko
|
33
|
*
|
34
|
*/
|
35
|
@RunWith(MockitoJUnitRunner.class)
|
36
|
public class ISLookUpServiceImplTest {
|
37
|
|
38
|
/**
|
39
|
* test profile id.
|
40
|
*/
|
41
|
private static final String PROF_ID = "first_c2Vjb25k";
|
42
|
|
43
|
/**
|
44
|
* List of ids.
|
45
|
*/
|
46
|
private static final List<String> ID_LIST = Arrays.asList(new String[] { "fff", "ggg", "hhh" });
|
47
|
|
48
|
/**
|
49
|
* class under test.
|
50
|
*/
|
51
|
private transient ISLookUpServiceImpl lookup;
|
52
|
|
53
|
/**
|
54
|
* is store service mock.
|
55
|
*/
|
56
|
@Mock
|
57
|
private transient ISStore storeService;
|
58
|
|
59
|
/**
|
60
|
* common test case init.
|
61
|
*/
|
62
|
@Before
|
63
|
public void setUp() {
|
64
|
lookup = new ISLookUpServiceImpl();
|
65
|
lookup.setXqueryUtils(new XQueryUtilsImpl());
|
66
|
lookup.setIsStore(storeService);
|
67
|
}
|
68
|
|
69
|
/**
|
70
|
* test get file name from id.
|
71
|
*/
|
72
|
@Test
|
73
|
public void testGetFileNameForId() {
|
74
|
assertEquals("check", "first", lookup.getFileNameForId(PROF_ID));
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* test get file coll from id.
|
79
|
*/
|
80
|
@Test
|
81
|
public void testGetFileCollForId() {
|
82
|
assertEquals("check", "second", lookup.getFileCollForId(PROF_ID));
|
83
|
}
|
84
|
|
85
|
/**
|
86
|
* test get resource profile.
|
87
|
*
|
88
|
* @throws ISLookUpException
|
89
|
* shouldn't happen
|
90
|
* @throws ISStoreException
|
91
|
* shouldn't happen
|
92
|
*/
|
93
|
@Test
|
94
|
public void testGetResourceProfile() throws ISLookUpException, ISStoreException {
|
95
|
when(storeService.getXML("first", "/db/DRIVER/second")).thenReturn("<someResult/>");
|
96
|
|
97
|
assertNotNull("get profile", lookup.getResourceProfile(PROF_ID));
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* test get resource profile when no such document exists.
|
102
|
*
|
103
|
* @throws ISLookUpException
|
104
|
* expected
|
105
|
*/
|
106
|
@Test(expected = ISLookUpDocumentNotFoundException.class)
|
107
|
public void testGetResourceProfileNonExisting() throws ISLookUpException {
|
108
|
lookup.getResourceProfile(PROF_ID);
|
109
|
}
|
110
|
|
111
|
/**
|
112
|
* test getCollection.
|
113
|
*
|
114
|
* @throws IOException
|
115
|
* could happen
|
116
|
* @throws ISStoreException
|
117
|
* could happen
|
118
|
* @throws ISLookUpException
|
119
|
* could happen
|
120
|
* @throws DocumentException
|
121
|
* could happen
|
122
|
*/
|
123
|
@Test
|
124
|
public void testGetCollection() throws IOException, ISStoreException, ISLookUpException, DocumentException {
|
125
|
final StringWriter writer = new StringWriter();
|
126
|
IOUtils.copy(getClass().getResourceAsStream("collProfile.xml"), writer);
|
127
|
final String simpleProf = writer.getBuffer().toString();
|
128
|
|
129
|
when(storeService.getXML(anyString(), anyString())).thenReturn(simpleProf);
|
130
|
when(
|
131
|
storeService.quickSearchXML("for $x in collection('/db/DRIVER/CollectionDSResources') where $x//FATHER/@id = '" + PROF_ID
|
132
|
+ "' return $x//RESOURCE_IDENTIFIER/@value/string()")).thenReturn(ID_LIST);
|
133
|
|
134
|
final String profile = lookup.retrieveCollection(PROF_ID);
|
135
|
final SAXReader reader = new SAXReader();
|
136
|
final Document doc = reader.read(new StringReader(profile));
|
137
|
|
138
|
assertEquals("children", ID_LIST.size(), doc.selectNodes("//CHILDREN/CHILD").size());
|
139
|
for (final String id : ID_LIST) {
|
140
|
assertNotNull("child", doc.selectSingleNode("//CHILDREN/CHILD[@id='" + id + "']"));
|
141
|
}
|
142
|
}
|
143
|
|
144
|
}
|