package org.apereo.cas.support.saml.web.idp.profile.builders.subject;

import org.apereo.cas.support.saml.BaseSamlIdPConfigurationTests;
import org.apereo.cas.support.saml.services.idp.metadata.SamlRegisteredServiceServiceProviderMetadataFacade;

import lombok.val;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.opensaml.messaging.context.MessageContext;
import org.opensaml.saml.common.xml.SAMLConstants;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

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

/**
 * This is {@link SamlProfileSamlSubjectBuilderTests}.
 *
 * @author Misagh Moayyed
 * @since 6.3.0
 */
@Tag("SAML")
public class SamlProfileSamlSubjectBuilderTests extends BaseSamlIdPConfigurationTests {

    @Test
    public void verifySubjectWithNoNameId() {
        val request = new MockHttpServletRequest();
        val response = new MockHttpServletResponse();

        val service = getSamlRegisteredServiceForTestShib(true, true);
        service.setSkewAllowance(1000);
        service.setSkipGeneratingAssertionNameId(true);
        service.setSkipGeneratingSubjectConfirmationNotOnOrAfter(true);
        service.setSkipGeneratingSubjectConfirmationNameId(true);
        val adaptor = SamlRegisteredServiceServiceProviderMetadataFacade.get(
            samlRegisteredServiceCachingMetadataResolver,
            service, service.getServiceId()).get();

        val authnRequest = getAuthnRequestFor(service);
        val assertion = getAssertion();

        val result = samlProfileSamlSubjectBuilder.build(authnRequest, request, response,
            assertion, service, adaptor,
            SAMLConstants.SAML2_POST_BINDING_URI,
            new MessageContext());
        assertNotNull(result);
    }

    @Test
    public void verifySubjectWithSkewedConfData() {
        val request = new MockHttpServletRequest();
        val response = new MockHttpServletResponse();

        val service = getSamlRegisteredServiceForTestShib(true, true);
        service.setSkewAllowance(1000);
        val adaptor = SamlRegisteredServiceServiceProviderMetadataFacade.get(
            samlRegisteredServiceCachingMetadataResolver,
            service, service.getServiceId()).get();

        val authnRequest = getAuthnRequestFor(service);
        val assertion = getAssertion();

        val result = samlProfileSamlSubjectBuilder.build(authnRequest, request, response,
            assertion, service, adaptor,
            SAMLConstants.SAML2_POST_BINDING_URI,
            new MessageContext());
        assertNotNull(result);

        var subjectData = result.getSubjectConfirmations().get(0).getSubjectConfirmationData();
        assertEquals(assertion.getValidFromDate().toInstant().plusSeconds(service.getSkewAllowance()), subjectData.getNotOnOrAfter());
    }
}

@@ -551,24 +551,124 @@
 tResponse;%0A%0A
+import java.time.ZoneOffset;%0Aimport java.time.ZonedDateTime;%0Aimport java.time.temporal.ChronoUnit;%0A%0A
 import stati
@@ -2494,37 +2494,189 @@
 );%0A%0A        val 
-resul
+now = ZonedDateTime.now(ZoneOffset.UTC)%0A            .plusSeconds(service.getSkewAllowance()).toInstant().truncatedTo(ChronoUnit.SECONDS);%0A%0A        val subjec
 t = samlProfileS
@@ -2876,29 +2876,30 @@
 sertNotNull(
-resul
+subjec
 t);%0A%0A       
@@ -2901,17 +2901,17 @@
       va
-r
+l
  subject
@@ -2917,21 +2917,22 @@
 tData = 
-resul
+subjec
 t.getSub
@@ -3013,118 +3013,73 @@
 als(
-assertion.getValidFromDate().toInstant().plusSeconds(service.getSkewAllowance()), subjectData.getNotOnOrAfter(
+now, subjectData.getNotOnOrAfter().truncatedTo(ChronoUnit.SECONDS
 ));%0A

fix tests