package at.fhj.swd14.pse.message;

import at.fhj.swd14.pse.community.Community;
import at.fhj.swd14.pse.repository.internal.MessageRepositoryImpl;
import at.fhj.swd14.pse.user.User;
import at.fhj.swd14.pse.user.UserConverter;
import at.fhj.swd14.pse.user.UserDto;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(MockitoJUnitRunner.class)
public class MessageServiceImplTest {


    @InjectMocks
    private MessageServiceImpl messageServiceImpl;

    @Mock
    private MessageRepositoryImpl messageRepository;

    private final Long communityMessageId = 1L;
    private final Long globalMessageId = 2L;
    private final Long privateMessageId = 3L;
    private User author;
    private User recipient;
    private Community community;
    private final List<Message> messages = new ArrayList<>();

    @Before
    public void setup() {

        author = new User(100L);
        recipient = new User(200L);
        community = new Community();
        community.setId(300L);

        List<UserDto> userList = new ArrayList<>();
        userList.add(UserConverter.convert(recipient));

        community.setAllowedUsers(UserConverter.convertToDoList(userList));
        community.setAuthor(author);

        messages.add(MessageTestHelper.getCommunityMessageDummy(communityMessageId, author, community));
        messages.add(MessageTestHelper.getGlobalMessageDummy(globalMessageId, author));
        messages.add(MessageTestHelper.getPrivateMessageDummy(privateMessageId, author, recipient));
    }
    
    @Test
    public void saveTest(){
    	Mockito.doNothing().when(messageRepository).save(messages.get(0));
    	Long id = messageServiceImpl.save(MessageConverter.convert(messages.get(0)));
    	Assert.assertEquals(communityMessageId, id);
    }

    @Test
    public void findByIdTest() {
        Mockito.when(messageRepository.find(communityMessageId)).thenReturn(messages.get(0));
        MessageDto dto = messageServiceImpl.find(communityMessageId);

        MessageDtoTester.assertEquals(MessageConverter.convert(messages.get(0)), dto);
    }

    @Test
    public void findByAuthorTest() {

        Map<String, Object> parameter = new HashMap<>();
        parameter.put("userId", author.getId());

        Mockito.when(messageRepository.executeNamedQuery("Message.findByAuthorId", parameter)).thenReturn(messages);

        List<MessageDto> dtoList = messageServiceImpl.findByAuthorId(author.getId());
        MessageDtoTester.assertEquals(new ArrayList<>(MessageConverter.convertToDtoList(messages)), dtoList);
    }

    @Test
    public void findByCommunityTest() {

        Map<String, Object> parameter = new HashMap<>();
        parameter.put("communityId", community.getId());

        List<Message> expectedMessages = messages.subList(0, 1);

        Mockito.when(messageRepository.executeNamedQuery("Message.findByCommunityId", parameter)).thenReturn(expectedMessages);

        List<MessageDto> dtoList = messageServiceImpl.findByCommunityId(community.getId());
        MessageDtoTester.assertEquals(new ArrayList<>(MessageConverter.convertToDtoList(expectedMessages)), dtoList);
    }

    @Test
    public void findGlobalTest() {
        List<Message> expectedMessages = messages.subList(1, 2);

        Mockito.when(messageRepository.executeNamedQuery("Message.findGlobalMessages")).thenReturn(expectedMessages);

        List<MessageDto> dtoList = messageServiceImpl.findGlobalMesssages();
        MessageDtoTester.assertEquals(new ArrayList<>(MessageConverter.convertToDtoList(expectedMessages)), dtoList);
    }

    @Test
    public void findUsersPrivateTest() {

        Map<String, Object> parameter = new HashMap<>();
        parameter.put("userId", recipient.getId());

        List<Message> expectedMessages = messages.subList(2, 3);

        Mockito.when(messageRepository.executeNamedQuery("Message.findUsersPrivateMessage", parameter)).thenReturn(expectedMessages);

        List<MessageDto> dtoList = messageServiceImpl.findUsersPrivateMessages(recipient.getId());
        MessageDtoTester.assertEquals(new ArrayList<>(MessageConverter.convertToDtoList(expectedMessages)), dtoList);
    }

    @Test
    public void findUserRelatedTest() {

        Map<String, Object> parameter = new HashMap<>();
        parameter.put("userId", recipient.getId());

        Mockito.when(messageRepository.executeNamedQuery("Message.findUserRelated", parameter)).thenReturn(messages);

        List<MessageDto> dtoList = messageServiceImpl.findUserRelated(recipient.getId());
        MessageDtoTester.assertEquals(new ArrayList<>(MessageConverter.convertToDtoList(messages)), dtoList);
    }

}

@@ -2342,32 +2342,34 @@
 ), dto);%0A    %7D%0A%0A
+//
     @Test%0A    pu
@@ -2354,32 +2354,34 @@
  %7D%0A%0A//    @Test%0A
+//
     public void 
@@ -2393,33 +2393,37 @@
 yAuthorTest() %7B%0A
-%0A
+//%0A//
         Map%3CStri
@@ -2455,32 +2455,34 @@
 ew HashMap%3C%3E();%0A
+//
         paramete
@@ -2506,33 +2506,37 @@
 uthor.getId());%0A
-%0A
+//%0A//
         Mockito.
@@ -2628,33 +2628,37 @@
 turn(messages);%0A
-%0A
+//%0A//
         List%3CMes
@@ -2719,32 +2719,34 @@
 uthor.getId());%0A
+//
         MessageD
@@ -2831,32 +2831,34 @@
 es)), dtoList);%0A
+//
     %7D%0A%0A    @Test

Test auskommentiert: geht Schief. Wurde nicht behandelt.