forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test UserAgent header for Event Grid (Azure#35774)
* add UserAgent header for Event Grid
- Loading branch information
Showing
1 changed file
with
68 additions
and
0 deletions.
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
...est/java/com/azure/spring/cloud/autoconfigure/useragent/http/EventGridUserAgentTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.spring.cloud.autoconfigure.useragent.http; | ||
|
||
import com.azure.core.util.BinaryData; | ||
import com.azure.messaging.eventgrid.EventGridEvent; | ||
import com.azure.messaging.eventgrid.EventGridPublisherAsyncClient; | ||
import com.azure.messaging.eventgrid.EventGridPublisherClient; | ||
import com.azure.messaging.eventgrid.EventGridPublisherClientBuilder; | ||
import com.azure.spring.cloud.autoconfigure.context.AzureGlobalProperties; | ||
import com.azure.spring.cloud.autoconfigure.implementation.eventgrid.AzureEventGridAutoConfiguration; | ||
import com.azure.spring.cloud.autoconfigure.implementation.eventgrid.properties.AzureEventGridProperties; | ||
import com.azure.spring.cloud.core.implementation.util.AzureSpringIdentifier; | ||
import com.azure.spring.cloud.service.implementation.eventgrid.factory.EventGridPublisherClientBuilderFactory; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.junit.jupiter.api.parallel.Isolated; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
import org.springframework.boot.test.system.CapturedOutput; | ||
import org.springframework.boot.test.system.OutputCaptureExtension; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
@Isolated("Run this by itself as it captures System.out") | ||
@ExtendWith(OutputCaptureExtension.class) | ||
class EventGridUserAgentTests { | ||
|
||
@Test | ||
@SuppressWarnings("unchecked") | ||
void userAgentTest(CapturedOutput output) { | ||
new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(AzureEventGridAutoConfiguration.class)) | ||
.withPropertyValues( | ||
"spring.cloud.azure.eventgrid.endpoint=https://sample.somelocation.eventgrid.azure.net/api/eventseventgrid.azure.net/api/events", | ||
"spring.cloud.azure.eventgrid.key=some-key", | ||
"spring.cloud.azure.eventgrid.client.logging.level=headers", | ||
"spring.cloud.azure.eventgrid.client.logging.allowed-header-names=User-Agent", | ||
"spring.cloud.azure.eventgrid.retry.fixed.delay=1", | ||
"spring.cloud.azure.eventgrid.retry.fixed.max-retries=0", | ||
"spring.cloud.azure.eventgrid.retry.mode=fixed" | ||
) | ||
.withBean(AzureGlobalProperties.class, AzureGlobalProperties::new) | ||
.run(context -> { | ||
assertThat(context).hasSingleBean(AzureEventGridAutoConfiguration.class); | ||
assertThat(context).hasSingleBean(AzureEventGridProperties.class); | ||
assertThat(context).hasSingleBean(EventGridPublisherClientBuilderFactory.class); | ||
assertThat(context).hasSingleBean(EventGridPublisherClientBuilder.class); | ||
assertThat(context).hasSingleBean(EventGridPublisherClient.class); | ||
assertThat(context).hasSingleBean(EventGridPublisherAsyncClient.class); | ||
|
||
EventGridPublisherClient<EventGridEvent> eventGridPublisherClient = (EventGridPublisherClient<EventGridEvent>) context.getBean(EventGridPublisherClient.class); | ||
try { | ||
EventGridEvent event = new EventGridEvent("A user is created", "User.Created.Object", BinaryData.fromObject("user1"), "0.1"); // topic must be set when sending to an Event Grid Domain. | ||
eventGridPublisherClient.sendEvent(event); | ||
} catch (Exception exception) { | ||
// Eat it because we just want the log. | ||
} | ||
String allOutput = output.getAll(); | ||
String format1 = String.format("User-Agent:%s", AzureSpringIdentifier.AZURE_SPRING_EVENT_GRID); | ||
String format2 = String.format("\"User-Agent\":\"%s", | ||
AzureSpringIdentifier.AZURE_SPRING_EVENT_GRID); | ||
assertTrue(allOutput.contains(format1) || allOutput.contains(format2)); | ||
}); | ||
} | ||
} |