Search in sources :

Example 1 with ResourceUtil

use of com.github.hakko.musiccabinet.util.ResourceUtil in project musiccabinet by hakko.

the class ArtistTopTracksServiceTest method getArtistTopTracksClient.

@SuppressWarnings("unchecked")
private ArtistTopTracksClient getArtistTopTracksClient(WebserviceHistoryService historyService) throws IOException {
    // create a HTTP client that always returns Cher top tracks
    HttpClient httpClient = mock(HttpClient.class);
    ClientConnectionManager connectionManager = mock(ClientConnectionManager.class);
    when(httpClient.getConnectionManager()).thenReturn(connectionManager);
    String httpResponse = new ResourceUtil(CHER_TOP_TRACKS).getContent();
    when(httpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).thenReturn(httpResponse);
    // create a throttling service that allows calls at any rate
    ThrottleService throttleService = mock(ThrottleService.class);
    // create a client that allows all calls and returns Cher top tracks
    ArtistTopTracksClient attClient = new ArtistTopTracksClient();
    attClient.setWebserviceHistoryService(historyService);
    attClient.setHttpClient(httpClient);
    attClient.setThrottleService(throttleService);
    return attClient;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) ResponseHandler(org.apache.http.client.ResponseHandler) ArtistTopTracksClient(com.github.hakko.musiccabinet.ws.lastfm.ArtistTopTracksClient) HttpClient(org.apache.http.client.HttpClient) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 2 with ResourceUtil

use of com.github.hakko.musiccabinet.util.ResourceUtil in project musiccabinet by hakko.

the class ScrobbledTracksServiceTest method getScrobbledTracksClient.

@SuppressWarnings("unchecked")
private ScrobbledTracksClient getScrobbledTracksClient() throws IOException {
    // create a HistoryDao that allows all calls
    WebserviceHistoryService historyService = mock(WebserviceHistoryService.class);
    when(historyService.isWebserviceInvocationAllowed(Mockito.any(WebserviceInvocation.class))).thenReturn(true);
    // create a HTTP client that always returns sampled scrobbled tracks from last.fm
    HttpClient httpClient = mock(HttpClient.class);
    ClientConnectionManager connectionManager = mock(ClientConnectionManager.class);
    when(httpClient.getConnectionManager()).thenReturn(connectionManager);
    String httpResponse = new ResourceUtil(SCROBBLED_TRACKS).getContent();
    when(httpClient.execute(Mockito.any(HttpUriRequest.class), Mockito.any(ResponseHandler.class))).thenReturn(httpResponse);
    // create a client out of the components above
    ScrobbledTracksClient stClient = new ScrobbledTracksClient();
    stClient.setWebserviceHistoryService(historyService);
    stClient.setHttpClient(httpClient);
    // create a throttling service that allows calls at any rate
    ThrottleService throttleService = mock(ThrottleService.class);
    stClient.setThrottleService(throttleService);
    return stClient;
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) ResponseHandler(org.apache.http.client.ResponseHandler) HttpClient(org.apache.http.client.HttpClient) ThrottleService(com.github.hakko.musiccabinet.service.lastfm.ThrottleService) WebserviceInvocation(com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation) ScrobbledTracksClient(com.github.hakko.musiccabinet.ws.lastfm.ScrobbledTracksClient) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager)

Example 3 with ResourceUtil

use of com.github.hakko.musiccabinet.util.ResourceUtil in project musiccabinet by hakko.

the class UserTopArtistsServiceTest method updatesTopArtistsWhenImportIsOnlyAllowedForOneUser.

/*
	 * User story: user1 and user2 have (previously imported) top artists.
	 * During import, new data can only be fetched for user1.
	 * Expected: top artists for user1 are updated, while user2 keeps original artists.
	 */
@Test
public void updatesTopArtistsWhenImportIsOnlyAllowedForOneUser() throws Exception {
    PostgreSQLUtil.truncateTables(userTopArtistsDao);
    createArtistInfosAndLocalFiles();
    lastFmDao.createOrUpdateLastFmUser(new LastFmUser(USER1));
    lastFmDao.createOrUpdateLastFmUser(new LastFmUser(USER2));
    LastFmUser user1 = lastFmDao.getLastFmUser(USER1), user2 = lastFmDao.getLastFmUser(USER2);
    LastFmSettingsService lastFmSettingsService = mock(LastFmSettingsService.class);
    when(lastFmSettingsService.getLastFmUsers()).thenReturn(asList(user1, user2));
    UserTopArtistsClient userTopArtistsClient = mock(UserTopArtistsClient.class);
    for (Period period : Period.values()) {
        String fileName = format(TOP_ARTISTS_FILE, period.getDescription());
        when(userTopArtistsClient.getUserTopArtists(user1, period)).thenReturn(new WSResponse(new ResourceUtil(fileName, UTF8).getContent()));
        when(userTopArtistsClient.getUserTopArtists(user2, period)).thenReturn(new WSResponse(false, 403, "Forbidden"));
    }
    UserTopArtistsService userTopArtistsService = new UserTopArtistsService();
    userTopArtistsService.setLastFmSettingsService(lastFmSettingsService);
    userTopArtistsService.setUserTopArtistsClient(userTopArtistsClient);
    userTopArtistsService.setUserTopArtistsDao(userTopArtistsDao);
    userTopArtistsDao.createUserTopArtists(asList(new UserTopArtists(user1, OVERALL, asList(new Artist("M83"))), new UserTopArtists(user2, SIX_MONTHS, asList(new Artist("Zola Jesus")))));
    assertEquals("M83", userTopArtistsService.getUserTopArtists(user1, OVERALL, 0, 10).get(0).getArtistName());
    assertEquals("Zola Jesus", userTopArtistsService.getUserTopArtists(user2, SIX_MONTHS, 0, 10).get(0).getArtistName());
    userTopArtistsService.updateSearchIndex();
    assertEquals(1, userTopArtistsService.getUserTopArtists(user2, SIX_MONTHS, 0, 10).size());
    for (Period period : Period.values()) {
        assertEquals("Expected 50 artists for period " + period, 50, userTopArtistsService.getUserTopArtists(user1, period, 0, 50).size());
    }
}
Also used : Artist(com.github.hakko.musiccabinet.domain.model.music.Artist) UserTopArtistsClient(com.github.hakko.musiccabinet.ws.lastfm.UserTopArtistsClient) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) LastFmUser(com.github.hakko.musiccabinet.domain.model.library.LastFmUser) Period(com.github.hakko.musiccabinet.domain.model.library.Period) UserTopArtists(com.github.hakko.musiccabinet.domain.model.aggr.UserTopArtists) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) Test(org.junit.Test)

