<commit_before>package ru.pmsoft.twitterkiller.rest;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import ru.pmsoft.twitterkiller.domain.dto.TweetOutput;
import ru.pmsoft.twitterkiller.domain.entity.UserSession;
import ru.pmsoft.twitterkiller.domain.entity.Tweet;
import ru.pmsoft.twitterkiller.domain.entity.User;
import ru.pmsoft.twitterkiller.domain.repository.SessionRepository;
import ru.pmsoft.twitterkiller.domain.repository.TweetRepository;
import ru.pmsoft.twitterkiller.domain.repository.UserRepository;
import ru.pmsoft.twitterkiller.rest.exceptions.ClientException;
import ru.pmsoft.twitterkiller.rest.exceptions.ExceptionBody;

import javax.ws.rs.core.Response;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.testng.AssertJUnit.assertTrue;

public class TweetResourceTestCase {

    private static TweetResource createSystemUnderTest(UserRepository repository,
                                                      SessionRepository sessionRepository,
                                                      TweetRepository tweetRepository) {
        return new TweetResource(repository==null ? mock(UserRepository.class) : repository,
                sessionRepository == null ? mock(SessionRepository.class) : sessionRepository,
                tweetRepository == null ? mock(TweetRepository.class) : tweetRepository);
    }

    @Test (dataProvider = "invalidDataSourceForAddTweet")
    public void addTweet_whenTextOfTweetIsNullOrEmpty_shouldThrowClientException(String token, String text) {
        TweetResource sut = createSystemUnderTest(null, null, null);
        try{
             sut.addTweet(token, text);
        }
        catch (ClientException ex)
        {
            assertEquals(ex.getResponse().getEntity(),
                    new ExceptionBody("Tweet can not be empty or less than 140 letters"));
            return;
        }
        fail();

    }
    @DataProvider
    public Object[][] invalidDataSourceForAddTweet()
    {
        return new Object[][]
                {
                  new Object[]{"foo", null},
                        new Object[]{"foo", "             "},
                        new Object[]{"foo", createLongString()}
                };
    }


    @Test
    public void addTweet_shouldWorkWell()
    {
        SessionRepository sessionRepository =  mock(SessionRepository.class);
        UserRepository userRepository = mock(UserRepository.class);
        User user = mock(User.class);
        when(userRepository.getById(any(Integer.class))).thenReturn(user);
        UserSession session = mock(UserSession.class);
        when((sessionRepository.getByToken(any(String.class)))).thenReturn(session);
        when(session.isExpired()).thenReturn(false);
        when(user.getId()).thenReturn(0);

        TweetResource sut = createSystemUnderTest
                (userRepository,
                sessionRepository,
                null);

        Response response = sut.addTweet("foo", "bar");
        assertEquals((String)response.getEntity(), "Tweet is saved");
    }


    @Test
    public void allTweets_whenUsernameIsEmpty_shouldThrowClientException() {
        TweetResource sut = createSystemUnderTest(mock(UserRepository.class),
                        null,
                        null);
        try {
            sut.allTweets("foo", "");
        }
        catch (ClientException ex) {
            assertEquals(ex.getResponse().getEntity(), new ExceptionBody("Login can not be empty"));
            return;
        }
        fail();
    }

    @Test (dataProvider = "InvalidReturnForDifferentTesting")
    public void alltestingmethods_whenSessionIsExpired_shouldThrowClientException(String method)
    {
        SessionRepository sessionRepository =  mock(SessionRepository.class);
        UserSession session = mock(UserSession.class);
        when((sessionRepository.getByToken(any(String.class)))).thenReturn(session);
        when(session.isExpired()).thenReturn(true);
        TweetResource sut = createSystemUnderTest(null,
                sessionRepository,
                null);

        try{
            switch(method)
            {
                case "addTweet": sut.addTweet("foo", "bar"); break;
                case "getTweet":  sut.getTweet("foo", 1); break;
                case "allTweets": sut.allTweets("foo", "bar"); break;
            }
        }
        catch (ClientException ex)
        {
            assertEquals(ex.getResponse().getEntity(),
                    new ExceptionBody("Your token is invalid"));
            return;
        }
        fail();
    }

