Project

General

Profile

1
package eu.dnetlib.enabling.is.lookup;
2

    
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertNotNull;
5
import static org.mockito.Matchers.anyString;
6
import static org.mockito.Mockito.when;
7

    
8
import java.io.IOException;
9
import java.io.StringReader;
10
import java.io.StringWriter;
11
import java.util.Arrays;
12
import java.util.List;
13

    
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 eu.dnetlib.enabling.is.lookup.rmi.ISLookUpDocumentNotFoundException;
25
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
26
import eu.dnetlib.enabling.is.store.rmi.ISStoreException;
27
import eu.dnetlib.enabling.is.store.rmi.ISStoreService;
28
import eu.dnetlib.enabling.tools.XQueryUtilsImpl;
29

    
30
/**
31
 * ISLookUpService test.
32
 * 
33
 * @author marko
34
 * 
35
 */
36
@RunWith(MockitoJUnitRunner.class)
37
public class ISLookUpServiceImplTest {
38

    
39
	/**
40
	 * test profile id.
41
	 */
42
	private static final String PROF_ID = "first_c2Vjb25k";
43

    
44
	/**
45
	 * List of ids.
46
	 */
47
	private static final List<String> ID_LIST = Arrays.asList(new String[] { "fff", "ggg", "hhh" });
48

    
49
	/**
50
	 * class under test.
51
	 */
52
	private transient ISLookUpServiceImpl lookup;
53

    
54
	/**
55
	 * is store service mock.
56
	 */
57
	@Mock
58
	private transient ISStoreService storeService;
59

    
60
	/**
61
	 * common test case init.
62
	 */
63
	@Before
64
	public void setUp() {
65
		lookup = new ISLookUpServiceImpl();
66
		lookup.setXqueryUtils(new XQueryUtilsImpl());
67
		lookup.setIsStore(storeService);
68
	}
69

    
70
	/**
71
	 * test get file name from id.
72
	 */
73
	@Test
74
	public void testGetFileNameForId() {
75
		assertEquals("check", "first", lookup.getFileNameForId(PROF_ID));
76
	}
77

    
78
	/**
79
	 * test get file coll from id.
80
	 */
81
	@Test
82
	public void testGetFileCollForId() {
83
		assertEquals("check", "second", lookup.getFileCollForId(PROF_ID));
84
	}
85

    
86
	/**
87
	 * test get resource profile.
88
	 * 
89
	 * @throws ISLookUpException
90
	 *             shouldn't happen
91
	 * @throws ISStoreException
92
	 *             shouldn't happen
93
	 */
94
	@Test
95
	public void testGetResourceProfile() throws ISLookUpException, ISStoreException {
96
		when(storeService.getXML("first", "/db/DRIVER/second")).thenReturn("<someResult/>");
97

    
98
		assertNotNull("get profile", lookup.getResourceProfile(PROF_ID));
99
	}
100

    
101
	/**
102
	 * test get resource profile when no such document exists.
103
	 * 
104
	 * @throws ISLookUpException
105
	 *             expected
106
	 */
107
	@Test(expected = ISLookUpDocumentNotFoundException.class)
108
	public void testGetResourceProfileNonExisting() throws ISLookUpException {
109
		lookup.getResourceProfile(PROF_ID);
110
	}
111

    
112
	/**
113
	 * test getCollection.
114
	 * 
115
	 * @throws IOException
116
	 *             could happen
117
	 * @throws ISStoreException
118
	 *             could happen
119
	 * @throws ISLookUpException
120
	 *             could happen
121
	 * @throws DocumentException
122
	 *             could happen
123
	 */
124
	@Test
125
	public void testGetCollection() throws IOException, ISStoreException, ISLookUpException, DocumentException {
126
		final StringWriter writer = new StringWriter();
127
		IOUtils.copy(getClass().getResourceAsStream("collProfile.xml"), writer);
128
		final String simpleProf = writer.getBuffer().toString();
129

    
130
		when(storeService.getXML(anyString(), anyString())).thenReturn(simpleProf);
131
		when(
132
				storeService.quickSearchXML("for $x in collection('/db/DRIVER/CollectionDSResources') where $x//FATHER/@id = '" + PROF_ID
133
						+ "' return $x//RESOURCE_IDENTIFIER/@value/string()")).thenReturn(ID_LIST);
134

    
135
		final String profile = lookup.retrieveCollection(PROF_ID);
136
		final SAXReader reader = new SAXReader();
137
		final Document doc = reader.read(new StringReader(profile));
138

    
139
		assertEquals("children", ID_LIST.size(), doc.selectNodes("//CHILDREN/CHILD").size());
140
		for (final String id : ID_LIST) {
141
			assertNotNull("child", doc.selectSingleNode("//CHILDREN/CHILD[@id='" + id + "']"));
142
		}
143
	}
144

    
145
}
    (1-1/1)