@Test
 public void findPrimaryConnection_afterRemove() {
   insertFacebookConnection();
   insertFacebookConnection2();
   // 9 is the providerUserId of the first Facebook connection
   connectionRepository.removeConnection(new ConnectionKey("facebook", "9"));
   assertEquals(1, connectionRepository.findConnections(TestFacebookApi.class).size());
   assertNotNull(connectionRepository.findPrimaryConnection(TestFacebookApi.class));
 }
 @Test
 public void addConnection() {
   Connection<TestFacebookApi> connection =
       connectionFactory.createConnection(new AccessGrant("123456789", null, "987654321", 3600L));
   connectionRepository.addConnection(connection);
   Connection<TestFacebookApi> restoredConnection =
       connectionRepository.getPrimaryConnection(TestFacebookApi.class);
   assertEquals(connection, restoredConnection);
   assertNewConnection(restoredConnection);
 }
 @Test
 public void findConnectionByApiToUser() {
   insertFacebookConnection();
   insertFacebookConnection2();
   assertFacebookConnection(connectionRepository.getConnection(TestFacebookApi.class, "9"));
   assertEquals(
       "10",
       connectionRepository
           .getConnection(TestFacebookApi.class, "10")
           .getKey()
           .getProviderUserId());
 }
 @Test
 public void updateConnectionProfileFields() {
   connectionFactoryRegistry.addConnectionFactory(new TestTwitterConnectionFactory());
   insertTwitterConnection();
   Connection<TestTwitterApi> twitter =
       connectionRepository.getPrimaryConnection(TestTwitterApi.class);
   assertEquals("http://twitter.com/kdonald/picture", twitter.getImageUrl());
   twitter.sync();
   assertEquals("http://twitter.com/kdonald/a_new_picture", twitter.getImageUrl());
   connectionRepository.updateConnection(twitter);
   Connection<TestTwitterApi> twitter2 =
       connectionRepository.getPrimaryConnection(TestTwitterApi.class);
   assertEquals("http://twitter.com/kdonald/a_new_picture", twitter2.getImageUrl());
 }
 @Test
 public void updateConnectionAccessFields() {
   insertFacebookConnection();
   Connection<TestFacebookApi> facebook =
       connectionRepository.getPrimaryConnection(TestFacebookApi.class);
   assertEquals("234567890", facebook.getApi().getAccessToken());
   facebook.refresh();
   connectionRepository.updateConnection(facebook);
   Connection<TestFacebookApi> facebook2 =
       connectionRepository.getPrimaryConnection(TestFacebookApi.class);
   assertEquals("765432109", facebook2.getApi().getAccessToken());
   ConnectionData data = facebook.createData();
   assertEquals("654321098", data.getRefreshToken());
 }
 @Test
 public void removeConnection() {
   SocialUserConnection facebookConnection = insertFacebookConnection();
   assertThat(socialUserConnectionRepository.findOne(facebookConnection.getId())).isNotNull();
   connectionRepository.removeConnection(new ConnectionKey("facebook", "9"));
   assertThat(socialUserConnectionRepository.findOne(facebookConnection.getId())).isNull();
 }
 @Test(expected = DataIntegrityViolationException.class)
 public void addConnectionDuplicate() {
   Connection<TestFacebookApi> connection =
       connectionFactory.createConnection(new AccessGrant("123456789", null, "987654321", 3600L));
   connectionRepository.addConnection(connection);
   connectionRepository.addConnection(connection);
 }
 @Test
 @SuppressWarnings("unchecked")
 public void findConnectionByKey() {
   insertFacebookConnection();
   assertFacebookConnection(
       (Connection<TestFacebookApi>)
           connectionRepository.getConnection(new ConnectionKey("facebook", "9")));
 }
 @Test
 public void findUserIdWithConnection() {
   insertFacebookConnection();
   List<String> userIds =
       usersConnectionRepository.findUserIdsWithConnection(
           connectionRepository.getPrimaryConnection(TestFacebookApi.class));
   assertEquals("1", userIds.get(0));
 }
 @Test
 public void findAllConnectionsEmptyResult() {
   connectionFactoryRegistry.addConnectionFactory(new TestTwitterConnectionFactory());
   MultiValueMap<String, Connection<?>> connections = connectionRepository.findAllConnections();
   assertEquals(2, connections.size());
   assertEquals(0, connections.get("facebook").size());
   assertEquals(0, connections.get("twitter").size());
 }
 @Test
 public void findConnectionsByApi() {
   insertFacebookConnection();
   insertFacebookConnection2();
   List<Connection<TestFacebookApi>> connections =
       connectionRepository.findConnections(TestFacebookApi.class);
   assertEquals(2, connections.size());
   assertFacebookConnection(connections.get(0));
 }
 @Test
 @SuppressWarnings("unchecked")
 public void findConnectionsByProviderId() {
   connectionFactoryRegistry.addConnectionFactory(new TestTwitterConnectionFactory());
   insertTwitterConnection();
   List<Connection<?>> connections = connectionRepository.findConnections("twitter");
   assertEquals(1, connections.size());
   assertTwitterConnection((Connection<TestTwitterApi>) connections.get(0));
 }
 @Test
 public void findUserIdWithConnectionMultipleConnectionsToSameProviderUser() {
   insertFacebookConnection();
   insertFacebookConnectionSameFacebookUser();
   List<String> localUserIds =
       usersConnectionRepository.findUserIdsWithConnection(
           connectionRepository.getPrimaryConnection(TestFacebookApi.class));
   assertEquals(2, localUserIds.size());
   assertEquals("1", localUserIds.get(0));
   assertEquals("2", localUserIds.get(1));
 }
 @Test
 @SuppressWarnings("unchecked")
 public void findAllConnections() {
   connectionFactoryRegistry.addConnectionFactory(new TestTwitterConnectionFactory());
   insertTwitterConnection();
   insertFacebookConnection();
   MultiValueMap<String, Connection<?>> connections = connectionRepository.findAllConnections();
   assertEquals(2, connections.size());
   Connection<TestFacebookApi> facebook =
       (Connection<TestFacebookApi>) connections.getFirst("facebook");
   assertFacebookConnection(facebook);
   Connection<TestTwitterApi> twitter =
       (Connection<TestTwitterApi>) connections.getFirst("twitter");
   assertTwitterConnection(twitter);
 }
 @Test
 @SuppressWarnings("unchecked")
 public void findConnectionsToUsers() {
   connectionFactoryRegistry.addConnectionFactory(new TestTwitterConnectionFactory());
   insertTwitterConnection();
   insertFacebookConnection();
   insertFacebookConnection2();
   MultiValueMap<String, String> providerUsers = new LinkedMultiValueMap<>();
   providerUsers.add("facebook", "10");
   providerUsers.add("facebook", "9");
   providerUsers.add("twitter", "1");
   MultiValueMap<String, Connection<?>> connectionsForUsers =
       connectionRepository.findConnectionsToUsers(providerUsers);
   assertEquals(2, connectionsForUsers.size());
   assertEquals("10", connectionsForUsers.getFirst("facebook").getKey().getProviderUserId());
   assertFacebookConnection(
       (Connection<TestFacebookApi>) connectionsForUsers.get("facebook").get(1));
   assertTwitterConnection((Connection<TestTwitterApi>) connectionsForUsers.getFirst("twitter"));
 }
 @Test(expected = NoSuchConnectionException.class)
 public void findConnectionByApiToUserNoSuchConnection() {
   assertFacebookConnection(connectionRepository.getConnection(TestFacebookApi.class, "9"));
 }
 @Test(expected = IllegalArgumentException.class)
 public void noSuchConnectionFactory() {
   insertTwitterConnection();
   connectionRepository.findAllConnections();
 }
 @Test
 public void findConnectionsByProviderIdEmptyResult() {
   assertTrue(connectionRepository.findConnections("facebook").isEmpty());
 }
 @Test
 public void findConnectionsByApiEmptyResult() {
   assertTrue(connectionRepository.findConnections(TestFacebookApi.class).isEmpty());
 }
 @Test
 public void findConnectionsToUsersEmptyResult() {
   MultiValueMap<String, String> providerUsers = new LinkedMultiValueMap<>();
   providerUsers.add("facebook", "1");
   assertTrue(connectionRepository.findConnectionsToUsers(providerUsers).isEmpty());
 }
 @Test
 public void removeConnectionNoOp() {
   connectionRepository.removeConnection(new ConnectionKey("facebook", "1"));
 }
 @Test(expected = IllegalArgumentException.class)
 public void findConnectionsToUsersEmptyInput() {
   MultiValueMap<String, String> providerUsers = new LinkedMultiValueMap<>();
   connectionRepository.findConnectionsToUsers(providerUsers);
 }
 @Test(expected = NoSuchConnectionException.class)
 public void findConnectionByKeyNoSuchConnection() {
   connectionRepository.getConnection(new ConnectionKey("facebook", "bogus"));
 }
 @Test
 public void removeConnectionsToProviderNoOp() {
   connectionRepository.removeConnections("twitter");
 }
 @Test(expected = NotConnectedException.class)
 public void findPrimaryConnectionNotConnected() {
   connectionRepository.getPrimaryConnection(TestFacebookApi.class);
 }
 @Test
 public void findPrimaryConnectionSelectFromMultipleByRank() {
   insertFacebookConnection2();
   insertFacebookConnection();
   assertFacebookConnection(connectionRepository.getPrimaryConnection(TestFacebookApi.class));
 }
 @Test
 public void findPrimaryConnection() {
   insertFacebookConnection();
   assertFacebookConnection(connectionRepository.getPrimaryConnection(TestFacebookApi.class));
 }