use of com.github.hakko.musiccabinet.domain.model.aggr.UserTopArtists in project musiccabinet by hakko.
the class JdbcUserTopArtistsDao method createUserTopArtists.
@Override
public void createUserTopArtists(List<UserTopArtists> userTopArtists) {
if (userTopArtists.size() > 0) {
clearImportTable();
for (UserTopArtists uta : userTopArtists) {
batchInsert(uta.getArtists(), uta.getUser(), uta.getPeriod());
}
updateUserTopArtists();
}
}
use of com.github.hakko.musiccabinet.domain.model.aggr.UserTopArtists 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());
}
}
use of com.github.hakko.musiccabinet.domain.model.aggr.UserTopArtists 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);
}
use of com.github.hakko.musiccabinet.domain.model.aggr.UserTopArtists in project musiccabinet by hakko.
the class JdbcUserTopArtistsDaoTest method loadFunctionDependency.
@Before
public void loadFunctionDependency() throws ApplicationException {
PostgreSQLUtil.loadFunction(dao, UPDATE_USER_TOP_ARTISTS);
arnOverall = new UserTopArtists(arn, Period.OVERALL, new UserTopArtistsParserImpl(new ResourceUtil(ARN_OVERALL_FILE).getInputStream()).getArtists());
arn6month = new UserTopArtists(arn, Period.SIX_MONTHS, new UserTopArtistsParserImpl(new ResourceUtil(ARN_6MONTH_FILE).getInputStream()).getArtists());
sys3month = new UserTopArtists(sys, Period.THREE_MONTHS, new UserTopArtistsParserImpl(new ResourceUtil(SYS_3MONTH_FILE).getInputStream()).getArtists());
createArtistMetaData();
}
use of com.github.hakko.musiccabinet.domain.model.aggr.UserTopArtists in project musiccabinet by hakko.
the class JdbcUserTopArtistsDaoTest method createArtistMetaData.
private void createArtistMetaData() {
Set<Artist> artists = new HashSet<>();
for (UserTopArtists uta : Arrays.asList(arnOverall, arn6month, sys3month)) {
for (Artist artist : uta.getArtists()) {
artists.add(artist);
}
}
List<File> files = new ArrayList<>();
for (Artist artist : artists) {
files.add(getFile(artist.getName(), null, null));
}
List<ArtistInfo> artistInfos = new ArrayList<>();
for (Artist artist : artists) {
artistInfos.add(new ArtistInfo(artist, "/url/to/" + artist.getName()));
}
additionDao.getJdbcTemplate().execute("truncate library.file cascade");
UnittestLibraryUtil.submitFile(additionDao, files);
artistInfoDao.createArtistInfo(artistInfos);
}
Aggregations