Skip to content

Commit 1ef6fce

Browse files
author
Siqi Ding
committed
Making change on code based on comments. Refactor tken avalidation logic, rename test classes, and move test into a new package 'testcustomauth'
1 parent e918e31 commit 1ef6fce

9 files changed

+109
-295
lines changed

Diff for: data-prepper-plugins/armeria-common/src/main/java/org/opensearch/dataprepper/CustomAuthenticationExceptionHandler.java

-98
This file was deleted.

Diff for: data-prepper-plugins/armeria-common/src/main/java/org/opensearch/dataprepper/armeria/authentication/CustomAuthenticationConfig.java

-23
This file was deleted.

Diff for: data-prepper-plugins/armeria-common/src/test/java/org/opensearch/dataprepper/CustomAuthenticationExceptionHandlerTest.java

-126
This file was deleted.

Diff for: data-prepper-plugins/armeria-common/src/test/java/org/opensearch/dataprepper/plugins/CustomAuthenticationProviderTest.java

-26
This file was deleted.

Diff for: data-prepper-plugins/armeria-common/src/test/java/org/opensearch/dataprepper/plugins/CustomBasicAuthenticationProviderTest.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121
import org.junit.jupiter.api.Nested;
2222
import org.junit.jupiter.api.Test;
2323
import org.junit.jupiter.api.extension.RegisterExtension;
24-
import org.opensearch.dataprepper.armeria.authentication.CustomAuthenticationConfig;
24+
import org.opensearch.dataprepper.plugins.testcustomauth.TestCustomAuthenticationConfig;
2525
import org.opensearch.dataprepper.armeria.authentication.GrpcAuthenticationProvider;
26+
import org.opensearch.dataprepper.plugins.testcustomauth.TestCustomGrpcAuthenticationProvider;
2627

2728
import java.nio.charset.Charset;
2829
import java.util.Collections;
@@ -36,16 +37,18 @@
3637

3738
public class CustomBasicAuthenticationProviderTest {
3839
private static final String TOKEN = UUID.randomUUID().toString();
40+
private static final String HEADER_NAME = "x-" + UUID.randomUUID();
3941
private static GrpcAuthenticationProvider grpcAuthenticationProvider;
4042

4143
@RegisterExtension
4244
static ServerExtension server = new ServerExtension() {
4345
@Override
4446
protected void configure(ServerBuilder sb) {
45-
CustomAuthenticationConfig config = mock(CustomAuthenticationConfig.class);
47+
TestCustomAuthenticationConfig config = mock(TestCustomAuthenticationConfig.class);
4648
when(config.customToken()).thenReturn(TOKEN);
49+
when(config.header()).thenReturn(HEADER_NAME);
4750

48-
grpcAuthenticationProvider = new CustomGrpcAuthenticationProvider(config);
51+
grpcAuthenticationProvider = new TestCustomGrpcAuthenticationProvider(config);
4952

5053
GrpcServiceBuilder grpcServiceBuilder = GrpcService.builder()
5154
.enableUnframedRequests(true)
@@ -68,16 +71,16 @@ public void check(HealthCheckRequest request, StreamObserver<HealthCheckResponse
6871

6972
@Nested
7073
class ConstructorTests {
71-
CustomAuthenticationConfig config;
74+
TestCustomAuthenticationConfig config;
7275

7376
@BeforeEach
7477
void setUp() {
75-
config = mock(CustomAuthenticationConfig.class);
78+
config = mock(TestCustomAuthenticationConfig.class);
7679
}
7780

7881
@Test
7982
void constructor_with_null_config_throws() {
80-
assertThrows(NullPointerException.class, () -> new CustomGrpcAuthenticationProvider(null));
83+
assertThrows(NullPointerException.class, () -> new TestCustomGrpcAuthenticationProvider(null));
8184
}
8285
}
8386

@@ -100,7 +103,7 @@ void request_without_token_responds_Unauthorized() {
100103
@Test
101104
void request_with_invalid_token_responds_Unauthorized() {
102105
WebClient client = WebClient.builder(server.httpUri())
103-
.addHeader("authentication", "invalid-token")
106+
.addHeader(HEADER_NAME, "invalid-token")
104107
.build();
105108

106109
HttpRequest request = HttpRequest.of(RequestHeaders.builder()
@@ -117,7 +120,7 @@ void request_with_invalid_token_responds_Unauthorized() {
117120
@Test
118121
void request_with_valid_token_responds_OK() {
119122
WebClient client = WebClient.builder(server.httpUri())
120-
.addHeader("authentication", TOKEN)
123+
.addHeader(HEADER_NAME, TOKEN)
121124
.build();
122125

123126
HttpRequest request = HttpRequest.of(RequestHeaders.builder()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.opensearch.dataprepper.plugins;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.api.extension.ExtendWith;
7+
import org.mockito.Mock;
8+
import org.mockito.junit.jupiter.MockitoExtension;
9+
import org.opensearch.dataprepper.plugins.testcustomauth.TestCustomAuthenticationConfig;
10+
import org.opensearch.dataprepper.plugins.testcustomauth.TestCustomGrpcAuthenticationProvider;
11+
12+
import static org.mockito.Mockito.when;
13+
14+
@ExtendWith(MockitoExtension.class)
15+
public class TestCustomAuthenticationProviderTest {
16+
17+
private static final String TOKEN = "test-token";
18+
private static final String HEADER = "authentication";
19+
20+
@Mock
21+
private TestCustomAuthenticationConfig config;
22+
23+
private TestCustomGrpcAuthenticationProvider provider;
24+
25+
@BeforeEach
26+
void setUp() {
27+
when(config.customToken()).thenReturn(TOKEN);
28+
when(config.header()).thenReturn(HEADER);
29+
30+
provider = new TestCustomGrpcAuthenticationProvider(config);
31+
}
32+
33+
@Test
34+
void testGetHttpAuthenticationService_shouldReturnValidOptional() {
35+
var optionalService = provider.getHttpAuthenticationService();
36+
Assertions.assertTrue(optionalService.isPresent());
37+
}
38+
39+
@Test
40+
void testGetAuthenticationInterceptor_shouldReturnNonNull() {
41+
var interceptor = provider.getAuthenticationInterceptor();
42+
Assertions.assertNotNull(interceptor);
43+
}
44+
}
45+

0 commit comments

Comments
 (0)