@Override
    public int delete(final String username) {
        DBObject query = new BasicDBObject();
        query.put(UserImpl.USERNAME, username);

        final List<DBObject> result = query(UserImpl.class, query);
        if (result == null || result.isEmpty()) {
            return 0;
        }

        final ImmutableList.Builder<UserDeletedEvent> deletedUsersBuilder = ImmutableList.builder();
        result.forEach(userObject -> {
            final ObjectId userId = (ObjectId) userObject.get("_id");
            deletedUsersBuilder.add(UserDeletedEvent.create(userId.toHexString(), username));
        });

        LOG.debug("Deleting user(s) with username \"{}\"", username);
        query = BasicDBObjectBuilder.start(UserImpl.USERNAME, username).get();
        final int deleteCount = destroy(query, UserImpl.COLLECTION_NAME);

        if (deleteCount > 1) {
            LOG.warn("Removed {} users matching username \"{}\".", deleteCount, username);
        }
        accesstokenService.deleteAllForUser(username); //TODO: probably should go through listener subscribing to delete event
        final ImmutableList<UserDeletedEvent> deletedUsers = deletedUsersBuilder.build();
        deletedUsers.forEach(serverEventBus::post);
        return deleteCount;
    }
@Test
    @MongoDBFixtures("UserServiceImplTest.json")
    public void testDeleteByName() throws Exception {
        assertThat(userService.delete("user1")).isEqualTo(1);
        assertThat(userService.delete("user-duplicate")).isEqualTo(2);
        assertThat(userService.delete("user-does-not-exist")).isEqualTo(0);
    }