Example 4 with ResourceUtil

use of com.github.hakko.musiccabinet.util.ResourceUtil in project musiccabinet by hakko.

the class WSResponseTest method illegalControlCharactersAreChomped1.

/*
	 * Use an authentic response from last.fm, and assert that WSResponse
	 * silently replaces illegal XML characters in it.
	 */
@Test
public void illegalControlCharactersAreChomped1() throws ApplicationException {
    String ctrlCharResponse = new ResourceUtil(CTRL_CHAR_RESPONSE_1).getContent();
    WSResponse response = new WSResponse(ctrlCharResponse);
    // supposed to work, as WSResponse chomps illegal control characters
    new ArtistInfoParserImpl(new StringUtil(response.getResponseBody()).getInputStream());
    try {
        // supposed to fail, as it hasn't passed WSResponse
        new ArtistInfoParserImpl(new ResourceUtil(CTRL_CHAR_RESPONSE_1).getInputStream());
        Assert.fail();
    } catch (ApplicationException e) {
    }
}
Also used : ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) ArtistInfoParserImpl(com.github.hakko.musiccabinet.parser.lastfm.ArtistInfoParserImpl) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) Test(org.junit.Test)

Example 5 with ResourceUtil

use of com.github.hakko.musiccabinet.util.ResourceUtil in project musiccabinet by hakko.

the class WSResponseTest method illegalControlCharactersAreChomped2.

/*
	 * Use an authentic response from last.fm, and assert that WSResponse
	 * silently replaces illegal XML characters in it.
	 */
@Test
public void illegalControlCharactersAreChomped2() throws ApplicationException {
    String ctrlCharResponse = new ResourceUtil(CTRL_CHAR_RESPONSE_2).getContent();
    WSResponse response = new WSResponse(ctrlCharResponse);
    // supposed to work, as WSResponse chomps illegal control characters
    new ArtistTopTracksParserImpl(new StringUtil(response.getResponseBody()).getInputStream());
    try {
        // supposed to fail, as it hasn't passed WSResponse
        new ArtistTopTracksParserImpl(new ResourceUtil(CTRL_CHAR_RESPONSE_2).getInputStream());
        Assert.fail();
    } catch (ApplicationException e) {
    }
}
Also used : ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) ArtistTopTracksParserImpl(com.github.hakko.musiccabinet.parser.lastfm.ArtistTopTracksParserImpl) Test(org.junit.Test)

Aggregations

ResourceUtil (com.github.hakko.musiccabinet.util.ResourceUtil)45 Test (org.junit.Test)25 Before (org.junit.Before)7 HttpClient (org.apache.http.client.HttpClient)6 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)6 Track (com.github.hakko.musiccabinet.domain.model.music.Track)5 ResponseHandler (org.apache.http.client.ResponseHandler)5 File (com.github.hakko.musiccabinet.domain.model.library.File)4 Artist (com.github.hakko.musiccabinet.domain.model.music.Artist)4 ArtistInfo (com.github.hakko.musiccabinet.domain.model.music.ArtistInfo)4 StringUtil (com.github.hakko.musiccabinet.util.StringUtil)4 WSResponse (com.github.hakko.musiccabinet.ws.lastfm.WSResponse)4 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)4 LastFmUser (com.github.hakko.musiccabinet.domain.model.library.LastFmUser)3 MBArtist (com.github.hakko.musiccabinet.domain.model.music.MBArtist)3 ArtistTopTracksParserImpl (com.github.hakko.musiccabinet.parser.lastfm.ArtistTopTracksParserImpl)3 UnittestLibraryUtil.getFile (com.github.hakko.musiccabinet.util.UnittestLibraryUtil.getFile)3 ArrayList (java.util.ArrayList)3 UserLovedTracks (com.github.hakko.musiccabinet.domain.model.aggr.UserLovedTracks)2 UserStarredTrack (com.github.hakko.musiccabinet.domain.model.aggr.UserStarredTrack)2