Skip to content
Snippets Groups Projects
GroupInvitationManagerImplTest.java 33.6 KiB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924
package org.briarproject.privategroup.invitation;

import org.briarproject.BriarMockTestCase;
import org.briarproject.TestUtils;
import org.briarproject.api.FormatException;
import org.briarproject.api.clients.ClientHelper;
import org.briarproject.api.clients.ContactGroupFactory;
import org.briarproject.api.clients.MessageTracker;
import org.briarproject.api.clients.SessionId;
import org.briarproject.api.contact.Contact;
import org.briarproject.api.contact.ContactId;
import org.briarproject.api.data.BdfDictionary;
import org.briarproject.api.data.BdfEntry;
import org.briarproject.api.data.BdfList;
import org.briarproject.api.data.MetadataParser;
import org.briarproject.api.db.DatabaseComponent;
import org.briarproject.api.db.DbException;
import org.briarproject.api.db.Metadata;
import org.briarproject.api.db.Transaction;
import org.briarproject.api.identity.Author;
import org.briarproject.api.identity.AuthorId;
import org.briarproject.api.privategroup.PrivateGroup;
import org.briarproject.api.privategroup.PrivateGroupFactory;
import org.briarproject.api.privategroup.PrivateGroupManager;
import org.briarproject.api.privategroup.invitation.GroupInvitationItem;
import org.briarproject.api.privategroup.invitation.GroupInvitationRequest;
import org.briarproject.api.privategroup.invitation.GroupInvitationResponse;
import org.briarproject.api.sharing.InvitationMessage;
import org.briarproject.api.sync.Group;
import org.briarproject.api.sync.GroupId;
import org.briarproject.api.sync.Message;
import org.briarproject.api.sync.MessageId;
import org.jetbrains.annotations.Nullable;
import org.jmock.AbstractExpectations;
import org.jmock.Expectations;
import org.jmock.lib.legacy.ClassImposteriser;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static junit.framework.TestCase.fail;
import static org.briarproject.TestUtils.getRandomBytes;
import static org.briarproject.TestUtils.getRandomId;
import static org.briarproject.TestUtils.getRandomString;
import static org.briarproject.api.privategroup.invitation.GroupInvitationManager.CLIENT_ID;
import static org.briarproject.api.sync.Group.Visibility.SHARED;
import static org.briarproject.api.sync.SyncConstants.MESSAGE_HEADER_LENGTH;
import static org.briarproject.privategroup.invitation.GroupInvitationConstants.GROUP_KEY_CONTACT_ID;
import static org.briarproject.privategroup.invitation.MessageType.ABORT;
import static org.briarproject.privategroup.invitation.MessageType.INVITE;
import static org.briarproject.privategroup.invitation.MessageType.JOIN;
import static org.briarproject.privategroup.invitation.MessageType.LEAVE;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class GroupInvitationManagerImplTest extends BriarMockTestCase {

	private final GroupInvitationManagerImpl groupInvitationManager;
	private final DatabaseComponent db = context.mock(DatabaseComponent.class);
	private final ClientHelper clientHelper = context.mock(ClientHelper.class);
	private final ContactGroupFactory contactGroupFactory =
			context.mock(ContactGroupFactory.class);
	private final PrivateGroupFactory privateGroupFactory =
			context.mock(PrivateGroupFactory.class);
	private final PrivateGroupManager privateGroupManager =
			context.mock(PrivateGroupManager.class);
	private final MessageParser messageParser =
			context.mock(MessageParser.class);
	private final SessionParser sessionParser =
			context.mock(SessionParser.class);
	private final SessionEncoder sessionEncoder =
			context.mock(SessionEncoder.class);
	private final ProtocolEngineFactory engineFactory =
			context.mock(ProtocolEngineFactory.class);
	private final CreatorProtocolEngine creatorEngine;
	private final InviteeProtocolEngine inviteeEngine;
	private final PeerProtocolEngine peerEngine;
	private final CreatorSession creatorSession;
	private final InviteeSession inviteeSession;
	private final PeerSession peerSession;
	private final Group localGroup =
			new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5));
	private final Transaction txn = new Transaction(null, false);
	private final ContactId contactId = new ContactId(0);
	private final Author author =
			new Author(new AuthorId(getRandomId()), getRandomString(5),
					getRandomBytes(5));
	private final Contact contact =
			new Contact(contactId, author, new AuthorId(getRandomId()), true,
					true);
	private final Group contactGroup =
			new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5));
	private final Group privateGroup =
			new Group(new GroupId(getRandomId()), CLIENT_ID, getRandomBytes(5));
	private final BdfDictionary meta = BdfDictionary.of(new BdfEntry("f", "o"));
	private final Message message =
			new Message(new MessageId(getRandomId()), contactGroup.getId(),
					0L, getRandomBytes(MESSAGE_HEADER_LENGTH + 1));
	private final BdfList body = new BdfList();
	private final Message storageMessage =
			new Message(new MessageId(getRandomId()), contactGroup.getId(),
					0L, getRandomBytes(MESSAGE_HEADER_LENGTH + 1));

	public GroupInvitationManagerImplTest() {
		context.setImposteriser(ClassImposteriser.INSTANCE);
		creatorEngine = context.mock(CreatorProtocolEngine.class);
		inviteeEngine = context.mock(InviteeProtocolEngine.class);
		peerEngine = context.mock(PeerProtocolEngine.class);

		creatorSession = context.mock(CreatorSession.class);
		inviteeSession = context.mock(InviteeSession.class);
		peerSession = context.mock(PeerSession.class);

		context.checking(new Expectations() {{
			oneOf(engineFactory).createCreatorEngine();
			will(returnValue(creatorEngine));
			oneOf(engineFactory).createInviteeEngine();
			will(returnValue(inviteeEngine));
			oneOf(engineFactory).createPeerEngine();
			will(returnValue(peerEngine));
			oneOf(contactGroupFactory).createLocalGroup(CLIENT_ID);
			will(returnValue(localGroup));
		}});
		MetadataParser metadataParser = context.mock(MetadataParser.class);
		MessageTracker messageTracker = context.mock(MessageTracker.class);
		groupInvitationManager =
				new GroupInvitationManagerImpl(db, clientHelper, metadataParser,
						messageTracker, contactGroupFactory,
						privateGroupFactory, privateGroupManager, messageParser,
						sessionParser, sessionEncoder, engineFactory);
	}

	@Test
	public void testCreateLocalState() throws Exception {
		context.checking(new Expectations() {{
			oneOf(db).addGroup(txn, localGroup);
			oneOf(db).getContacts(txn);
			will(returnValue(Collections.singletonList(contact)));
		}});
		expectAddingContact(contact, true);
		groupInvitationManager.createLocalState(txn);
	}

	private void expectAddingContact(final Contact c,
			final boolean contactExists) throws Exception {
		context.checking(new Expectations() {{
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, c);
			will(returnValue(contactGroup));
			oneOf(db).containsGroup(txn, contactGroup.getId());
			will(returnValue(contactExists));
		}});
		if (contactExists) return;

		final BdfDictionary meta = BdfDictionary
				.of(new BdfEntry(GROUP_KEY_CONTACT_ID, c.getId().getInt()));
		context.checking(new Expectations() {{
			oneOf(db).addGroup(txn, contactGroup);
			oneOf(db).setGroupVisibility(txn, c.getId(), contactGroup.getId(),
					SHARED);
			oneOf(clientHelper)
					.mergeGroupMetadata(txn, contactGroup.getId(), meta);
			oneOf(db).getGroups(txn, PrivateGroupManager.CLIENT_ID);
			will(returnValue(Collections.singletonList(privateGroup)));
			oneOf(privateGroupManager)
					.isMember(txn, privateGroup.getId(), c.getAuthor());
			will(returnValue(true));
		}});
		expectAddingMember(privateGroup.getId(), c);
	}

	private void expectAddingMember(final GroupId g, final Contact c)
			throws Exception {
		context.checking(new Expectations() {{
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, c);
			will(returnValue(contactGroup));
		}});
		expectGetSession(Collections.<MessageId, BdfDictionary>emptyMap(),
				new SessionId(g.getBytes()));

		context.checking(new Expectations() {{
			oneOf(peerEngine).onMemberAddedAction(with(equal(txn)),
					with(any(PeerSession.class)));
			will(returnValue(peerSession));
		}});
		expectStoreSession(peerSession, storageMessage.getId());
		expectCreateStorageId();
	}

	private void expectCreateStorageId() throws DbException {
		context.checking(new Expectations() {{
			oneOf(clientHelper)
					.createMessageForStoringMetadata(contactGroup.getId());
			will(returnValue(storageMessage));
			oneOf(db).addLocalMessage(txn, storageMessage, new Metadata(),
					false);
		}});
	}

	private void expectStoreSession(final Session session,
			final MessageId storageId) throws Exception {
		context.checking(new Expectations() {{
			oneOf(sessionEncoder).encodeSession(session);
			will(returnValue(meta));
			oneOf(clientHelper).mergeMessageMetadata(txn, storageId, meta);
		}});
	}

	private void expectGetSession(final Map<MessageId, BdfDictionary> result,
			final SessionId sessionId) throws Exception {
		final BdfDictionary query = BdfDictionary.of(new BdfEntry("q", "u"));
		context.checking(new Expectations() {{
			oneOf(sessionParser).getSessionQuery(sessionId);
			will(returnValue(query));
			oneOf(clientHelper)
					.getMessageMetadataAsDictionary(txn, contactGroup.getId(),
							query);
			will(returnValue(result));
		}});
	}

	@Test
	public void testAddingContact() throws Exception {
		expectAddingContact(contact, false);
		groupInvitationManager.addingContact(txn, contact);
	}

	@Test
	public void testRemovingContact() throws Exception {
		context.checking(new Expectations() {{
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
			oneOf(db).removeGroup(txn, contactGroup);
		}});
		groupInvitationManager.removingContact(txn, contact);
	}

	@Test(expected = FormatException.class)
	public void testIncomingUnknownMessage() throws Exception {
		expectFirstIncomingMessage(Role.INVITEE, ABORT);
		groupInvitationManager.incomingMessage(txn, message, body, meta);
	}

	@Test
	public void testIncomingFirstInviteMessage() throws Exception {
		expectFirstIncomingMessage(Role.INVITEE, INVITE);
		groupInvitationManager.incomingMessage(txn, message, body, meta);
	}

	@Test
	public void testIncomingFirstJoinMessage() throws Exception {
		expectFirstIncomingMessage(Role.PEER, JOIN);
		groupInvitationManager.incomingMessage(txn, message, body, meta);
	}

	@Test
	public void testIncomingInviteMessage() throws Exception {
		expectIncomingMessage(Role.INVITEE, INVITE);
		groupInvitationManager.incomingMessage(txn, message, body, meta);
	}

	@Test
	public void testIncomingJoinMessage() throws Exception {
		expectIncomingMessage(Role.INVITEE, JOIN);
		groupInvitationManager.incomingMessage(txn, message, body, meta);
	}

	@Test
	public void testIncomingJoinMessageForCreator() throws Exception {
		expectIncomingMessage(Role.CREATOR, JOIN);
		groupInvitationManager.incomingMessage(txn, message, body, meta);
	}

	@Test
	public void testIncomingLeaveMessage() throws Exception {
		expectIncomingMessage(Role.INVITEE, LEAVE);
		groupInvitationManager.incomingMessage(txn, message, body, meta);
	}

	@Test
	public void testIncomingAbortMessage() throws Exception {
		expectIncomingMessage(Role.INVITEE, ABORT);
		groupInvitationManager.incomingMessage(txn, message, body, meta);
	}

	private void expectFirstIncomingMessage(Role role, MessageType type)
			throws Exception {
		expectIncomingMessage(role, type,
				Collections.<MessageId, BdfDictionary>emptyMap());
	}

	private void expectIncomingMessage(Role role, MessageType type)
			throws Exception {
		BdfDictionary state = BdfDictionary.of(new BdfEntry("state", "test"));
		Map<MessageId, BdfDictionary> states =
				Collections.singletonMap(storageMessage.getId(), state);
		expectIncomingMessage(role, type, states);
	}

	private void expectIncomingMessage(final Role role,
			final MessageType type, final Map<MessageId, BdfDictionary> states)
			throws Exception {
		final MessageMetadata messageMetadata =
				context.mock(MessageMetadata.class);
		context.checking(new Expectations() {{
			oneOf(messageParser).parseMetadata(meta);
			will(returnValue(messageMetadata));
			oneOf(messageMetadata).getPrivateGroupId();
			will(returnValue(privateGroup.getId()));
		}});
		expectGetSession(states,
				new SessionId(privateGroup.getId().getBytes()));

		Session session;
		if (states.isEmpty()) {
			session = expectHandleFirstMessage(role, messageMetadata, type);
			if (session != null) {
				expectCreateStorageId();
				expectStoreSession(session, storageMessage.getId());
			}
		} else {
			assertEquals(1, states.size());
			session = expectHandleMessage(role, messageMetadata,
					states.values().iterator().next(), type);
			expectStoreSession(session, storageMessage.getId());
		}
	}

	@Nullable
	private Session expectHandleFirstMessage(Role role,
			final MessageMetadata messageMetadata, final MessageType type)
			throws Exception {
		context.checking(new Expectations() {{
			oneOf(messageMetadata).getPrivateGroupId();
			will(returnValue(privateGroup.getId()));
			oneOf(messageMetadata).getMessageType();
			will(returnValue(type));
		}});
		if (type == ABORT || type == LEAVE) return null;

		AbstractProtocolEngine engine;
		Session session;
		if (type == INVITE) {
			assertEquals(Role.INVITEE, role);
			engine = inviteeEngine;
			session = inviteeSession;
		} else if (type == JOIN) {
			assertEquals(Role.PEER, role);
			engine = peerEngine;
			session = peerSession;
		} else {
			throw new RuntimeException();
		}
		expectIndividualMessage(type, engine, session);
		return session;
	}

	@Nullable
	private Session expectHandleMessage(final Role role,
			final MessageMetadata messageMetadata, final BdfDictionary state,
			final MessageType type) throws Exception {
		context.checking(new Expectations() {{
			oneOf(messageMetadata).getMessageType();
			will(returnValue(type));
			oneOf(sessionParser).getRole(state);
			will(returnValue(role));
		}});
		if (role == Role.CREATOR) {
			context.checking(new Expectations() {{
				oneOf(sessionParser)
						.parseCreatorSession(contactGroup.getId(), state);
				will(returnValue(creatorSession));
			}});
			expectIndividualMessage(type, creatorEngine, creatorSession);
			return creatorSession;
		} else if (role == Role.INVITEE) {
			context.checking(new Expectations() {{
				oneOf(sessionParser)
						.parseInviteeSession(contactGroup.getId(), state);
				will(returnValue(inviteeSession));
			}});
			expectIndividualMessage(type, inviteeEngine, inviteeSession);
			return inviteeSession;
		} else if (role == Role.PEER) {
			context.checking(new Expectations() {{
				oneOf(sessionParser)
						.parsePeerSession(contactGroup.getId(), state);
				will(returnValue(peerSession));
			}});
			expectIndividualMessage(type, peerEngine, peerSession);
			return peerSession;
		} else {
			fail();
			throw new RuntimeException();
		}
	}

	private <S extends Session> void expectIndividualMessage(
			final MessageType type, final ProtocolEngine<S> engine,
			final S session) throws Exception {
		if (type == INVITE) {
			final InviteMessage msg = context.mock(InviteMessage.class);
			context.checking(new Expectations() {{
				oneOf(messageParser).parseInviteMessage(message, body);
				will(returnValue(msg));
				oneOf(engine).onInviteMessage(with(equal(txn)),
						with(AbstractExpectations.<S>anything()),
						with(equal(msg)));
				will(returnValue(session));
			}});
		} else if (type == JOIN) {
			final JoinMessage msg = context.mock(JoinMessage.class);
			context.checking(new Expectations() {{
				oneOf(messageParser).parseJoinMessage(message, body);
				will(returnValue(msg));
				oneOf(engine).onJoinMessage(with(equal(txn)),
						with(AbstractExpectations.<S>anything()),
						with(equal(msg)));
				will(returnValue(session));
			}});
		} else if (type == LEAVE) {
			final LeaveMessage msg = context.mock(LeaveMessage.class);
			context.checking(new Expectations() {{
				oneOf(messageParser).parseLeaveMessage(message, body);
				will(returnValue(msg));
				oneOf(engine).onLeaveMessage(with(equal(txn)),
						with(AbstractExpectations.<S>anything()),
						with(equal(msg)));
				will(returnValue(session));
			}});
		} else if (type == ABORT) {
			final AbortMessage msg = context.mock(AbortMessage.class);
			context.checking(new Expectations() {{
				oneOf(messageParser).parseAbortMessage(message, body);
				will(returnValue(msg));
				oneOf(engine).onAbortMessage(with(equal(txn)),
						with(AbstractExpectations.<S>anything()),
						with(equal(msg)));
				will(returnValue(session));
			}});
		} else {
			fail();
		}
	}

	@Test
	public void testSendFirstInvitation() throws Exception {
		final String msg = "Invitation text for first invitation";
		final long time = 42L;
		final byte[] signature = TestUtils.getRandomBytes(42);
		Map<MessageId, BdfDictionary> states = Collections.emptyMap();

		expectGetSession(states,
				new SessionId(privateGroup.getId().getBytes()));
		context.checking(new Expectations() {{
			oneOf(db).startTransaction(false);
			will(returnValue(txn));
			oneOf(db).getContact(txn, contactId);
			will(returnValue(contact));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
		}});
		expectCreateStorageId();
		context.checking(new Expectations() {{
			oneOf(creatorEngine).onInviteAction(with(same(txn)),
					with(any(CreatorSession.class)), with(equal(msg)),
					with(equal(time)), with(equal(signature)));
			will(returnValue(creatorSession));
		}});
		expectStoreSession(creatorSession, storageMessage.getId());
		context.checking(new Expectations() {{
			oneOf(db).commitTransaction(txn);
			oneOf(db).endTransaction(txn);
		}});
		groupInvitationManager.sendInvitation(privateGroup.getId(), contactId,
				msg, time, signature);
	}

	@Test
	public void testSendSubsequentInvitation() throws Exception {
		final String msg = "Invitation text for subsequent invitation";
		final long time = 43L;
		final byte[] signature = TestUtils.getRandomBytes(43);
		final BdfDictionary state =
				BdfDictionary.of(new BdfEntry("state", "test"));
		Map<MessageId, BdfDictionary> states =
				Collections.singletonMap(storageMessage.getId(), state);

		expectGetSession(states,
				new SessionId(privateGroup.getId().getBytes()));
		context.checking(new Expectations() {{
			oneOf(db).startTransaction(false);
			will(returnValue(txn));
			oneOf(db).getContact(txn, contactId);
			will(returnValue(contact));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
			oneOf(sessionParser)
					.parseCreatorSession(contactGroup.getId(), state);
			will(returnValue(creatorSession));
			oneOf(creatorEngine).onInviteAction(with(same(txn)),
					with(any(CreatorSession.class)), with(equal(msg)),
					with(equal(time)), with(equal(signature)));
			will(returnValue(creatorSession));
		}});
		expectStoreSession(creatorSession, storageMessage.getId());
		context.checking(new Expectations() {{
			oneOf(db).commitTransaction(txn);
			oneOf(db).endTransaction(txn);
		}});
		groupInvitationManager.sendInvitation(privateGroup.getId(), contactId,
				msg, time, signature);
	}

	@Test(expected = IllegalArgumentException.class)
	public void testRespondToInvitationWithoutSession() throws Exception {
		final SessionId sessionId = new SessionId(getRandomId());

		context.checking(new Expectations() {{
			oneOf(db).startTransaction(false);
			will(returnValue(txn));
			oneOf(db).getContact(txn, contactId);
			will(returnValue(contact));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
			oneOf(db).endTransaction(txn);
		}});
		expectGetSession(Collections.<MessageId, BdfDictionary>emptyMap(),
				sessionId);

		groupInvitationManager.respondToInvitation(contactId, sessionId, true);
	}

	@Test
	public void testAcceptInvitationWithSession() throws Exception {
		final boolean accept = true;
		SessionId sessionId = new SessionId(getRandomId());
		BdfDictionary state = BdfDictionary.of(new BdfEntry("f", "o"));
		Map<MessageId, BdfDictionary> states =
				Collections.singletonMap(storageMessage.getId(), state);

		expectRespondToInvitation(states, sessionId, accept);
		groupInvitationManager
				.respondToInvitation(contactId, sessionId, accept);
	}

	@Test
	public void testDeclineInvitationWithSession() throws Exception {
		final boolean accept = false;
		SessionId sessionId = new SessionId(getRandomId());
		BdfDictionary state = BdfDictionary.of(new BdfEntry("f", "o"));
		Map<MessageId, BdfDictionary> states =
				Collections.singletonMap(storageMessage.getId(), state);

		expectRespondToInvitation(states, sessionId, accept);
		groupInvitationManager
				.respondToInvitation(contactId, sessionId, accept);
	}

	@Test
	public void testRespondToInvitationWithGroupId() throws Exception {
		final boolean accept = true;
		final PrivateGroup g = context.mock(PrivateGroup.class);
		SessionId sessionId = new SessionId(privateGroup.getId().getBytes());
		BdfDictionary state = BdfDictionary.of(new BdfEntry("f", "o"));
		Map<MessageId, BdfDictionary> states =
				Collections.singletonMap(storageMessage.getId(), state);

		context.checking(new Expectations() {{
			oneOf(g).getId();
			will(returnValue(privateGroup.getId()));
		}});
		expectRespondToInvitation(states, sessionId, accept);
		groupInvitationManager.respondToInvitation(contactId, g, accept);
	}

	private void expectRespondToInvitation(
			final Map<MessageId, BdfDictionary> states,
			final SessionId sessionId, final boolean accept) throws Exception {
		expectGetSession(states, sessionId);
		context.checking(new Expectations() {{
			oneOf(db).startTransaction(false);
			will(returnValue(txn));
			oneOf(db).getContact(txn, contactId);
			will(returnValue(contact));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
		}});

		if (states.isEmpty()) return;
		assertEquals(1, states.size());

		final BdfDictionary state = states.values().iterator().next();
		context.checking(new Expectations() {{
			oneOf(sessionParser)
					.parseInviteeSession(contactGroup.getId(), state);
			will(returnValue(inviteeSession));
			if (accept) oneOf(inviteeEngine).onJoinAction(txn, inviteeSession);
			else oneOf(inviteeEngine).onLeaveAction(txn, inviteeSession);
			will(returnValue(inviteeSession));
			oneOf(db).commitTransaction(txn);
			oneOf(db).endTransaction(txn);
		}});
		expectStoreSession(inviteeSession, storageMessage.getId());
	}

	@Test
	public void testRevealRelationship() throws Exception {
		SessionId sessionId = new SessionId(privateGroup.getId().getBytes());
		final BdfDictionary state = BdfDictionary.of(new BdfEntry("f", "o"));
		Map<MessageId, BdfDictionary> states =
				Collections.singletonMap(storageMessage.getId(), state);

		context.checking(new Expectations() {{
			oneOf(db).startTransaction(false);
			will(returnValue(txn));
			oneOf(db).getContact(txn, contactId);
			will(returnValue(contact));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
			oneOf(sessionParser).parsePeerSession(contactGroup.getId(), state);
			will(returnValue(peerSession));
			oneOf(peerEngine).onJoinAction(txn, peerSession);
			will(returnValue(peerSession));
			oneOf(db).commitTransaction(txn);
			oneOf(db).endTransaction(txn);
		}});
		expectGetSession(states, sessionId);
		expectStoreSession(peerSession, storageMessage.getId());

		groupInvitationManager
				.revealRelationship(contactId, privateGroup.getId());
	}

	@Test(expected = IllegalArgumentException.class)
	public void testRevealRelationshipWithoutSession() throws Exception {
		SessionId sessionId = new SessionId(privateGroup.getId().getBytes());

		context.checking(new Expectations() {{
			oneOf(db).startTransaction(false);
			will(returnValue(txn));
			oneOf(db).getContact(txn, contactId);
			will(returnValue(contact));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
			oneOf(db).endTransaction(txn);
		}});
		expectGetSession(Collections.<MessageId, BdfDictionary>emptyMap(),
				sessionId);

		groupInvitationManager
				.revealRelationship(contactId, privateGroup.getId());
	}

	@Test
	public void testGetInvitationMessages() throws Exception {
		final BdfDictionary query = new BdfDictionary();
		final BdfDictionary d1 = BdfDictionary.of(new BdfEntry("m1", "d"));
		final BdfDictionary d2 = BdfDictionary.of(new BdfEntry("m2", "d"));
		final Map<MessageId, BdfDictionary> results =	new HashMap<>();
		results.put(message.getId(), d1);
		results.put(storageMessage.getId(), d2);
		long time1 = 1L, time2 = 2L;
		final MessageMetadata meta1 =
				new MessageMetadata(INVITE, privateGroup.getId(), time1, true,
						true, true, true);
		final MessageMetadata meta2 =
				new MessageMetadata(JOIN, privateGroup.getId(), time2, true,
						true, true, true);
		final BdfList list1 = BdfList.of(1);

		context.checking(new Expectations() {{
			oneOf(db).startTransaction(true);
			will(returnValue(txn));
			oneOf(db).getContact(txn, contactId);
			will(returnValue(contact));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
			oneOf(messageParser).getMessagesVisibleInUiQuery();
			will(returnValue(query));
			oneOf(clientHelper)
					.getMessageMetadataAsDictionary(txn, contactGroup.getId(),
							query);
			will(returnValue(results));
			// first message
			oneOf(messageParser).parseMetadata(d1);
			will(returnValue(meta1));
			oneOf(db).getMessageStatus(txn, contactId, message.getId());
			oneOf(clientHelper).getMessage(txn, message.getId());
			will(returnValue(message));
			oneOf(clientHelper).toList(message);
			will(returnValue(list1));
			oneOf(messageParser).parseInviteMessage(message, list1);
			// second message
			oneOf(messageParser).parseMetadata(d2);
			will(returnValue(meta2));
			oneOf(db).getMessageStatus(txn, contactId, storageMessage.getId());
			// end transaction
			oneOf(db).commitTransaction(txn);
			oneOf(db).endTransaction(txn);
		}});

		Collection<InvitationMessage> messages =
				groupInvitationManager.getInvitationMessages(contactId);
		assertEquals(2, messages.size());
		for (InvitationMessage m : messages) {
			assertEquals(contactGroup.getId(), m.getGroupId());
			assertEquals(contactId, m.getContactId());
			if (m.getId().equals(message.getId())) {
				assertTrue(m instanceof GroupInvitationRequest);
				assertEquals(time1, m.getTimestamp());
			} else if (m.getId().equals(storageMessage.getId())) {
				assertTrue(m instanceof GroupInvitationResponse);
				assertEquals(time2, m.getTimestamp());
			}
		}
	}

	@Test
	public void testGetInvitations() throws Exception {
		final BdfDictionary query = new BdfDictionary();
		BdfDictionary d1 = BdfDictionary.of(new BdfEntry("m1", "d"));
		BdfDictionary d2 = BdfDictionary.of(new BdfEntry("m2", "d"));
		final Map<MessageId, BdfDictionary> results =	new HashMap<>();
		results.put(message.getId(), d1);
		results.put(storageMessage.getId(), d2);
		final BdfList list1 = BdfList.of(1);
		final BdfList list2 = BdfList.of(2);
		long time1 = 1L, time2 = 2L;
		final InviteMessage m1 =
				new InviteMessage(message.getId(), contactGroup.getId(),
						privateGroup.getId(), time1, "test", author,
						getRandomBytes(5), null, getRandomBytes(5));
		final InviteMessage m2 =
				new InviteMessage(message.getId(), contactGroup.getId(),
						privateGroup.getId(), time2, "test", author,
						getRandomBytes(5), null, getRandomBytes(5));
		final PrivateGroup g = context.mock(PrivateGroup.class);

		context.checking(new Expectations() {{
			oneOf(messageParser).getInvitesAvailableToAnswerQuery();
			will(returnValue(query));
			oneOf(db).startTransaction(true);
			will(returnValue(txn));
			oneOf(db).getContacts(txn);
			will(returnValue(Collections.singletonList(contact)));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
			oneOf(clientHelper)
					.getMessageMetadataAsDictionary(txn, contactGroup.getId(),
							query);
			will(returnValue(results));
			// message 1
			oneOf(clientHelper).getMessage(txn, message.getId());
			will(returnValue(message));
			oneOf(clientHelper).toList(message);
			will(returnValue(list1));
			oneOf(messageParser).parseInviteMessage(message, list1);
			will(returnValue(m1));
			oneOf(privateGroupFactory)
					.createPrivateGroup(m1.getGroupName(), m1.getCreator(),
							m1.getSalt());
			will(returnValue(g));
			// message 2
			oneOf(clientHelper).getMessage(txn, storageMessage.getId());
			will(returnValue(storageMessage));
			oneOf(clientHelper).toList(storageMessage);
			will(returnValue(list2));
			oneOf(messageParser).parseInviteMessage(storageMessage, list2);
			will(returnValue(m2));
			oneOf(privateGroupFactory)
					.createPrivateGroup(m2.getGroupName(), m2.getCreator(),
							m2.getSalt());
			will(returnValue(g));
			// end transaction
			oneOf(db).commitTransaction(txn);
			oneOf(db).endTransaction(txn);
		}});

		Collection<GroupInvitationItem> items =
				groupInvitationManager.getInvitations();
		assertEquals(2, items.size());
		for (GroupInvitationItem i : items) {
			assertEquals(contact, i.getCreator());
			assertEquals(author, i.getCreator().getAuthor());
		}
	}

	@Test
	public void testIsInvitationAllowed() throws Exception {
		expectIsInvitationAllowed(CreatorState.START);
		assertTrue(groupInvitationManager
				.isInvitationAllowed(contact, privateGroup.getId()));
	}

	@Test
	public void testIsNotInvitationAllowed() throws Exception {
		expectIsInvitationAllowed(CreatorState.DISSOLVED);
		assertFalse(groupInvitationManager
				.isInvitationAllowed(contact, privateGroup.getId()));

		expectIsInvitationAllowed(CreatorState.ERROR);
		assertFalse(groupInvitationManager
				.isInvitationAllowed(contact, privateGroup.getId()));

		expectIsInvitationAllowed(CreatorState.INVITED);
		assertFalse(groupInvitationManager
				.isInvitationAllowed(contact, privateGroup.getId()));

		expectIsInvitationAllowed(CreatorState.JOINED);
		assertFalse(groupInvitationManager
				.isInvitationAllowed(contact, privateGroup.getId()));

		expectIsInvitationAllowed(CreatorState.LEFT);
		assertFalse(groupInvitationManager
				.isInvitationAllowed(contact, privateGroup.getId()));
	}

	private void expectIsInvitationAllowed(final CreatorState state)
			throws Exception {
		final SessionId sessionId =
				new SessionId(privateGroup.getId().getBytes());
		final BdfDictionary meta = BdfDictionary.of(new BdfEntry("m", "d"));
		final Map<MessageId, BdfDictionary> results = new HashMap<>();
		results.put(message.getId(), meta);

		expectGetSession(results, sessionId);
		context.checking(new Expectations() {{
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
			oneOf(db).startTransaction(true);
			will(returnValue(txn));
			oneOf(sessionParser)
					.parseCreatorSession(contactGroup.getId(), meta);
			will(returnValue(creatorSession));
			oneOf(creatorSession).getState();
			will(returnValue(state));
			oneOf(db).commitTransaction(txn);
			oneOf(db).endTransaction(txn);
		}});
	}

	@Test
	public void testAddingMember() throws Exception {
		expectAddingMember(privateGroup.getId(), contact);
		context.checking(new Expectations() {{
			oneOf(db).getContactsByAuthorId(txn, author.getId());
			will(returnValue(Collections.singletonList(contact)));
		}});
		groupInvitationManager.addingMember(txn, privateGroup.getId(), author);
	}

	@Test
	public void testRemovingGroupEndsSessions() throws Exception {
		final Contact contact2 =
				new Contact(new ContactId(2), author, author.getId(), true,
						true);
		final Contact contact3 =
				new Contact(new ContactId(3), author, author.getId(), true,
						true);
		final Collection<Contact> contacts = new ArrayList<>();
		contacts.add(contact);
		contacts.add(contact2);
		contacts.add(contact3);
		final MessageId mId2 = new MessageId(getRandomId());
		final MessageId mId3 = new MessageId(getRandomId());
		final BdfDictionary meta1 = BdfDictionary.of(new BdfEntry("m1", "d"));
		final BdfDictionary meta2 = BdfDictionary.of(new BdfEntry("m2", "d"));
		final BdfDictionary meta3 = BdfDictionary.of(new BdfEntry("m3", "d"));

		expectGetSession(
				Collections.singletonMap(storageMessage.getId(), meta1),
				new SessionId(privateGroup.getId().getBytes()));
		expectGetSession(
				Collections.singletonMap(mId2, meta2),
				new SessionId(privateGroup.getId().getBytes()));
		expectGetSession(
				Collections.singletonMap(mId3, meta3),
				new SessionId(privateGroup.getId().getBytes()));
		context.checking(new Expectations() {{
			oneOf(db).getContacts(txn);
			will(returnValue(contacts));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact);
			will(returnValue(contactGroup));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact2);
			will(returnValue(contactGroup));
			oneOf(contactGroupFactory).createContactGroup(CLIENT_ID, contact3);
			will(returnValue(contactGroup));
			// session 1
			oneOf(sessionParser).getRole(meta1);
			will(returnValue(Role.CREATOR));
			oneOf(sessionParser)
					.parseCreatorSession(contactGroup.getId(), meta1);
			will(returnValue(creatorSession));
			oneOf(creatorEngine).onLeaveAction(txn, creatorSession);
			will(returnValue(creatorSession));
			// session 2
			oneOf(sessionParser).getRole(meta2);
			will(returnValue(Role.INVITEE));
			oneOf(sessionParser)
					.parseInviteeSession(contactGroup.getId(), meta2);
			will(returnValue(inviteeSession));
			oneOf(inviteeEngine).onLeaveAction(txn, inviteeSession);
			will(returnValue(inviteeSession));
			// session 3
			oneOf(sessionParser).getRole(meta3);
			will(returnValue(Role.PEER));
			oneOf(sessionParser)
					.parsePeerSession(contactGroup.getId(), meta3);
			will(returnValue(peerSession));
			oneOf(peerEngine).onLeaveAction(txn, peerSession);
			will(returnValue(peerSession));
		}});
		expectStoreSession(creatorSession, storageMessage.getId());
		expectStoreSession(inviteeSession, mId2);
		expectStoreSession(peerSession, mId3);
		groupInvitationManager.removingGroup(txn, privateGroup.getId());
	}

}