@Override
    public Optional<User> loadByAuthServiceUidOrUsername(String authServiceUid, String username) {
        checkArgument(!isBlank(authServiceUid), "authServiceUid cannot be blank");
        checkArgument(!isBlank(username), "username cannot be blank");

        LOG.debug("Loading user by auth service UID <{}> or username <{}>", authServiceUid, username);

        // special case for the locally defined user, we don't store that in MongoDB.
        if (!configuration.isRootUserDisabled() && configuration.getRootUsername().equals(username)) {
            LOG.debug("User <{}> is the built-in admin user", username);
            return Optional.ofNullable(userFactory.createLocalAdminUser(roleService.getAdminRoleObjectId()));
        }

        final DBObject query = new BasicDBObject("$or", List.of(
                new BasicDBObject(UserImpl.AUTH_SERVICE_UID, authServiceUid),
                new BasicDBObject(UserImpl.USERNAME, username)
        ));

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

        if (result.size() > 1) {
            final String msg = "There was more than one matching user for auth service UID <" + authServiceUid + "> or username <" + username + ">. This should never happen.";
            LOG.error(msg);
            throw new DuplicateUserException(msg);
        }

        final DBObject userObject = result.get(0);
        final Object userId = userObject.get("_id");

        LOG.debug("Loaded user {}/{}/{} from MongoDB", authServiceUid, username, userId);
        return Optional.ofNullable(userFactory.create((ObjectId) userId, userObject.toMap()));
    }
@Test
    @MongoDBFixtures("UserServiceImplTest.json")
    public void testLoadByAuthServiceUidOrUsername() throws Exception {
        final Optional<User> byAuthServiceUid = userService.loadByAuthServiceUidOrUsername("NmIxY2E3ZWQtMTk3NC00NGM4LTkwOTYtN2Q3OTBlM2Y2MjRmCg==", "external-user1");

        assertThat(byAuthServiceUid).get().satisfies(user -> {
            assertThat(user.getAuthServiceId()).isEqualTo("54e3deadbeefdeadbeef0001");
            assertThat(user.getAuthServiceUid()).isEqualTo("NmIxY2E3ZWQtMTk3NC00NGM4LTkwOTYtN2Q3OTBlM2Y2MjRmCg==");
            assertThat(user.getEmail()).isEqualTo("external-user1@example.com");
        });

        final Optional<User> byUsername = userService.loadByAuthServiceUidOrUsername("__nope__", "external-user1");

        assertThat(byUsername).get().satisfies(user -> {
            assertThat(user.getAuthServiceId()).isEqualTo("54e3deadbeefdeadbeef0001");
            assertThat(user.getAuthServiceUid()).isEqualTo("NmIxY2E3ZWQtMTk3NC00NGM4LTkwOTYtN2Q3OTBlM2Y2MjRmCg==");
            assertThat(user.getEmail()).isEqualTo("external-user1@example.com");
        });

        assertThat(userService.loadByAuthServiceUidOrUsername("__nope__", "__nope__"))
                .isNotPresent();

        assertThatThrownBy(() -> userService.loadByAuthServiceUidOrUsername(null, "__nope__"))
                .hasMessageContaining("authServiceUid")
                .isInstanceOf(IllegalArgumentException.class);
        assertThatThrownBy(() -> userService.loadByAuthServiceUidOrUsername("__nope__", null))
                .hasMessageContaining("username")
                .isInstanceOf(IllegalArgumentException.class);
        assertThatThrownBy(() -> userService.loadByAuthServiceUidOrUsername(null, null))
                .isInstanceOf(IllegalArgumentException.class);
    }