package org.apereo.cas.authentication;

import org.apereo.cas.authentication.principal.PrincipalFactoryUtils;
import org.apereo.cas.authentication.surrogate.SimpleSurrogateAuthenticationService;
import org.apereo.cas.services.RegisteredServiceTestUtils;
import org.apereo.cas.services.ServicesManager;
import org.apereo.cas.util.CollectionUtils;

import lombok.val;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * This is {@link SurrogatePrincipalElectionStrategyTests}.
 *
 * @author Misagh Moayyed
 * @since 6.0.0
 */
public class SurrogatePrincipalElectionStrategyTests {
    @Test
    public void verifyOperation() {
        val strategy = new SurrogatePrincipalElectionStrategy();
        val attributes = CollectionUtils.wrap(
            "formalName", CollectionUtils.wrapSet("cas"),
            "theName", CollectionUtils.wrapSet("user"),
            "sysuser", CollectionUtils.wrapSet("casuser"),
            "firstName", CollectionUtils.wrapSet("cas-first"),
            "lastName", CollectionUtils.wrapSet("cas-last"));

        val authentications = new ArrayList<Authentication>();
        val primaryAuth = CoreAuthenticationTestUtils.getAuthentication("casuser");
        authentications.add(primaryAuth);

        val attributeRepository = CoreAuthenticationTestUtils.getAttributeRepository();
        val surrogatePrincipalBuilder = new SurrogatePrincipalBuilder(PrincipalFactoryUtils.newPrincipalFactory(), attributeRepository,
            new SimpleSurrogateAuthenticationService(Map.of("test", List.of("surrogate")), mock(ServicesManager.class)));
        val surrogatePrincipal = surrogatePrincipalBuilder.buildSurrogatePrincipal("cas-surrogate",
            primaryAuth.getPrincipal(), RegisteredServiceTestUtils.getRegisteredService());

        authentications.add(CoreAuthenticationTestUtils.getAuthentication(surrogatePrincipal));
        val principal = strategy.nominate(authentications, (Map) attributes);
        assertNotNull(principal);
        assertEquals("cas-surrogate", principal.getId());
        assertEquals(attributeRepository.getBackingMap().size(), principal.getAttributes().size());

        val result = attributeRepository.getBackingMap().keySet()
            .stream()
            .filter(key -> !principal.getAttributes().containsKey(key))
            .findAny();
        if (result.isPresent()) {
            fail();
        }
    }
}

@@ -33,16 +33,74 @@
 ation;%0A%0A
+import org.apereo.cas.authentication.principal.Principal;%0A
 import o
@@ -157,24 +157,24 @@
 ctoryUtils;%0A
-
 import org.a
@@ -413,24 +413,82 @@
 lombok.val;%0A
+import org.apereo.services.persondir.IPersonAttributeDao;%0A
 import org.j
@@ -841,16 +841,1274 @@
 Tests %7B%0A
+    private static Principal buildSurrogatePrincipal(final String surrogateId,%0A                                                     final Authentication primaryAuth,%0A                                                     final IPersonAttributeDao attributeRepository) %7B%0A        val surrogatePrincipalBuilder = new SurrogatePrincipalBuilder(PrincipalFactoryUtils.newPrincipalFactory(), attributeRepository,%0A            new SimpleSurrogateAuthenticationService(Map.of(%22test%22, List.of(%22surrogate%22)),%0A                mock(ServicesManager.class)));%0A        return surrogatePrincipalBuilder.buildSurrogatePrincipal(surrogateId,%0A            primaryAuth.getPrincipal(),%0A            RegisteredServiceTestUtils.getRegisteredService());%0A    %7D%0A%0A    @Test%0A    public void verifyNominate() %7B%0A        val surrogate = buildSurrogatePrincipal(%22cas-surrogate%22,%0A            CoreAuthenticationTestUtils.getAuthentication(%22casuser%22),%0A            CoreAuthenticationTestUtils.getAttributeRepository());%0A%0A        val strategy = new SurrogatePrincipalElectionStrategy();%0A        val result = strategy.nominate(List.of(CoreAuthenticationTestUtils.getPrincipal(%22two%22), surrogate),%0A            CoreAuthenticationTestUtils.getAttributes());%0A        assertEquals(result, surrogate);%0A    %7D%0A%0A
     @Tes
@@ -2868,425 +2868,84 @@
 ipal
-Builder = new SurrogatePrincipalBuilder(PrincipalFactoryUtils.newPrincipalFactory(), attributeRepository,%0A            new SimpleSurrogateAuthenticationService(Map.of(%22test%22, List.of(%22surrogate%22)), mock(ServicesManager.class)));%0A        val surrogatePrincipal = surrogatePrincipalBuilder.buildSurrogatePrincipal(%22cas-surrogate%22,%0A            primaryAuth.getPrincipal(), RegisteredServiceTestUtils.getRegisteredService()
+ = buildSurrogatePrincipal(%22cas-surrogate%22, primaryAuth, attributeRepository
 );%0A%0A

fix tests