Skip to content

Commit

Permalink
Fix test-latest-deps (#12909)
Browse files Browse the repository at this point in the history
  • Loading branch information
trask authored Dec 18, 2024
1 parent b7ae6c5 commit 8d02fd5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
package io.opentelemetry.javaagent.instrumentation.spring.security.config.v6_0.servlet;

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

import io.opentelemetry.instrumentation.spring.security.config.v6_0.servlet.EnduserAttributesCapturingServletFilter;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
Expand All @@ -29,8 +29,6 @@ class HttpSecurityInstrumentationTest {
@Configuration
static class TestConfiguration {}

@Mock ObjectPostProcessor<Object> objectPostProcessor;

/**
* Ensures that {@link HttpSecurityInstrumentation} registers a {@link
* EnduserAttributesCapturingServletFilter} in the filter chain.
Expand All @@ -40,14 +38,7 @@ static class TestConfiguration {}
@Test
void ensureFilterRegistered(@Autowired ApplicationContext applicationContext) throws Exception {

AuthenticationManagerBuilder authenticationBuilder =
new AuthenticationManagerBuilder(objectPostProcessor);

HttpSecurity httpSecurity =
new HttpSecurity(
objectPostProcessor,
authenticationBuilder,
Collections.singletonMap(ApplicationContext.class, applicationContext));
HttpSecurity httpSecurity = createHttpSecurity(applicationContext);

DefaultSecurityFilterChain filterChain = httpSecurity.build();

Expand All @@ -59,4 +50,29 @@ void ensureFilterRegistered(@Autowired ApplicationContext applicationContext) th
.endsWith(EnduserAttributesCapturingServletFilter.class.getSimpleName()))
.hasSize(1);
}

private static HttpSecurity createHttpSecurity(ApplicationContext applicationContext)
throws Exception {

Class<?> processorClass = getObjectPostProcessorClass();
Object processor = mock(processorClass);
AuthenticationManagerBuilder authenticationBuilder =
AuthenticationManagerBuilder.class.getConstructor(processorClass).newInstance(processor);

return HttpSecurity.class
.getConstructor(processorClass, AuthenticationManagerBuilder.class, Map.class)
.newInstance(
processor,
authenticationBuilder,
Collections.singletonMap(ApplicationContext.class, applicationContext));
}

private static Class<?> getObjectPostProcessorClass() throws ClassNotFoundException {
try {
return Class.forName("org.springframework.security.config.ObjectPostProcessor");
} catch (ClassNotFoundException e) {
// this was marked deprecated for removal in 6.4.2
return Class.forName("org.springframework.security.config.annotation.ObjectPostProcessor");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
package io.opentelemetry.instrumentation.spring.security.config.v6_0.servlet;

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

import io.opentelemetry.instrumentation.spring.security.config.v6_0.EnduserAttributesCapturer;
import java.util.Collections;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.ObjectPostProcessor;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
Expand All @@ -29,8 +29,6 @@ class EnduserAttributesHttpSecurityCustomizerTest {
@Configuration
static class TestConfiguration {}

@Mock ObjectPostProcessor<Object> objectPostProcessor;

/**
* Ensures that the {@link EnduserAttributesHttpSecurityCustomizer} registers a {@link
* EnduserAttributesCapturingServletFilter} in the filter chain.
Expand All @@ -40,13 +38,7 @@ static class TestConfiguration {}
@Test
void ensureFilterRegistered(@Autowired ApplicationContext applicationContext) throws Exception {

AuthenticationManagerBuilder authenticationBuilder =
new AuthenticationManagerBuilder(objectPostProcessor);
HttpSecurity httpSecurity =
new HttpSecurity(
objectPostProcessor,
authenticationBuilder,
Collections.singletonMap(ApplicationContext.class, applicationContext));
HttpSecurity httpSecurity = createHttpSecurity(applicationContext);

EnduserAttributesHttpSecurityCustomizer customizer =
new EnduserAttributesHttpSecurityCustomizer(new EnduserAttributesCapturer());
Expand All @@ -58,4 +50,29 @@ void ensureFilterRegistered(@Autowired ApplicationContext applicationContext) th
.filteredOn(EnduserAttributesCapturingServletFilter.class::isInstance)
.hasSize(1);
}

private static HttpSecurity createHttpSecurity(ApplicationContext applicationContext)
throws Exception {

Class<?> processorClass = getObjectPostProcessorClass();
Object processor = mock(processorClass);
AuthenticationManagerBuilder authenticationBuilder =
AuthenticationManagerBuilder.class.getConstructor(processorClass).newInstance(processor);

return HttpSecurity.class
.getConstructor(processorClass, AuthenticationManagerBuilder.class, Map.class)
.newInstance(
processor,
authenticationBuilder,
Collections.singletonMap(ApplicationContext.class, applicationContext));
}

private static Class<?> getObjectPostProcessorClass() throws ClassNotFoundException {
try {
return Class.forName("org.springframework.security.config.ObjectPostProcessor");
} catch (ClassNotFoundException e) {
// this was marked deprecated for removal in 6.4.2
return Class.forName("org.springframework.security.config.annotation.ObjectPostProcessor");
}
}
}

0 comments on commit 8d02fd5

Please sign in to comment.