package com.tipi.conversations.domains.conversations;

import com.tipi.conversations.domains.conversations.repositories.InMemoryConversationsRepository;
import com.tipi.conversations.domains.users.User;
import com.tipi.conversations.domains.users.Users;
import com.tipi.conversations.domains.users.repositories.InMemoryUsersRepository;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.data.repository.CrudRepository;

import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

/**
 * Created by Maximilien on 03/01/2016.
 */
public class ConversationsTest {

	private Users users;
	private CrudRepository usersRepository;
	private Conversations conversations;

	@Rule
	public ExpectedException expectedException = ExpectedException.none();

	@Before
	public void prepareUsers() {
		users = new Users(new InMemoryUsersRepository());
	}

	@Before
	public void prepareConversations() {
		conversations = new Conversations(new InMemoryConversationsRepository());
	}

	@Test
	public void should_contain_two_participants() {
		// given
		User firstParticipant = new User("0001");
		users.add(firstParticipant);
		User secondParticipant = new User("0002");
		users.add(secondParticipant);

		// when
		Conversation conversation = new Conversation("0001");
		conversation.addParticipant(firstParticipant);
		conversation.addParticipant(secondParticipant);
		conversations.add(conversation);

		// then
		Conversation storedConversation = conversations.getByConversationId("0001");
		assertThat(storedConversation.getParticipants()).hasSize(2);
	}

	@Test
	public void should_return_an_error_when_conversation_has_only_one_participant() {
		// given
		User firstParticipant = new User("0001");
		users.add(firstParticipant);
		expectedException.expect(IllegalArgumentException.class);
		expectedException.expectMessage("Cannot add conversation, reason: not enough participants.");

		// when
		Conversation conversation = new Conversation("0001");
		conversation.addParticipant(firstParticipant);
		conversations.add(conversation);
	}

}

@@ -325,24 +325,90 @@
 Repository;%0A
+import com.tipi.conversations.infrastructure.sequences.Sequences;%0A
 import org.j
@@ -755,16 +755,84 @@
  users;%0A
+%09private Conversations conversations;%0A%09private Sequences sequences;%0A
 %09private
@@ -867,46 +867,8 @@
 ory;
-%0A%09private Conversations conversations;
 %0A%0A%09@
@@ -1163,24 +1163,102 @@
 ory());%0A%09%7D%0A%0A
+%09@Before%0A%09public void prepareSequences() %7B%0A%09%09sequences = new Sequences();%0A%09%7D%0A%0A
 %09@Test%0A%09publ
@@ -1339,38 +1339,57 @@
 pant = new User(
-%220001%22
+sequences.getNextUserId()
 );%0A%09%09users.add(f
@@ -1446,14 +1446,33 @@
 ser(
-%220002%22
+sequences.getNextUserId()
 );%0A%09
@@ -1996,22 +1996,41 @@
 ew User(
-%220001%22
+sequences.getNextUserId()
 );%0A%09%09use

Update conversations service to use sequences service for user id generation