Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.codex.service.social; import com.google.common.collect.HashMultiset; import com.google.common.collect.LinkedHashMultiset; import com.google.common.collect.Lists; import com.google.common.collect.Multiset; import com.google.common.collect.Multisets; import com.seajas.search.codex.service.social.dto.MentionedDto; import com.seajas.search.codex.service.social.dto.ProfileRequestDto; import com.seajas.search.codex.service.social.dto.SocialProfileDto; import com.seajas.search.codex.service.social.dto.SocialProfileResponseDto; import com.seajas.search.codex.service.social.dto.TwitterProfileSummaryDto; import com.seajas.search.media.wsdl.IMediaNotification; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.Spy; import org.mockito.runners.MockitoJUnitRunner; import org.springframework.social.facebook.api.Facebook; import org.springframework.social.facebook.api.FacebookProfile; import org.springframework.social.facebook.api.FqlResult; import org.springframework.social.facebook.api.FqlResultMapper; import org.springframework.social.facebook.api.Reference; import org.springframework.social.facebook.api.impl.FacebookTemplate; import org.springframework.social.twitter.api.Entities; import org.springframework.social.twitter.api.HashTagEntity; import org.springframework.social.twitter.api.MentionEntity; import org.springframework.social.twitter.api.Tweet; import org.springframework.social.twitter.api.TwitterProfile; import java.util.Date; import java.util.List; import java.util.Locale; import static junit.framework.Assert.assertEquals; import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isNull; import static org.mockito.Mockito.anyListOf; import static org.mockito.Mockito.*; @RunWith(MockitoJUnitRunner.class) public class SocialProfileServiceTest { @Mock private IMediaNotification mediaServiceMock; @Mock private SocialFacade socialFacadeMock; @Spy @InjectMocks private SocialProfileService socialProfileService; @Test public void testGetSocialProfileSuggestions() throws Exception { List<String> mediaUrls = Lists.newArrayList("a"); when(mediaServiceMock.storeUrls(anyListOf(String.class), (String) isNull(), eq("image"))) .thenReturn(mediaUrls); List<SocialProfileDto> facebookProfiles = Lists.newArrayList(new SocialProfileDto("1", "1")); doReturn(facebookProfiles).when(socialProfileService).searchFacebookProfiles(anyString(), eq(true)); List<SocialProfileDto> facebookPages = Lists.newArrayList(); doReturn(facebookPages).when(socialProfileService).searchFacebookPages(anyString(), eq(true)); List<SocialProfileDto> twitterProfiles = Lists.newArrayList(new SocialProfileDto("2", "2")); doReturn(twitterProfiles).when(socialProfileService).searchTwitterProfile(anyString(), eq(true)); List<String> persons = Lists.newArrayList("1", "2", "3", "4", "5", "6", "7"); SocialProfileResponseDto suggestions = socialProfileService.getSocialProfileSuggestions(persons); Assert.assertEquals(10, suggestions.count()); } @Test public void shouldGetTwitterProfile() throws Exception { TwitterProfile twitterProfile = new TwitterProfile(1, "screenName", "name", "url", "imageUrl", "desc", "loc", new Date()); when(socialFacadeMock.getTwitterProfiles(Lists.newArrayList(new Long(123456L)))) .thenReturn(Lists.newArrayList(twitterProfile)); List<SocialProfileDto> response = socialProfileService.getTwitterProfile(Lists.newArrayList("123456")); assertEquals(1, response.size()); } @Test public void shouldGetFacebookProfile() throws Exception { FacebookProfile fbp1 = new FacebookProfile("id", "username", "name", "first", "last", "gender", null); List<FacebookProfile> profiles = Lists.newArrayList(fbp1); when(socialFacadeMock.getFacebookProfiles(Lists.newArrayList("123456"))).thenReturn(profiles); List<SocialProfileDto> facebookProfiles = socialProfileService .getFacebookProfile(Lists.newArrayList("123456")); assertEquals(1, facebookProfiles.size()); assertEquals("name", facebookProfiles.get(0).getName()); } @Test public void testRetrieveFacebookProfile() throws Exception { List<String> mediaUrls = Lists.newArrayList("a"); when(mediaServiceMock.storeUrls(anyListOf(String.class), (String) isNull(), eq("image"))) .thenReturn(mediaUrls); ProfileRequestDto requestDto = new ProfileRequestDto(); requestDto.getFacebookProfileIds().add("one"); doReturn(buildDummyResponse()).when(socialProfileService) .getFacebookProfile(requestDto.getFacebookProfileIds()); SocialProfileResponseDto socialProfiles = socialProfileService.getSocialProfiles(requestDto); verify(socialProfileService).getFacebookProfile(requestDto.getFacebookProfileIds()); verify(socialProfileService, never()).getTwitterProfile(requestDto.getTwitterIds()); verify(socialProfileService, never()).getFacebookPage(requestDto.getFacebookPageIds()); assertEquals(1, socialProfiles.getFacebookProfiles().size()); } private List<SocialProfileDto> buildDummyResponse() { SocialProfileDto socialProfileDto = new SocialProfileDto(); socialProfileDto.setId("one"); return Lists.newArrayList(socialProfileDto); } @Test public void testRetrieveFacebookPage() throws Exception { List<String> mediaUrls = Lists.newArrayList("a"); when(mediaServiceMock.storeUrls(anyListOf(String.class), (String) isNull(), eq("image"))) .thenReturn(mediaUrls); ProfileRequestDto requestDto = new ProfileRequestDto(); requestDto.getFacebookPageIds().add("one"); doReturn(buildDummyResponse()).when(socialProfileService).getFacebookPage(requestDto.getFacebookPageIds()); SocialProfileResponseDto socialProfiles = socialProfileService.getSocialProfiles(requestDto); verify(socialProfileService).getFacebookPage(requestDto.getFacebookPageIds()); verify(socialProfileService, never()).getTwitterProfile(requestDto.getTwitterIds()); verify(socialProfileService, never()).getFacebookProfile(requestDto.getFacebookProfileIds()); assertEquals(1, socialProfiles.getFacebookPages().size()); } @Test public void testRetrieveSpecificProfiles() throws Exception { List<String> mediaUrls = Lists.newArrayList("a"); when(mediaServiceMock.storeUrls(anyListOf(String.class), (String) isNull(), eq("image"))) .thenReturn(mediaUrls); ProfileRequestDto requestDto = new ProfileRequestDto(); requestDto.getTwitterIds().add("one"); doReturn(buildDummyResponse()).when(socialProfileService).getTwitterProfile(requestDto.getTwitterIds()); SocialProfileResponseDto socialProfiles = socialProfileService.getSocialProfiles(requestDto); verify(socialProfileService).getTwitterProfile(requestDto.getTwitterIds()); verify(socialProfileService, never()).getFacebookPage(requestDto.getFacebookPageIds()); verify(socialProfileService, never()).getFacebookProfile(requestDto.getFacebookProfileIds()); assertEquals(1, socialProfiles.getTwitterProfiles().size()); } @Test public void testGetSocialProfiles() throws Exception { List<SocialProfileDto> facebookProfiles = Lists.newArrayList(); doReturn(facebookProfiles).when(socialProfileService).searchFacebookProfiles("testPerson", false); List<SocialProfileDto> facebookPages = Lists.newArrayList(); doReturn(facebookPages).when(socialProfileService).searchFacebookPages("testPerson", false); List<SocialProfileDto> twitterProfiles = Lists.newArrayList(); doReturn(twitterProfiles).when(socialProfileService).searchTwitterProfile("testPerson", false); SocialProfileResponseDto responseDto = socialProfileService.getSocialProfiles("testPerson"); Assert.assertEquals(facebookProfiles, responseDto.getFacebookProfiles()); Assert.assertEquals(facebookPages, responseDto.getFacebookPages()); Assert.assertEquals(twitterProfiles, responseDto.getTwitterProfiles()); } @Test public void testSearchFacebookProfile() throws Exception { Reference ref1 = new Reference("id"); Reference ref2 = new Reference("id2"); FacebookProfile fbp1 = new FacebookProfile("id", "username", "name", "first", "last", "gender", Locale.GERMAN); FacebookProfile fbp2 = new FacebookProfile("id2", "username", "name", "first", "last", "gender", Locale.GERMAN); when(socialFacadeMock.searchFacebookProfiles("testPerson")).thenReturn(Lists.newArrayList(ref1, ref2)); when(socialFacadeMock.getFacebookProfiles(Lists.newArrayList("id", "id2"))) .thenReturn(Lists.newArrayList(fbp1, fbp2)); List<SocialProfileDto> dtos = socialProfileService.searchFacebookProfiles("testPerson", false); List profiles = Lists.newArrayList(SocialProfileDto.translate(fbp1), SocialProfileDto.translate(fbp2)); Assert.assertEquals(profiles, dtos); verify(mediaServiceMock, times(2)).storeUrl(anyString(), (String) isNull(), eq("image")); } @Test public void testSearchFacebookProfileSuggestions() throws Exception { List<Reference> profileReferences = Lists.newArrayList(new Reference("id", "testPerson"), new Reference("id2", "anotherPerson")); when(socialFacadeMock.searchFacebookProfiles("testPerson")).thenReturn(profileReferences); FacebookProfile fbp1 = new FacebookProfile("id", "username", "testPerson", "first", "last", "gender", Locale.GERMAN); FacebookProfile fbp2 = new FacebookProfile("id2", "username", "anotherPerson", "first", "last", "gender", Locale.GERMAN); when(socialFacadeMock.getFacebookProfiles(Lists.newArrayList("id", "id2"))) .thenReturn(Lists.newArrayList(fbp1, fbp2)); List<SocialProfileDto> dtos = socialProfileService.searchFacebookProfiles("testPerson", true); List profiles = Lists.newArrayList(SocialProfileDto.translate(fbp1)); Assert.assertEquals(profiles, dtos); verify(mediaServiceMock).storeUrl(anyString(), (String) isNull(), eq("image")); } @Test public void testSearchFacebookPages() throws Exception { List<Reference> pageReferences = Lists.newArrayList(new Reference("id3", "name3")); FacebookProfile fbp3 = new FacebookProfile("id", "username", "name", "first", "last", "gender", Locale.GERMAN); when(socialFacadeMock.searchFacebookPages("testPerson")).thenReturn(pageReferences); when(socialFacadeMock.getFacebookPages(Lists.newArrayList("id3"))).thenReturn(Lists.newArrayList(fbp3)); List<SocialProfileDto> dtos = socialProfileService.searchFacebookPages("testPerson", false); verify(mediaServiceMock, times(1)).storeUrl(anyString(), (String) isNull(), eq("image")); List profiles = Lists.newArrayList(SocialProfileDto.translate(fbp3)); Assert.assertEquals(profiles, dtos); } @Test public void testGetFacebookPagesSuggestions() throws Exception { List<Reference> pageReferences = Lists.newArrayList(new Reference("id3", "testPerson"), new Reference("id4", "testPerson"), new Reference("id5", "testPerson")); FacebookProfile fbp3 = new FacebookProfile("id", "username", "testPerson", "first", "last", "gender", Locale.GERMAN); FacebookProfile fbp4 = new FacebookProfile("id", "username", "testPerson", "first", "last", "gender", Locale.GERMAN); FacebookProfile fbp5 = new FacebookProfile("id", "username", "testPerson", "first", "last", "gender", Locale.GERMAN); when(socialFacadeMock.searchFacebookPages("testPerson")).thenReturn(pageReferences); when(socialFacadeMock.getFacebookPages(Lists.newArrayList("id3", "id4", "id5"))) .thenReturn(Lists.newArrayList(fbp3, fbp4, fbp5)); List<SocialProfileDto> dtos = socialProfileService.searchFacebookPages("testPerson", true); verify(mediaServiceMock, times(2)).storeUrl(anyString(), (String) isNull(), eq("image")); List profiles = Lists.newArrayList(SocialProfileDto.translate(fbp3), SocialProfileDto.translate(fbp4)); Assert.assertEquals(profiles, dtos); } @Test public void shouldAddProfilePictures() throws Exception { SocialProfileDto socialProfileDto = new SocialProfileDto(); socialProfileDto.setId("id"); SocialProfileDto socialProfileDto2 = new SocialProfileDto(); socialProfileDto2.setId("id2"); SocialProfileDto socialProfileDto3 = new SocialProfileDto(); socialProfileDto3.setId("id3"); List<SocialProfileDto> list = Lists.newArrayList(socialProfileDto, socialProfileDto2, socialProfileDto3); List<String> imageList = Lists.newArrayList("http://graph.facebook.com/id/picture", "http://graph.facebook.com/id2/picture", "http://graph.facebook.com/id3/picture"); List<String> mocks = Lists.newArrayList("mockurl", "mockurl2", "mockurl3"); doReturn(mocks).when(socialProfileService).storeImagesOnMediaServer(imageList); List<SocialProfileDto> socialProfileDtos = socialProfileService.addFacebookPictures(list); assertEquals("id", socialProfileDtos.get(0).getId()); assertEquals("http://graph.facebook.com/id/picture", socialProfileDtos.get(0).getProfileImageUrl()); assertEquals("mockurl", socialProfileDtos.get(0).getProfileImageMediaUrl()); } @Test public void testSearchTwitterProfile() throws Exception { // Set up twitter profiles TwitterProfile twitterProfile = new TwitterProfile(1, "screenName", "name", "url", "imageUrl", "desc", "loc", new Date()); TwitterProfile twitterProfile2 = new TwitterProfile(2, "screenName", "name", "url", "imageUrl", "desc", "loc", new Date()); TwitterProfile twitterProfile3 = new TwitterProfile(3, "screenName", "name", "url", "imageUrl", "desc", "loc", new Date()); List<TwitterProfile> twitterProfiles = Lists.newArrayList(twitterProfile, twitterProfile2, twitterProfile3); when(socialFacadeMock.searchTwitter("testPerson")).thenReturn(twitterProfiles); List<SocialProfileDto> dtos = socialProfileService.searchTwitterProfile("testPerson", false); verify(mediaServiceMock, times(3)).storeUrl(anyString(), (String) isNull(), eq("image")); List profiles = Lists.newArrayList(SocialProfileDto.translate(twitterProfile), SocialProfileDto.translate(twitterProfile2), SocialProfileDto.translate(twitterProfile3)); Assert.assertEquals(profiles, dtos); } @Test @Ignore("resolves internet content") public void shouldGetFacebookPage() throws Exception { Facebook f = new FacebookTemplate( "AAAGsPMuLCa0BAAN2TxBMTWDaLFBMY8UJfLqY5R9qycuFcGzTpSD7HBKgaqzfKBxtudxpEeKXXLpypwSBwxD0gZC2gHk6yLXndRGbXJwZDZD"); FqlResultMapper<FacebookProfile> objectFqlResultMapper = new FqlResultMapper<FacebookProfile>() { @Override public FacebookProfile mapObject(FqlResult objectValues) { String id = objectValues.getString("page_id"); String username = objectValues.getString("username"); String name = objectValues.getString("name"); String firstName = objectValues.getString("first_name"); String lastName = objectValues.getString("last_name"); String gender = null; System.out.println(objectValues.getString("about")); return new FacebookProfile(id, username, name, firstName, lastName, gender, null); } }; List<FacebookProfile> query = f.fqlOperations().query( "SELECT page_id, name, pic_small, pic_big, pic_square, pic, username, location, about FROM page where page_id in ('144938055520551', '132168606820734')", objectFqlResultMapper); for (FacebookProfile facebookProfile : query) { SocialProfileDto translate = SocialProfileDto.translate(facebookProfile); System.out.println("translate = " + translate); } } @Test public void testGetTwitterProfileSuggestions() throws Exception { // Set up twitter profiles TwitterProfile twitterProfile0 = new TwitterProfile(0, "screenName", "anotherPerson", "url", "imageUrl", "desc", "loc", new Date()); TwitterProfile twitterProfile1 = new TwitterProfile(1, "screenName", "testPerson", "url", "imageUrl", "desc", "loc", new Date()); TwitterProfile twitterProfile2 = new TwitterProfile(2, "screenName", "testPerson", "url", "imageUrl", "desc", "loc", new Date()); TwitterProfile twitterProfile3 = new TwitterProfile(3, "screenName", "testPerson", "url", "imageUrl", "desc", "loc", new Date()); List<TwitterProfile> twitterProfiles = Lists.newArrayList(twitterProfile0, twitterProfile1, twitterProfile2, twitterProfile3); when(socialFacadeMock.searchTwitter("testPerson")).thenReturn(twitterProfiles); List<SocialProfileDto> dtos = socialProfileService.searchTwitterProfile("testPerson", true); verify(mediaServiceMock, times(2)).storeUrl(anyString(), (String) isNull(), eq("image")); List profiles = Lists.newArrayList(SocialProfileDto.translate(twitterProfile1), SocialProfileDto.translate(twitterProfile2)); Assert.assertEquals(profiles, dtos); } @Test public void testCountTwitterEntities() throws Exception { List<Tweet> tweets = Lists.newArrayList(); List<HashTagEntity> tags = Lists.newArrayList(); List<MentionEntity> mentions = Lists.newArrayList(); Tweet tweet = new Tweet(1, "test", new Date(), "fromuser", "image", 0L, 1, "nl", "source"); tags.add(new HashTagEntity("irn", new int[] { 0, 5 })); tags.add(new HashTagEntity("irn", new int[] { 0, 5 })); mentions.add(new MentionEntity(1, "screenDip", "dipje", new int[] { 10, 20 })); Entities entities = new Entities(null, tags, mentions, null); tweet.setEntities(entities); tweets.add(tweet); Multiset<Long> mentioned = HashMultiset.create(); Multiset<String> hashTags = HashMultiset.create(); socialProfileService.countTwitterEntities(tweets, mentioned, hashTags); assertEquals(2, hashTags.count("irn")); assertEquals(1, mentioned.count(new Long(1))); } @Test public void testBuildTwitterMentionedList() throws Exception { TwitterProfile twitterProfile0 = new TwitterProfile(1, "testPerson", "testPerson", "url", "imageUrl", "desc", "loc", new Date()); TwitterProfile twitterProfile1 = new TwitterProfile(2, "anotherPerson2", "anotherPerson2", "url", "imageUrl", "desc", "loc", new Date()); TwitterProfile twitterProfile2 = new TwitterProfile(3, "anotherPerson", "anotherPerson", "url", "imageUrl", "desc", "loc", new Date()); List<TwitterProfile> twitterProfiles = Lists.newArrayList(twitterProfile2, twitterProfile1, twitterProfile0); when(socialFacadeMock.getTwitterProfiles(Lists.newArrayList(1L, 2L, 3L, 4L))).thenReturn(twitterProfiles); List<String> urls = Lists.newArrayList("imageUrl", "imageUrl", "imageUrl"); List<String> mediaUrls = Lists.newArrayList("mediaUrl", "mediaUrl", "mediaUrl"); when(mediaServiceMock.storeUrls(eq(urls), (String) isNull(), eq("image"))).thenReturn(mediaUrls); Multiset<Long> mentions = LinkedHashMultiset.create(); mentions.add(3L, 5); mentions.add(1L, 10); mentions.add(2L, 7); // Mentioned user that does not exist mentions.add(4L, 1); List<MentionedDto> mentionedDtos = socialProfileService .buildTwitterMentionedList(Multisets.copyHighestCountFirst(mentions)); assertEquals(3, mentionedDtos.size()); Assert.assertEquals("1", mentionedDtos.get(0).getSocialProfile().getId()); Assert.assertEquals("2", mentionedDtos.get(1).getSocialProfile().getId()); Assert.assertEquals("3", mentionedDtos.get(2).getSocialProfile().getId()); } @Test public void testGetTwitterMentions() throws Exception { List<Tweet> tweets = Lists.newArrayList(); Multiset<String> tags = HashMultiset.create(); Multiset<Long> mentions = HashMultiset.create(); doNothing().when(socialProfileService).countTwitterEntities(tweets, mentions, tags); List<MentionedDto> mentionsDtos = Lists.newArrayList(); mentionsDtos.add(new MentionedDto(new SocialProfileDto("id", "name"), 1)); doReturn(mentionsDtos).when(socialProfileService).buildTwitterMentionedList(mentions); TwitterProfileSummaryDto summaryDto = socialProfileService.getTwitterProfileSummary(100); assertEquals(mentionsDtos, summaryDto.getMentions()); } }