Search in sources :

Example 1 with Tweet

use of com.baeldung.models.Tweet in project tutorials by eugenp.

the class DummyContentUtil method generateDummyTweets.

public static final List<Tweet> generateDummyTweets(List<AppUser> users) {
    List<Tweet> tweets = new ArrayList<>();
    Random random = new Random();
    IntStream.range(0, 9).sequential().forEach(i -> {
        Tweet twt = new Tweet(String.format("Tweet %d", i), users.get(random.nextInt(users.size())).getUsername());
        twt.getLikes().addAll(users.subList(0, random.nextInt(users.size())).stream().map(AppUser::getUsername).collect(Collectors.toSet()));
        tweets.add(twt);
    });
    return tweets;
}
Also used : Random(java.util.Random) Tweet(com.baeldung.models.Tweet) ArrayList(java.util.ArrayList) AppUser(com.baeldung.models.AppUser)

Example 2 with Tweet

use of com.baeldung.models.Tweet in project tutorials by eugenp.

the class SpringDataWithSecurityTest method givenAppUser_whenLoginSuccessful_shouldReadMyPagedTweets.

@Test
public void givenAppUser_whenLoginSuccessful_shouldReadMyPagedTweets() {
    AppUser appUser = userRepository.findByUsername("lionel@messi.com");
    Authentication auth = new UsernamePasswordAuthenticationToken(new AppUserPrincipal(appUser), null, DummyContentUtil.getAuthorities());
    SecurityContextHolder.getContext().setAuthentication(auth);
    Page<Tweet> page = null;
    do {
        page = tweetRepository.getMyTweetsAndTheOnesILiked(new PageRequest(page != null ? page.getNumber() + 1 : 0, 5));
        for (Tweet twt : page.getContent()) {
            isTrue((twt.getOwner() == appUser.getUsername()) || (twt.getLikes().contains(appUser.getUsername())), "I do not have any Tweets");
        }
    } while (page.hasNext());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) Authentication(org.springframework.security.core.Authentication) Tweet(com.baeldung.models.Tweet) AppUser(com.baeldung.models.AppUser) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) AppUserPrincipal(com.baeldung.security.AppUserPrincipal) Test(org.junit.Test)

Aggregations

AppUser (com.baeldung.models.AppUser)2 Tweet (com.baeldung.models.Tweet)2 AppUserPrincipal (com.baeldung.security.AppUserPrincipal)1 ArrayList (java.util.ArrayList)1 Random (java.util.Random)1 Test (org.junit.Test)1 PageRequest (org.springframework.data.domain.PageRequest)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1