package com.magistr.app.config.tenant;

import com.magistr.app.config.auth.AuthorizationInterceptor;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.handler.MappedInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;

class TenantWebMvcConfigTest {

    @Test
    @DisplayName("Actuator probes исключены из проверки домена тенанта")
    void actuatorProbesAreExcludedFromTenantInterceptor() {
        TenantRoutingDataSource routingDataSource = mock(TenantRoutingDataSource.class);
        AuthorizationInterceptor authorizationInterceptor = mock(AuthorizationInterceptor.class);
        TenantWebMvcConfig config = new TenantWebMvcConfig(routingDataSource, authorizationInterceptor);
        ExposedInterceptorRegistry registry = new ExposedInterceptorRegistry();

        config.addInterceptors(registry);

        MappedInterceptor tenantInterceptor = (MappedInterceptor) registry.interceptors().get(0);
        AntPathMatcher pathMatcher = new AntPathMatcher();

        assertThat(tenantInterceptor.matches("/actuator/health/liveness", pathMatcher)).isFalse();
        assertThat(tenantInterceptor.matches("/actuator/health/readiness", pathMatcher)).isFalse();
        assertThat(tenantInterceptor.matches("/api/schedule", pathMatcher)).isTrue();
    }

    private static final class ExposedInterceptorRegistry extends InterceptorRegistry {

        private List<Object> interceptors() {
            return getInterceptors();
        }
    }
}