    @Test (dataProvider = "InvalidReturnForDifferentTesting")
    public void alltestingmethods_whenSessionIsNull_shouldThrowClientException(String method)
    {
        SessionRepository sessionRepository =  mock(SessionRepository.class);
        UserSession session = null;
        when((sessionRepository.getByToken(any(String.class)))).thenReturn(session);
        TweetResource sut = createSystemUnderTest(null,
                sessionRepository,
                null);

        try{
            switch(method)
            {
                case "addTweet": sut.addTweet("foo", "bar"); break;
                case "getTweet":  sut.getTweet("foo", 1); break;
                case "allTweets": sut.allTweets("foo", "bar"); break;
            }
        }
        catch (ClientException ex)
        {
            assertTrue(ex.getResponse().getEntity().
                    equals(new ExceptionBody("Your token is invalid")));
            return;
        }
        fail();
    }


    @Test
    public void allTweets_shouldWorkWell()
    {
        List<Tweet> alltweets = new ArrayList<Tweet>();
        alltweets.add(new Tweet(0, "foo"));
        alltweets.add(new Tweet(0, "bar"));
        TweetRepository tweetRepository = mock(TweetRepository.class);
        SessionRepository sessionRepository =  mock(SessionRepository.class);
        UserSession session = mock(UserSession.class);
        when((sessionRepository.getByToken(any(String.class)))).thenReturn(session);
        when(session.isExpired()).thenReturn(false);
        when(tweetRepository.getAllByLogin(anyString())).thenReturn(alltweets);
        TweetResource sut = createSystemUnderTest(null,
                sessionRepository,
                tweetRepository);

        Response response = sut.allTweets("foo", "bar");
        TweetOutput output = (TweetOutput)response.getEntity();
        assertEquals(output.getTweets(), alltweets);
    }

