Project

General

Profile

1
package eu.dnetlib.enabling.resultset.push;
2

    
3
import static org.junit.Assert.*; // NOPMD
4
import static org.mockito.Mockito.*; // NOPMD
5

    
6
import java.util.ArrayList;
7
import java.util.List;
8

    
9
import org.junit.Before;
10
import org.junit.Test;
11
import org.junit.runner.RunWith;
12
import org.mockito.Mock;
13
import org.mockito.junit.MockitoJUnitRunner;
14

    
15
import com.google.common.collect.Lists;
16

    
17
import eu.dnetlib.enabling.resultset.ResultSetRegistry;
18

    
19
/**
20
 * test the transient push resultset.
21
 *
22
 * @author marko
23
 *
24
 */
25
@RunWith(MockitoJUnitRunner.class)
26
public class TransientPushResultSetImplTest {
27

    
28
	/**
29
	 * first test value.
30
	 */
31
	private static final String ONE = "one";
32

    
33
	/**
34
	 * second test value.
35
	 */
36
	private static final String TWO = "two";
37

    
38
	/**
39
	 * test rs id.
40
	 */
41
	private static final String RS_ID = "123";
42

    
43
	/**
44
	 * instance to be tested.
45
	 */
46
	private transient TransientPushResultSetImpl resultSet;
47

    
48
	/**
49
	 * dao mock.
50
	 */
51
	@Mock
52
	private transient TransientPushResultSetDao dao;
53

    
54
	/**
55
	 * resultset registry mock.
56
	 */
57
	@Mock
58
	private transient ResultSetRegistry registry;
59

    
60
	/**
61
	 * setup class to be tested.
62
	 *
63
	 * @throws Exception
64
	 */
65
	@Before
66
	public void setUp() {
67
		resultSet = new TransientPushResultSetImpl(dao);
68
		resultSet.setIdentifier(RS_ID);
69
		resultSet.getDao(); // getter code coverage hack
70
	}
71

    
72
	/**
73
	 * test adding elements.
74
	 */
75
	@Test
76
	public void testAddElements() {
77
		final List<String> list = new ArrayList<String>();
78
		list.add(ONE);
79

    
80
		resultSet.addElements(list);
81

    
82
		verify(dao).addElements(RS_ID, list);
83
		assertNotNull("dummy", resultSet);
84
	}
85

    
86
	/**
87
	 * test get number of results.
88
	 */
89
	@Test
90
	public void testGetNumberOfResults() {
91
		when(dao.getSize(RS_ID)).thenReturn(1);
92

    
93
		assertEquals("check size", 1, resultSet.getNumberOfResults());
94
	}
95

    
96
	/**
97
	 * get result.
98
	 */
99
	@Test
100
	public void testGetResults() {
101
		final List<String> list = new ArrayList<String>();
102
		list.add(TWO);
103

    
104
		when(dao.getSize(RS_ID)).thenReturn(2);
105
		when(dao.getElements(RS_ID, 2, 2)).thenReturn(list);
106

    
107
		assertEquals("check list", TWO, resultSet.getResults(2, 2).get(0));
108
		assertEquals("check size", 1, resultSet.getResults(2, 2).size());
109
	}
110

    
111
	/**
112
	 * test when indices are out of range, trailing stuff should be skipped.
113
	 */
114
	@Test
115
	public void testGetResultOutOfRange() {
116
		final List<String> list = new ArrayList<String>();
117
		list.add(TWO);
118

    
119
		when(dao.getSize(RS_ID)).thenReturn(2);
120
		when(dao.getElements(RS_ID, 2, 2)).thenReturn(list);
121

    
122
		assertEquals("check list", TWO, resultSet.getResults(2, 2 + 1).get(0));
123
		assertEquals("check size", 1, resultSet.getResults(2, 2 + 1).size());
124
	}
125

    
126
	/**
127
	 * open.
128
	 */
129
	@Test
130
	public void testIsOpen() {
131
		assertTrue("check is open by default", resultSet.isOpen());
132
	}
133

    
134
	/**
135
	 * test close.
136
	 */
137
	@Test
138
	public void testClose() {
139
		resultSet.addObserver(registry);
140
		assertEquals("observer should be added", 1, resultSet.countObservers());
141

    
142
		resultSet.destroy();
143
		assertTrue("should be destroyed", resultSet.isDestroyed());
144

    
145
		assertEquals("observers should be cleared", 0, resultSet.countObservers());
146
		verify(registry, times(1)).update(resultSet, null);
147
	}
148

    
149
	@Test
150
	public void testFromAfterSize() {
151
		final List<String> list = new ArrayList<String>();
152
		list.add(TWO);
153

    
154
		when(dao.getSize(RS_ID)).thenReturn(2);
155
		when(dao.getElements(RS_ID, 2, 2)).thenReturn(list);
156

    
157
		resultSet.getResults(3, 2);
158
	}
159

    
160
	@Test(expected = IllegalStateException.class)
161
	public void testWriteClosed() {
162
		resultSet.close();
163
		resultSet.addElements(Lists.newArrayList("test"));
164
	}
165

    
166
}
(5-5/5)