Search in sources :

Example 6 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException 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 7 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException 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)

Example 8 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException in project musiccabinet by hakko.

the class MusicBrainzServiceTest method handlesArtistFailureDuringUpdate.

@Test
public void handlesArtistFailureDuringUpdate() throws ApplicationException {
    final String revName = reverse(artistName);
    submitFile(additionDao, getFile(revName, albumName, trackName));
    Mockito.when(service.artistQueryClient.get(revName)).thenThrow(new ApplicationException("Fail", new HttpResponseException(503, "Overloaded")));
    service.updateDiscography();
    List<MBAlbum> albums = service.getMissingAlbums(artistName, asList(TYPE_EP), null, -1, 0);
    Assert.assertEquals(2, albums.size());
    assertEquals("Switchblade / Cult of Luna", albums.get(0).getTitle());
    assertEquals("Bodies / Recluse", albums.get(1).getTitle());
}
Also used : ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) HttpResponseException(org.apache.http.client.HttpResponseException) MBAlbum(com.github.hakko.musiccabinet.domain.model.music.MBAlbum) Test(org.junit.Test)

Example 9 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException 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)

Example 10 with ApplicationException

use of com.github.hakko.musiccabinet.exception.ApplicationException in project musiccabinet by hakko.

the class UserTopArtistsService method updateSearchIndex.

@Override
protected void updateSearchIndex() throws ApplicationException {
    List<LastFmUser> users = lastFmSettingsService.getLastFmUsers();
    setTotalOperations(users.size() * Period.values().length);
    List<UserTopArtists> userTopArtists = new ArrayList<>();
    for (LastFmUser user : users) {
        for (Period period : Period.values()) {
            try {
                WSResponse wsResponse = userTopArtistsClient.getUserTopArtists(user, period);
                if (wsResponse.wasCallAllowed() && wsResponse.wasCallSuccessful()) {
                    StringUtil stringUtil = new StringUtil(wsResponse.getResponseBody());
                    UserTopArtistsParser parser = new UserTopArtistsParserImpl(stringUtil.getInputStream());
                    userTopArtists.add(new UserTopArtists(user, period, parser.getArtists()));
                }
            } catch (ApplicationException e) {
                LOG.warn("Fetching top artist for " + user.getLastFmUsername() + ", " + period.getDescription() + " failed.", e);
            }
            addFinishedOperation();
        }
    }
    userTopArtistsDao.createUserTopArtists(userTopArtists);
}
Also used : UserTopArtistsParserImpl(com.github.hakko.musiccabinet.parser.lastfm.UserTopArtistsParserImpl) ApplicationException(com.github.hakko.musiccabinet.exception.ApplicationException) UserTopArtistsParser(com.github.hakko.musiccabinet.parser.lastfm.UserTopArtistsParser) ArrayList(java.util.ArrayList) 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) StringUtil(com.github.hakko.musiccabinet.util.StringUtil)

Aggregations

ApplicationException (com.github.hakko.musiccabinet.exception.ApplicationException)43 Test (org.junit.Test)20 NameValuePair (org.apache.http.NameValuePair)18 StringUtil (com.github.hakko.musiccabinet.util.StringUtil)14 WebserviceInvocation (com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation)11 WSResponse (com.github.hakko.musiccabinet.ws.lastfm.WSResponse)11 Artist (com.github.hakko.musiccabinet.domain.model.music.Artist)10 WebserviceHistoryService (com.github.hakko.musiccabinet.service.lastfm.WebserviceHistoryService)10 ArrayList (java.util.ArrayList)8 LastFmUser (com.github.hakko.musiccabinet.domain.model.library.LastFmUser)7 IOException (java.io.IOException)6 Track (com.github.hakko.musiccabinet.domain.model.music.Track)4 ArtistUserTag (com.github.hakko.musiccabinet.domain.model.aggr.ArtistUserTag)2 Scrobble (com.github.hakko.musiccabinet.domain.model.aggr.Scrobble)2 LastFmGroup (com.github.hakko.musiccabinet.domain.model.library.LastFmGroup)2 Period (com.github.hakko.musiccabinet.domain.model.library.Period)2 Album (com.github.hakko.musiccabinet.domain.model.music.Album)2 MBArtist (com.github.hakko.musiccabinet.domain.model.music.MBArtist)2 Tag (com.github.hakko.musiccabinet.domain.model.music.Tag)2 ArtistInfoParserImpl (com.github.hakko.musiccabinet.parser.lastfm.ArtistInfoParserImpl)2