    @Test
    public void getTweet_shouldWorkWell() {

        SessionRepository sessionRepository =  mock(SessionRepository.class);
        UserSession session = mock(UserSession.class);
        when((sessionRepository.getByToken(any(String.class)))).thenReturn(session);<commit_msg>Workes tests<commit_after>package ru.pmsoft.twitterkiller.rest;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import ru.pmsoft.twitterkiller.domain.dto.TweetOutput;
import ru.pmsoft.twitterkiller.domain.entity.UserSession;
import ru.pmsoft.twitterkiller.domain.entity.Tweet;
import ru.pmsoft.twitterkiller.domain.entity.User;
import ru.pmsoft.twitterkiller.domain.repository.SessionRepository;
import ru.pmsoft.twitterkiller.domain.repository.TweetRepository;
import ru.pmsoft.twitterkiller.domain.repository.UserRepository;
import ru.pmsoft.twitterkiller.rest.exceptions.ClientException;
import ru.pmsoft.twitterkiller.rest.exceptions.ExceptionBody;

import javax.ws.rs.core.Response;

import java.util.ArrayList;
import java.util.List;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.testng.AssertJUnit.assertTrue;

public class TweetResourceTestCase {

    private static TweetResource createSystemUnderTest(UserRepository repository,
                                                      SessionRepository sessionRepository,
                                                      TweetRepository tweetRepository) {
        return new TweetResource(repository==null ? mock(UserRepository.class) : repository,
                sessionRepository == null ? mock(SessionRepository.class) : sessionRepository,
                tweetRepository == null ? mock(TweetRepository.class) : tweetRepository);
    }

    @Test (dataProvider = "invalidDataSourceForAddTweet")
    public void addTweet_whenTextOfTweetIsNullOrEmpty_shouldThrowClientException(String token, String text) {
        TweetResource sut = createSystemUnderTest(null, null, null);
        try{
             sut.addTweet(token, text);
        }
        catch (ClientException ex)
        {
            assertEquals(ex.getResponse().getEntity(),
                    new ExceptionBody("Tweet can not be empty or less than 140 letters"));
            return;
        }
        fail();

    }
    @DataProvider
    public Object[][] invalidDataSourceForAddTweet()
    {
        return new Object[][]
                {
                  new Object[]{"foo", null},
                        new Object[]{"foo", "             "},
                        new Object[]{"foo", createLongString()}
                };
    }


    @Test
    public void addTweet_shouldWorkWell()
    {
        SessionRepository sessionRepository =  mock(SessionRepository.class);
        UserRepository userRepository = mock(UserRepository.class);
        User user = mock(User.class);
        when(userRepository.getById(any(Integer.class))).thenReturn(user);
        UserSession session = mock(UserSession.class);
        when((sessionRepository.getByToken(any(String.class)))).thenReturn(session);
        when(session.isExpired()).thenReturn(false);
        when(user.getId()).thenReturn(0);

        TweetResource sut = createSystemUnderTest
                (userRepository,
                sessionRepository,
                null);

        Response response = sut.addTweet("foo", "bar");
        assertEquals((String)response.getEntity(), "Tweet is saved");
    }


    @Test
    public void allTweets_whenUsernameIsEmpty_shouldThrowClientException() {
        TweetResource sut = createSystemUnderTest(mock(UserRepository.class),
                        null,
                        null);
        try {
            sut.allTweets("foo", "");
        }
        catch (ClientException ex) {
            assertEquals(ex.getResponse().getEntity(), new ExceptionBody("Login can not be empty"));
            return;
        }
        fail();
    }

    @Test (dataProvider = "InvalidReturnForDifferentTesting")
    public void alltestingmethods_whenSessionIsExpired_shouldThrowClientException(String method)
    {
        UserRepository userRepository = mock(UserRepository.class);
        when(userRepository.getByLogin(any(String.class))).thenReturn(new User("foo"));
        SessionRepository sessionRepository =  mock(SessionRepository.class);
        UserSession session = mock(UserSession.class);
        when((sessionRepository.getByToken(any(String.class)))).thenReturn(session);
        when(session.isExpired()).thenReturn(true);
        TweetResource sut = createSystemUnderTest(userRepository,
                sessionRepository,
                null);

        try{
            switch(method)
            {
                case "addTweet": sut.addTweet("foo", "bar"); break;
                case "getTweet":  sut.getTweet("foo", 1); break;
                case "allTweets": sut.allTweets("foo", "bar"); break;
            }
        }
        catch (ClientException ex)
        {
            assertEquals(ex.getResponse().getEntity(),
                    new ExceptionBody("Your token is invalid"));
            return;
        }
        fail();
    }

    @Test (dataProvider = "InvalidReturnForDifferentTesting")
    public void alltestingmethods_whenSessionIsNull_shouldThrowClientException(String method)
    {
        UserRepository userRepository = mock(UserRepository.class);
        when(userRepository.getByLogin(any(String.class))).thenReturn(new User("foo"));
        SessionRepository sessionRepository =  mock(SessionRepository.class);
        UserSession session = null;
        when((sessionRepository.getByToken(any(String.class)))).thenReturn(session);
        TweetResource sut = createSystemUnderTest(userRepository,
                sessionRepository,
                null);

        try{
            switch(method)
            {
                case "addTweet": sut.addTweet("foo", "bar"); break;
                case "getTweet":  sut.getTweet("foo", 1); break;
                case "allTweets": sut.allTweets("foo", "bar"); break;
            }
        }
        catch (ClientException ex)
        {
            assertTrue(ex.getResponse().getEntity().
                    equals(new ExceptionBody("Your token is invalid")));
            return;
        }
        fail();
    }


    @Test
    public void allTweets_shouldWorkWell()
    {
        UserRepository userRepository = mock(UserRepository.class);
        when(userRepository.getByLogin(any(String.class))).thenReturn(new User("foo"));
        List<Tweet> alltweets = new ArrayList<Tweet>();
        alltweets.add(new Tweet(0, "foo"));
        alltweets.add(new Tweet(0, "bar"));

        TweetRepository tweetRepository = mock(TweetRepository.class);
        SessionRepository sessionRepository =  mock(SessionRepository.class);
        UserSession session = mock(UserSession.class);
        when((sessionRepository.getByToken(any(String.class)))).thenReturn(session);
        when(session.isExpired()).thenReturn(false);
        when(tweetRepository.getAllByLogin(anyString())).thenReturn(alltweets);
        TweetResource sut = createSystemUnderTest(userRepository,
                sessionRepository,
                tweetRepository);

        Response response = sut.allTweets("foo", "bar");
        TweetOutput output = (TweetOutput)response.getEntity();
        assertEquals(output.getTweets(), alltweets);
    }

    @Test
    public void getTweet_shouldWorkWell() {

        SessionRepository sessionRepository =  mock(SessionRepository.class);
        UserSession session = mock(UserSession.class);
        when((sessionRepository.getByToken(any(String.class)))).thenReturn(session);
{'fraction_non_alphanumeric': 0.10384406411301277, 'fraction_numerical': 0.0010866612333604998, 'mean_word_length': 2.602887203327624, 'pattern_counts': {'":': 12, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4671112', 'n_tokens_mistral': 3883, 'n_tokens_neox': 3756, 'n_words': 677}