package com.atlassian.bitbucket.internal.mirroring.mirror.user;

import com.atlassian.bitbucket.internal.mirroring.user.ApplicationUserWithPermissions;
import com.atlassian.bitbucket.permission.EffectivePermission;
import org.hamcrest.FeatureMatcher;
import org.hamcrest.Matcher;

import javax.annotation.Nonnull;

import static com.atlassian.bitbucket.hamcrest.StashUserMatchers.userWithId;
import static com.atlassian.bitbucket.hamcrest.StashUserMatchers.userWithName;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.hamcrest.Matchers.allOf;

public class ApplicationUserWithPermissionsMatchers {
    public static Matcher<? super ApplicationUserWithPermissions> userWithPermissions(@Nonnull Integer expectedId, @Nonnull String expectedName,
                                                                                @Nonnull Iterable<EffectivePermission> expectedEffectivePermissions) {
        return allOf(userWithId(expectedId), userWithName(expectedName),
                userWithPermissionsThat(EffectivePermissionsMatchers.matcherFor(expectedEffectivePermissions)));
    }

    public static Matcher<? super ApplicationUserWithPermissions> userWithPermissionsThat(@Nonnull Matcher<Iterable<? extends EffectivePermission>> matcher) {
        checkNotNull(matcher, "matcher");

        return new FeatureMatcher<ApplicationUserWithPermissions, Iterable<EffectivePermission>>(matcher, "effective permissions that", "effectivePermissions") {
            @Override
            protected Iterable<EffectivePermission> featureValueOf(ApplicationUserWithPermissions actual) {
                return actual.getEffectivePermissions();
            }
        };
    }
}
