1   package org.musicontroller.test.db;
2   
3   import java.util.ArrayList;
4   import java.util.Calendar;
5   import java.util.GregorianCalendar;
6   import java.util.HashMap;
7   import java.util.HashSet;
8   import java.util.Iterator;
9   import java.util.List;
10  import java.util.Map;
11  
12  import org.junit.Test;
13  import static org.junit.Assert.*;
14  
15  import org.musicontroller.core.AIBag;
16  import org.musicontroller.core.AIRelation;
17  import org.musicontroller.core.Artist;
18  import org.musicontroller.core.Band;
19  import org.musicontroller.core.Event;
20  import org.musicontroller.core.Instrument;
21  import org.musicontroller.core.Keyword;
22  import org.musicontroller.core.Keywordbag;
23  import org.musicontroller.core.Playlist;
24  import org.musicontroller.core.Song;
25  import org.musicontroller.dao.BagAndKeywordUtils;
26  import org.musicontroller.security.IUser;
27  import org.musicontroller.test.HSQLTestCase;
28  
29  
30  /**
31   * @author Varienaja
32   */
33  public class DaoTest extends HSQLTestCase {
34  	
35  	@Test
36  	public void testGetSongIds() {
37  		List<Long> songids = getDao().getSongIds();
38  		assertNotNull(songids);
39  		assertTrue(songids.size()>0);
40  	}
41  	
42  	@Test
43  	public void testGetSongById() {
44  		Song song = getDao().getSongById(TestSongId1);
45  		assertNotNull(song);
46  		song = getDao().getSongById(-1);
47  		assertNull(song);
48  	}
49  	
50  	@Test
51  	public void testGetBandById() {
52  		Band band = getDao().getBandById(TestBandId);
53  		assertNotNull(band);
54  		band = getDao().getBandById(-1);
55  		assertNull(band);
56  	}
57  
58  	@Test
59  	public void testGetKeywordById() {
60  		Keyword k = getDao().getKeywordById(TestKeywordId1);
61  		assertNotNull("The test keyword should be present.",k);
62  		k = getDao().getKeywordById(Long.MAX_VALUE);
63  		assertNull("This keyword does not exist in the test database.",k);
64  		k = getDao().getKeywordById(Long.MIN_VALUE);
65  		assertNull("This keyword does not exist in the test database.",k);
66  	}
67  
68  	@Test
69  	public void testGetKeywordbagById() {
70  		// There are no keyword bags in the initial test database.
71  		Keywordbag kw = getDao().getKeywordBagById(1);
72  		assertNull(kw);
73  		// Now create a keyword bag. This will receive id 1. Retrieve it.
74  		BagAndKeywordUtils.getKeywordBag("a,b");
75  		kw = getDao().getKeywordBagById(1);
76  		assertNotNull(kw);
77  	}
78  
79  	@Test
80  	public void testGetArtistById() {
81  		Artist artist = getDao().getArtistById(TestArtistId);
82  		assertNotNull(artist);
83  		artist = getDao().getArtistById(-1);
84  		assertNull(artist);
85  	}
86  
87  	@Test
88  	public void testGetAiBagById() {
89  		// The test database contains no ai_bags objects.
90  		AIBag bag = getDao().getAIBagById(1);
91  		assertNull(bag);
92  		// Now create a bag. This should receive id 1.
93  		BagAndKeywordUtils.getAIBag(new HashSet<AIRelation>());
94  		bag = getDao().getAIBagById(1);
95  		assertNotNull("The new AIBag should be retrievable.",bag);
96  		assertEquals("The new AIBag should be persistent, with a real database id.",1,bag.getId());
97  	}
98  	
99  	@Test
100 	public void testGetInstrumentById() {
101 		Instrument instrument = getDao().getInstrumentById(TestInstrumentId);
102 		assertNotNull(instrument);
103 		instrument = getDao().getInstrumentById(-1);
104 		assertNull(instrument);
105 	}
106 	
107 	@Test
108 	public void testGetSong() {
109 		Band band = getDao().getBandById(TestBandId);
110 		Song song = getDao().getSong(band, TestSongName1);
111 		assertNotNull(song);
112 		song = getDao().getSong(null, TestSongName1);
113 		assertNull(song);
114 		song = getDao().getSong(band, null);
115 		assertNull(song);
116 	}
117 	
118 	@Test
119 	public void testSongsByBand() {
120 		Playlist playlist = getDao().songsByBand(TestBandId);
121 		assertTrue(playlist.getSongs().size()>0);
122 		playlist = getDao().songsByBand(-1);
123 		assertEquals(0,playlist.getSongs().size());
124 	}
125 	
126 	@Test
127 	public void testSearchBand() {
128 		Band band = getDao().searchBand(TestBandName);
129 		assertNotNull(band);
130 		band = getDao().searchBand(null);
131 		assertNull(band);
132 	}
133 	
134 	@Test
135 	public void testSearchPlaylist() {
136 		Playlist playlist = getDao().songsByBand(TestBandId);
137 		assertNotNull(playlist);
138 		playlist.setName("test");
139 		getDao().save(playlist);
140 		List<Playlist> hits = getDao().searchPlaylist(playlist.getName());
141 		assertNotNull(hits);
142 		assertTrue(hits.size()>0);
143 		assertTrue(hits.contains(playlist));
144 		hits = getDao().searchPlaylist(null);
145 		assertNotNull(hits);
146 		assertTrue(hits.size()==0);
147 	}
148 	
149 	/**
150 	 * Selects all songs joined with their performing bands.
151 	 * The number of results should equal the number of songs.
152 	 */
153 	@SuppressWarnings("unchecked")
154 	@Test
155 	public void testBandsListerHql() {
156 		IUser user = getDao().getUserById(persistentUserId);
157 		Calendar lastYear = new GregorianCalendar();
158 		lastYear.add(Calendar.YEAR,-1);
159 
160 		String hql = "from Band b";
161 		List result = getDao().search(hql, null, 0);
162 		int totalBands = result.size();
163 		
164 		hql = "from Song s join s.band b left outer join s.events ev" +
165 				" with ev.moment>:startdate and ev.eventkind=:kind and ev.user.id=:userid";
166 		Map<String,Object> params = new HashMap<String,Object>();
167 		params.put("userid", user.getId());
168 		params.put("kind", Event.played);
169 		params.put("startdate",lastYear.getTime());
170 
171 		result = getDao().search(hql, params, 0);
172 		System.out.println("list size: "+result.size()+", total bands: "+totalBands);
173 		assertEquals("All songs should be included in the result",7,result.size());
174 	}
175 	
176 	@Test
177 	public void testListBands() {
178 		List<Object[]> bandInfos = getDao().listBands(getDao().getUserById(persistentUserId));
179 		assertNotNull("The list of band information should not be null.",bandInfos);
180 		assertNotNull(bandInfos);
181 		assertTrue(bandInfos.size()>0);
182 		bandInfos = getDao().listBands(null);
183 		assertNotNull("The list for the NULL user should not be null.",bandInfos);
184 		assertEquals("The list of band information should contain 3 entries",3,bandInfos.size());
185 		Iterator<Object[]> it = bandInfos.iterator();
186 		while(it.hasNext()) {
187 			Object[] elt = it.next();
188 			// The first element must be long, the second String and the third an integer.
189 			@SuppressWarnings("unused")
190 			Long e1 = (Long) elt[0];
191 			@SuppressWarnings("unused")
192 			String e2 = (String) elt[1];
193 			@SuppressWarnings("unused")
194 			Long e3 = (Long)elt[2];
195 		}
196 	}
197 
198 	@SuppressWarnings("unchecked")
199 	@Test
200 	public void testListKeywords() {
201 		IUser user = getDao().getUserById(persistentUserId);
202 		// Construct a keyword bag on song 1.
203 		Song s = getDao().getSongById(TestSongId1);
204 		Keyword kw = getDao().searchKeyword("Rock");
205 		assertNotNull("The Rock keyword should exist.",kw);
206 		BagAndKeywordUtils.addKeywordToSong(s, kw);
207 		getDao().save(s);
208 				
209 		List results = getDao().search("from Keywordbag kwb", null, 0);
210 		System.out.println("There are "+results.size()+" keyword bags.");
211 		assertEquals("There should be one keyword bag now.",1,results.size());
212 				
213 		// Construct a keyword bag on song 2, containing two keywords..
214 		Song s2 = getDao().getSongById(TestSongId2);
215 		Keyword kw2 = getDao().searchKeyword("Reggae");
216 		assertNotNull("Test song 2 should exist.",s2);
217 		assertNotNull("The Reggae keyword should exist.",kw2);
218 		BagAndKeywordUtils.addKeywordToSong(s2, kw);
219 		BagAndKeywordUtils.addKeywordToSong(s2, kw2);
220 		getDao().save(s);
221 		
222 		List<Object[]> result = getDao().listKeywords(user, null);
223 		assertNotNull("The result should not be null.",result);
224 		assertEquals("All Keywords existing in the keyword bag (Rock, Reggae) should be in the list.",2,result.size());
225 
226 		// filter the result on the keyword bag having only "Rock" in it. The result should
227 		// contain just one result, for Rock.
228 		Keywordbag filter = s.getKeywordbag();
229 		List<Keywordbag> filterList = new ArrayList<Keywordbag>();
230 		filterList.add(filter);
231 		result = getDao().listKeywords(user, filterList);
232 		assertNotNull("The result should not be null.",result);
233 		assertEquals("Only Keywords existing in the filter bag (Rock) should be in the list.",1,result.size());		
234 	}
235 	
236 	@Test
237 	public void testListKeywordsbags() {
238 		List<Keyword> param = new ArrayList<Keyword>();
239 		
240 		// Construct a keyword bag on song 1.
241 		Song s = getDao().getSongById(TestSongId1);
242 		Keyword kw = getDao().searchKeyword("Rock");
243 		assertNotNull("The Rock keyword should exist.",kw);
244 		BagAndKeywordUtils.addKeywordToSong(s, kw);
245 		getDao().save(s);
246 		Keywordbag withoutSoul = s.getKeywordbag();
247 		param.add(kw);
248 		List<Keywordbag> result = getDao().listKeywordsBags(param);
249 		assertNotNull("The result should contain the keyword bag of song 1",result);
250 		assertEquals("The result should contain the keyword bag of song 1",1,result.size());
251 		
252 		// Assign another keyword "Soul" to the song. Ask for a list of all keyword bags
253 		// containing the second keyword. The keyword bag not including "Soul" should not
254 		// be in the result.
255 		Keyword kw2 = getDao().searchKeyword("Soul");
256 		assertNotNull("The Soul keyword should be present in the test database!", kw2);
257 		BagAndKeywordUtils.addKeywordToSong(s, kw2);
258 		getDao().save(s);
259 		
260 		param.clear();
261 		param.add(kw2);
262 		result = getDao().listKeywordsBags(param);
263 		assertTrue("The keyword bag of song should be included.",result.contains(s.getKeywordbag()));
264 		assertFalse("The keyword bag not containing 'Soul' should not be included.", result.contains(withoutSoul));
265 
266 		result = getDao().listKeywordsBags(null);
267 		assertNull("The result should be null if the parameter is null.",result);
268 		result = getDao().listKeywordsBags(new ArrayList<Keyword>());
269 		assertNull("The result should be null if the parameter is empty.",result);		
270 	}
271 	
272 	@Test
273 	public void testSearchKeyword() {
274 		Keyword rock = getDao().searchKeyword("rock");
275 		Keyword Rock = getDao().searchKeyword("Rock   ");
276 		assertNotNull(rock);
277 		assertNotNull(Rock);
278 		assertEquals(rock, Rock);
279 		rock = getDao().searchKeyword(null);
280 		assertNull(rock);
281 	}
282 
283 	@Test
284 	public void testSearchArtist() {
285 		Artist artist = new Artist();
286 		artist.setFirstname(null);
287 		artist.setLastname("Asheton");
288 		getDao().save(artist);
289 		Artist found = getDao().searchArtist(null, "Asheton");
290 		assertNotNull(found);
291 		assertEquals(artist,found);
292 	}
293 	
294 	@Test
295 	public void testMergePlaylists() {
296 		getDao().mergePlaylists(null, null);
297 	}	
298 	
299 	@Test
300 	public void testDeleteArtist() {
301 		Artist artist = new Artist();
302 		artist.setFirstname(null);
303 		artist.setLastname("Asheton");
304 		getDao().save(artist);
305 		long artid = artist.getId();
306 		// Delete the artist.
307 		getDao().deleteArtist(artist);
308 		// The artist should be gone now.
309 		artist = getDao().getArtistById(artid);
310 		assertNull("The artist should not exist anymore.",artist);
311 		getDao().deleteArtist(null);
312 	}
313 	
314 	@Test
315 	public void testDeleteAiBag() {
316 		AIBag bag = BagAndKeywordUtils.getAIBag(BagAndKeywordUtils.createRelations("Hans", "Drexler", getDao().getBandById(TestBandId), "a,b"));
317 		long bagid = bag.getId();
318 		assertNotNull("An Ai bag should have been returned.",bag);
319 		assertTrue("The ai bag should be persistent",bagid>0);
320 		// Delete the bag.
321 		getDao().deleteAiBag(bag);
322 		// The artist should be gone now.
323 		bag = getDao().getAIBagById(bagid);
324 		assertNull("The ai bag should not exist anymore.",bag);
325 		getDao().deleteAiBag(null);
326 	}
327 
328 }