Search in sources :

Example 1 with WSResponse

use of com.github.hakko.musiccabinet.ws.lastfm.WSResponse 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 2 with WSResponse

use of com.github.hakko.musiccabinet.ws.lastfm.WSResponse in project musiccabinet by hakko.

the class ScrobbleServiceTest method createTestData.

@Before
public void createTestData() throws ApplicationException {
    scrobbleService = new ScrobbleService();
    UpdateNowPlayingClient nowPlayinglient = mock(UpdateNowPlayingClient.class);
    when(nowPlayinglient.updateNowPlaying(Mockito.any(Scrobble.class))).thenReturn(new WSResponse(false, 404, "Not found"));
    scrobbleService.setUpdateNowPlayingClient(nowPlayinglient);
    ScrobbleClient scrobbleClient = mock(ScrobbleClient.class);
    when(scrobbleClient.scrobble(Mockito.any(Scrobble.class))).thenReturn(new WSResponse("<lfm status=\"ok\"></lfm>"));
    scrobbleService.setScrobbleClient(scrobbleClient);
    PlayCountDao playCountDao = mock(PlayCountDao.class);
    scrobbleService.setPlayCountDao(playCountDao);
    MetaData metaData1 = new MetaData();
    metaData1.setArtist("artist 1");
    metaData1.setArtistId(artist1Id);
    metaData1.setAlbum("album 1");
    metaData1.setAlbumId(album1Id);
    track1 = new Track(track1Id, "track 1", metaData1);
    track2 = new Track(track2Id, "track 2", metaData1);
    user1 = new LastFmUser(username1, sessionKey1);
    user2 = new LastFmUser(username2, sessionKey2);
}
Also used : ScrobbleClient(com.github.hakko.musiccabinet.ws.lastfm.ScrobbleClient) MetaData(com.github.hakko.musiccabinet.domain.model.library.MetaData) UpdateNowPlayingClient(com.github.hakko.musiccabinet.ws.lastfm.UpdateNowPlayingClient) LastFmUser(com.github.hakko.musiccabinet.domain.model.library.LastFmUser) Scrobble(com.github.hakko.musiccabinet.domain.model.aggr.Scrobble) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) Track(com.github.hakko.musiccabinet.domain.model.music.Track) PlayCountDao(com.github.hakko.musiccabinet.dao.PlayCountDao) Before(org.junit.Before)

Example 3 with WSResponse

use of com.github.hakko.musiccabinet.ws.lastfm.WSResponse in project musiccabinet by hakko.

the class TagUpdateServiceTest method createTestData.

@Before
public void createTestData() throws ApplicationException {
    responseOK = new WSResponse("<lfm status=\"ok\"></lfm>");
    responseFail = new WSResponse(false, 404, "Not found");
}
Also used : WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) Before(org.junit.Before)

Example 4 with WSResponse

use of com.github.hakko.musiccabinet.ws.lastfm.WSResponse in project musiccabinet by hakko.

the class JdbcUserRecommendedArtistsDaoTest method loadFunctionDependency.

@Before
public void loadFunctionDependency() throws ApplicationException {
    PostgreSQLUtil.loadFunction(dao, UPDATE_USER_RECOMMENDED_ARTISTS);
    joanRec = new UserRecommendedArtists(joan, new UserRecommendedArtistsParserImpl(new ResourceUtil(JOAN_FILE).getInputStream()).getArtists());
    rjRec = new UserRecommendedArtists(rj, new UserRecommendedArtistsParserImpl(new ResourceUtil(RJ_FILE).getInputStream()).getArtists());
    String body = new WSResponse(new ResourceUtil(FTPAREA_FILE).getContent()).getResponseBody();
    ftpareaRec = new UserRecommendedArtists(ftparea, new UserRecommendedArtistsParserImpl(new StringUtil(body).getInputStream()).getArtists());
    createArtistMetaData();
}
Also used : UserRecommendedArtists(com.github.hakko.musiccabinet.domain.model.aggr.UserRecommendedArtists) ResourceUtil(com.github.hakko.musiccabinet.util.ResourceUtil) UserRecommendedArtistsParserImpl(com.github.hakko.musiccabinet.parser.lastfm.UserRecommendedArtistsParserImpl) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse) StringUtil(com.github.hakko.musiccabinet.util.StringUtil) Before(org.junit.Before)

Example 5 with WSResponse

use of com.github.hakko.musiccabinet.ws.lastfm.WSResponse in project musiccabinet by hakko.

the class UpdateNowPlayingService method receive.

@SuppressWarnings("unchecked")
public void receive() {
    while (true) {
        Message<Scrobble> message = (Message<Scrobble>) scrobbleChannel.receive();
        if (message == null || message.equals(FINISHED_MESSAGE)) {
            break;
        } else {
            try {
                LOG.debug("Try updating now playing.");
                Scrobble scrobble = message.getPayload();
                Scrobble previous = getPrevious(scrobble);
                LOG.debug("previous: " + previous + ", scrobble = " + scrobble);
                if (previous != null && tooClose(scrobble, previous) && scrobble.getTrack().getId() == previous.getTrack().getId()) {
                    LOG.debug("Same track was scrobbled just recently, ignore.");
                } else {
                    addScrobble(scrobble);
                    WSResponse wsResponse = client.updateNowPlaying(message.getPayload());
                    LOG.debug("Successful: " + wsResponse.wasCallSuccessful());
                    LOG.debug("Response: " + wsResponse.getResponseBody());
                }
            } catch (ApplicationException e) {
                LOG.warn("Could not update now playing at last.fm.", e);
            }
        }
    }
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) Message(org.springframework.integration.Message) Scrobble(com.github.hakko.musiccabinet.domain.model.aggr.Scrobble) WSResponse(com.github.hakko.musiccabinet.ws.lastfm.WSResponse)

Aggregations

WSResponse (com.github.hakko.musiccabinet.ws.lastfm.WSResponse)25 StringUtil (com.github.hakko.musiccabinet.util.StringUtil)15 ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)11 ArrayList (java.util.ArrayList)8 LastFmUser (com.github.hakko.musiccabinet.domain.model.library.LastFmUser)7 Scrobble (com.github.hakko.musiccabinet.domain.model.aggr.Scrobble)5 Artist (com.github.hakko.musiccabinet.domain.model.music.Artist)5 ResourceUtil (com.github.hakko.musiccabinet.util.ResourceUtil)4 Track (com.github.hakko.musiccabinet.domain.model.music.Track)3 Before (org.junit.Before)3 ArtistUserTag (com.github.hakko.musiccabinet.domain.model.aggr.ArtistUserTag)2 UserRecommendedArtists (com.github.hakko.musiccabinet.domain.model.aggr.UserRecommendedArtists)2 UserTopArtists (com.github.hakko.musiccabinet.domain.model.aggr.UserTopArtists)2 Period (com.github.hakko.musiccabinet.domain.model.library.Period)2 UserRecommendedArtistsParserImpl (com.github.hakko.musiccabinet.parser.lastfm.UserRecommendedArtistsParserImpl)2 Test (org.junit.Test)2 PlayCountDao (com.github.hakko.musiccabinet.dao.PlayCountDao)1 GroupWeeklyArtistChart (com.github.hakko.musiccabinet.domain.model.aggr.GroupWeeklyArtistChart)1 TagTopArtists (com.github.hakko.musiccabinet.domain.model.aggr.TagTopArtists)1 UserLovedTracks (com.github.hakko.musiccabinet.domain.model.aggr.UserLovedTracks)1