package org.apereo.cas.support.oauth.web.response.accesstoken;

import org.apereo.cas.AbstractOAuth20Tests;
import org.apereo.cas.support.oauth.web.response.accesstoken.response.OAuth20JwtAccessTokenEncoder;

import lombok.val;
import org.jose4j.jws.AlgorithmIdentifiers;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

import java.util.LinkedHashSet;

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

/**
 * This is {@link OAuth20AccessTokenAtHashGeneratorTests}.
 *
 * @author Misagh Moayyed
 * @since 6.1.0
 */
@Tag("OAuth")
public class OAuth20AccessTokenAtHashGeneratorTests extends AbstractOAuth20Tests {
    @Test
    public void verifyNoneAlgorithm() {
        val hash = generateHashWithAlgorithm("none");
        assertEquals("QUJDRA", hash);
    }

    private String generateHashWithAlgorithm(final String alg) {
        val accessToken = getAccessToken();
        val registeredService = getRegisteredService("example", "secret", new LinkedHashSet<>());

        val encodedAccessToken = OAuth20JwtAccessTokenEncoder.builder()
            .accessToken(accessToken)
            .registeredService(registeredService)
            .service(accessToken.getService())
            .casProperties(casProperties)
            .accessTokenJwtBuilder(accessTokenJwtBuilder)
            .build()
            .encode();

        return OAuth20AccessTokenAtHashGenerator.builder()
            .algorithm(alg)
            .registeredService(registeredService)
            .encodedAccessToken(encodedAccessToken)
            .build()
            .generate();
    }

    @Test
    public void verifySha512Algorithm() {
        val hash = generateHashWithAlgorithm(AlgorithmIdentifiers.RSA_USING_SHA512);
        assertEquals("SexVvYP81ng449OFzoMWaeP4Faf0S3ql-NUrXUI1TEY", hash);
    }

    @Test
    public void verifySha256Algorithm() {
        val hash = generateHashWithAlgorithm(AlgorithmIdentifiers.RSA_USING_SHA256);
        assertEquals("4S4RWs9FUrJWi1XpPL05OQ", hash);
    }

    @Test
    public void verifySha384Algorithm() {
        val hash = generateHashWithAlgorithm(AlgorithmIdentifiers.RSA_USING_SHA384);
        assertEquals("bxfiOJnSNFoVa69p58Arvdo74Fc2eEnA", hash);
    }

    @Test
    public void verifyUnknownAlgorithm() {
        assertThrows(IllegalArgumentException.class, () -> generateHashWithAlgorithm("xyz"));
    }
}

@@ -759,13 +759,19 @@
 s(%22Q
-UJDRA
+VQtMTIzNDU2
 %22, h
@@ -1748,51 +1748,51 @@
 ls(%22
-SexVvYP81ng449OFzoMWaeP4Faf0S3ql-NUrXUI1TEY
+EZsVCVYBY_zw4RAkJ3s759RxOZ8UingrP-52KQX4G6E
 %22, h
@@ -1967,29 +1967,29 @@
 ls(%22
-4S4RWs9FUrJWi1XpPL05O
+IzG3xSUlcgF_gHDCvKd3f
 Q%22, 
@@ -2165,40 +2165,40 @@
 ls(%22
-bxfiOJnSNFoVa69p58Arvdo74Fc2eEnA
+9Kb1tRRQ1YATHOB95-YAH3LHmnF2Lu7l
 %22, h

fix tests