From 130895cee17d4b24829a193db260d3d2a47b97bb Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Mon, 12 Jun 2023 16:50:25 -0700 Subject: [PATCH 01/44] Add missing sync test cases --- .../EventGridPublisherClientTests.java | 167 +++++++++++++++++- 1 file changed, 166 insertions(+), 1 deletion(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index 270a35472ee5c..ebccb86b89631 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -10,12 +10,12 @@ import com.azure.core.http.HttpHeader; import com.azure.core.http.policy.RetryPolicy; import com.azure.core.http.rest.Response; +import com.azure.core.models.CloudEvent; import com.azure.core.models.CloudEventDataFormat; import com.azure.core.serializer.json.jackson.JacksonJsonSerializerBuilder; import com.azure.core.test.TestBase; import com.azure.core.util.BinaryData; import com.azure.core.util.Context; -import com.azure.core.models.CloudEvent; import com.azure.core.util.serializer.JacksonAdapter; import com.azure.identity.DefaultAzureCredential; import com.azure.identity.DefaultAzureCredentialBuilder; @@ -40,6 +40,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; public class EventGridPublisherClientTests extends TestBase { @@ -470,6 +471,58 @@ public void publishEventGridEventSync() { egClient.sendEvent(event); } + @Test + public void publishWithSasTokenSync() { + + String sasToken = EventGridPublisherAsyncClient.generateSas( + getEndpoint(EVENTGRID_ENDPOINT), + getKey(EVENTGRID_KEY), + OffsetDateTime.now().plusMinutes(20) + ); + + EventGridPublisherClient egClient = builder + .credential(new AzureSasCredential(sasToken)) + .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) + .buildEventGridEventPublisherClient(); + + EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), + "1.0") + .setEventTime(OffsetDateTime.now()); + + egClient.sendEvent(event); + } + + @Disabled + @Test + public void publishWithTokenCredentialSync() { + DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build(); + EventGridPublisherClient egClient = builder + .credential(defaultCredential) + .endpoint(getEndpoint(CLOUD_ENDPOINT)) + .buildCloudEventPublisherClient(); + + List events = new ArrayList<>(); + events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), CloudEventDataFormat.JSON, "application/json") + .setSubject("Test") + .setTime(OffsetDateTime.now())); + + egClient.sendEvents(events); + } + @Test public void publishCloudEventsSync() { EventGridPublisherClient egClient = builder @@ -517,6 +570,95 @@ public void publishCloudEventSync() { egClient.sendEvent(event); } + @Test + public void publishCloudEventsToPartnerTopicSync() { + EventGridPublisherClient egClient = builder + .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) + .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) + .addPolicy((httpPipelineCallContext, httpPipelineNextPolicy) -> { + HttpHeader httpHeader = httpPipelineCallContext.getHttpRequest().getHeaders().get("aeg-channel-name"); + assertNotNull(httpHeader); + return httpPipelineNextPolicy.process(); + }) + .buildCloudEventPublisherClient(); + + CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), CloudEventDataFormat.JSON, "application/json") + .setSubject("Test") + .setTime(OffsetDateTime.now()); + + Response response = egClient.sendEventsWithResponse(Arrays.asList(event), + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); + assertEquals(200, response.getStatusCode()); + } + + @Test + public void publishEventGridEventToPartnerTopicSync() { + EventGridPublisherClient egClient = builder + .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) + .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) + .addPolicy((httpPipelineCallContext, httpPipelineNextPolicy) -> { + HttpHeader httpHeader = httpPipelineCallContext.getHttpRequest().getHeaders().get("aeg-channel-name"); + assertNotNull(httpHeader); + return httpPipelineNextPolicy.process(); + }) + .buildEventGridEventPublisherClient(); + + EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), + "1.0") + .setEventTime(OffsetDateTime.now()); + + + HttpResponseException exception = assertThrows(HttpResponseException.class, () -> { + egClient.sendEventsWithResponse(Arrays.asList(event), + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); + }); + assertEquals(400, exception.getResponse().getStatusCode()); + } + + @Test + public void publishCloudEventsCustomSerializerSync() { + // Custom Serializer for testData + JacksonAdapter customSerializer = new JacksonAdapter(); + customSerializer.serializer().registerModule(new SimpleModule().addSerializer(TestData.class, + new JsonSerializer() { + @Override + public void serialize(TestData testData, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) + throws IOException { + jsonGenerator.writeString(testData.getName()); + } + })); + + EventGridPublisherClient egClient = builder + .credential(getKey(CLOUD_KEY)) + .endpoint(getEndpoint(CLOUD_ENDPOINT)) + .buildCloudEventPublisherClient(); + + List events = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new TestData().setName("Hello " + i)), CloudEventDataFormat.JSON, null) + .setSubject("Test " + i) + ); + } + + Response response = egClient.sendEventsWithResponse(events, Context.NONE); + assertEquals(200, response.getStatusCode()); + } + @Test public void publishCustomEventsSync() { EventGridPublisherClient egClient = builder @@ -541,6 +683,29 @@ public void publishCustomEventsSync() { assertEquals(response.getStatusCode(), 200); } + @Test + public void publishCustomEventsWithSerializerSync() { + EventGridPublisherClient egClient = builder + .credential(getKey(CUSTOM_KEY)) + .endpoint(getEndpoint(CUSTOM_ENDPOINT)) + .buildCustomEventPublisherClient(); + + List events = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + events.add(BinaryData.fromObject(new HashMap() { + { + put("id", UUID.randomUUID().toString()); + put("time", OffsetDateTime.now().toString()); + put("subject", "Test"); + put("foo", "bar"); + put("type", "Microsoft.MockPublisher.TestEvent"); + } + }, new JacksonJsonSerializerBuilder().build())); + } + Response response = egClient.sendEventsWithResponse(events, Context.NONE); + assertEquals(200, response.getStatusCode()); + } + @Test public void publishCustomEventSync() { EventGridPublisherClient egClient = builder From 5dc3fca88a449bd171eb0441e77ac5ec60b26a63 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Thu, 22 Jun 2023 17:48:48 -0700 Subject: [PATCH 02/44] Enable sync-stack --- .../azure-messaging-eventgrid/CHANGELOG.md | 4 +- .../eventgrid/EventGridPublisherClient.java | 118 ++++++++++++++-- .../EventGridPublisherClientBuilder.java | 28 ++-- .../EventGridPublisherClientImpl.java | 129 +++++++++++++++++- .../PublishCloudEventsToPartnerTopic.java | 3 +- .../EventGridPublisherClientTests.java | 19 +-- .../swagger/README.md | 1 + 7 files changed, 262 insertions(+), 40 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md b/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md index b51b9f56b4b9a..7283f117dd1b6 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md +++ b/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md @@ -54,7 +54,7 @@ ### Other Changes -#### Dependency Updates +~~#### Dependency Updates - Upgraded `azure-core-http-netty` from `1.13.0` to version `1.13.1`. - Upgraded `azure-core` from `1.36.0` to version `1.37.0`. @@ -83,7 +83,7 @@ - `Microsoft.ApiManagement.GatewayDeleted` - `Microsoft.ApiManagement.GatewayHostnameConfigurationCreated` - `Microsoft.ApiManagement.GatewayHostnameConfigurationDeleted` - - `Microsoft.ApiManagement.GatewayHostnameConfigurationUpdated` + - `Microsoft.ApiManagement.GatewayHostnameConfigurationUpdated`~~ - `Microsoft.ApiManagement.GatewayUpdated` - `Microsoft.DataBox.CopyCompleted` - `Microsoft.DataBox.CopyStarted` diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java index a56268373c5a4..afe8375814bda 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java @@ -8,11 +8,21 @@ import com.azure.core.annotation.ServiceMethod; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.credential.AzureSasCredential; +import com.azure.core.http.HttpPipeline; import com.azure.core.http.rest.Response; import com.azure.core.models.CloudEvent; +import com.azure.core.util.BinaryData; import com.azure.core.util.Context; +import com.azure.core.util.logging.ClientLogger; +import com.azure.messaging.eventgrid.implementation.EventGridPublisherClientImpl; +import com.azure.messaging.eventgrid.implementation.EventGridPublisherClientImplBuilder; +import com.fasterxml.jackson.databind.util.RawValue; import java.time.OffsetDateTime; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; /** * A service client that publishes events to an EventGrid topic or domain. Use {@link EventGridPublisherClientBuilder} @@ -124,10 +134,19 @@ */ @ServiceClient(builder = EventGridPublisherClientBuilder.class) public final class EventGridPublisherClient { + private final ClientLogger logger = new ClientLogger(EventGridPublisherClient.class); - private final EventGridPublisherAsyncClient asyncClient; - EventGridPublisherClient(EventGridPublisherAsyncClient client) { - this.asyncClient = client; + private final EventGridPublisherClientImpl impl; + private final String hostname; + private final Class eventClass; + EventGridPublisherClient(HttpPipeline pipeline, String hostname, EventGridServiceVersion serviceVersion, + Class eventClass) { + this.hostname = hostname; + this.eventClass = eventClass; + this.impl = new EventGridPublisherClientImplBuilder() + .pipeline(pipeline) + .apiVersion(serviceVersion.getVersion()) + .buildClient(); } /** @@ -173,38 +192,111 @@ public static String generateSas(String endpoint, AzureKeyCredential keyCredenti * @throws NullPointerException if events is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) + @SuppressWarnings("unchecked") public void sendEvents(Iterable events) { - asyncClient.sendEvents(events).block(); + if (this.eventClass == CloudEvent.class) { + this.sendCloudEvents((Iterable) events); + } else if (this.eventClass == EventGridEvent.class) { + this.sendEventGridEvents((Iterable) events); + } else { + this.sendCustomEvents((Iterable) events); + } + } + + private void sendCustomEvents(Iterable events) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + List objectEvents = StreamSupport.stream(events.spliterator(), false) + .map(event -> (Object) new RawValue(event.toString())) + .collect(Collectors.toList()); + this.impl.publishCustomEventEvents(this.hostname, objectEvents); + } + + private void sendEventGridEvents(Iterable events) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + List eventGridEvents = StreamSupport.stream(events.spliterator(), false) + .map(EventGridEvent::toImpl) + .collect(Collectors.toList()); + this.impl.publishEventGridEvents(this.hostname, eventGridEvents); + } + + private void sendCloudEvents(Iterable events) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + List cloudEvents = StreamSupport.stream(events.spliterator(), false) + .collect(Collectors.toList()); + this.impl.publishCloudEventEvents(this.hostname, cloudEvents, null); + } /** * Publishes the given events to the set topic or domain and gives the response issued by EventGrid. * @param events the events to publish. - * @param context the context to use along the pipeline. * * @return the response from the EventGrid service. * @throws NullPointerException if events is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) - public Response sendEventsWithResponse(Iterable events, Context context) { - return asyncClient.sendEventsWithResponse(events, context).block(); + public Response sendEventsWithResponse(Iterable events) { + return this.sendEventsWithResponse(events, null); } + /** * Publishes the given events to the set topic or domain and gives the response issued by EventGrid. * @param events the events to publish. * @param channelName the channel name to send to Event Grid service. This is only applicable for sending * Cloud Events to a partner topic in partner namespace. For more details, refer to * Partner Events Overview. - * @param context the context to use along the pipeline. * * @return the response from the EventGrid service. * @throws NullPointerException if events is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) - public Response sendEventsWithResponse(Iterable events, String channelName, - Context context) { - return asyncClient.sendEventsWithResponse(events, channelName, context).block(); + @SuppressWarnings("unchecked") + public Response sendEventsWithResponse(Iterable events, String channelName) { + if (this.eventClass == CloudEvent.class) { + return this.sendCloudEventsWithResponse((Iterable) events, channelName); + } else if (this.eventClass == EventGridEvent.class) { + return this.sendEventGridEventsWithResponse((Iterable) events); + } else { + return this.sendCustomEventsWithResponse((Iterable) events); + } + } + + private Response sendCustomEventsWithResponse(Iterable events) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + List objectEvents = StreamSupport.stream(events.spliterator(), false) + .map(event -> (Object) new RawValue(event.toString())) + .collect(Collectors.toList()); + return this.impl.publishCustomEventEventsWithResponse(this.hostname, objectEvents, Context.NONE); + } + + private Response sendEventGridEventsWithResponse(Iterable events) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + + List eventGridEvents = StreamSupport.stream(events.spliterator(), false) + .map(EventGridEvent::toImpl) + .collect(Collectors.toList()); + return this.impl.publishEventGridEventsWithResponse(this.hostname, eventGridEvents, Context.NONE); + } + + private Response sendCloudEventsWithResponse(Iterable events, String channelName) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + + List cloudEvents = StreamSupport.stream(events.spliterator(), false) + .collect(Collectors.toList()); + return this.impl.publishCloudEventEventsWithResponse(this.hostname, cloudEvents, channelName, Context.NONE); } /** @@ -215,7 +307,7 @@ public Response sendEventsWithResponse(Iterable events, String channelN */ @ServiceMethod(returns = ReturnType.SINGLE) public void sendEvent(T event) { - - asyncClient.sendEvent(event).block(); + List events = Collections.singletonList(event); + this.sendEvents(events); } } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClientBuilder.java b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClientBuilder.java index 02cbd88f404c2..91ac85ccb31c0 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClientBuilder.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClientBuilder.java @@ -132,14 +132,21 @@ public EventGridPublisherClientBuilder() { */ private EventGridPublisherAsyncClient buildAsyncClient(Class eventClass) { Objects.requireNonNull(endpoint, "'endpoint' is required and can not be null."); + + return new EventGridPublisherAsyncClient((httpPipeline != null ? httpPipeline : getHttpPipeline()), + endpoint, + getEventGridServiceVersion(), + eventClass); + } + + private EventGridServiceVersion getEventGridServiceVersion() { EventGridServiceVersion buildServiceVersion = serviceVersion == null ? EventGridServiceVersion.getLatest() : serviceVersion; + return buildServiceVersion; + } - if (httpPipeline != null) { - return new EventGridPublisherAsyncClient(httpPipeline, endpoint, buildServiceVersion, eventClass); - } - + private HttpPipeline getHttpPipeline() { Configuration buildConfiguration = (configuration == null) ? Configuration.getGlobalConfiguration() : configuration; @@ -216,20 +223,21 @@ private EventGridPublisherAsyncClient buildAsyncClient(Class eventClas .clientOptions(clientOptions) .tracer(tracer) .build(); - - - return new EventGridPublisherAsyncClient(buildPipeline, endpoint, buildServiceVersion, eventClass); + return buildPipeline; } /** * Build a publisher client with synchronous publishing methods and the current settings. Endpoint and a credential * must be set (either keyCredential or sharedAccessSignatureCredential), all other settings have defaults and/or are optional. - * Note that currently the asynchronous client created by the method above is the recommended version for higher - * performance, as the synchronous client simply blocks on the same asynchronous calls. * @return a publisher client with synchronous publishing methods. */ private EventGridPublisherClient buildClient(Class eventClass) { - return new EventGridPublisherClient(buildAsyncClient(eventClass)); + Objects.requireNonNull(endpoint, "'endpoint' is required and can not be null."); + + return new EventGridPublisherClient((httpPipeline != null ? httpPipeline : getHttpPipeline()), + endpoint, + getEventGridServiceVersion(), + eventClass); } @Override diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/implementation/EventGridPublisherClientImpl.java b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/implementation/EventGridPublisherClientImpl.java index 9e217a1fc35ba..e0fb4c8ed53dc 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/implementation/EventGridPublisherClientImpl.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/implementation/EventGridPublisherClientImpl.java @@ -28,9 +28,10 @@ import com.azure.core.util.serializer.JacksonAdapter; import com.azure.core.util.serializer.SerializerAdapter; import com.azure.messaging.eventgrid.implementation.models.EventGridEvent; -import java.util.List; import reactor.core.publisher.Mono; +import java.util.List; + /** Initializes a new instance of the EventGridPublisherClient type. */ public final class EventGridPublisherClientImpl { @@ -129,6 +130,15 @@ Mono> publishEventGridEvents( @BodyParam("application/json") List events, Context context); + @Post("") + @ExpectedResponses({200}) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Response publishEventGridEventsSync( + @HostParam("topicHostname") String topicHostname, + @QueryParam("api-version") String apiVersion, + @BodyParam("application/json") List events, + Context context); + @Post("") @ExpectedResponses({200}) @UnexpectedResponseExceptionType(HttpResponseException.class) @@ -139,6 +149,16 @@ Mono> publishCloudEventEvents( @BodyParam("application/cloudevents-batch+json; charset=utf-8") List events, Context context); + @Post("") + @ExpectedResponses({200}) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Response publishCloudEventEventsSync( + @HostParam("topicHostname") String topicHostname, + @QueryParam("api-version") String apiVersion, + @HeaderParam("aeg-channel-name") String aegChannelName, + @BodyParam("application/cloudevents-batch+json; charset=utf-8") List events, + Context context); + @Post("") @ExpectedResponses({200}) @UnexpectedResponseExceptionType(HttpResponseException.class) @@ -147,6 +167,15 @@ Mono> publishCustomEventEvents( @QueryParam("api-version") String apiVersion, @BodyParam("application/json") List events, Context context); + + @Post("") + @ExpectedResponses({200}) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Response publishCustomEventEventsSync( + @HostParam("topicHostname") String topicHostname, + @QueryParam("api-version") String apiVersion, + @BodyParam("application/json") List events, + Context context); } /** @@ -214,6 +243,37 @@ public Mono publishEventGridEventsAsync(String topicHostname, List Mono.empty()); } + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @param context The context to associate with this operation. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return the {@link Response}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Response publishEventGridEventsWithResponse( + String topicHostname, List events, Context context) { + return service.publishEventGridEventsSync(topicHostname, this.getApiVersion(), events, context); + } + + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public void publishEventGridEvents(String topicHostname, List events) { + publishEventGridEventsWithResponse(topicHostname, events, Context.NONE); + } + /** * Publishes a batch of events to an Azure Event Grid topic. * @@ -293,6 +353,42 @@ public Mono publishCloudEventEventsAsync( .flatMap(ignored -> Mono.empty()); } + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @param aegChannelName Required only when publishing to partner namespaces with partner topic routing mode + * ChannelNameHeader. + * @param context The context to associate with this operation. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return the {@link Response}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Response publishCloudEventEventsWithResponse( + String topicHostname, List events, String aegChannelName, Context context) { + return service.publishCloudEventEventsSync( + topicHostname, this.getApiVersion(), aegChannelName, events, context); + } + + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @param aegChannelName Required only when publishing to partner namespaces with partner topic routing mode + * ChannelNameHeader. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public void publishCloudEventEvents(String topicHostname, List events, String aegChannelName) { + publishCloudEventEventsWithResponse(topicHostname, events, aegChannelName, Context.NONE); + } + /** * Publishes a batch of events to an Azure Event Grid topic. * @@ -357,4 +453,35 @@ public Mono publishCustomEventEventsAsync(String topicHostname, List Mono.empty()); } + + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @param context The context to associate with this operation. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return the {@link Response}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Response publishCustomEventEventsWithResponse( + String topicHostname, List events, Context context) { + return service.publishCustomEventEventsSync(topicHostname, this.getApiVersion(), events, context); + } + + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public void publishCustomEventEvents(String topicHostname, List events) { + publishCustomEventEventsWithResponse(topicHostname, events, Context.NONE); + } } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java b/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java index 6793d67d3fafa..ea58c67db8934 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java @@ -7,7 +7,6 @@ import com.azure.core.models.CloudEvent; import com.azure.core.models.CloudEventDataFormat; import com.azure.core.util.BinaryData; -import com.azure.core.util.Context; import com.azure.messaging.eventgrid.EventGridPublisherClient; import com.azure.messaging.eventgrid.EventGridPublisherClientBuilder; import com.azure.messaging.eventgrid.samples.models.User; @@ -59,6 +58,6 @@ public static void main(String[] args) { events.add(cloudEventBytes.addExtensionAttribute("extension", "value")); // send to "my-channel-name" channel - publisherClient.sendEventsWithResponse(events, "my-channel-name", Context.NONE); + publisherClient.sendEventsWithResponse(events, "my-channel-name"); } } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index ebccb86b89631..1051a801a4d15 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -444,7 +444,7 @@ public void publishEventGridEventsSync() { "1.0") .setEventTime(OffsetDateTime.now())); - Response response = egClient.sendEventsWithResponse(events, Context.NONE); + Response response = egClient.sendEventsWithResponse(events); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -543,7 +543,7 @@ public void publishCloudEventsSync() { .setSubject("Test") .setTime(OffsetDateTime.now())); - Response response = egClient.sendEventsWithResponse(events, Context.NONE); + Response response = egClient.sendEventsWithResponse(events); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -594,7 +594,7 @@ public void publishCloudEventsToPartnerTopicSync() { .setTime(OffsetDateTime.now()); Response response = egClient.sendEventsWithResponse(Arrays.asList(event), - getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); assertEquals(200, response.getStatusCode()); } @@ -603,11 +603,6 @@ public void publishEventGridEventToPartnerTopicSync() { EventGridPublisherClient egClient = builder .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) - .addPolicy((httpPipelineCallContext, httpPipelineNextPolicy) -> { - HttpHeader httpHeader = httpPipelineCallContext.getHttpRequest().getHeaders().get("aeg-channel-name"); - assertNotNull(httpHeader); - return httpPipelineNextPolicy.process(); - }) .buildEventGridEventPublisherClient(); EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", @@ -624,7 +619,7 @@ public void publishEventGridEventToPartnerTopicSync() { HttpResponseException exception = assertThrows(HttpResponseException.class, () -> { egClient.sendEventsWithResponse(Arrays.asList(event), - getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); }); assertEquals(400, exception.getResponse().getStatusCode()); } @@ -655,7 +650,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer ); } - Response response = egClient.sendEventsWithResponse(events, Context.NONE); + Response response = egClient.sendEventsWithResponse(events); assertEquals(200, response.getStatusCode()); } @@ -677,7 +672,7 @@ public void publishCustomEventsSync() { } })); } - Response response = egClient.sendEventsWithResponse(events, Context.NONE); + Response response = egClient.sendEventsWithResponse(events); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -702,7 +697,7 @@ public void publishCustomEventsWithSerializerSync() { } }, new JacksonJsonSerializerBuilder().build())); } - Response response = egClient.sendEventsWithResponse(events, Context.NONE); + Response response = egClient.sendEventsWithResponse(events); assertEquals(200, response.getStatusCode()); } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/swagger/README.md b/sdk/eventgrid/azure-messaging-eventgrid/swagger/README.md index 070db8a4a9a2a..b8c46b924f6fd 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/swagger/README.md +++ b/sdk/eventgrid/azure-messaging-eventgrid/swagger/README.md @@ -35,6 +35,7 @@ models-subpackage: systemevents customization-class: src/main/java/EventGridCustomization.java service-interface-as-public: true url-as-string: true +enable-sync-stack: true directive: - rename-model: from: ResourceActionCancelData From 2daff85667d548399d13aa6bbc4b359c443a492a Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Fri, 23 Jun 2023 09:27:36 -0700 Subject: [PATCH 03/44] Remove extra set of aeg-channel-name. --- .../EventGridPublisherAsyncClient.java | 27 +++---------------- .../EventGridPublisherClientTests.java | 5 ---- 2 files changed, 3 insertions(+), 29 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherAsyncClient.java b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherAsyncClient.java index 321ce3287a492..19ffd703eac8f 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherAsyncClient.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherAsyncClient.java @@ -8,14 +8,11 @@ import com.azure.core.annotation.ServiceMethod; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.credential.AzureSasCredential; -import com.azure.core.http.HttpHeaders; import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.AddHeadersFromContextPolicy; import com.azure.core.http.rest.Response; import com.azure.core.models.CloudEvent; import com.azure.core.util.BinaryData; import com.azure.core.util.Context; -import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; import com.azure.core.util.tracing.Tracer; import com.azure.messaging.eventgrid.implementation.Constants; @@ -39,7 +36,6 @@ import java.util.Base64; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Objects; import static com.azure.core.util.FluxUtil.monoError; @@ -155,7 +151,6 @@ @ServiceClient(builder = EventGridPublisherClientBuilder.class, isAsync = true) public final class EventGridPublisherAsyncClient { - private static final String PARTNER_CHANNEL_HEADER_NAME = "aeg-channel-name"; private final String hostname; private final EventGridPublisherClientImpl impl; @@ -312,25 +307,9 @@ Mono> sendEventsWithResponse(Iterable events, String channelNa if (context == null) { context = Context.NONE; } - if (!CoreUtils.isNullOrEmpty(channelName)) { - String requestHttpHeadersKey = AddHeadersFromContextPolicy.AZURE_REQUEST_HTTP_HEADERS_KEY; - Map keyValues = context.getValues(); - if (keyValues != null && keyValues.containsKey(requestHttpHeadersKey)) { - // if the given Context instance already contains custom headers, - // add partner channel header to HttpHeaders - Object value = keyValues.get(requestHttpHeadersKey); - if (value instanceof HttpHeaders) { - HttpHeaders headers = (HttpHeaders) value; - headers.add(PARTNER_CHANNEL_HEADER_NAME, channelName); - } - } else { - context = context.addData(requestHttpHeadersKey, - new HttpHeaders().add(PARTNER_CHANNEL_HEADER_NAME, channelName)); - } - } if (this.eventClass == CloudEvent.class) { - return this.sendCloudEventsWithResponse((Iterable) events, context); + return this.sendCloudEventsWithResponse((Iterable) events, channelName, context); } else if (this.eventClass == EventGridEvent.class) { return this.sendEventGridEventsWithResponse((Iterable) events, context); } else { @@ -395,7 +374,7 @@ Mono> sendEventGridEventsWithResponse(Iterable ev .flatMap(list -> this.impl.publishEventGridEventsWithResponseAsync(this.hostname, list, finalContext)); } - Mono> sendCloudEventsWithResponse(Iterable events, Context context) { + Mono> sendCloudEventsWithResponse(Iterable events, String channelName, Context context) { if (events == null) { return monoError(logger, new NullPointerException("'events' cannot be null.")); } @@ -403,7 +382,7 @@ Mono> sendCloudEventsWithResponse(Iterable events, Co this.addCloudEventTracePlaceHolder(events); return Flux.fromIterable(events) .collectList() - .flatMap(list -> this.impl.publishCloudEventEventsWithResponseAsync(this.hostname, list, null, finalContext)); + .flatMap(list -> this.impl.publishCloudEventEventsWithResponseAsync(this.hostname, list, channelName, finalContext)); } Mono> sendCustomEventsWithResponse(Iterable events, Context context) { diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index 1051a801a4d15..cd6b2e3669cf1 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -281,11 +281,6 @@ public void publishEventGridEventToPartnerTopic() { EventGridPublisherAsyncClient egClient = builder .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) - .addPolicy((httpPipelineCallContext, httpPipelineNextPolicy) -> { - HttpHeader httpHeader = httpPipelineCallContext.getHttpRequest().getHeaders().get("aeg-channel-name"); - assertNotNull(httpHeader); - return httpPipelineNextPolicy.process(); - }) .buildEventGridEventPublisherAsyncClient(); EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", From 6a0874a5b7699641bd7fe86c3bb1517517749822 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Fri, 23 Jun 2023 10:03:40 -0700 Subject: [PATCH 04/44] enable AssertingHttpClient --- .../EventGridPublisherClientTests.java | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index cd6b2e3669cf1..6dd1840f6849e 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -7,6 +7,7 @@ import com.azure.core.credential.AzureKeyCredential; import com.azure.core.credential.AzureSasCredential; import com.azure.core.exception.HttpResponseException; +import com.azure.core.http.HttpClient; import com.azure.core.http.HttpHeader; import com.azure.core.http.policy.RetryPolicy; import com.azure.core.http.rest.Response; @@ -14,6 +15,7 @@ import com.azure.core.models.CloudEventDataFormat; import com.azure.core.serializer.json.jackson.JacksonJsonSerializerBuilder; import com.azure.core.test.TestBase; +import com.azure.core.test.http.AssertingHttpClientBuilder; import com.azure.core.util.BinaryData; import com.azure.core.util.Context; import com.azure.core.util.serializer.JacksonAdapter; @@ -46,6 +48,8 @@ public class EventGridPublisherClientTests extends TestBase { private EventGridPublisherClientBuilder builder; + private EventGridPublisherClientBuilder syncBuilder; + // Event Grid endpoint for a topic accepting EventGrid schema events private static final String EVENTGRID_ENDPOINT = "AZURE_EVENTGRID_EVENT_ENDPOINT"; @@ -82,10 +86,14 @@ protected void beforeTest() { StepVerifier.setDefaultTimeout(Duration.ofSeconds(30)); builder = new EventGridPublisherClientBuilder(); + syncBuilder = new EventGridPublisherClientBuilder(); if (interceptorManager.isPlaybackMode()) { - builder.httpClient(interceptorManager.getPlaybackClient()); + builder.httpClient(buildAssertingClient(interceptorManager.getPlaybackClient(), false)); + syncBuilder.httpClient(buildAssertingClient(interceptorManager.getPlaybackClient(), true)); } else { + builder.httpClient(buildAssertingClient(HttpClient.createDefault(), false)); + syncBuilder.httpClient(buildAssertingClient(HttpClient.createDefault(), true)); builder.addPolicy(interceptorManager.getRecordPolicy()) .retryPolicy(new RetryPolicy()); } @@ -422,7 +430,7 @@ public void publishCustomEvent() { @Test public void publishEventGridEventsSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(EVENTGRID_KEY)) .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) .buildEventGridEventPublisherClient(); @@ -447,7 +455,7 @@ public void publishEventGridEventsSync() { @Test public void publishEventGridEventSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(EVENTGRID_KEY)) .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) .buildEventGridEventPublisherClient(); @@ -475,7 +483,7 @@ public void publishWithSasTokenSync() { OffsetDateTime.now().plusMinutes(20) ); - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(new AzureSasCredential(sasToken)) .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) .buildEventGridEventPublisherClient(); @@ -498,7 +506,7 @@ public void publishWithSasTokenSync() { @Test public void publishWithTokenCredentialSync() { DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build(); - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(defaultCredential) .endpoint(getEndpoint(CLOUD_ENDPOINT)) .buildCloudEventPublisherClient(); @@ -520,7 +528,7 @@ public void publishWithTokenCredentialSync() { @Test public void publishCloudEventsSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CLOUD_KEY)) .endpoint(getEndpoint(CLOUD_ENDPOINT)) .buildCloudEventPublisherClient(); @@ -546,7 +554,7 @@ public void publishCloudEventsSync() { @Test public void publishCloudEventSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CLOUD_KEY)) .endpoint(getEndpoint(CLOUD_ENDPOINT)) .buildCloudEventPublisherClient(); @@ -567,7 +575,7 @@ public void publishCloudEventSync() { @Test public void publishCloudEventsToPartnerTopicSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) .addPolicy((httpPipelineCallContext, httpPipelineNextPolicy) -> { @@ -595,7 +603,7 @@ public void publishCloudEventsToPartnerTopicSync() { @Test public void publishEventGridEventToPartnerTopicSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) .buildEventGridEventPublisherClient(); @@ -632,7 +640,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer } })); - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CLOUD_KEY)) .endpoint(getEndpoint(CLOUD_ENDPOINT)) .buildCloudEventPublisherClient(); @@ -651,7 +659,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer @Test public void publishCustomEventsSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CUSTOM_KEY)) .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherClient(); @@ -675,7 +683,7 @@ public void publishCustomEventsSync() { @Test public void publishCustomEventsWithSerializerSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CUSTOM_KEY)) .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherClient(); @@ -698,7 +706,7 @@ public void publishCustomEventsWithSerializerSync() { @Test public void publishCustomEventSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CUSTOM_KEY)) .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherClient(); @@ -740,4 +748,16 @@ private String getChannelName(String liveEnvName) { assertNotNull(channelName, "System environment variable " + liveEnvName + " is null"); return channelName; } + + + private HttpClient buildAssertingClient(HttpClient httpClient, boolean sync) { + AssertingHttpClientBuilder builder = new AssertingHttpClientBuilder(httpClient) + .skipRequest((ignored1, ignored2) -> false); + if (sync) { + builder.assertSync(); + } else { + builder.assertAsync(); + } + return builder.build(); + } } From b80c1a87e012a1b62450cb250e78dea113bb43d0 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Thu, 29 Jun 2023 12:34:03 -0700 Subject: [PATCH 05/44] add back missing Context arguments --- .../eventgrid/EventGridPublisherClient.java | 26 ++++++++++--------- .../PublishCloudEventsToPartnerTopic.java | 3 ++- .../EventGridPublisherClientTests.java | 14 +++++----- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java index afe8375814bda..6ef14b530097e 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java @@ -236,13 +236,14 @@ private void sendCloudEvents(Iterable events) { /** * Publishes the given events to the set topic or domain and gives the response issued by EventGrid. * @param events the events to publish. + * @param context the context to use along the pipeline. * * @return the response from the EventGrid service. * @throws NullPointerException if events is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) - public Response sendEventsWithResponse(Iterable events) { - return this.sendEventsWithResponse(events, null); + public Response sendEventsWithResponse(Iterable events, Context context) { + return this.sendEventsWithResponse(events, null, context); } @@ -252,33 +253,34 @@ public Response sendEventsWithResponse(Iterable events) { * @param channelName the channel name to send to Event Grid service. This is only applicable for sending * Cloud Events to a partner topic in partner namespace. For more details, refer to * Partner Events Overview. + * @param context the context to use along the pipeline. * * @return the response from the EventGrid service. * @throws NullPointerException if events is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) @SuppressWarnings("unchecked") - public Response sendEventsWithResponse(Iterable events, String channelName) { + public Response sendEventsWithResponse(Iterable events, String channelName, Context context) { if (this.eventClass == CloudEvent.class) { - return this.sendCloudEventsWithResponse((Iterable) events, channelName); + return this.sendCloudEventsWithResponse((Iterable) events, channelName, context); } else if (this.eventClass == EventGridEvent.class) { - return this.sendEventGridEventsWithResponse((Iterable) events); + return this.sendEventGridEventsWithResponse((Iterable) events, context); } else { - return this.sendCustomEventsWithResponse((Iterable) events); + return this.sendCustomEventsWithResponse((Iterable) events, context); } } - private Response sendCustomEventsWithResponse(Iterable events) { + private Response sendCustomEventsWithResponse(Iterable events, Context context) { if (events == null) { throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); } List objectEvents = StreamSupport.stream(events.spliterator(), false) .map(event -> (Object) new RawValue(event.toString())) .collect(Collectors.toList()); - return this.impl.publishCustomEventEventsWithResponse(this.hostname, objectEvents, Context.NONE); + return this.impl.publishCustomEventEventsWithResponse(this.hostname, objectEvents, context); } - private Response sendEventGridEventsWithResponse(Iterable events) { + private Response sendEventGridEventsWithResponse(Iterable events, Context context) { if (events == null) { throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); } @@ -286,17 +288,17 @@ private Response sendEventGridEventsWithResponse(Iterable List eventGridEvents = StreamSupport.stream(events.spliterator(), false) .map(EventGridEvent::toImpl) .collect(Collectors.toList()); - return this.impl.publishEventGridEventsWithResponse(this.hostname, eventGridEvents, Context.NONE); + return this.impl.publishEventGridEventsWithResponse(this.hostname, eventGridEvents, context); } - private Response sendCloudEventsWithResponse(Iterable events, String channelName) { + private Response sendCloudEventsWithResponse(Iterable events, String channelName, Context context) { if (events == null) { throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); } List cloudEvents = StreamSupport.stream(events.spliterator(), false) .collect(Collectors.toList()); - return this.impl.publishCloudEventEventsWithResponse(this.hostname, cloudEvents, channelName, Context.NONE); + return this.impl.publishCloudEventEventsWithResponse(this.hostname, cloudEvents, channelName, context); } /** diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java b/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java index ea58c67db8934..6793d67d3fafa 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java @@ -7,6 +7,7 @@ import com.azure.core.models.CloudEvent; import com.azure.core.models.CloudEventDataFormat; import com.azure.core.util.BinaryData; +import com.azure.core.util.Context; import com.azure.messaging.eventgrid.EventGridPublisherClient; import com.azure.messaging.eventgrid.EventGridPublisherClientBuilder; import com.azure.messaging.eventgrid.samples.models.User; @@ -58,6 +59,6 @@ public static void main(String[] args) { events.add(cloudEventBytes.addExtensionAttribute("extension", "value")); // send to "my-channel-name" channel - publisherClient.sendEventsWithResponse(events, "my-channel-name"); + publisherClient.sendEventsWithResponse(events, "my-channel-name", Context.NONE); } } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index 6dd1840f6849e..42691676b22c6 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -447,7 +447,7 @@ public void publishEventGridEventsSync() { "1.0") .setEventTime(OffsetDateTime.now())); - Response response = egClient.sendEventsWithResponse(events); + Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -546,7 +546,7 @@ public void publishCloudEventsSync() { .setSubject("Test") .setTime(OffsetDateTime.now())); - Response response = egClient.sendEventsWithResponse(events); + Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -597,7 +597,7 @@ public void publishCloudEventsToPartnerTopicSync() { .setTime(OffsetDateTime.now()); Response response = egClient.sendEventsWithResponse(Arrays.asList(event), - getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); assertEquals(200, response.getStatusCode()); } @@ -622,7 +622,7 @@ public void publishEventGridEventToPartnerTopicSync() { HttpResponseException exception = assertThrows(HttpResponseException.class, () -> { egClient.sendEventsWithResponse(Arrays.asList(event), - getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); }); assertEquals(400, exception.getResponse().getStatusCode()); } @@ -653,7 +653,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer ); } - Response response = egClient.sendEventsWithResponse(events); + Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertEquals(200, response.getStatusCode()); } @@ -675,7 +675,7 @@ public void publishCustomEventsSync() { } })); } - Response response = egClient.sendEventsWithResponse(events); + Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -700,7 +700,7 @@ public void publishCustomEventsWithSerializerSync() { } }, new JacksonJsonSerializerBuilder().build())); } - Response response = egClient.sendEventsWithResponse(events); + Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertEquals(200, response.getStatusCode()); } From 043e39ade6b23fe556e7bcfd12365b358bebe421 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Fri, 14 Jul 2023 16:11:42 -0700 Subject: [PATCH 06/44] Don't stop trying dev credentials on failures Fixes #34733 For our dev time credentials we want to always keep going. This change wraps any failure from the credentials in a `CredentialUnavailableException` so `ChainedTokenCredential` will continue them properly. It only does so in the context of a `ChainedTokenCredential`. Regular uses of these credentials is unaffected. --- .../azure/identity/AzureCliCredential.java | 15 +++++++++++-- .../identity/AzureDeveloperCliCredential.java | 15 +++++++++++-- .../identity/AzurePowerShellCredential.java | 9 +++++++- .../DefaultAzureCredentialBuilder.java | 1 + .../azure/identity/IntelliJCredential.java | 9 +++++++- .../identity/SharedTokenCacheCredential.java | 9 +++++++- .../implementation/IdentityClientOptions.java | 21 +++++++++++++++++++ 7 files changed, 72 insertions(+), 7 deletions(-) diff --git a/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureCliCredential.java b/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureCliCredential.java index c7068bc9b9f56..0ee540096db13 100644 --- a/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureCliCredential.java +++ b/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureCliCredential.java @@ -83,7 +83,14 @@ public Mono getToken(TokenRequestContext request) { return identityClient.authenticateWithAzureCli(request) .doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request)) .doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request, - error)); + error)) + .onErrorMap(error -> { + if (identityClient.getIdentityClientOptions().isChained()) { + return new CredentialUnavailableException(error.getMessage(), error); + } else { + return error; + } + }); } @Override @@ -94,7 +101,11 @@ public AccessToken getTokenSync(TokenRequestContext request) { return accessToken; } catch (Exception e) { LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request, e); - throw e; + if (identityClient.getIdentityClientOptions().isChained()) { + throw new CredentialUnavailableException(e.getMessage(), e); + } else { + throw e; + } } } } diff --git a/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureDeveloperCliCredential.java b/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureDeveloperCliCredential.java index e8871cf98e0c1..c43f5d2ae307d 100644 --- a/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureDeveloperCliCredential.java +++ b/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzureDeveloperCliCredential.java @@ -83,7 +83,14 @@ public Mono getToken(TokenRequestContext request) { return identityClient.authenticateWithAzureDeveloperCli(request) .doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request)) .doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request, - error)); + error)) + .onErrorMap(error -> { + if (identityClient.getIdentityClientOptions().isChained()) { + return new CredentialUnavailableException(error.getMessage(), error); + } else { + return error; + } + }); } @Override @@ -94,7 +101,11 @@ public AccessToken getTokenSync(TokenRequestContext request) { return accessToken; } catch (Exception e) { LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request, e); - throw e; + if (identityClient.getIdentityClientOptions().isChained()) { + throw new CredentialUnavailableException(e.getMessage(), e); + } else { + throw e; + } } } } diff --git a/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzurePowerShellCredential.java b/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzurePowerShellCredential.java index 1714e0cb55898..44c1d0f89a1b0 100644 --- a/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzurePowerShellCredential.java +++ b/sdk/identity/azure-identity/src/main/java/com/azure/identity/AzurePowerShellCredential.java @@ -75,6 +75,13 @@ public Mono getToken(TokenRequestContext request) { return identityClient.authenticateWithAzurePowerShell(request) .doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request)) .doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), request, - error)); + error)) + .onErrorMap(error -> { + if (identityClient.getIdentityClientOptions().isChained()) { + return new CredentialUnavailableException(error.getMessage(), error); + } else { + return error; + } + }); } } diff --git a/sdk/identity/azure-identity/src/main/java/com/azure/identity/DefaultAzureCredentialBuilder.java b/sdk/identity/azure-identity/src/main/java/com/azure/identity/DefaultAzureCredentialBuilder.java index ad64181775eb0..91b1ea94cc15c 100644 --- a/sdk/identity/azure-identity/src/main/java/com/azure/identity/DefaultAzureCredentialBuilder.java +++ b/sdk/identity/azure-identity/src/main/java/com/azure/identity/DefaultAzureCredentialBuilder.java @@ -76,6 +76,7 @@ public class DefaultAzureCredentialBuilder extends CredentialBuilderBase getToken(TokenRequestContext request) { }) .doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request)) .doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), - request, error)); + request, error)) + .onErrorMap(error -> { + if (identityClient.getIdentityClientOptions().isChained()) { + return new CredentialUnavailableException(error.getMessage(), error); + } else { + return error; + } + }); } } diff --git a/sdk/identity/azure-identity/src/main/java/com/azure/identity/SharedTokenCacheCredential.java b/sdk/identity/azure-identity/src/main/java/com/azure/identity/SharedTokenCacheCredential.java index 51810f6d389db..bb3b31a95003a 100644 --- a/sdk/identity/azure-identity/src/main/java/com/azure/identity/SharedTokenCacheCredential.java +++ b/sdk/identity/azure-identity/src/main/java/com/azure/identity/SharedTokenCacheCredential.java @@ -101,7 +101,14 @@ public Mono getToken(TokenRequestContext request) { .map(this::updateCache) .doOnNext(token -> LoggingUtil.logTokenSuccess(LOGGER, request)) .doOnError(error -> LoggingUtil.logTokenError(LOGGER, identityClient.getIdentityClientOptions(), - request, error)); + request, error)) + .onErrorMap(error -> { + if (identityClient.getIdentityClientOptions().isChained()) { + return new CredentialUnavailableException(error.getMessage(), error); + } else { + return error; + } + }); } private AccessToken updateCache(MsalToken msalToken) { diff --git a/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/IdentityClientOptions.java b/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/IdentityClientOptions.java index c3519ecc31886..df97ec95d4d46 100644 --- a/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/IdentityClientOptions.java +++ b/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/IdentityClientOptions.java @@ -15,6 +15,7 @@ import com.azure.core.util.logging.ClientLogger; import com.azure.identity.AzureAuthorityHosts; import com.azure.identity.AuthenticationRecord; +import com.azure.identity.ChainedTokenCredential; import com.azure.identity.TokenCachePersistenceOptions; import com.azure.identity.implementation.util.IdentityConstants; import com.azure.identity.implementation.util.ValidationUtil; @@ -72,6 +73,8 @@ public final class IdentityClientOptions implements Cloneable { private Duration credentialProcessTimeout = Duration.ofSeconds(10); + private boolean isChained; + /** * Creates an instance of IdentityClientOptions with default settings. */ @@ -708,6 +711,24 @@ public void setCredentialProcessTimeout(Duration credentialProcessTimeout) { this.credentialProcessTimeout = credentialProcessTimeout; } + /** + * Indicates whether this options instance is part of a {@link ChainedTokenCredential}. + * @return true if this options instance is part of a {@link ChainedTokenCredential}, false otherwise. + */ + public boolean isChained() { + return this.isChained; + } + + /** + * Sets whether this options instance is part of a {@link ChainedTokenCredential}. + * @param isChained + * @return the updated client options + */ + public IdentityClientOptions setChained(boolean isChained) { + this.isChained = isChained; + return this; + } + public IdentityClientOptions clone() { IdentityClientOptions clone = new IdentityClientOptions() .setAdditionallyAllowedTenants(this.additionallyAllowedTenants) From dbc2b6c9833eef4041fa868f4e76df0706682380 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Wed, 6 Sep 2023 14:16:56 -0700 Subject: [PATCH 07/44] Tests now use test proxy and recordings are pushed to assets repo --- .../azure-messaging-eventgrid/assets.json | 6 + .../azure-messaging-eventgrid/pom.xml | 2 +- .../EventGridPublisherClientTests.java | 317 ++---------------- .../EventGridPublisherImplTests.java | 108 +----- .../eventgrid/EventGridTestBase.java | 148 ++++++++ ...ests.publishCloudEventsToPartnerTopic.json | 23 -- ...s.publishEventGridEventToPartnerTopic.json | 24 -- .../session-records/publishCloudEvent.json | 23 -- .../publishCloudEventSync.json | 23 -- .../session-records/publishCloudEvents.json | 23 -- .../publishCloudEventsCustomSerializer.json | 23 -- .../publishCloudEventsImpl.json | 21 -- .../publishCloudEventsSync.json | 23 -- .../session-records/publishCustomEvent.json | 23 -- .../publishCustomEventSync.json | 23 -- .../session-records/publishCustomEvents.json | 24 -- .../publishCustomEventsImpl.json | 21 -- .../publishCustomEventsSync.json | 23 -- .../publishCustomEventsWithSerializer.json | 23 -- .../publishEventGridEvent.json | 23 -- .../publishEventGridEventSync.json | 23 -- .../publishEventGridEvents.json | 42 --- .../publishEventGridEventsImpl.json | 21 -- .../publishEventGridEventsSync.json | 23 -- .../session-records/publishWithSasToken.json | 23 -- .../publishWithTokenCredential.json | 23 -- 26 files changed, 204 insertions(+), 875 deletions(-) create mode 100644 sdk/eventgrid/azure-messaging-eventgrid/assets.json create mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridTestBase.java delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishCloudEventsToPartnerTopic.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishEventGridEventToPartnerTopic.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvent.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvents.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsCustomSerializer.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsImpl.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvent.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvents.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsImpl.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsWithSerializer.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvent.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvents.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsImpl.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithSasToken.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithTokenCredential.json diff --git a/sdk/eventgrid/azure-messaging-eventgrid/assets.json b/sdk/eventgrid/azure-messaging-eventgrid/assets.json new file mode 100644 index 0000000000000..37ab80f8b8562 --- /dev/null +++ b/sdk/eventgrid/azure-messaging-eventgrid/assets.json @@ -0,0 +1,6 @@ +{ + "AssetsRepo": "Azure/azure-sdk-assets", + "AssetsRepoPrefixPath": "java", + "TagPrefix": "java/eventgrid/azure-messaging-eventgrid", + "Tag": "java/eventgrid/azure-messaging-eventgrid_7bb36a1579" +} diff --git a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml index acfe9482e1bbb..febb734b2f733 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml +++ b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml @@ -118,7 +118,7 @@ com.azure azure-core-test - 1.18.0 + 1.19.0-beta.1 test diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index 42691676b22c6..e870cc5f8162a 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -4,7 +4,6 @@ package com.azure.messaging.eventgrid; -import com.azure.core.credential.AzureKeyCredential; import com.azure.core.credential.AzureSasCredential; import com.azure.core.exception.HttpResponseException; import com.azure.core.http.HttpClient; @@ -12,9 +11,6 @@ import com.azure.core.http.policy.RetryPolicy; import com.azure.core.http.rest.Response; import com.azure.core.models.CloudEvent; -import com.azure.core.models.CloudEventDataFormat; -import com.azure.core.serializer.json.jackson.JacksonJsonSerializerBuilder; -import com.azure.core.test.TestBase; import com.azure.core.test.http.AssertingHttpClientBuilder; import com.azure.core.util.BinaryData; import com.azure.core.util.Context; @@ -35,50 +31,20 @@ import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; -public class EventGridPublisherClientTests extends TestBase { +public class EventGridPublisherClientTests extends EventGridTestBase { private EventGridPublisherClientBuilder builder; private EventGridPublisherClientBuilder syncBuilder; - // Event Grid endpoint for a topic accepting EventGrid schema events - private static final String EVENTGRID_ENDPOINT = "AZURE_EVENTGRID_EVENT_ENDPOINT"; - // Event Grid endpoint for a topic accepting CloudEvents schema events - private static final String CLOUD_ENDPOINT = "AZURE_EVENTGRID_CLOUDEVENT_ENDPOINT"; - - // Event Grid endpoint for a topic accepting custom schema events - private static final String CUSTOM_ENDPOINT = "AZURE_EVENTGRID_CUSTOM_ENDPOINT"; - - // Event Grid access key for a topic accepting EventGrid schema events - private static final String EVENTGRID_KEY = "AZURE_EVENTGRID_EVENT_KEY"; - - // Event Grid access key for a topic accepting CloudEvents schema events - private static final String CLOUD_KEY = "AZURE_EVENTGRID_CLOUDEVENT_KEY"; - - // Event Grid access key for a topic accepting custom schema events - private static final String CUSTOM_KEY = "AZURE_EVENTGRID_CUSTOM_KEY"; - - // Endpoint, key and channel name for publishing to partner topic - private static final String EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT = "EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT"; - private static final String EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY = "EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY"; - private static final String EVENTGRID_PARTNER_CHANNEL_NAME = "EVENTGRID_PARTNER_CHANNEL_NAME"; - - private static final String DUMMY_ENDPOINT = "https://www.dummyEndpoint.com/api/events"; - - private static final String DUMMY_KEY = "dummyKey"; - - private static final String DUMMY_CHANNEL_NAME = "dummy-channel"; @Override protected void beforeTest() { @@ -91,19 +57,23 @@ protected void beforeTest() { if (interceptorManager.isPlaybackMode()) { builder.httpClient(buildAssertingClient(interceptorManager.getPlaybackClient(), false)); syncBuilder.httpClient(buildAssertingClient(interceptorManager.getPlaybackClient(), true)); - } else { + } else { // both record and live will use these clients builder.httpClient(buildAssertingClient(HttpClient.createDefault(), false)); syncBuilder.httpClient(buildAssertingClient(HttpClient.createDefault(), true)); + } + + if (interceptorManager.isRecordMode()) { builder.addPolicy(interceptorManager.getRecordPolicy()) .retryPolicy(new RetryPolicy()); + syncBuilder.addPolicy(interceptorManager.getRecordPolicy()) + .retryPolicy(new RetryPolicy()); } - } - @Override - protected void afterTest() { - StepVerifier.resetDefaultTimeout(); + setupSanitizers(); } + + @Test public void publishEventGridEvents() { EventGridPublisherAsyncClient egClient = builder @@ -112,16 +82,7 @@ public void publishEventGridEvents() { .buildEventGridEventPublisherAsyncClient(); List events = new ArrayList<>(); - events.add(new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now())); + events.add(getEventGridEvent()); StepVerifier.create(egClient.sendEventsWithResponse(events)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -138,16 +99,7 @@ public void publishEventGridEvent() { .credential(getKey(EVENTGRID_KEY)) .buildEventGridEventPublisherAsyncClient(); - EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now()); + EventGridEvent event = getEventGridEvent(); StepVerifier.create(egClient.sendEvent(event)) .verifyComplete(); } @@ -166,16 +118,7 @@ public void publishWithSasToken() { .buildEventGridEventPublisherAsyncClient(); List events = new ArrayList<>(); - events.add(new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now())); + events.add(getEventGridEvent()); StepVerifier.create(egClient.sendEventsWithResponse(events, Context.NONE)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -192,16 +135,7 @@ public void publishWithTokenCredential() { .buildCloudEventPublisherAsyncClient(); List events = new ArrayList<>(); - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now())); + events.add(getCloudEvent()); StepVerifier.create(egClient.sendEventsWithResponse(events, Context.NONE)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -216,16 +150,7 @@ public void publishCloudEvents() { .buildCloudEventPublisherAsyncClient(); List events = new ArrayList<>(); - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now())); + events.add(getCloudEvent()); StepVerifier.create(egClient.sendEventsWithResponse(events, Context.NONE)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -239,16 +164,7 @@ public void publishCloudEvent() { .credential(getKey(CLOUD_KEY)) .buildCloudEventPublisherAsyncClient(); - CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now()); + CloudEvent event = getCloudEvent(); StepVerifier.create(egClient.sendEvent(event)) .verifyComplete(); @@ -266,16 +182,7 @@ public void publishCloudEventsToPartnerTopic() { }) .buildCloudEventPublisherAsyncClient(); - CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now()); + CloudEvent event = getCloudEvent(); Mono> responseMono = egClient.sendEventsWithResponse(Arrays.asList(event), getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); @@ -291,16 +198,7 @@ public void publishEventGridEventToPartnerTopic() { .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) .buildEventGridEventPublisherAsyncClient(); - EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now()); + EventGridEvent event = getEventGridEvent(); Mono> responseMono = egClient.sendEventsWithResponse(Arrays.asList(event), getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); @@ -349,9 +247,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new TestData().setName("Hello " + i)), CloudEventDataFormat.JSON, null) - .setSubject("Test " + i) + events.add(getCloudEvent(i) ); } @@ -370,15 +266,7 @@ public void publishCustomEvents() { List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - })); + events.add(getCustomEvent()); } StepVerifier.create(egClient.sendEventsWithResponse(events)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -394,15 +282,7 @@ public void publishCustomEventsWithSerializer() { List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }, new JacksonJsonSerializerBuilder().build())); + events.add(getCustomEventWithSerializer()); } StepVerifier.create(egClient.sendEventsWithResponse(events, Context.NONE)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -416,16 +296,7 @@ public void publishCustomEvent() { .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherAsyncClient(); - BinaryData event = BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }); - StepVerifier.create(egClient.sendEvent(event)).verifyComplete(); + StepVerifier.create(egClient.sendEvent(getCustomEvent())).verifyComplete(); } @Test @@ -436,16 +307,7 @@ public void publishEventGridEventsSync() { .buildEventGridEventPublisherClient(); List events = new ArrayList<>(); - events.add(new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now())); + events.add(getEventGridEvent()); Response response = egClient.sendEventsWithResponse(events, Context.NONE); @@ -460,16 +322,7 @@ public void publishEventGridEventSync() { .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) .buildEventGridEventPublisherClient(); - EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now()); + EventGridEvent event = getEventGridEvent(); egClient.sendEvent(event); } @@ -488,16 +341,7 @@ public void publishWithSasTokenSync() { .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) .buildEventGridEventPublisherClient(); - EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now()); + EventGridEvent event = getEventGridEvent(); egClient.sendEvent(event); } @@ -512,16 +356,7 @@ public void publishWithTokenCredentialSync() { .buildCloudEventPublisherClient(); List events = new ArrayList<>(); - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now())); + events.add(getCloudEvent()); egClient.sendEvents(events); } @@ -534,17 +369,7 @@ public void publishCloudEventsSync() { .buildCloudEventPublisherClient(); List events = new ArrayList<>(); - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setId(UUID.randomUUID().toString()) - .setSubject("Test") - .setTime(OffsetDateTime.now())); + events.add(getCloudEvent()); Response response = egClient.sendEventsWithResponse(events, Context.NONE); @@ -559,17 +384,7 @@ public void publishCloudEventSync() { .endpoint(getEndpoint(CLOUD_ENDPOINT)) .buildCloudEventPublisherClient(); - CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setId(UUID.randomUUID().toString()) - .setSubject("Test") - .setTime(OffsetDateTime.now()); + CloudEvent event = getCloudEvent(); egClient.sendEvent(event); } @@ -585,16 +400,7 @@ public void publishCloudEventsToPartnerTopicSync() { }) .buildCloudEventPublisherClient(); - CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now()); + CloudEvent event = getCloudEvent(); Response response = egClient.sendEventsWithResponse(Arrays.asList(event), getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); @@ -608,16 +414,7 @@ public void publishEventGridEventToPartnerTopicSync() { .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) .buildEventGridEventPublisherClient(); - EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now()); + EventGridEvent event = getEventGridEvent(); HttpResponseException exception = assertThrows(HttpResponseException.class, () -> { @@ -627,6 +424,8 @@ public void publishEventGridEventToPartnerTopicSync() { assertEquals(400, exception.getResponse().getStatusCode()); } + + @Test public void publishCloudEventsCustomSerializerSync() { // Custom Serializer for testData @@ -647,10 +446,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new TestData().setName("Hello " + i)), CloudEventDataFormat.JSON, null) - .setSubject("Test " + i) - ); + events.add(getCloudEvent(i)); } Response response = egClient.sendEventsWithResponse(events, Context.NONE); @@ -666,14 +462,7 @@ public void publishCustomEventsSync() { List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - })); + events.add(getCustomEvent()); } Response response = egClient.sendEventsWithResponse(events, Context.NONE); @@ -690,15 +479,7 @@ public void publishCustomEventsWithSerializerSync() { List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }, new JacksonJsonSerializerBuilder().build())); + events.add(getCustomEventWithSerializer()); } Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertEquals(200, response.getStatusCode()); @@ -711,33 +492,7 @@ public void publishCustomEventSync() { .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherClient(); - Map event = new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }; - egClient.sendEvent(BinaryData.fromObject(event)); - } - - private String getEndpoint(String liveEnvName) { - if (interceptorManager.isPlaybackMode()) { - return DUMMY_ENDPOINT; - } - String endpoint = System.getenv(liveEnvName); - assertNotNull(endpoint, "System environment variable " + liveEnvName + " is null"); - return endpoint; - } - - private AzureKeyCredential getKey(String liveEnvName) { - if (interceptorManager.isPlaybackMode()) { - return new AzureKeyCredential(DUMMY_KEY); - } - AzureKeyCredential key = new AzureKeyCredential(System.getenv(liveEnvName)); - assertNotNull(key.getKey(), "System environment variable " + liveEnvName + " is null"); - return key; + egClient.sendEvent(getCustomEvent()); } private String getChannelName(String liveEnvName) { diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java index a439c44dfde8d..b5da862fef7ae 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java @@ -7,52 +7,26 @@ import com.azure.core.http.HttpPipelineBuilder; import com.azure.core.http.policy.AddHeadersPolicy; import com.azure.core.http.policy.RetryPolicy; -import com.azure.core.models.CloudEventDataFormat; -import com.azure.core.test.TestBase; -import com.azure.core.util.BinaryData; +import com.azure.core.models.CloudEvent; import com.azure.messaging.eventgrid.implementation.EventGridPublisherClientImpl; import com.azure.messaging.eventgrid.implementation.EventGridPublisherClientImplBuilder; -import com.azure.core.models.CloudEvent; import com.azure.messaging.eventgrid.implementation.models.EventGridEvent; import org.junit.jupiter.api.Test; import reactor.test.StepVerifier; import java.net.MalformedURLException; import java.time.Duration; -import java.time.OffsetDateTime; -import java.util.*; - -import static org.junit.jupiter.api.Assertions.assertNotNull; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; -public class EventGridPublisherImplTests extends TestBase { +public class EventGridPublisherImplTests extends EventGridTestBase { private HttpPipelineBuilder pipelineBuilder; private EventGridPublisherClientImplBuilder clientBuilder; - // Event Grid endpoint for a topic accepting EventGrid schema events - private static final String EVENTGRID_ENDPOINT = "AZURE_EVENTGRID_EVENT_ENDPOINT"; - - // Event Grid endpoint for a topic accepting CloudEvents schema events - private static final String CLOUD_ENDPOINT = "AZURE_EVENTGRID_CLOUDEVENT_ENDPOINT"; - - // Event Grid endpoint for a topic accepting custom schema events - private static final String CUSTOM_ENDPOINT = "AZURE_EVENTGRID_CUSTOM_ENDPOINT"; - - // Event Grid access key for a topic accepting EventGrid schema events - private static final String EVENTGRID_KEY = "AZURE_EVENTGRID_EVENT_KEY"; - - // Event Grid access key for a topic accepting CloudEvents schema events - private static final String CLOUD_KEY = "AZURE_EVENTGRID_CLOUDEVENT_KEY"; - - // Event Grid access key for a topic accepting custom schema events - private static final String CUSTOM_KEY = "AZURE_EVENTGRID_CUSTOM_KEY"; - - private static final String DUMMY_ENDPOINT = "https://www.dummyEndpoint.com/api/events"; - - private static final String DUMMY_KEY = "dummyKey"; - @Override protected void beforeTest() { StepVerifier.setDefaultTimeout(Duration.ofSeconds(30)); @@ -63,39 +37,22 @@ protected void beforeTest() { if (interceptorManager.isPlaybackMode()) { pipelineBuilder.httpClient(interceptorManager.getPlaybackClient()); - } else { + } else if (interceptorManager.isRecordMode()) { pipelineBuilder.policies(interceptorManager.getRecordPolicy(), new RetryPolicy()); } - } - @Override - protected void afterTest() { - StepVerifier.resetDefaultTimeout(); + setupSanitizers(); } @Test public void publishEventGridEventsImpl() throws MalformedURLException { EventGridPublisherClientImpl egClient = clientBuilder .pipeline(pipelineBuilder.policies( - new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(EVENTGRID_KEY)))) + new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(EVENTGRID_KEY).getKey()))) .build()) .buildClient(); - List events = Collections.singletonList( - new EventGridEvent() - .setId(UUID.randomUUID().toString()) - .setSubject("Test") - .setEventType("Microsoft.MockPublisher.TestEvent") - .setData(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }) - .setDataVersion("1.0") - .setEventTime(OffsetDateTime.now()) - ); + List events = Collections.singletonList(getEventGridEvent().toImpl()); StepVerifier.create(egClient.publishEventGridEventsWithResponseAsync(getEndpoint(EVENTGRID_ENDPOINT), events)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -106,23 +63,11 @@ public void publishEventGridEventsImpl() throws MalformedURLException { public void publishCloudEventsImpl() throws MalformedURLException { EventGridPublisherClientImpl egClient = clientBuilder .pipeline(pipelineBuilder.policies( - new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(CLOUD_KEY)))) + new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(CLOUD_KEY).getKey()))) .build()) .buildClient(); - List events = Collections.singletonList( - new CloudEvent("TestSource", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setId(UUID.randomUUID().toString()) - .setSubject("Test") - .setTime(OffsetDateTime.now()) - ); + List events = Collections.singletonList(getCloudEvent()); StepVerifier.create(egClient.publishCloudEventEventsWithResponseAsync(getEndpoint(CLOUD_ENDPOINT), events, null)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -133,44 +78,17 @@ public void publishCloudEventsImpl() throws MalformedURLException { public void publishCustomEventsImpl() throws MalformedURLException { EventGridPublisherClientImpl egClient = clientBuilder .pipeline(pipelineBuilder.policies( - new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(CUSTOM_KEY)))) + new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(CUSTOM_KEY).getKey()))) .build()) .buildClient(); List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }); + events.add(getCustomEvent()); } StepVerifier.create(egClient.publishCustomEventEventsWithResponseAsync(getEndpoint(CUSTOM_ENDPOINT), events)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) .verifyComplete(); } - - - private String getEndpoint(String liveEnvName) { - if (interceptorManager.isPlaybackMode()) { - return DUMMY_ENDPOINT; - } - String endpoint = System.getenv(liveEnvName); - assertNotNull(endpoint, "System environment variable " + liveEnvName + "is null"); - return endpoint; - } - - private String getKey(String liveEnvName) { - if (interceptorManager.isPlaybackMode()) { - return DUMMY_KEY; - } - String key = System.getenv(liveEnvName); - assertNotNull(key, "System environment variable " + liveEnvName + "is null"); - return key; - } } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridTestBase.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridTestBase.java new file mode 100644 index 0000000000000..fa3124472c21d --- /dev/null +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridTestBase.java @@ -0,0 +1,148 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.messaging.eventgrid; + +import com.azure.core.credential.AzureKeyCredential; +import com.azure.core.models.CloudEvent; +import com.azure.core.models.CloudEventDataFormat; +import com.azure.core.serializer.json.jackson.JacksonJsonSerializerBuilder; +import com.azure.core.test.TestProxyTestBase; +import com.azure.core.test.models.TestProxySanitizer; +import com.azure.core.test.models.TestProxySanitizerType; +import com.azure.core.util.BinaryData; +import reactor.test.StepVerifier; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class EventGridTestBase extends TestProxyTestBase { + + // Event Grid endpoint for a topic accepting EventGrid schema events + static final String EVENTGRID_ENDPOINT = "AZURE_EVENTGRID_EVENT_ENDPOINT"; + + // Event Grid endpoint for a topic accepting CloudEvents schema events + static final String CLOUD_ENDPOINT = "AZURE_EVENTGRID_CLOUDEVENT_ENDPOINT"; + + // Event Grid endpoint for a topic accepting custom schema events + static final String CUSTOM_ENDPOINT = "AZURE_EVENTGRID_CUSTOM_ENDPOINT"; + + // Event Grid access key for a topic accepting EventGrid schema events + static final String EVENTGRID_KEY = "AZURE_EVENTGRID_EVENT_KEY"; + + // Event Grid access key for a topic accepting CloudEvents schema events + static final String CLOUD_KEY = "AZURE_EVENTGRID_CLOUDEVENT_KEY"; + + // Event Grid access key for a topic accepting custom schema events + static final String CUSTOM_KEY = "AZURE_EVENTGRID_CUSTOM_KEY"; + + // Endpoint, key and channel name for publishing to partner topic + static final String EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT = "EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT"; + static final String EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY = "EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY"; + static final String EVENTGRID_PARTNER_CHANNEL_NAME = "EVENTGRID_PARTNER_CHANNEL_NAME"; + + static final String DUMMY_ENDPOINT = "https://www.dummyEndpoint.com/api/events"; + + static final String DUMMY_KEY = "dummyKey"; + + static final String DUMMY_CHANNEL_NAME = "dummy-channel"; + + @Override + protected void afterTest() { + StepVerifier.resetDefaultTimeout(); + } + + void setupSanitizers() { + if (!interceptorManager.isLiveMode()) { + List sanitizers = new ArrayList<>(); + sanitizers.add(new TestProxySanitizer("aeg-sas-token", null, "REDACTED", TestProxySanitizerType.HEADER)); + sanitizers.add(new TestProxySanitizer("aeg-sas-key", null, "REDACTED", TestProxySanitizerType.HEADER)); + sanitizers.add(new TestProxySanitizer("aeg-channel-name", null, "REDACTED", TestProxySanitizerType.HEADER)); + interceptorManager.addSanitizers(sanitizers); + } + } + + EventGridEvent getEventGridEvent() { + EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), + "1.0") + .setEventTime(testResourceNamer.now()) + .setId(testResourceNamer.randomUuid()); + return event; + } + + BinaryData getCustomEvent() { + return BinaryData.fromObject(new HashMap() { + { + put("id", testResourceNamer.randomUuid()); + put("time", testResourceNamer.now().toString()); + put("subject", "Test"); + put("foo", "bar"); + put("type", "Microsoft.MockPublisher.TestEvent"); + } + }); + } + + BinaryData getCustomEventWithSerializer() { + return BinaryData.fromObject(new HashMap() { + { + put("id", testResourceNamer.randomUuid()); + put("time", testResourceNamer.now().toString()); + put("subject", "Test"); + put("foo", "bar"); + put("type", "Microsoft.MockPublisher.TestEvent"); + } + }, new JacksonJsonSerializerBuilder().build()); + } + + CloudEvent getCloudEvent() { + return new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), CloudEventDataFormat.JSON, "application/json") + .setSubject("Test") + .setTime(testResourceNamer.now()) + .setId(testResourceNamer.randomUuid()); + } + + CloudEvent getCloudEvent(int i) { + return new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new EventGridPublisherClientTests.TestData().setName("Hello " + i)), CloudEventDataFormat.JSON, null) + .setSubject("Test " + i) + .setTime(testResourceNamer.now()) + .setId(testResourceNamer.randomUuid()); + } + + + String getEndpoint(String liveEnvName) { + if (interceptorManager.isPlaybackMode()) { + return DUMMY_ENDPOINT; + } + String endpoint = System.getenv(liveEnvName); + assertNotNull(endpoint, "System environment variable " + liveEnvName + " is null"); + return endpoint; + } + + AzureKeyCredential getKey(String liveEnvName) { + if (interceptorManager.isPlaybackMode()) { + return new AzureKeyCredential(DUMMY_KEY); + } + AzureKeyCredential key = new AzureKeyCredential(System.getenv(liveEnvName)); + assertNotNull(key.getKey(), "System environment variable " + liveEnvName + " is null"); + return key; + } + +} diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishCloudEventsToPartnerTopic.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishCloudEventsToPartnerTopic.json deleted file mode 100644 index 752563b375ebf..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishCloudEventsToPartnerTopic.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.centraluseuap-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (14.0.2; Windows 10; 10.0)", - "x-ms-client-request-id" : "ca44182b-dcce-416c-978a-455877cd4dd6", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "6f1699a0-55e9-4633-97fa-37c5781cb3c3", - "Date" : "Mon, 04 Apr 2022 07:01:45 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishEventGridEventToPartnerTopic.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishEventGridEventToPartnerTopic.json deleted file mode 100644 index ff60382e9d659..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishEventGridEventToPartnerTopic.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.centraluseuap-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (14.0.2; Windows 10; 10.0)", - "x-ms-client-request-id" : "efbf8e14-c3d0-4e14-b521-9bfca43c2271", - "Content-Type" : "application/json" - }, - "Response" : { - "content-length" : "827", - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "retry-after" : "0", - "StatusCode" : "400", - "x-ms-request-id" : "d8f0e713-14d1-4fef-96b1-e0c78efe356d", - "Body" : "{\r\n \"error\": {\r\n \"code\": \"BadRequest\",\r\n \"message\": \"This resource is configured to receive event in 'CloudEventV10' schema. The JSON received does not conform to the expected schema. Token Expected: StartObject, Actual Token Received: StartArray. Report 'd8f0e713-14d1-4fef-96b1-e0c78efe356d:8:4/4/2022 7:12:27 AM (UTC)' to our forums for assistance or raise a support ticket.\",\r\n \"details\": [{\r\n \"code\": \"InputJsonInvalid\",\r\n \"message\": \"This resource is configured to receive event in 'CloudEventV10' schema. The JSON received does not conform to the expected schema. Token Expected: StartObject, Actual Token Received: StartArray. Report 'd8f0e713-14d1-4fef-96b1-e0c78efe356d:8:4/4/2022 7:12:27 AM (UTC)' to our forums for assistance or raise a support ticket.\"\r\n }]\r\n }\r\n}", - "Date" : "Mon, 04 Apr 2022 07:12:27 GMT", - "Content-Type" : "application/json; charset=utf-8" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvent.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvent.json deleted file mode 100644 index a9efdc165821e..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvent.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "bb67c974-8040-40c5-8712-5cdd76317a26", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "91537e41-1a96-4b8e-b29d-6fae94dde57a", - "Date" : "Thu, 04 Mar 2021 00:41:03 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventSync.json deleted file mode 100644 index 0dea66821cf07..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "2819c5bc-dbda-4ed9-b4be-36274276dd05", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "7785fded-6605-4856-baa0-4af648365a31", - "Date" : "Thu, 04 Mar 2021 00:41:03 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvents.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvents.json deleted file mode 100644 index 5cac968c57009..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvents.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "d33fe779-c1eb-426f-aa96-4f052036bdb7", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "ffe28d08-d2b6-45b1-94e3-8a45f8fbed54", - "Date" : "Tue, 11 Aug 2020 17:21:10 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsCustomSerializer.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsCustomSerializer.json deleted file mode 100644 index 810d2e5df84a0..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsCustomSerializer.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "c6669339-b393-463b-ac0f-99ce6b45da46", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "75bd8425-5c77-4499-9af0-0aa835357284", - "Date" : "Tue, 11 Aug 2020 17:19:22 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsImpl.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsImpl.json deleted file mode 100644 index 2a637d570f864..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsImpl.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "28f39f04-5f7c-4e77-b9b5-7a2617797a43", - "Date" : "Tue, 11 Aug 2020 17:44:23 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsSync.json deleted file mode 100644 index 8dd0e697e21ba..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "03ef7d02-18af-425a-9daa-d0075f8a4b43", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "baa0bc8e-ed85-4ade-ba43-157d7ff76e75", - "Date" : "Tue, 11 Aug 2020 17:41:21 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvent.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvent.json deleted file mode 100644 index 7d63f99b62bce..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvent.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "683534b9-dfed-4578-9b3e-8a47ded6fe49", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "dbaab228-a4a4-4bb1-9271-f9c3610b0754", - "Date" : "Thu, 04 Mar 2021 00:41:03 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventSync.json deleted file mode 100644 index 1f535757a365f..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "5030108e-5bc2-498b-967b-4442e2c25dd3", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "4d5b4d46-fe5d-4478-9e2d-21f1b1f60883", - "Date" : "Thu, 04 Mar 2021 00:41:02 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvents.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvents.json deleted file mode 100644 index 8f944e6ec944c..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvents.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "f68db376-0cea-4f04-a27e-f427720546e6", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "Connection" : "close", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "37152ed5-e2ff-4c09-b68a-97988fa484e5", - "Date" : "Tue, 11 Aug 2020 17:19:17 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsImpl.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsImpl.json deleted file mode 100644 index 4e53aefeef2f7..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsImpl.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "b53f39a3-c15a-4182-bfa2-109938bd70ab", - "Date" : "Tue, 11 Aug 2020 17:44:22 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsSync.json deleted file mode 100644 index 411f53d47ca6e..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "efa97ec4-6df5-4ca0-99cc-55ddb6a0e18c", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "d1c7c818-61e5-4b41-b457-bf52d4346a91", - "Date" : "Tue, 11 Aug 2020 17:41:22 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsWithSerializer.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsWithSerializer.json deleted file mode 100644 index 63a6faffe3a3f..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsWithSerializer.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "defbc48f-0a9b-45c7-b589-0b10e0643e6d", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "cbd376af-eb8a-4a06-87e2-fd2d63343e3a", - "Date" : "Fri, 05 Mar 2021 20:56:33 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvent.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvent.json deleted file mode 100644 index 2c91305537bc0..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvent.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "ecbcca83-377d-4454-ba8e-6f727d6f88ed", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "6768ba48-3ed2-45fd-82d3-ad191d15024a", - "Date" : "Thu, 04 Mar 2021 00:41:03 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventSync.json deleted file mode 100644 index 19c3724e8b81c..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "8fdd6235-af20-4f88-a80f-8316cb1e7958", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "f4f7922a-93fa-47bf-8dfa-b3210a9d435d", - "Date" : "Thu, 04 Mar 2021 00:41:02 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvents.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvents.json deleted file mode 100644 index a52bd91b0675b..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvents.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "313923b6-018b-4bd5-b85c-50ab6fbcba5a", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "72bc43d9-cd75-4c02-8aaa-386d286d0094", - "Date" : "Fri, 05 Mar 2021 10:16:09 GMT" - }, - "Exception" : null - }, { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "9e550170-6c36-42fa-9e9a-211c4448e4d5", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "8fd1ea89-c886-4bf6-9c55-abee3a658c4c", - "Date" : "Fri, 05 Mar 2021 10:16:09 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsImpl.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsImpl.json deleted file mode 100644 index 8e907ba09c91c..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsImpl.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "60773b4e-3405-454e-97d9-0b588435f3f6", - "Date" : "Tue, 11 Aug 2020 17:48:05 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsSync.json deleted file mode 100644 index 5c85e18996057..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "34f4e8c8-8147-472c-afda-ddef9aabb8a7", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "d9ba16b2-cf9d-40a4-ade9-832a054682d0", - "Date" : "Tue, 11 Aug 2020 17:19:21 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithSasToken.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithSasToken.json deleted file mode 100644 index 45c09ca52c566..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithSasToken.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "4bfcef57-2a14-4e59-b55f-b7742459d419", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "14bac486-ca11-48cb-8294-b979f490b1a3", - "Date" : "Thu, 04 Mar 2021 00:41:03 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithTokenCredential.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithTokenCredential.json deleted file mode 100644 index 95823bab3c354..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithTokenCredential.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid-int.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (15.0.2; Windows 10; 10.0)", - "x-ms-client-request-id" : "d9d08daa-f908-4338-ab35-ad6042dc5b9c", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "83028bf1-abe4-49ee-a244-ad5e66335d25", - "Date" : "Fri, 25 Jun 2021 21:19:01 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file From f9ed9a0c841c0416627d6d606027c8bd2f15ef99 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Thu, 14 Sep 2023 12:21:36 -0700 Subject: [PATCH 08/44] address pr comments --- sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md b/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md index 3fa301183576a..5d1a97af88ed2 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md +++ b/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md @@ -18,7 +18,7 @@ ### Other Changes #### Dependency Updates -- + - Upgraded `azure-core` from `1.42.0` to version `1.43.0`. - Upgraded `azure-core-http-netty` from `1.13.6` to version `1.13.7`. @@ -86,7 +86,7 @@ ### Other Changes -~~#### Dependency Updates +#### Dependency Updates - Upgraded `azure-core-http-netty` from `1.13.0` to version `1.13.1`. - Upgraded `azure-core` from `1.36.0` to version `1.37.0`. @@ -115,7 +115,7 @@ - `Microsoft.ApiManagement.GatewayDeleted` - `Microsoft.ApiManagement.GatewayHostnameConfigurationCreated` - `Microsoft.ApiManagement.GatewayHostnameConfigurationDeleted` - - `Microsoft.ApiManagement.GatewayHostnameConfigurationUpdated`~~ + - `Microsoft.ApiManagement.GatewayHostnameConfigurationUpdated` - `Microsoft.ApiManagement.GatewayUpdated` - `Microsoft.DataBox.CopyCompleted` - `Microsoft.DataBox.CopyStarted` From 64f5e98110d761722c239d2788824d3fabd2d1fd Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Thu, 14 Sep 2023 12:47:25 -0700 Subject: [PATCH 09/44] fix merge conflicts --- .../identity/implementation/IdentityClientOptions.java | 9 --------- 1 file changed, 9 deletions(-) diff --git a/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/IdentityClientOptions.java b/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/IdentityClientOptions.java index b513ffdac2032..a8ecccc072546 100644 --- a/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/IdentityClientOptions.java +++ b/sdk/identity/azure-identity/src/main/java/com/azure/identity/implementation/IdentityClientOptions.java @@ -15,10 +15,7 @@ import com.azure.core.util.logging.ClientLogger; import com.azure.identity.AzureAuthorityHosts; import com.azure.identity.AuthenticationRecord; -<<<<<<< HEAD -======= import com.azure.identity.BrowserCustomizationOptions; ->>>>>>> upstream/main import com.azure.identity.ChainedTokenCredential; import com.azure.identity.TokenCachePersistenceOptions; import com.azure.identity.implementation.util.IdentityConstants; @@ -78,10 +75,7 @@ public final class IdentityClientOptions implements Cloneable { private Duration credentialProcessTimeout = Duration.ofSeconds(10); private boolean isChained; -<<<<<<< HEAD -======= private boolean enableUnsafeSupportLogging; ->>>>>>> upstream/main /** * Creates an instance of IdentityClientOptions with default settings. @@ -732,8 +726,6 @@ public IdentityClientOptions setChained(boolean isChained) { return this; } -<<<<<<< HEAD -======= /** * Gets the status whether support logging is enabled or not. * @return the flag indicating if support logging is enabled or not. @@ -751,7 +743,6 @@ public IdentityClientOptions enableUnsafeSupportLogging() { return this; } ->>>>>>> upstream/main public IdentityClientOptions clone() { IdentityClientOptions clone = new IdentityClientOptions() .setAdditionallyAllowedTenants(this.additionallyAllowedTenants) From 1b35edc8941851fb8f7219292bb5ccdb365d2594 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Fri, 15 Sep 2023 13:00:40 -0700 Subject: [PATCH 10/44] add exports --- sdk/eventgrid/azure-messaging-eventgrid/pom.xml | 4 +++- .../eventgrid/EventGridPublisherClientTests.java | 12 ++---------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml index 39557032f79f9..9da17d5dd0b21 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml +++ b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml @@ -56,6 +56,8 @@ --add-opens com.azure.messaging.eventgrid/com.azure.messaging.eventgrid.implementation=ALL-UNNAMED --add-reads com.azure.messaging.eventgrid=com.azure.core.serializer.json.jackson + --add-exports com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED + --add-exports com.azure.core/com.azure.core.implementation=ALL-UNNAMED @@ -118,7 +120,7 @@ com.azure azure-core-test - 1.20.0 + 1.21.0-beta.1 test diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index 4260a968311c5..b454f05a63829 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -305,16 +305,8 @@ public void publishCustomEvent() { .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherAsyncClient(); - BinaryData event = BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }); - StepVerifier.create(egClient.sendEvent(event)) + BinaryData customEvent = getCustomEvent(); + StepVerifier.create(egClient.sendEvent(customEvent)) .expectComplete() .verify(DEFAULT_TIMEOUT); } From dcf148a22e8bad5f71b3bb5aa2ef1f04f658451a Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Fri, 15 Sep 2023 13:06:57 -0700 Subject: [PATCH 11/44] fix version --- sdk/eventgrid/azure-messaging-eventgrid/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml index 9da17d5dd0b21..fe1394700585f 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml +++ b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml @@ -120,7 +120,7 @@ com.azure azure-core-test - 1.21.0-beta.1 + 1.20.0 test From c6a2bb3165329c090fe4e2e2e58e9fcdd81b5f91 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Fri, 15 Sep 2023 13:17:30 -0700 Subject: [PATCH 12/44] remove unused imports --- .../messaging/eventgrid/EventGridPublisherClientTests.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index b454f05a63829..6ca69a5e89ece 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -31,9 +31,7 @@ import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; From 26cc35a88541e96d930a5ffc7073dec6812e99b0 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Tue, 19 Sep 2023 13:26:48 -0700 Subject: [PATCH 13/44] use unreleased test version --- eng/versioning/version_client.txt | 1 + .../test/http/TestProxyPlaybackClient.java | 40 +++++++++++++++++-- .../azure-messaging-eventgrid/pom.xml | 2 +- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/eng/versioning/version_client.txt b/eng/versioning/version_client.txt index 1f48fa508eb5e..c4da4049d229d 100644 --- a/eng/versioning/version_client.txt +++ b/eng/versioning/version_client.txt @@ -439,6 +439,7 @@ com.azure.tools:azure-sdk-build-tool;1.0.0;1.1.0-beta.1 # note: The unreleased dependencies will not be manipulated with the automatic PR creation code. # In the pom, the version update tag after the version should name the unreleased package and the dependency version: # +unreleased_com.azure:azure-core-test;dependency-1.21.0-beta.1 # Released Beta dependencies: Copy the entry from above, prepend "beta_", remove the current # version and set the version to the released beta. Released beta dependencies are only valid diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java index 5864ee515c387..cdffe27067a1f 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java @@ -14,6 +14,7 @@ import com.azure.core.test.utils.HttpURLConnectionHttpClient; import com.azure.core.test.utils.TestProxyUtils; import com.azure.core.util.Context; +import com.azure.core.util.logging.ClientLogger; import com.azure.core.util.serializer.JacksonAdapter; import com.azure.core.util.serializer.SerializerAdapter; import com.azure.core.util.serializer.SerializerEncoding; @@ -32,6 +33,7 @@ import java.util.List; import java.util.Map; import java.util.Queue; +import java.util.concurrent.TimeUnit; import static com.azure.core.test.implementation.TestingHelpers.X_RECORDING_FILE_LOCATION; import static com.azure.core.test.implementation.TestingHelpers.X_RECORDING_ID; @@ -46,6 +48,7 @@ */ public class TestProxyPlaybackClient implements HttpClient { + private static final ClientLogger LOGGER = new ClientLogger(TestProxyPlaybackClient.class); private final HttpClient client; private final URL proxyUrl; private String xRecordingId; @@ -92,7 +95,8 @@ public Queue startPlayback(File recordFile, Path testClassPath) { } catch (IOException e) { throw new RuntimeException(e); } - try (HttpResponse response = client.sendSync(request, Context.NONE)) { + + try (HttpResponse response = sendRequestWithRetries(request)) { checkForTestProxyErrors(response); xRecordingId = response.getHeaderValue(X_RECORDING_ID); xRecordingFileLocation @@ -127,13 +131,41 @@ public Queue startPlayback(File recordFile, Path testClassPath) { } } + private HttpResponse sendRequestWithRetries(HttpRequest request) { + return sendRequestWithRetries(request, Context.NONE); + } + + private HttpResponse sendRequestWithRetries(HttpRequest request, Context context) { + int retries = 0; + while (true) { + try { + HttpResponse response = client.sendSync(request, Context.NONE); + if (response.getStatusCode() / 100 != 2) { + throw new RuntimeException("Test proxy returned a non-successful status code. " + response.getStatusCode()); + } + return response; + } catch (Exception e) { + retries++; + if(retries >= 3) { + throw e; + } + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException ex) { + throw new RuntimeException(ex); + } + LOGGER.warning("Retrying request to test proxy. Retry attempt: " + retries); + } + } + } + /** * Stops playback of a test recording. */ public void stopPlayback() { HttpRequest request = new HttpRequest(HttpMethod.POST, String.format("%s/playback/stop", proxyUrl.toString())) .setHeader(X_RECORDING_ID, xRecordingId); - client.sendSync(request, Context.NONE); + sendRequestWithRetries(request); } /** @@ -192,7 +224,7 @@ public void addProxySanitization(List sanitizers) { getSanitizerRequests(sanitizers, proxyUrl) .forEach(request -> { request.setHeader(X_RECORDING_ID, xRecordingId); - client.sendSync(request, Context.NONE); + sendRequestWithRetries(request); }); } else { this.sanitizers.addAll(sanitizers); @@ -211,7 +243,7 @@ public void addMatcherRequests(List matchers) { } matcherRequests.forEach(request -> { request.setHeader(X_RECORDING_ID, xRecordingId); - client.sendSync(request, Context.NONE); + sendRequestWithRetries(request); }); } else { this.matchers.addAll(matchers); diff --git a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml index fe1394700585f..67b77b5f6efe8 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml +++ b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml @@ -120,7 +120,7 @@ com.azure azure-core-test - 1.20.0 + 1.21.0-beta.1 test From dc41e496d7a7d751ae738c8b74853a269645eb9a Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Tue, 19 Sep 2023 13:45:10 -0700 Subject: [PATCH 14/44] fix tag --- sdk/eventgrid/azure-messaging-eventgrid/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml index 67b77b5f6efe8..61c27a7a7df8b 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml +++ b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml @@ -120,7 +120,7 @@ com.azure azure-core-test - 1.21.0-beta.1 + 1.21.0-beta.1 test From fb2098994334a99cff2c2ce24b21960aab476e65 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Tue, 19 Sep 2023 15:33:09 -0700 Subject: [PATCH 15/44] fix tag --- eng/versioning/version_client.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/versioning/version_client.txt b/eng/versioning/version_client.txt index c4da4049d229d..95652e9b1c15c 100644 --- a/eng/versioning/version_client.txt +++ b/eng/versioning/version_client.txt @@ -439,7 +439,7 @@ com.azure.tools:azure-sdk-build-tool;1.0.0;1.1.0-beta.1 # note: The unreleased dependencies will not be manipulated with the automatic PR creation code. # In the pom, the version update tag after the version should name the unreleased package and the dependency version: # -unreleased_com.azure:azure-core-test;dependency-1.21.0-beta.1 +unreleased_com.azure:azure-core-test;1.21.0-beta.1 # Released Beta dependencies: Copy the entry from above, prepend "beta_", remove the current # version and set the version to the released beta. Released beta dependencies are only valid From 5560758d22d241747d8074a5380e720dd9084c5e Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Wed, 20 Sep 2023 11:13:09 -0700 Subject: [PATCH 16/44] refactor --- .../test/http/TestProxyPlaybackClient.java | 28 ++++++++----------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java index cdffe27067a1f..26fc4f1246121 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java @@ -92,16 +92,12 @@ public Queue startPlayback(File recordFile, Path testClassPath) { SerializerEncoding.JSON)) .setHeader(HttpHeaderName.ACCEPT, "application/json") .setHeader(HttpHeaderName.CONTENT_TYPE, "application/json"); - } catch (IOException e) { - throw new RuntimeException(e); - } - - try (HttpResponse response = sendRequestWithRetries(request)) { + HttpResponse response = sendRequestWithRetries(request); checkForTestProxyErrors(response); xRecordingId = response.getHeaderValue(X_RECORDING_ID); xRecordingFileLocation = new String(Base64.getUrlDecoder().decode( - response.getHeaders().get(X_RECORDING_FILE_LOCATION).getValue()), StandardCharsets.UTF_8); + response.getHeaders().get(X_RECORDING_FILE_LOCATION).getValue()), StandardCharsets.UTF_8); addProxySanitization(this.sanitizers); addMatcherRequests(this.matchers); String body = response.getBodyAsString().block(); @@ -132,10 +128,6 @@ public Queue startPlayback(File recordFile, Path testClassPath) { } private HttpResponse sendRequestWithRetries(HttpRequest request) { - return sendRequestWithRetries(request, Context.NONE); - } - - private HttpResponse sendRequestWithRetries(HttpRequest request, Context context) { int retries = 0; while (true) { try { @@ -146,19 +138,23 @@ private HttpResponse sendRequestWithRetries(HttpRequest request, Context context return response; } catch (Exception e) { retries++; - if(retries >= 3) { + if (retries >= 3) { throw e; } - try { - TimeUnit.SECONDS.sleep(1); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } + sleep(1); LOGGER.warning("Retrying request to test proxy. Retry attempt: " + retries); } } } + private void sleep(int durationInSeconds) { + try { + TimeUnit.SECONDS.sleep(durationInSeconds); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + /** * Stops playback of a test recording. */ From 44e43fd8edd2a04aa8a9b2432c2cdf51dfece762 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Wed, 20 Sep 2023 11:49:22 -0700 Subject: [PATCH 17/44] fix spring test after sync stack migration --- eng/versioning/version_client.txt | 1 + sdk/spring/spring-cloud-azure-autoconfigure/pom.xml | 2 +- .../eventgrid/AzureEventGridAutoConfigurationTests.java | 3 +-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/eng/versioning/version_client.txt b/eng/versioning/version_client.txt index 95652e9b1c15c..fbb0ce58949ea 100644 --- a/eng/versioning/version_client.txt +++ b/eng/versioning/version_client.txt @@ -440,6 +440,7 @@ com.azure.tools:azure-sdk-build-tool;1.0.0;1.1.0-beta.1 # In the pom, the version update tag after the version should name the unreleased package and the dependency version: # unreleased_com.azure:azure-core-test;1.21.0-beta.1 +unreleased_com.azure:azure-messaging-eventgrid;4.19.0-beta.1 # Released Beta dependencies: Copy the entry from above, prepend "beta_", remove the current # version and set the version to the released beta. Released beta dependencies are only valid diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/pom.xml b/sdk/spring/spring-cloud-azure-autoconfigure/pom.xml index aa11c90e069db..1af10ea005b6b 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/pom.xml +++ b/sdk/spring/spring-cloud-azure-autoconfigure/pom.xml @@ -177,7 +177,7 @@ com.azure azure-messaging-eventgrid - 4.18.0 + 4.19.0-beta.1 true diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/implementation/eventgrid/AzureEventGridAutoConfigurationTests.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/implementation/eventgrid/AzureEventGridAutoConfigurationTests.java index 3b79fb194c714..bd1644395b61d 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/implementation/eventgrid/AzureEventGridAutoConfigurationTests.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/implementation/eventgrid/AzureEventGridAutoConfigurationTests.java @@ -152,8 +152,7 @@ void clientCanOverride() { assertThat(context).hasBean("myCustomClient"); Object eventClassOfAsyncClient = ReflectionUtils.getField(EventGridPublisherAsyncClient.class, "eventClass", context.getBean(EventGridPublisherAsyncClient.class)); - Object eventGridPublisherAsyncClient = ReflectionUtils.getField(EventGridPublisherClient.class, "asyncClient", context.getBean(EventGridPublisherClient.class)); - Object eventClassOfSyncClient = ReflectionUtils.getField(EventGridPublisherAsyncClient.class, "eventClass", eventGridPublisherAsyncClient); + Object eventClassOfSyncClient = ReflectionUtils.getField(EventGridPublisherClient.class, "eventClass", context.getBean(EventGridPublisherClient.class)); assertEquals(EventGridEvent.class, eventClassOfAsyncClient); assertEquals(BinaryData.class, eventClassOfSyncClient); }); From 1d2d8202a2efb85c35566bd256beb3aea9272ce9 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Wed, 20 Sep 2023 14:22:46 -0700 Subject: [PATCH 18/44] log the test proxy response on error --- .../java/com/azure/core/test/http/TestProxyPlaybackClient.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java index 26fc4f1246121..affc0a6b5d15f 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java @@ -133,7 +133,8 @@ private HttpResponse sendRequestWithRetries(HttpRequest request) { try { HttpResponse response = client.sendSync(request, Context.NONE); if (response.getStatusCode() / 100 != 2) { - throw new RuntimeException("Test proxy returned a non-successful status code. " + response.getStatusCode()); + throw new RuntimeException("Test proxy returned a non-successful status code. " + + response.getStatusCode() + "; response: " + response.getBodyAsString().block()); } return response; } catch (Exception e) { From 48911dbd05ec046b1740b947f6490ad3c025f5b4 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Fri, 22 Sep 2023 14:39:44 -0700 Subject: [PATCH 19/44] fix header sanitizer in Service Bus --- .../ServiceBusAdministrationAsyncClientIntegrationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/administration/ServiceBusAdministrationAsyncClientIntegrationTest.java b/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/administration/ServiceBusAdministrationAsyncClientIntegrationTest.java index f58899b1faf3c..0c213d3ac87f0 100644 --- a/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/administration/ServiceBusAdministrationAsyncClientIntegrationTest.java +++ b/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/administration/ServiceBusAdministrationAsyncClientIntegrationTest.java @@ -92,7 +92,7 @@ class ServiceBusAdministrationAsyncClientIntegrationTest extends TestProxyTestBa static final List TEST_PROXY_REQUEST_MATCHERS; static { - AUTHORIZATION_HEADER = new TestProxySanitizer("SupplementaryAuthorization", "SharedAccessSignature sr=https%3A%2F%2Ffoo.servicebus.windows.net&sig=dummyValue%3D&se=1687267490&skn=dummyKey", TestProxySanitizerType.HEADER); + AUTHORIZATION_HEADER = new TestProxySanitizer("SupplementaryAuthorization", null, "SharedAccessSignature sr=https%3A%2F%2Ffoo.servicebus.windows.net&sig=dummyValue%3D&se=1687267490&skn=dummyKey", TestProxySanitizerType.HEADER); TEST_PROXY_SANITIZERS = Collections.singletonList(AUTHORIZATION_HEADER); final List skippedHeaders = Arrays.asList("ServiceBusDlqSupplementaryAuthorization", "ServiceBusSupplementaryAuthorization"); From edbe426ad9c6e3ec5277b4c5ff8a919ca674a2bd Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Sat, 23 Sep 2023 19:22:35 -0700 Subject: [PATCH 20/44] fix regex for quantum job tests --- sdk/quantum/azure-quantum-jobs/pom.xml | 4 ++++ .../java/com/azure/quantum/jobs/QuantumClientTestBase.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/sdk/quantum/azure-quantum-jobs/pom.xml b/sdk/quantum/azure-quantum-jobs/pom.xml index aed6cd635105e..dd5033bc4d67a 100644 --- a/sdk/quantum/azure-quantum-jobs/pom.xml +++ b/sdk/quantum/azure-quantum-jobs/pom.xml @@ -39,6 +39,10 @@ false + + --add-exports com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED + --add-opens com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED + diff --git a/sdk/quantum/azure-quantum-jobs/src/test/java/com/azure/quantum/jobs/QuantumClientTestBase.java b/sdk/quantum/azure-quantum-jobs/src/test/java/com/azure/quantum/jobs/QuantumClientTestBase.java index 86c68702db32c..2299df6ca4a22 100644 --- a/sdk/quantum/azure-quantum-jobs/src/test/java/com/azure/quantum/jobs/QuantumClientTestBase.java +++ b/sdk/quantum/azure-quantum-jobs/src/test/java/com/azure/quantum/jobs/QuantumClientTestBase.java @@ -62,7 +62,7 @@ QuantumClientBuilder getClientBuilder(HttpClient httpClient) { customSanitizers.add(new TestProxySanitizer(getResourceGroup(), RESOURCE_GROUP, TestProxySanitizerType.URL)); customSanitizers.add(new TestProxySanitizer(getWorkspaceName(), WORKSPACE, TestProxySanitizerType.URL)); customSanitizers.add(new TestProxySanitizer(getLocation(), LOCATION, TestProxySanitizerType.URL)); - customSanitizers.add(new TestProxySanitizer("(?:\\?(sv|sig|se|srt|ss|sp)=)(?.*)", SANITIZED, TestProxySanitizerType.BODY_REGEX).setGroupForReplace("secret")); + customSanitizers.add(new TestProxySanitizer("(?:\\\\?(sv|sig|se|srt|ss|sp)=)(?.*)", SANITIZED, TestProxySanitizerType.BODY_REGEX).setGroupForReplace("secret")); customSanitizers.add(new TestProxySanitizer("$..sasUri", null, SANITIZED, TestProxySanitizerType.BODY_KEY)); customSanitizers.add(new TestProxySanitizer("$..containerUri", null, SANITIZED, TestProxySanitizerType.BODY_KEY)); customSanitizers.add(new TestProxySanitizer("$..inputDataUri", null, SANITIZED, TestProxySanitizerType.BODY_KEY)); From f4d8493d61ea5cb3b4b3beab731591a5e6f82013 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Mon, 12 Jun 2023 16:50:25 -0700 Subject: [PATCH 21/44] Add missing sync test cases --- .../EventGridPublisherClientTests.java | 167 +++++++++++++++++- 1 file changed, 166 insertions(+), 1 deletion(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index 4af767cdfd548..060cf9cdc0863 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -10,12 +10,12 @@ import com.azure.core.http.HttpHeader; import com.azure.core.http.policy.RetryPolicy; import com.azure.core.http.rest.Response; +import com.azure.core.models.CloudEvent; import com.azure.core.models.CloudEventDataFormat; import com.azure.core.serializer.json.jackson.JacksonJsonSerializerBuilder; import com.azure.core.test.TestBase; import com.azure.core.util.BinaryData; import com.azure.core.util.Context; -import com.azure.core.models.CloudEvent; import com.azure.core.util.serializer.JacksonAdapter; import com.azure.identity.DefaultAzureCredential; import com.azure.identity.DefaultAzureCredentialBuilder; @@ -40,6 +40,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; public class EventGridPublisherClientTests extends TestBase { @@ -476,6 +477,58 @@ public void publishEventGridEventSync() { egClient.sendEvent(event); } + @Test + public void publishWithSasTokenSync() { + + String sasToken = EventGridPublisherAsyncClient.generateSas( + getEndpoint(EVENTGRID_ENDPOINT), + getKey(EVENTGRID_KEY), + OffsetDateTime.now().plusMinutes(20) + ); + + EventGridPublisherClient egClient = builder + .credential(new AzureSasCredential(sasToken)) + .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) + .buildEventGridEventPublisherClient(); + + EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), + "1.0") + .setEventTime(OffsetDateTime.now()); + + egClient.sendEvent(event); + } + + @Disabled + @Test + public void publishWithTokenCredentialSync() { + DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build(); + EventGridPublisherClient egClient = builder + .credential(defaultCredential) + .endpoint(getEndpoint(CLOUD_ENDPOINT)) + .buildCloudEventPublisherClient(); + + List events = new ArrayList<>(); + events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), CloudEventDataFormat.JSON, "application/json") + .setSubject("Test") + .setTime(OffsetDateTime.now())); + + egClient.sendEvents(events); + } + @Test public void publishCloudEventsSync() { EventGridPublisherClient egClient = builder @@ -523,6 +576,95 @@ public void publishCloudEventSync() { egClient.sendEvent(event); } + @Test + public void publishCloudEventsToPartnerTopicSync() { + EventGridPublisherClient egClient = builder + .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) + .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) + .addPolicy((httpPipelineCallContext, httpPipelineNextPolicy) -> { + HttpHeader httpHeader = httpPipelineCallContext.getHttpRequest().getHeaders().get("aeg-channel-name"); + assertNotNull(httpHeader); + return httpPipelineNextPolicy.process(); + }) + .buildCloudEventPublisherClient(); + + CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), CloudEventDataFormat.JSON, "application/json") + .setSubject("Test") + .setTime(OffsetDateTime.now()); + + Response response = egClient.sendEventsWithResponse(Arrays.asList(event), + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); + assertEquals(200, response.getStatusCode()); + } + + @Test + public void publishEventGridEventToPartnerTopicSync() { + EventGridPublisherClient egClient = builder + .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) + .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) + .addPolicy((httpPipelineCallContext, httpPipelineNextPolicy) -> { + HttpHeader httpHeader = httpPipelineCallContext.getHttpRequest().getHeaders().get("aeg-channel-name"); + assertNotNull(httpHeader); + return httpPipelineNextPolicy.process(); + }) + .buildEventGridEventPublisherClient(); + + EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), + "1.0") + .setEventTime(OffsetDateTime.now()); + + + HttpResponseException exception = assertThrows(HttpResponseException.class, () -> { + egClient.sendEventsWithResponse(Arrays.asList(event), + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); + }); + assertEquals(400, exception.getResponse().getStatusCode()); + } + + @Test + public void publishCloudEventsCustomSerializerSync() { + // Custom Serializer for testData + JacksonAdapter customSerializer = new JacksonAdapter(); + customSerializer.serializer().registerModule(new SimpleModule().addSerializer(TestData.class, + new JsonSerializer() { + @Override + public void serialize(TestData testData, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) + throws IOException { + jsonGenerator.writeString(testData.getName()); + } + })); + + EventGridPublisherClient egClient = builder + .credential(getKey(CLOUD_KEY)) + .endpoint(getEndpoint(CLOUD_ENDPOINT)) + .buildCloudEventPublisherClient(); + + List events = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new TestData().setName("Hello " + i)), CloudEventDataFormat.JSON, null) + .setSubject("Test " + i) + ); + } + + Response response = egClient.sendEventsWithResponse(events, Context.NONE); + assertEquals(200, response.getStatusCode()); + } + @Test public void publishCustomEventsSync() { EventGridPublisherClient egClient = builder @@ -547,6 +689,29 @@ public void publishCustomEventsSync() { assertEquals(response.getStatusCode(), 200); } + @Test + public void publishCustomEventsWithSerializerSync() { + EventGridPublisherClient egClient = builder + .credential(getKey(CUSTOM_KEY)) + .endpoint(getEndpoint(CUSTOM_ENDPOINT)) + .buildCustomEventPublisherClient(); + + List events = new ArrayList<>(); + for (int i = 0; i < 5; i++) { + events.add(BinaryData.fromObject(new HashMap() { + { + put("id", UUID.randomUUID().toString()); + put("time", OffsetDateTime.now().toString()); + put("subject", "Test"); + put("foo", "bar"); + put("type", "Microsoft.MockPublisher.TestEvent"); + } + }, new JacksonJsonSerializerBuilder().build())); + } + Response response = egClient.sendEventsWithResponse(events, Context.NONE); + assertEquals(200, response.getStatusCode()); + } + @Test public void publishCustomEventSync() { EventGridPublisherClient egClient = builder From 503813cdc4ea60ba6b5b72c22bf88ea345eb8808 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Thu, 22 Jun 2023 17:48:48 -0700 Subject: [PATCH 22/44] Enable sync-stack --- .../azure-messaging-eventgrid/CHANGELOG.md | 4 +- .../eventgrid/EventGridPublisherClient.java | 118 ++++++++++++++-- .../EventGridPublisherClientBuilder.java | 28 ++-- .../EventGridPublisherClientImpl.java | 129 +++++++++++++++++- .../PublishCloudEventsToPartnerTopic.java | 3 +- .../EventGridPublisherClientTests.java | 19 +-- .../swagger/README.md | 1 + 7 files changed, 262 insertions(+), 40 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md b/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md index 323e6513caf46..3fa301183576a 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md +++ b/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md @@ -86,7 +86,7 @@ ### Other Changes -#### Dependency Updates +~~#### Dependency Updates - Upgraded `azure-core-http-netty` from `1.13.0` to version `1.13.1`. - Upgraded `azure-core` from `1.36.0` to version `1.37.0`. @@ -115,7 +115,7 @@ - `Microsoft.ApiManagement.GatewayDeleted` - `Microsoft.ApiManagement.GatewayHostnameConfigurationCreated` - `Microsoft.ApiManagement.GatewayHostnameConfigurationDeleted` - - `Microsoft.ApiManagement.GatewayHostnameConfigurationUpdated` + - `Microsoft.ApiManagement.GatewayHostnameConfigurationUpdated`~~ - `Microsoft.ApiManagement.GatewayUpdated` - `Microsoft.DataBox.CopyCompleted` - `Microsoft.DataBox.CopyStarted` diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java index a56268373c5a4..afe8375814bda 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java @@ -8,11 +8,21 @@ import com.azure.core.annotation.ServiceMethod; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.credential.AzureSasCredential; +import com.azure.core.http.HttpPipeline; import com.azure.core.http.rest.Response; import com.azure.core.models.CloudEvent; +import com.azure.core.util.BinaryData; import com.azure.core.util.Context; +import com.azure.core.util.logging.ClientLogger; +import com.azure.messaging.eventgrid.implementation.EventGridPublisherClientImpl; +import com.azure.messaging.eventgrid.implementation.EventGridPublisherClientImplBuilder; +import com.fasterxml.jackson.databind.util.RawValue; import java.time.OffsetDateTime; +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; /** * A service client that publishes events to an EventGrid topic or domain. Use {@link EventGridPublisherClientBuilder} @@ -124,10 +134,19 @@ */ @ServiceClient(builder = EventGridPublisherClientBuilder.class) public final class EventGridPublisherClient { + private final ClientLogger logger = new ClientLogger(EventGridPublisherClient.class); - private final EventGridPublisherAsyncClient asyncClient; - EventGridPublisherClient(EventGridPublisherAsyncClient client) { - this.asyncClient = client; + private final EventGridPublisherClientImpl impl; + private final String hostname; + private final Class eventClass; + EventGridPublisherClient(HttpPipeline pipeline, String hostname, EventGridServiceVersion serviceVersion, + Class eventClass) { + this.hostname = hostname; + this.eventClass = eventClass; + this.impl = new EventGridPublisherClientImplBuilder() + .pipeline(pipeline) + .apiVersion(serviceVersion.getVersion()) + .buildClient(); } /** @@ -173,38 +192,111 @@ public static String generateSas(String endpoint, AzureKeyCredential keyCredenti * @throws NullPointerException if events is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) + @SuppressWarnings("unchecked") public void sendEvents(Iterable events) { - asyncClient.sendEvents(events).block(); + if (this.eventClass == CloudEvent.class) { + this.sendCloudEvents((Iterable) events); + } else if (this.eventClass == EventGridEvent.class) { + this.sendEventGridEvents((Iterable) events); + } else { + this.sendCustomEvents((Iterable) events); + } + } + + private void sendCustomEvents(Iterable events) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + List objectEvents = StreamSupport.stream(events.spliterator(), false) + .map(event -> (Object) new RawValue(event.toString())) + .collect(Collectors.toList()); + this.impl.publishCustomEventEvents(this.hostname, objectEvents); + } + + private void sendEventGridEvents(Iterable events) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + List eventGridEvents = StreamSupport.stream(events.spliterator(), false) + .map(EventGridEvent::toImpl) + .collect(Collectors.toList()); + this.impl.publishEventGridEvents(this.hostname, eventGridEvents); + } + + private void sendCloudEvents(Iterable events) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + List cloudEvents = StreamSupport.stream(events.spliterator(), false) + .collect(Collectors.toList()); + this.impl.publishCloudEventEvents(this.hostname, cloudEvents, null); + } /** * Publishes the given events to the set topic or domain and gives the response issued by EventGrid. * @param events the events to publish. - * @param context the context to use along the pipeline. * * @return the response from the EventGrid service. * @throws NullPointerException if events is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) - public Response sendEventsWithResponse(Iterable events, Context context) { - return asyncClient.sendEventsWithResponse(events, context).block(); + public Response sendEventsWithResponse(Iterable events) { + return this.sendEventsWithResponse(events, null); } + /** * Publishes the given events to the set topic or domain and gives the response issued by EventGrid. * @param events the events to publish. * @param channelName the channel name to send to Event Grid service. This is only applicable for sending * Cloud Events to a partner topic in partner namespace. For more details, refer to * Partner Events Overview. - * @param context the context to use along the pipeline. * * @return the response from the EventGrid service. * @throws NullPointerException if events is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) - public Response sendEventsWithResponse(Iterable events, String channelName, - Context context) { - return asyncClient.sendEventsWithResponse(events, channelName, context).block(); + @SuppressWarnings("unchecked") + public Response sendEventsWithResponse(Iterable events, String channelName) { + if (this.eventClass == CloudEvent.class) { + return this.sendCloudEventsWithResponse((Iterable) events, channelName); + } else if (this.eventClass == EventGridEvent.class) { + return this.sendEventGridEventsWithResponse((Iterable) events); + } else { + return this.sendCustomEventsWithResponse((Iterable) events); + } + } + + private Response sendCustomEventsWithResponse(Iterable events) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + List objectEvents = StreamSupport.stream(events.spliterator(), false) + .map(event -> (Object) new RawValue(event.toString())) + .collect(Collectors.toList()); + return this.impl.publishCustomEventEventsWithResponse(this.hostname, objectEvents, Context.NONE); + } + + private Response sendEventGridEventsWithResponse(Iterable events) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + + List eventGridEvents = StreamSupport.stream(events.spliterator(), false) + .map(EventGridEvent::toImpl) + .collect(Collectors.toList()); + return this.impl.publishEventGridEventsWithResponse(this.hostname, eventGridEvents, Context.NONE); + } + + private Response sendCloudEventsWithResponse(Iterable events, String channelName) { + if (events == null) { + throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); + } + + List cloudEvents = StreamSupport.stream(events.spliterator(), false) + .collect(Collectors.toList()); + return this.impl.publishCloudEventEventsWithResponse(this.hostname, cloudEvents, channelName, Context.NONE); } /** @@ -215,7 +307,7 @@ public Response sendEventsWithResponse(Iterable events, String channelN */ @ServiceMethod(returns = ReturnType.SINGLE) public void sendEvent(T event) { - - asyncClient.sendEvent(event).block(); + List events = Collections.singletonList(event); + this.sendEvents(events); } } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClientBuilder.java b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClientBuilder.java index 02cbd88f404c2..91ac85ccb31c0 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClientBuilder.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClientBuilder.java @@ -132,14 +132,21 @@ public EventGridPublisherClientBuilder() { */ private EventGridPublisherAsyncClient buildAsyncClient(Class eventClass) { Objects.requireNonNull(endpoint, "'endpoint' is required and can not be null."); + + return new EventGridPublisherAsyncClient((httpPipeline != null ? httpPipeline : getHttpPipeline()), + endpoint, + getEventGridServiceVersion(), + eventClass); + } + + private EventGridServiceVersion getEventGridServiceVersion() { EventGridServiceVersion buildServiceVersion = serviceVersion == null ? EventGridServiceVersion.getLatest() : serviceVersion; + return buildServiceVersion; + } - if (httpPipeline != null) { - return new EventGridPublisherAsyncClient(httpPipeline, endpoint, buildServiceVersion, eventClass); - } - + private HttpPipeline getHttpPipeline() { Configuration buildConfiguration = (configuration == null) ? Configuration.getGlobalConfiguration() : configuration; @@ -216,20 +223,21 @@ private EventGridPublisherAsyncClient buildAsyncClient(Class eventClas .clientOptions(clientOptions) .tracer(tracer) .build(); - - - return new EventGridPublisherAsyncClient(buildPipeline, endpoint, buildServiceVersion, eventClass); + return buildPipeline; } /** * Build a publisher client with synchronous publishing methods and the current settings. Endpoint and a credential * must be set (either keyCredential or sharedAccessSignatureCredential), all other settings have defaults and/or are optional. - * Note that currently the asynchronous client created by the method above is the recommended version for higher - * performance, as the synchronous client simply blocks on the same asynchronous calls. * @return a publisher client with synchronous publishing methods. */ private EventGridPublisherClient buildClient(Class eventClass) { - return new EventGridPublisherClient(buildAsyncClient(eventClass)); + Objects.requireNonNull(endpoint, "'endpoint' is required and can not be null."); + + return new EventGridPublisherClient((httpPipeline != null ? httpPipeline : getHttpPipeline()), + endpoint, + getEventGridServiceVersion(), + eventClass); } @Override diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/implementation/EventGridPublisherClientImpl.java b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/implementation/EventGridPublisherClientImpl.java index 9e217a1fc35ba..e0fb4c8ed53dc 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/implementation/EventGridPublisherClientImpl.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/implementation/EventGridPublisherClientImpl.java @@ -28,9 +28,10 @@ import com.azure.core.util.serializer.JacksonAdapter; import com.azure.core.util.serializer.SerializerAdapter; import com.azure.messaging.eventgrid.implementation.models.EventGridEvent; -import java.util.List; import reactor.core.publisher.Mono; +import java.util.List; + /** Initializes a new instance of the EventGridPublisherClient type. */ public final class EventGridPublisherClientImpl { @@ -129,6 +130,15 @@ Mono> publishEventGridEvents( @BodyParam("application/json") List events, Context context); + @Post("") + @ExpectedResponses({200}) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Response publishEventGridEventsSync( + @HostParam("topicHostname") String topicHostname, + @QueryParam("api-version") String apiVersion, + @BodyParam("application/json") List events, + Context context); + @Post("") @ExpectedResponses({200}) @UnexpectedResponseExceptionType(HttpResponseException.class) @@ -139,6 +149,16 @@ Mono> publishCloudEventEvents( @BodyParam("application/cloudevents-batch+json; charset=utf-8") List events, Context context); + @Post("") + @ExpectedResponses({200}) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Response publishCloudEventEventsSync( + @HostParam("topicHostname") String topicHostname, + @QueryParam("api-version") String apiVersion, + @HeaderParam("aeg-channel-name") String aegChannelName, + @BodyParam("application/cloudevents-batch+json; charset=utf-8") List events, + Context context); + @Post("") @ExpectedResponses({200}) @UnexpectedResponseExceptionType(HttpResponseException.class) @@ -147,6 +167,15 @@ Mono> publishCustomEventEvents( @QueryParam("api-version") String apiVersion, @BodyParam("application/json") List events, Context context); + + @Post("") + @ExpectedResponses({200}) + @UnexpectedResponseExceptionType(HttpResponseException.class) + Response publishCustomEventEventsSync( + @HostParam("topicHostname") String topicHostname, + @QueryParam("api-version") String apiVersion, + @BodyParam("application/json") List events, + Context context); } /** @@ -214,6 +243,37 @@ public Mono publishEventGridEventsAsync(String topicHostname, List Mono.empty()); } + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @param context The context to associate with this operation. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return the {@link Response}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Response publishEventGridEventsWithResponse( + String topicHostname, List events, Context context) { + return service.publishEventGridEventsSync(topicHostname, this.getApiVersion(), events, context); + } + + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public void publishEventGridEvents(String topicHostname, List events) { + publishEventGridEventsWithResponse(topicHostname, events, Context.NONE); + } + /** * Publishes a batch of events to an Azure Event Grid topic. * @@ -293,6 +353,42 @@ public Mono publishCloudEventEventsAsync( .flatMap(ignored -> Mono.empty()); } + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @param aegChannelName Required only when publishing to partner namespaces with partner topic routing mode + * ChannelNameHeader. + * @param context The context to associate with this operation. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return the {@link Response}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Response publishCloudEventEventsWithResponse( + String topicHostname, List events, String aegChannelName, Context context) { + return service.publishCloudEventEventsSync( + topicHostname, this.getApiVersion(), aegChannelName, events, context); + } + + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @param aegChannelName Required only when publishing to partner namespaces with partner topic routing mode + * ChannelNameHeader. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public void publishCloudEventEvents(String topicHostname, List events, String aegChannelName) { + publishCloudEventEventsWithResponse(topicHostname, events, aegChannelName, Context.NONE); + } + /** * Publishes a batch of events to an Azure Event Grid topic. * @@ -357,4 +453,35 @@ public Mono publishCustomEventEventsAsync(String topicHostname, List Mono.empty()); } + + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @param context The context to associate with this operation. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + * @return the {@link Response}. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public Response publishCustomEventEventsWithResponse( + String topicHostname, List events, Context context) { + return service.publishCustomEventEventsSync(topicHostname, this.getApiVersion(), events, context); + } + + /** + * Publishes a batch of events to an Azure Event Grid topic. + * + * @param topicHostname The host name of the topic, e.g. topic1.westus2-1.eventgrid.azure.net. + * @param events An array of events to be published to Event Grid. + * @throws IllegalArgumentException thrown if parameters fail the validation. + * @throws HttpResponseException thrown if the request is rejected by server. + * @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent. + */ + @ServiceMethod(returns = ReturnType.SINGLE) + public void publishCustomEventEvents(String topicHostname, List events) { + publishCustomEventEventsWithResponse(topicHostname, events, Context.NONE); + } } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java b/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java index 6793d67d3fafa..ea58c67db8934 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java @@ -7,7 +7,6 @@ import com.azure.core.models.CloudEvent; import com.azure.core.models.CloudEventDataFormat; import com.azure.core.util.BinaryData; -import com.azure.core.util.Context; import com.azure.messaging.eventgrid.EventGridPublisherClient; import com.azure.messaging.eventgrid.EventGridPublisherClientBuilder; import com.azure.messaging.eventgrid.samples.models.User; @@ -59,6 +58,6 @@ public static void main(String[] args) { events.add(cloudEventBytes.addExtensionAttribute("extension", "value")); // send to "my-channel-name" channel - publisherClient.sendEventsWithResponse(events, "my-channel-name", Context.NONE); + publisherClient.sendEventsWithResponse(events, "my-channel-name"); } } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index 060cf9cdc0863..f0a707b72caee 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -450,7 +450,7 @@ public void publishEventGridEventsSync() { "1.0") .setEventTime(OffsetDateTime.now())); - Response response = egClient.sendEventsWithResponse(events, Context.NONE); + Response response = egClient.sendEventsWithResponse(events); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -549,7 +549,7 @@ public void publishCloudEventsSync() { .setSubject("Test") .setTime(OffsetDateTime.now())); - Response response = egClient.sendEventsWithResponse(events, Context.NONE); + Response response = egClient.sendEventsWithResponse(events); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -600,7 +600,7 @@ public void publishCloudEventsToPartnerTopicSync() { .setTime(OffsetDateTime.now()); Response response = egClient.sendEventsWithResponse(Arrays.asList(event), - getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); assertEquals(200, response.getStatusCode()); } @@ -609,11 +609,6 @@ public void publishEventGridEventToPartnerTopicSync() { EventGridPublisherClient egClient = builder .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) - .addPolicy((httpPipelineCallContext, httpPipelineNextPolicy) -> { - HttpHeader httpHeader = httpPipelineCallContext.getHttpRequest().getHeaders().get("aeg-channel-name"); - assertNotNull(httpHeader); - return httpPipelineNextPolicy.process(); - }) .buildEventGridEventPublisherClient(); EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", @@ -630,7 +625,7 @@ public void publishEventGridEventToPartnerTopicSync() { HttpResponseException exception = assertThrows(HttpResponseException.class, () -> { egClient.sendEventsWithResponse(Arrays.asList(event), - getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); }); assertEquals(400, exception.getResponse().getStatusCode()); } @@ -661,7 +656,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer ); } - Response response = egClient.sendEventsWithResponse(events, Context.NONE); + Response response = egClient.sendEventsWithResponse(events); assertEquals(200, response.getStatusCode()); } @@ -683,7 +678,7 @@ public void publishCustomEventsSync() { } })); } - Response response = egClient.sendEventsWithResponse(events, Context.NONE); + Response response = egClient.sendEventsWithResponse(events); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -708,7 +703,7 @@ public void publishCustomEventsWithSerializerSync() { } }, new JacksonJsonSerializerBuilder().build())); } - Response response = egClient.sendEventsWithResponse(events, Context.NONE); + Response response = egClient.sendEventsWithResponse(events); assertEquals(200, response.getStatusCode()); } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/swagger/README.md b/sdk/eventgrid/azure-messaging-eventgrid/swagger/README.md index 2bd89a83d9514..b153a7c9ba79b 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/swagger/README.md +++ b/sdk/eventgrid/azure-messaging-eventgrid/swagger/README.md @@ -35,6 +35,7 @@ models-subpackage: systemevents customization-class: src/main/java/EventGridCustomization.java service-interface-as-public: true url-as-string: true +enable-sync-stack: true directive: - rename-model: from: ResourceActionCancelData From a1f0197e2a44ba667bcd44ef4346569b1dd4fdfd Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Fri, 23 Jun 2023 09:27:36 -0700 Subject: [PATCH 23/44] Remove extra set of aeg-channel-name. --- .../EventGridPublisherAsyncClient.java | 27 +++---------------- .../EventGridPublisherClientTests.java | 5 ---- 2 files changed, 3 insertions(+), 29 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherAsyncClient.java b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherAsyncClient.java index 321ce3287a492..19ffd703eac8f 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherAsyncClient.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherAsyncClient.java @@ -8,14 +8,11 @@ import com.azure.core.annotation.ServiceMethod; import com.azure.core.credential.AzureKeyCredential; import com.azure.core.credential.AzureSasCredential; -import com.azure.core.http.HttpHeaders; import com.azure.core.http.HttpPipeline; -import com.azure.core.http.policy.AddHeadersFromContextPolicy; import com.azure.core.http.rest.Response; import com.azure.core.models.CloudEvent; import com.azure.core.util.BinaryData; import com.azure.core.util.Context; -import com.azure.core.util.CoreUtils; import com.azure.core.util.logging.ClientLogger; import com.azure.core.util.tracing.Tracer; import com.azure.messaging.eventgrid.implementation.Constants; @@ -39,7 +36,6 @@ import java.util.Base64; import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Objects; import static com.azure.core.util.FluxUtil.monoError; @@ -155,7 +151,6 @@ @ServiceClient(builder = EventGridPublisherClientBuilder.class, isAsync = true) public final class EventGridPublisherAsyncClient { - private static final String PARTNER_CHANNEL_HEADER_NAME = "aeg-channel-name"; private final String hostname; private final EventGridPublisherClientImpl impl; @@ -312,25 +307,9 @@ Mono> sendEventsWithResponse(Iterable events, String channelNa if (context == null) { context = Context.NONE; } - if (!CoreUtils.isNullOrEmpty(channelName)) { - String requestHttpHeadersKey = AddHeadersFromContextPolicy.AZURE_REQUEST_HTTP_HEADERS_KEY; - Map keyValues = context.getValues(); - if (keyValues != null && keyValues.containsKey(requestHttpHeadersKey)) { - // if the given Context instance already contains custom headers, - // add partner channel header to HttpHeaders - Object value = keyValues.get(requestHttpHeadersKey); - if (value instanceof HttpHeaders) { - HttpHeaders headers = (HttpHeaders) value; - headers.add(PARTNER_CHANNEL_HEADER_NAME, channelName); - } - } else { - context = context.addData(requestHttpHeadersKey, - new HttpHeaders().add(PARTNER_CHANNEL_HEADER_NAME, channelName)); - } - } if (this.eventClass == CloudEvent.class) { - return this.sendCloudEventsWithResponse((Iterable) events, context); + return this.sendCloudEventsWithResponse((Iterable) events, channelName, context); } else if (this.eventClass == EventGridEvent.class) { return this.sendEventGridEventsWithResponse((Iterable) events, context); } else { @@ -395,7 +374,7 @@ Mono> sendEventGridEventsWithResponse(Iterable ev .flatMap(list -> this.impl.publishEventGridEventsWithResponseAsync(this.hostname, list, finalContext)); } - Mono> sendCloudEventsWithResponse(Iterable events, Context context) { + Mono> sendCloudEventsWithResponse(Iterable events, String channelName, Context context) { if (events == null) { return monoError(logger, new NullPointerException("'events' cannot be null.")); } @@ -403,7 +382,7 @@ Mono> sendCloudEventsWithResponse(Iterable events, Co this.addCloudEventTracePlaceHolder(events); return Flux.fromIterable(events) .collectList() - .flatMap(list -> this.impl.publishCloudEventEventsWithResponseAsync(this.hostname, list, null, finalContext)); + .flatMap(list -> this.impl.publishCloudEventEventsWithResponseAsync(this.hostname, list, channelName, finalContext)); } Mono> sendCustomEventsWithResponse(Iterable events, Context context) { diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index f0a707b72caee..aca8aaab116d0 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -282,11 +282,6 @@ public void publishEventGridEventToPartnerTopic() { EventGridPublisherAsyncClient egClient = builder .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) - .addPolicy((httpPipelineCallContext, httpPipelineNextPolicy) -> { - HttpHeader httpHeader = httpPipelineCallContext.getHttpRequest().getHeaders().get("aeg-channel-name"); - assertNotNull(httpHeader); - return httpPipelineNextPolicy.process(); - }) .buildEventGridEventPublisherAsyncClient(); EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", From 5087c06fdafe31214743dec64ede75cb8c306498 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Fri, 23 Jun 2023 10:03:40 -0700 Subject: [PATCH 24/44] enable AssertingHttpClient --- .../EventGridPublisherClientTests.java | 46 +++++++++++++------ 1 file changed, 33 insertions(+), 13 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index aca8aaab116d0..e0cd5ea073930 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -7,6 +7,7 @@ import com.azure.core.credential.AzureKeyCredential; import com.azure.core.credential.AzureSasCredential; import com.azure.core.exception.HttpResponseException; +import com.azure.core.http.HttpClient; import com.azure.core.http.HttpHeader; import com.azure.core.http.policy.RetryPolicy; import com.azure.core.http.rest.Response; @@ -14,6 +15,7 @@ import com.azure.core.models.CloudEventDataFormat; import com.azure.core.serializer.json.jackson.JacksonJsonSerializerBuilder; import com.azure.core.test.TestBase; +import com.azure.core.test.http.AssertingHttpClientBuilder; import com.azure.core.util.BinaryData; import com.azure.core.util.Context; import com.azure.core.util.serializer.JacksonAdapter; @@ -47,6 +49,8 @@ public class EventGridPublisherClientTests extends TestBase { private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(30); private EventGridPublisherClientBuilder builder; + private EventGridPublisherClientBuilder syncBuilder; + // Event Grid endpoint for a topic accepting EventGrid schema events private static final String EVENTGRID_ENDPOINT = "AZURE_EVENTGRID_EVENT_ENDPOINT"; @@ -80,10 +84,14 @@ public class EventGridPublisherClientTests extends TestBase { @Override protected void beforeTest() { builder = new EventGridPublisherClientBuilder(); + syncBuilder = new EventGridPublisherClientBuilder(); if (interceptorManager.isPlaybackMode()) { - builder.httpClient(interceptorManager.getPlaybackClient()); + builder.httpClient(buildAssertingClient(interceptorManager.getPlaybackClient(), false)); + syncBuilder.httpClient(buildAssertingClient(interceptorManager.getPlaybackClient(), true)); } else { + builder.httpClient(buildAssertingClient(HttpClient.createDefault(), false)); + syncBuilder.httpClient(buildAssertingClient(HttpClient.createDefault(), true)); builder.addPolicy(interceptorManager.getRecordPolicy()) .retryPolicy(new RetryPolicy()); } @@ -428,7 +436,7 @@ public void publishCustomEvent() { @Test public void publishEventGridEventsSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(EVENTGRID_KEY)) .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) .buildEventGridEventPublisherClient(); @@ -453,7 +461,7 @@ public void publishEventGridEventsSync() { @Test public void publishEventGridEventSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(EVENTGRID_KEY)) .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) .buildEventGridEventPublisherClient(); @@ -481,7 +489,7 @@ public void publishWithSasTokenSync() { OffsetDateTime.now().plusMinutes(20) ); - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(new AzureSasCredential(sasToken)) .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) .buildEventGridEventPublisherClient(); @@ -504,7 +512,7 @@ public void publishWithSasTokenSync() { @Test public void publishWithTokenCredentialSync() { DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build(); - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(defaultCredential) .endpoint(getEndpoint(CLOUD_ENDPOINT)) .buildCloudEventPublisherClient(); @@ -526,7 +534,7 @@ public void publishWithTokenCredentialSync() { @Test public void publishCloudEventsSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CLOUD_KEY)) .endpoint(getEndpoint(CLOUD_ENDPOINT)) .buildCloudEventPublisherClient(); @@ -552,7 +560,7 @@ public void publishCloudEventsSync() { @Test public void publishCloudEventSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CLOUD_KEY)) .endpoint(getEndpoint(CLOUD_ENDPOINT)) .buildCloudEventPublisherClient(); @@ -573,7 +581,7 @@ public void publishCloudEventSync() { @Test public void publishCloudEventsToPartnerTopicSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) .addPolicy((httpPipelineCallContext, httpPipelineNextPolicy) -> { @@ -601,7 +609,7 @@ public void publishCloudEventsToPartnerTopicSync() { @Test public void publishEventGridEventToPartnerTopicSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .endpoint(getEndpoint(EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT)) .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) .buildEventGridEventPublisherClient(); @@ -638,7 +646,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer } })); - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CLOUD_KEY)) .endpoint(getEndpoint(CLOUD_ENDPOINT)) .buildCloudEventPublisherClient(); @@ -657,7 +665,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer @Test public void publishCustomEventsSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CUSTOM_KEY)) .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherClient(); @@ -681,7 +689,7 @@ public void publishCustomEventsSync() { @Test public void publishCustomEventsWithSerializerSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CUSTOM_KEY)) .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherClient(); @@ -704,7 +712,7 @@ public void publishCustomEventsWithSerializerSync() { @Test public void publishCustomEventSync() { - EventGridPublisherClient egClient = builder + EventGridPublisherClient egClient = syncBuilder .credential(getKey(CUSTOM_KEY)) .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherClient(); @@ -746,4 +754,16 @@ private String getChannelName(String liveEnvName) { assertNotNull(channelName, "System environment variable " + liveEnvName + " is null"); return channelName; } + + + private HttpClient buildAssertingClient(HttpClient httpClient, boolean sync) { + AssertingHttpClientBuilder builder = new AssertingHttpClientBuilder(httpClient) + .skipRequest((ignored1, ignored2) -> false); + if (sync) { + builder.assertSync(); + } else { + builder.assertAsync(); + } + return builder.build(); + } } From b224700a8a7f9fd844985f80a905e8a09b7d8276 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Thu, 29 Jun 2023 12:34:03 -0700 Subject: [PATCH 25/44] add back missing Context arguments --- .../eventgrid/EventGridPublisherClient.java | 26 ++++++++++--------- .../PublishCloudEventsToPartnerTopic.java | 3 ++- .../EventGridPublisherClientTests.java | 14 +++++----- 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java index afe8375814bda..6ef14b530097e 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/main/java/com/azure/messaging/eventgrid/EventGridPublisherClient.java @@ -236,13 +236,14 @@ private void sendCloudEvents(Iterable events) { /** * Publishes the given events to the set topic or domain and gives the response issued by EventGrid. * @param events the events to publish. + * @param context the context to use along the pipeline. * * @return the response from the EventGrid service. * @throws NullPointerException if events is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) - public Response sendEventsWithResponse(Iterable events) { - return this.sendEventsWithResponse(events, null); + public Response sendEventsWithResponse(Iterable events, Context context) { + return this.sendEventsWithResponse(events, null, context); } @@ -252,33 +253,34 @@ public Response sendEventsWithResponse(Iterable events) { * @param channelName the channel name to send to Event Grid service. This is only applicable for sending * Cloud Events to a partner topic in partner namespace. For more details, refer to * Partner Events Overview. + * @param context the context to use along the pipeline. * * @return the response from the EventGrid service. * @throws NullPointerException if events is {@code null}. */ @ServiceMethod(returns = ReturnType.SINGLE) @SuppressWarnings("unchecked") - public Response sendEventsWithResponse(Iterable events, String channelName) { + public Response sendEventsWithResponse(Iterable events, String channelName, Context context) { if (this.eventClass == CloudEvent.class) { - return this.sendCloudEventsWithResponse((Iterable) events, channelName); + return this.sendCloudEventsWithResponse((Iterable) events, channelName, context); } else if (this.eventClass == EventGridEvent.class) { - return this.sendEventGridEventsWithResponse((Iterable) events); + return this.sendEventGridEventsWithResponse((Iterable) events, context); } else { - return this.sendCustomEventsWithResponse((Iterable) events); + return this.sendCustomEventsWithResponse((Iterable) events, context); } } - private Response sendCustomEventsWithResponse(Iterable events) { + private Response sendCustomEventsWithResponse(Iterable events, Context context) { if (events == null) { throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); } List objectEvents = StreamSupport.stream(events.spliterator(), false) .map(event -> (Object) new RawValue(event.toString())) .collect(Collectors.toList()); - return this.impl.publishCustomEventEventsWithResponse(this.hostname, objectEvents, Context.NONE); + return this.impl.publishCustomEventEventsWithResponse(this.hostname, objectEvents, context); } - private Response sendEventGridEventsWithResponse(Iterable events) { + private Response sendEventGridEventsWithResponse(Iterable events, Context context) { if (events == null) { throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); } @@ -286,17 +288,17 @@ private Response sendEventGridEventsWithResponse(Iterable List eventGridEvents = StreamSupport.stream(events.spliterator(), false) .map(EventGridEvent::toImpl) .collect(Collectors.toList()); - return this.impl.publishEventGridEventsWithResponse(this.hostname, eventGridEvents, Context.NONE); + return this.impl.publishEventGridEventsWithResponse(this.hostname, eventGridEvents, context); } - private Response sendCloudEventsWithResponse(Iterable events, String channelName) { + private Response sendCloudEventsWithResponse(Iterable events, String channelName, Context context) { if (events == null) { throw logger.logExceptionAsError(new NullPointerException("'events' cannot be null.")); } List cloudEvents = StreamSupport.stream(events.spliterator(), false) .collect(Collectors.toList()); - return this.impl.publishCloudEventEventsWithResponse(this.hostname, cloudEvents, channelName, Context.NONE); + return this.impl.publishCloudEventEventsWithResponse(this.hostname, cloudEvents, channelName, context); } /** diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java b/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java index ea58c67db8934..6793d67d3fafa 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/samples/java/com/azure/messaging/eventgrid/samples/PublishCloudEventsToPartnerTopic.java @@ -7,6 +7,7 @@ import com.azure.core.models.CloudEvent; import com.azure.core.models.CloudEventDataFormat; import com.azure.core.util.BinaryData; +import com.azure.core.util.Context; import com.azure.messaging.eventgrid.EventGridPublisherClient; import com.azure.messaging.eventgrid.EventGridPublisherClientBuilder; import com.azure.messaging.eventgrid.samples.models.User; @@ -58,6 +59,6 @@ public static void main(String[] args) { events.add(cloudEventBytes.addExtensionAttribute("extension", "value")); // send to "my-channel-name" channel - publisherClient.sendEventsWithResponse(events, "my-channel-name"); + publisherClient.sendEventsWithResponse(events, "my-channel-name", Context.NONE); } } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index e0cd5ea073930..7992d8b8f07e7 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -453,7 +453,7 @@ public void publishEventGridEventsSync() { "1.0") .setEventTime(OffsetDateTime.now())); - Response response = egClient.sendEventsWithResponse(events); + Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -552,7 +552,7 @@ public void publishCloudEventsSync() { .setSubject("Test") .setTime(OffsetDateTime.now())); - Response response = egClient.sendEventsWithResponse(events); + Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -603,7 +603,7 @@ public void publishCloudEventsToPartnerTopicSync() { .setTime(OffsetDateTime.now()); Response response = egClient.sendEventsWithResponse(Arrays.asList(event), - getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); assertEquals(200, response.getStatusCode()); } @@ -628,7 +628,7 @@ public void publishEventGridEventToPartnerTopicSync() { HttpResponseException exception = assertThrows(HttpResponseException.class, () -> { egClient.sendEventsWithResponse(Arrays.asList(event), - getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); + getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); }); assertEquals(400, exception.getResponse().getStatusCode()); } @@ -659,7 +659,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer ); } - Response response = egClient.sendEventsWithResponse(events); + Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertEquals(200, response.getStatusCode()); } @@ -681,7 +681,7 @@ public void publishCustomEventsSync() { } })); } - Response response = egClient.sendEventsWithResponse(events); + Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertNotNull(response); assertEquals(response.getStatusCode(), 200); @@ -706,7 +706,7 @@ public void publishCustomEventsWithSerializerSync() { } }, new JacksonJsonSerializerBuilder().build())); } - Response response = egClient.sendEventsWithResponse(events); + Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertEquals(200, response.getStatusCode()); } From e9a00d1d96de5930ad6bb904abe93f158536e5c8 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Wed, 6 Sep 2023 14:16:56 -0700 Subject: [PATCH 26/44] Tests now use test proxy and recordings are pushed to assets repo --- .../azure-messaging-eventgrid/assets.json | 6 + .../azure-messaging-eventgrid/pom.xml | 2 +- .../EventGridPublisherClientTests.java | 315 ++---------------- .../EventGridPublisherImplTests.java | 100 +----- .../eventgrid/EventGridTestBase.java | 148 ++++++++ ...ests.publishCloudEventsToPartnerTopic.json | 23 -- ...s.publishEventGridEventToPartnerTopic.json | 24 -- .../session-records/publishCloudEvent.json | 23 -- .../publishCloudEventSync.json | 23 -- .../session-records/publishCloudEvents.json | 23 -- .../publishCloudEventsCustomSerializer.json | 23 -- .../publishCloudEventsImpl.json | 21 -- .../publishCloudEventsSync.json | 23 -- .../session-records/publishCustomEvent.json | 23 -- .../publishCustomEventSync.json | 23 -- .../session-records/publishCustomEvents.json | 24 -- .../publishCustomEventsImpl.json | 21 -- .../publishCustomEventsSync.json | 23 -- .../publishCustomEventsWithSerializer.json | 23 -- .../publishEventGridEvent.json | 23 -- .../publishEventGridEventSync.json | 23 -- .../publishEventGridEvents.json | 42 --- .../publishEventGridEventsImpl.json | 21 -- .../publishEventGridEventsSync.json | 23 -- .../session-records/publishWithSasToken.json | 23 -- .../publishWithTokenCredential.json | 23 -- 26 files changed, 207 insertions(+), 862 deletions(-) create mode 100644 sdk/eventgrid/azure-messaging-eventgrid/assets.json create mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridTestBase.java delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishCloudEventsToPartnerTopic.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishEventGridEventToPartnerTopic.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvent.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvents.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsCustomSerializer.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsImpl.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvent.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvents.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsImpl.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsWithSerializer.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvent.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvents.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsImpl.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsSync.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithSasToken.json delete mode 100644 sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithTokenCredential.json diff --git a/sdk/eventgrid/azure-messaging-eventgrid/assets.json b/sdk/eventgrid/azure-messaging-eventgrid/assets.json new file mode 100644 index 0000000000000..37ab80f8b8562 --- /dev/null +++ b/sdk/eventgrid/azure-messaging-eventgrid/assets.json @@ -0,0 +1,6 @@ +{ + "AssetsRepo": "Azure/azure-sdk-assets", + "AssetsRepoPrefixPath": "java", + "TagPrefix": "java/eventgrid/azure-messaging-eventgrid", + "Tag": "java/eventgrid/azure-messaging-eventgrid_7bb36a1579" +} diff --git a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml index 39557032f79f9..de96f6c58284f 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml +++ b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml @@ -118,7 +118,7 @@ com.azure azure-core-test - 1.20.0 + 1.19.0-beta.1 test diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java index 7992d8b8f07e7..921008a873c45 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherClientTests.java @@ -4,7 +4,6 @@ package com.azure.messaging.eventgrid; -import com.azure.core.credential.AzureKeyCredential; import com.azure.core.credential.AzureSasCredential; import com.azure.core.exception.HttpResponseException; import com.azure.core.http.HttpClient; @@ -12,9 +11,6 @@ import com.azure.core.http.policy.RetryPolicy; import com.azure.core.http.rest.Response; import com.azure.core.models.CloudEvent; -import com.azure.core.models.CloudEventDataFormat; -import com.azure.core.serializer.json.jackson.JacksonJsonSerializerBuilder; -import com.azure.core.test.TestBase; import com.azure.core.test.http.AssertingHttpClientBuilder; import com.azure.core.util.BinaryData; import com.azure.core.util.Context; @@ -35,51 +31,20 @@ import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Arrays; -import java.util.HashMap; import java.util.List; -import java.util.Map; -import java.util.UUID; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; -public class EventGridPublisherClientTests extends TestBase { +public class EventGridPublisherClientTests extends EventGridTestBase { private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(30); - private EventGridPublisherClientBuilder builder; private EventGridPublisherClientBuilder syncBuilder; - // Event Grid endpoint for a topic accepting EventGrid schema events - private static final String EVENTGRID_ENDPOINT = "AZURE_EVENTGRID_EVENT_ENDPOINT"; - - // Event Grid endpoint for a topic accepting CloudEvents schema events - private static final String CLOUD_ENDPOINT = "AZURE_EVENTGRID_CLOUDEVENT_ENDPOINT"; - - // Event Grid endpoint for a topic accepting custom schema events - private static final String CUSTOM_ENDPOINT = "AZURE_EVENTGRID_CUSTOM_ENDPOINT"; - - // Event Grid access key for a topic accepting EventGrid schema events - private static final String EVENTGRID_KEY = "AZURE_EVENTGRID_EVENT_KEY"; - - // Event Grid access key for a topic accepting CloudEvents schema events - private static final String CLOUD_KEY = "AZURE_EVENTGRID_CLOUDEVENT_KEY"; - - // Event Grid access key for a topic accepting custom schema events - private static final String CUSTOM_KEY = "AZURE_EVENTGRID_CUSTOM_KEY"; - - // Endpoint, key and channel name for publishing to partner topic - private static final String EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT = "EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT"; - private static final String EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY = "EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY"; - private static final String EVENTGRID_PARTNER_CHANNEL_NAME = "EVENTGRID_PARTNER_CHANNEL_NAME"; - private static final String DUMMY_ENDPOINT = "https://www.dummyEndpoint.com/api/events"; - - private static final String DUMMY_KEY = "dummyKey"; - - private static final String DUMMY_CHANNEL_NAME = "dummy-channel"; @Override protected void beforeTest() { @@ -89,14 +54,23 @@ protected void beforeTest() { if (interceptorManager.isPlaybackMode()) { builder.httpClient(buildAssertingClient(interceptorManager.getPlaybackClient(), false)); syncBuilder.httpClient(buildAssertingClient(interceptorManager.getPlaybackClient(), true)); - } else { + } else { // both record and live will use these clients builder.httpClient(buildAssertingClient(HttpClient.createDefault(), false)); syncBuilder.httpClient(buildAssertingClient(HttpClient.createDefault(), true)); + } + + if (interceptorManager.isRecordMode()) { builder.addPolicy(interceptorManager.getRecordPolicy()) .retryPolicy(new RetryPolicy()); + syncBuilder.addPolicy(interceptorManager.getRecordPolicy()) + .retryPolicy(new RetryPolicy()); } + + setupSanitizers(); } + + @Test public void publishEventGridEvents() { EventGridPublisherAsyncClient egClient = builder @@ -105,16 +79,7 @@ public void publishEventGridEvents() { .buildEventGridEventPublisherAsyncClient(); List events = new ArrayList<>(); - events.add(new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now())); + events.add(getEventGridEvent()); StepVerifier.create(egClient.sendEventsWithResponse(events)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -133,16 +98,7 @@ public void publishEventGridEvent() { .credential(getKey(EVENTGRID_KEY)) .buildEventGridEventPublisherAsyncClient(); - EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now()); + EventGridEvent event = getEventGridEvent(); StepVerifier.create(egClient.sendEvent(event)) .expectComplete() .verify(DEFAULT_TIMEOUT); @@ -162,16 +118,7 @@ public void publishWithSasToken() { .buildEventGridEventPublisherAsyncClient(); List events = new ArrayList<>(); - events.add(new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now())); + events.add(getEventGridEvent()); StepVerifier.create(egClient.sendEventsWithResponse(events, Context.NONE)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -189,16 +136,7 @@ public void publishWithTokenCredential() { .buildCloudEventPublisherAsyncClient(); List events = new ArrayList<>(); - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now())); + events.add(getCloudEvent()); StepVerifier.create(egClient.sendEventsWithResponse(events, Context.NONE)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -214,16 +152,7 @@ public void publishCloudEvents() { .buildCloudEventPublisherAsyncClient(); List events = new ArrayList<>(); - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now())); + events.add(getCloudEvent()); StepVerifier.create(egClient.sendEventsWithResponse(events, Context.NONE)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -238,16 +167,7 @@ public void publishCloudEvent() { .credential(getKey(CLOUD_KEY)) .buildCloudEventPublisherAsyncClient(); - CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now()); + CloudEvent event = getCloudEvent(); StepVerifier.create(egClient.sendEvent(event)) .expectComplete() @@ -266,16 +186,7 @@ public void publishCloudEventsToPartnerTopic() { }) .buildCloudEventPublisherAsyncClient(); - CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now()); + CloudEvent event = getCloudEvent(); Mono> responseMono = egClient.sendEventsWithResponse(Arrays.asList(event), getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); @@ -292,16 +203,7 @@ public void publishEventGridEventToPartnerTopic() { .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) .buildEventGridEventPublisherAsyncClient(); - EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now()); + EventGridEvent event = getEventGridEvent(); Mono> responseMono = egClient.sendEventsWithResponse(Arrays.asList(event), getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME)); @@ -350,9 +252,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new TestData().setName("Hello " + i)), CloudEventDataFormat.JSON, null) - .setSubject("Test " + i) + events.add(getCloudEvent(i) ); } @@ -372,15 +272,7 @@ public void publishCustomEvents() { List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - })); + events.add(getCustomEvent()); } StepVerifier.create(egClient.sendEventsWithResponse(events)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -397,15 +289,7 @@ public void publishCustomEventsWithSerializer() { List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }, new JacksonJsonSerializerBuilder().build())); + events.add(getCustomEventWithSerializer()); } StepVerifier.create(egClient.sendEventsWithResponse(events, Context.NONE)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -420,16 +304,7 @@ public void publishCustomEvent() { .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherAsyncClient(); - BinaryData event = BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }); - StepVerifier.create(egClient.sendEvent(event)) + StepVerifier.create(egClient.sendEvent(getCustomEvent())) .expectComplete() .verify(DEFAULT_TIMEOUT); } @@ -442,16 +317,7 @@ public void publishEventGridEventsSync() { .buildEventGridEventPublisherClient(); List events = new ArrayList<>(); - events.add(new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now())); + events.add(getEventGridEvent()); Response response = egClient.sendEventsWithResponse(events, Context.NONE); @@ -466,16 +332,7 @@ public void publishEventGridEventSync() { .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) .buildEventGridEventPublisherClient(); - EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now()); + EventGridEvent event = getEventGridEvent(); egClient.sendEvent(event); } @@ -494,16 +351,7 @@ public void publishWithSasTokenSync() { .endpoint(getEndpoint(EVENTGRID_ENDPOINT)) .buildEventGridEventPublisherClient(); - EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now()); + EventGridEvent event = getEventGridEvent(); egClient.sendEvent(event); } @@ -518,16 +366,7 @@ public void publishWithTokenCredentialSync() { .buildCloudEventPublisherClient(); List events = new ArrayList<>(); - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now())); + events.add(getCloudEvent()); egClient.sendEvents(events); } @@ -540,17 +379,7 @@ public void publishCloudEventsSync() { .buildCloudEventPublisherClient(); List events = new ArrayList<>(); - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setId(UUID.randomUUID().toString()) - .setSubject("Test") - .setTime(OffsetDateTime.now())); + events.add(getCloudEvent()); Response response = egClient.sendEventsWithResponse(events, Context.NONE); @@ -565,17 +394,7 @@ public void publishCloudEventSync() { .endpoint(getEndpoint(CLOUD_ENDPOINT)) .buildCloudEventPublisherClient(); - CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setId(UUID.randomUUID().toString()) - .setSubject("Test") - .setTime(OffsetDateTime.now()); + CloudEvent event = getCloudEvent(); egClient.sendEvent(event); } @@ -591,16 +410,7 @@ public void publishCloudEventsToPartnerTopicSync() { }) .buildCloudEventPublisherClient(); - CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setSubject("Test") - .setTime(OffsetDateTime.now()); + CloudEvent event = getCloudEvent(); Response response = egClient.sendEventsWithResponse(Arrays.asList(event), getChannelName(EVENTGRID_PARTNER_CHANNEL_NAME), Context.NONE); @@ -614,16 +424,7 @@ public void publishEventGridEventToPartnerTopicSync() { .credential(getKey(EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY)) .buildEventGridEventPublisherClient(); - EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), - "1.0") - .setEventTime(OffsetDateTime.now()); + EventGridEvent event = getEventGridEvent(); HttpResponseException exception = assertThrows(HttpResponseException.class, () -> { @@ -633,6 +434,8 @@ public void publishEventGridEventToPartnerTopicSync() { assertEquals(400, exception.getResponse().getStatusCode()); } + + @Test public void publishCloudEventsCustomSerializerSync() { // Custom Serializer for testData @@ -653,10 +456,7 @@ public void serialize(TestData testData, JsonGenerator jsonGenerator, Serializer List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new TestData().setName("Hello " + i)), CloudEventDataFormat.JSON, null) - .setSubject("Test " + i) - ); + events.add(getCloudEvent(i)); } Response response = egClient.sendEventsWithResponse(events, Context.NONE); @@ -672,14 +472,7 @@ public void publishCustomEventsSync() { List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - })); + events.add(getCustomEvent()); } Response response = egClient.sendEventsWithResponse(events, Context.NONE); @@ -696,15 +489,7 @@ public void publishCustomEventsWithSerializerSync() { List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(BinaryData.fromObject(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }, new JacksonJsonSerializerBuilder().build())); + events.add(getCustomEventWithSerializer()); } Response response = egClient.sendEventsWithResponse(events, Context.NONE); assertEquals(200, response.getStatusCode()); @@ -717,33 +502,7 @@ public void publishCustomEventSync() { .endpoint(getEndpoint(CUSTOM_ENDPOINT)) .buildCustomEventPublisherClient(); - Map event = new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }; - egClient.sendEvent(BinaryData.fromObject(event)); - } - - private String getEndpoint(String liveEnvName) { - if (interceptorManager.isPlaybackMode()) { - return DUMMY_ENDPOINT; - } - String endpoint = System.getenv(liveEnvName); - assertNotNull(endpoint, "System environment variable " + liveEnvName + " is null"); - return endpoint; - } - - private AzureKeyCredential getKey(String liveEnvName) { - if (interceptorManager.isPlaybackMode()) { - return new AzureKeyCredential(DUMMY_KEY); - } - AzureKeyCredential key = new AzureKeyCredential(System.getenv(liveEnvName)); - assertNotNull(key.getKey(), "System environment variable " + liveEnvName + " is null"); - return key; + egClient.sendEvent(getCustomEvent()); } private String getChannelName(String liveEnvName) { diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java index ff9ee0131b060..d82bc1295549d 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java @@ -18,6 +18,9 @@ import reactor.test.StepVerifier; import java.time.Duration; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; import java.time.OffsetDateTime; import java.util.ArrayList; import java.util.Collections; @@ -25,38 +28,14 @@ import java.util.List; import java.util.UUID; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -public class EventGridPublisherImplTests extends TestBase { +public class EventGridPublisherImplTests extends EventGridTestBase { private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(30); private HttpPipelineBuilder pipelineBuilder; private EventGridPublisherClientImplBuilder clientBuilder; - // Event Grid endpoint for a topic accepting EventGrid schema events - private static final String EVENTGRID_ENDPOINT = "AZURE_EVENTGRID_EVENT_ENDPOINT"; - - // Event Grid endpoint for a topic accepting CloudEvents schema events - private static final String CLOUD_ENDPOINT = "AZURE_EVENTGRID_CLOUDEVENT_ENDPOINT"; - - // Event Grid endpoint for a topic accepting custom schema events - private static final String CUSTOM_ENDPOINT = "AZURE_EVENTGRID_CUSTOM_ENDPOINT"; - - // Event Grid access key for a topic accepting EventGrid schema events - private static final String EVENTGRID_KEY = "AZURE_EVENTGRID_EVENT_KEY"; - - // Event Grid access key for a topic accepting CloudEvents schema events - private static final String CLOUD_KEY = "AZURE_EVENTGRID_CLOUDEVENT_KEY"; - - // Event Grid access key for a topic accepting custom schema events - private static final String CUSTOM_KEY = "AZURE_EVENTGRID_CUSTOM_KEY"; - - private static final String DUMMY_ENDPOINT = "https://www.dummyEndpoint.com/api/events"; - - private static final String DUMMY_KEY = "dummyKey"; - @Override protected void beforeTest() { pipelineBuilder = new HttpPipelineBuilder(); @@ -65,34 +44,24 @@ protected void beforeTest() { if (interceptorManager.isPlaybackMode()) { pipelineBuilder.httpClient(interceptorManager.getPlaybackClient()); - } else { + } else if (interceptorManager.isRecordMode()) { pipelineBuilder.policies(interceptorManager.getRecordPolicy(), new RetryPolicy()); } + + @Override + protected void afterTest() { + StepVerifier.resetDefaultTimeout(); } @Test public void publishEventGridEventsImpl() { EventGridPublisherClientImpl egClient = clientBuilder .pipeline(pipelineBuilder.policies( - new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(EVENTGRID_KEY)))) + new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(EVENTGRID_KEY).getKey()))) .build()) .buildClient(); - List events = Collections.singletonList( - new EventGridEvent() - .setId(UUID.randomUUID().toString()) - .setSubject("Test") - .setEventType("Microsoft.MockPublisher.TestEvent") - .setData(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }) - .setDataVersion("1.0") - .setEventTime(OffsetDateTime.now()) - ); + List events = Collections.singletonList(getEventGridEvent().toImpl()); StepVerifier.create(egClient.publishEventGridEventsWithResponseAsync(getEndpoint(EVENTGRID_ENDPOINT), events)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -104,23 +73,11 @@ public void publishEventGridEventsImpl() { public void publishCloudEventsImpl() { EventGridPublisherClientImpl egClient = clientBuilder .pipeline(pipelineBuilder.policies( - new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(CLOUD_KEY)))) + new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(CLOUD_KEY).getKey()))) .build()) .buildClient(); - List events = Collections.singletonList( - new CloudEvent("TestSource", "Microsoft.MockPublisher.TestEvent", - BinaryData.fromObject(new HashMap() { - { - put("Field1", "Value1"); - put("Field2", "Value2"); - put("Field3", "Value3"); - } - }), CloudEventDataFormat.JSON, "application/json") - .setId(UUID.randomUUID().toString()) - .setSubject("Test") - .setTime(OffsetDateTime.now()) - ); + List events = Collections.singletonList(getCloudEvent()); StepVerifier.create(egClient.publishCloudEventEventsWithResponseAsync(getEndpoint(CLOUD_ENDPOINT), events, null)) .expectNextMatches(voidResponse -> voidResponse.getStatusCode() == 200) @@ -132,21 +89,13 @@ public void publishCloudEventsImpl() { public void publishCustomEventsImpl() { EventGridPublisherClientImpl egClient = clientBuilder .pipeline(pipelineBuilder.policies( - new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(CUSTOM_KEY)))) + new AddHeadersPolicy(new HttpHeaders().put("aeg-sas-key", getKey(CUSTOM_KEY).getKey()))) .build()) .buildClient(); List events = new ArrayList<>(); for (int i = 0; i < 5; i++) { - events.add(new HashMap() { - { - put("id", UUID.randomUUID().toString()); - put("time", OffsetDateTime.now().toString()); - put("subject", "Test"); - put("foo", "bar"); - put("type", "Microsoft.MockPublisher.TestEvent"); - } - }); + events.add(getCustomEvent()); } StepVerifier.create(egClient.publishCustomEventEventsWithResponseAsync(getEndpoint(CUSTOM_ENDPOINT), events)) @@ -154,23 +103,4 @@ public void publishCustomEventsImpl() { .expectComplete() .verify(DEFAULT_TIMEOUT); } - - - private String getEndpoint(String liveEnvName) { - if (interceptorManager.isPlaybackMode()) { - return DUMMY_ENDPOINT; - } - String endpoint = System.getenv(liveEnvName); - assertNotNull(endpoint, "System environment variable " + liveEnvName + "is null"); - return endpoint; - } - - private String getKey(String liveEnvName) { - if (interceptorManager.isPlaybackMode()) { - return DUMMY_KEY; - } - String key = System.getenv(liveEnvName); - assertNotNull(key, "System environment variable " + liveEnvName + "is null"); - return key; - } } diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridTestBase.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridTestBase.java new file mode 100644 index 0000000000000..fa3124472c21d --- /dev/null +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridTestBase.java @@ -0,0 +1,148 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.messaging.eventgrid; + +import com.azure.core.credential.AzureKeyCredential; +import com.azure.core.models.CloudEvent; +import com.azure.core.models.CloudEventDataFormat; +import com.azure.core.serializer.json.jackson.JacksonJsonSerializerBuilder; +import com.azure.core.test.TestProxyTestBase; +import com.azure.core.test.models.TestProxySanitizer; +import com.azure.core.test.models.TestProxySanitizerType; +import com.azure.core.util.BinaryData; +import reactor.test.StepVerifier; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +public class EventGridTestBase extends TestProxyTestBase { + + // Event Grid endpoint for a topic accepting EventGrid schema events + static final String EVENTGRID_ENDPOINT = "AZURE_EVENTGRID_EVENT_ENDPOINT"; + + // Event Grid endpoint for a topic accepting CloudEvents schema events + static final String CLOUD_ENDPOINT = "AZURE_EVENTGRID_CLOUDEVENT_ENDPOINT"; + + // Event Grid endpoint for a topic accepting custom schema events + static final String CUSTOM_ENDPOINT = "AZURE_EVENTGRID_CUSTOM_ENDPOINT"; + + // Event Grid access key for a topic accepting EventGrid schema events + static final String EVENTGRID_KEY = "AZURE_EVENTGRID_EVENT_KEY"; + + // Event Grid access key for a topic accepting CloudEvents schema events + static final String CLOUD_KEY = "AZURE_EVENTGRID_CLOUDEVENT_KEY"; + + // Event Grid access key for a topic accepting custom schema events + static final String CUSTOM_KEY = "AZURE_EVENTGRID_CUSTOM_KEY"; + + // Endpoint, key and channel name for publishing to partner topic + static final String EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT = "EVENTGRID_PARTNER_NAMESPACE_TOPIC_ENDPOINT"; + static final String EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY = "EVENTGRID_PARTNER_NAMESPACE_TOPIC_KEY"; + static final String EVENTGRID_PARTNER_CHANNEL_NAME = "EVENTGRID_PARTNER_CHANNEL_NAME"; + + static final String DUMMY_ENDPOINT = "https://www.dummyEndpoint.com/api/events"; + + static final String DUMMY_KEY = "dummyKey"; + + static final String DUMMY_CHANNEL_NAME = "dummy-channel"; + + @Override + protected void afterTest() { + StepVerifier.resetDefaultTimeout(); + } + + void setupSanitizers() { + if (!interceptorManager.isLiveMode()) { + List sanitizers = new ArrayList<>(); + sanitizers.add(new TestProxySanitizer("aeg-sas-token", null, "REDACTED", TestProxySanitizerType.HEADER)); + sanitizers.add(new TestProxySanitizer("aeg-sas-key", null, "REDACTED", TestProxySanitizerType.HEADER)); + sanitizers.add(new TestProxySanitizer("aeg-channel-name", null, "REDACTED", TestProxySanitizerType.HEADER)); + interceptorManager.addSanitizers(sanitizers); + } + } + + EventGridEvent getEventGridEvent() { + EventGridEvent event = new EventGridEvent("Test", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), + "1.0") + .setEventTime(testResourceNamer.now()) + .setId(testResourceNamer.randomUuid()); + return event; + } + + BinaryData getCustomEvent() { + return BinaryData.fromObject(new HashMap() { + { + put("id", testResourceNamer.randomUuid()); + put("time", testResourceNamer.now().toString()); + put("subject", "Test"); + put("foo", "bar"); + put("type", "Microsoft.MockPublisher.TestEvent"); + } + }); + } + + BinaryData getCustomEventWithSerializer() { + return BinaryData.fromObject(new HashMap() { + { + put("id", testResourceNamer.randomUuid()); + put("time", testResourceNamer.now().toString()); + put("subject", "Test"); + put("foo", "bar"); + put("type", "Microsoft.MockPublisher.TestEvent"); + } + }, new JacksonJsonSerializerBuilder().build()); + } + + CloudEvent getCloudEvent() { + return new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new HashMap() { + { + put("Field1", "Value1"); + put("Field2", "Value2"); + put("Field3", "Value3"); + } + }), CloudEventDataFormat.JSON, "application/json") + .setSubject("Test") + .setTime(testResourceNamer.now()) + .setId(testResourceNamer.randomUuid()); + } + + CloudEvent getCloudEvent(int i) { + return new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent", + BinaryData.fromObject(new EventGridPublisherClientTests.TestData().setName("Hello " + i)), CloudEventDataFormat.JSON, null) + .setSubject("Test " + i) + .setTime(testResourceNamer.now()) + .setId(testResourceNamer.randomUuid()); + } + + + String getEndpoint(String liveEnvName) { + if (interceptorManager.isPlaybackMode()) { + return DUMMY_ENDPOINT; + } + String endpoint = System.getenv(liveEnvName); + assertNotNull(endpoint, "System environment variable " + liveEnvName + " is null"); + return endpoint; + } + + AzureKeyCredential getKey(String liveEnvName) { + if (interceptorManager.isPlaybackMode()) { + return new AzureKeyCredential(DUMMY_KEY); + } + AzureKeyCredential key = new AzureKeyCredential(System.getenv(liveEnvName)); + assertNotNull(key.getKey(), "System environment variable " + liveEnvName + " is null"); + return key; + } + +} diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishCloudEventsToPartnerTopic.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishCloudEventsToPartnerTopic.json deleted file mode 100644 index 752563b375ebf..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishCloudEventsToPartnerTopic.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.centraluseuap-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (14.0.2; Windows 10; 10.0)", - "x-ms-client-request-id" : "ca44182b-dcce-416c-978a-455877cd4dd6", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "6f1699a0-55e9-4633-97fa-37c5781cb3c3", - "Date" : "Mon, 04 Apr 2022 07:01:45 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishEventGridEventToPartnerTopic.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishEventGridEventToPartnerTopic.json deleted file mode 100644 index ff60382e9d659..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/EventGridPublisherClientTests.publishEventGridEventToPartnerTopic.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.centraluseuap-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (14.0.2; Windows 10; 10.0)", - "x-ms-client-request-id" : "efbf8e14-c3d0-4e14-b521-9bfca43c2271", - "Content-Type" : "application/json" - }, - "Response" : { - "content-length" : "827", - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "retry-after" : "0", - "StatusCode" : "400", - "x-ms-request-id" : "d8f0e713-14d1-4fef-96b1-e0c78efe356d", - "Body" : "{\r\n \"error\": {\r\n \"code\": \"BadRequest\",\r\n \"message\": \"This resource is configured to receive event in 'CloudEventV10' schema. The JSON received does not conform to the expected schema. Token Expected: StartObject, Actual Token Received: StartArray. Report 'd8f0e713-14d1-4fef-96b1-e0c78efe356d:8:4/4/2022 7:12:27 AM (UTC)' to our forums for assistance or raise a support ticket.\",\r\n \"details\": [{\r\n \"code\": \"InputJsonInvalid\",\r\n \"message\": \"This resource is configured to receive event in 'CloudEventV10' schema. The JSON received does not conform to the expected schema. Token Expected: StartObject, Actual Token Received: StartArray. Report 'd8f0e713-14d1-4fef-96b1-e0c78efe356d:8:4/4/2022 7:12:27 AM (UTC)' to our forums for assistance or raise a support ticket.\"\r\n }]\r\n }\r\n}", - "Date" : "Mon, 04 Apr 2022 07:12:27 GMT", - "Content-Type" : "application/json; charset=utf-8" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvent.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvent.json deleted file mode 100644 index a9efdc165821e..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvent.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "bb67c974-8040-40c5-8712-5cdd76317a26", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "91537e41-1a96-4b8e-b29d-6fae94dde57a", - "Date" : "Thu, 04 Mar 2021 00:41:03 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventSync.json deleted file mode 100644 index 0dea66821cf07..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "2819c5bc-dbda-4ed9-b4be-36274276dd05", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "7785fded-6605-4856-baa0-4af648365a31", - "Date" : "Thu, 04 Mar 2021 00:41:03 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvents.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvents.json deleted file mode 100644 index 5cac968c57009..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEvents.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "d33fe779-c1eb-426f-aa96-4f052036bdb7", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "ffe28d08-d2b6-45b1-94e3-8a45f8fbed54", - "Date" : "Tue, 11 Aug 2020 17:21:10 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsCustomSerializer.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsCustomSerializer.json deleted file mode 100644 index 810d2e5df84a0..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsCustomSerializer.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "c6669339-b393-463b-ac0f-99ce6b45da46", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "75bd8425-5c77-4499-9af0-0aa835357284", - "Date" : "Tue, 11 Aug 2020 17:19:22 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsImpl.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsImpl.json deleted file mode 100644 index 2a637d570f864..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsImpl.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "28f39f04-5f7c-4e77-b9b5-7a2617797a43", - "Date" : "Tue, 11 Aug 2020 17:44:23 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsSync.json deleted file mode 100644 index 8dd0e697e21ba..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCloudEventsSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "03ef7d02-18af-425a-9daa-d0075f8a4b43", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "baa0bc8e-ed85-4ade-ba43-157d7ff76e75", - "Date" : "Tue, 11 Aug 2020 17:41:21 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvent.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvent.json deleted file mode 100644 index 7d63f99b62bce..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvent.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "683534b9-dfed-4578-9b3e-8a47ded6fe49", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "dbaab228-a4a4-4bb1-9271-f9c3610b0754", - "Date" : "Thu, 04 Mar 2021 00:41:03 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventSync.json deleted file mode 100644 index 1f535757a365f..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "5030108e-5bc2-498b-967b-4442e2c25dd3", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "4d5b4d46-fe5d-4478-9e2d-21f1b1f60883", - "Date" : "Thu, 04 Mar 2021 00:41:02 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvents.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvents.json deleted file mode 100644 index 8f944e6ec944c..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEvents.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "f68db376-0cea-4f04-a27e-f427720546e6", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "Connection" : "close", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "37152ed5-e2ff-4c09-b68a-97988fa484e5", - "Date" : "Tue, 11 Aug 2020 17:19:17 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsImpl.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsImpl.json deleted file mode 100644 index 4e53aefeef2f7..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsImpl.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "b53f39a3-c15a-4182-bfa2-109938bd70ab", - "Date" : "Tue, 11 Aug 2020 17:44:22 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsSync.json deleted file mode 100644 index 411f53d47ca6e..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "efa97ec4-6df5-4ca0-99cc-55ddb6a0e18c", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "d1c7c818-61e5-4b41-b457-bf52d4346a91", - "Date" : "Tue, 11 Aug 2020 17:41:22 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsWithSerializer.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsWithSerializer.json deleted file mode 100644 index 63a6faffe3a3f..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishCustomEventsWithSerializer.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "defbc48f-0a9b-45c7-b589-0b10e0643e6d", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "cbd376af-eb8a-4a06-87e2-fd2d63343e3a", - "Date" : "Fri, 05 Mar 2021 20:56:33 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvent.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvent.json deleted file mode 100644 index 2c91305537bc0..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvent.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "ecbcca83-377d-4454-ba8e-6f727d6f88ed", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "6768ba48-3ed2-45fd-82d3-ad191d15024a", - "Date" : "Thu, 04 Mar 2021 00:41:03 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventSync.json deleted file mode 100644 index 19c3724e8b81c..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "8fdd6235-af20-4f88-a80f-8316cb1e7958", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "f4f7922a-93fa-47bf-8dfa-b3210a9d435d", - "Date" : "Thu, 04 Mar 2021 00:41:02 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvents.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvents.json deleted file mode 100644 index a52bd91b0675b..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEvents.json +++ /dev/null @@ -1,42 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "313923b6-018b-4bd5-b85c-50ab6fbcba5a", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "72bc43d9-cd75-4c02-8aaa-386d286d0094", - "Date" : "Fri, 05 Mar 2021 10:16:09 GMT" - }, - "Exception" : null - }, { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "9e550170-6c36-42fa-9e9a-211c4448e4d5", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "8fd1ea89-c886-4bf6-9c55-abee3a658c4c", - "Date" : "Fri, 05 Mar 2021 10:16:09 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsImpl.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsImpl.json deleted file mode 100644 index 8e907ba09c91c..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsImpl.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "60773b4e-3405-454e-97d9-0b588435f3f6", - "Date" : "Tue, 11 Aug 2020 17:48:05 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsSync.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsSync.json deleted file mode 100644 index 5c85e18996057..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishEventGridEventsSync.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.eastus-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (1.8.0_252; Windows 10; 10.0)", - "x-ms-client-request-id" : "34f4e8c8-8147-472c-afda-ddef9aabb8a7", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "d9ba16b2-cf9d-40a4-ade9-832a054682d0", - "Date" : "Tue, 11 Aug 2020 17:19:21 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithSasToken.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithSasToken.json deleted file mode 100644 index 45c09ca52c566..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithSasToken.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Windows 10; 10.0)", - "x-ms-client-request-id" : "4bfcef57-2a14-4e59-b55f-b7742459d419", - "Content-Type" : "application/json" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "Content-Length" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "14bac486-ca11-48cb-8294-b979f490b1a3", - "Date" : "Thu, 04 Mar 2021 00:41:03 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithTokenCredential.json b/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithTokenCredential.json deleted file mode 100644 index 95823bab3c354..0000000000000 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/resources/session-records/publishWithTokenCredential.json +++ /dev/null @@ -1,23 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.westus2-1.eventgrid-int.azure.net/api/events?api-version=2018-01-01", - "Headers" : { - "User-Agent" : "azsdk-java-UnknownName/UnknownVersion (15.0.2; Windows 10; 10.0)", - "x-ms-client-request-id" : "d9d08daa-f908-4338-ab35-ad6042dc5b9c", - "Content-Type" : "application/cloudevents-batch+json; charset=utf-8" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=31536000; includeSubDomains", - "Server" : "Microsoft-HTTPAPI/2.0", - "api-supported-versions" : "2018-01-01", - "retry-after" : "0", - "StatusCode" : "200", - "x-ms-request-id" : "83028bf1-abe4-49ee-a244-ad5e66335d25", - "Date" : "Fri, 25 Jun 2021 21:19:01 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file From 9b5a6eb1ebd2557b3117e274e29fc1d33697e1bb Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Thu, 14 Sep 2023 12:21:36 -0700 Subject: [PATCH 27/44] address pr comments --- sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md b/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md index 3fa301183576a..5d1a97af88ed2 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md +++ b/sdk/eventgrid/azure-messaging-eventgrid/CHANGELOG.md @@ -18,7 +18,7 @@ ### Other Changes #### Dependency Updates -- + - Upgraded `azure-core` from `1.42.0` to version `1.43.0`. - Upgraded `azure-core-http-netty` from `1.13.6` to version `1.13.7`. @@ -86,7 +86,7 @@ ### Other Changes -~~#### Dependency Updates +#### Dependency Updates - Upgraded `azure-core-http-netty` from `1.13.0` to version `1.13.1`. - Upgraded `azure-core` from `1.36.0` to version `1.37.0`. @@ -115,7 +115,7 @@ - `Microsoft.ApiManagement.GatewayDeleted` - `Microsoft.ApiManagement.GatewayHostnameConfigurationCreated` - `Microsoft.ApiManagement.GatewayHostnameConfigurationDeleted` - - `Microsoft.ApiManagement.GatewayHostnameConfigurationUpdated`~~ + - `Microsoft.ApiManagement.GatewayHostnameConfigurationUpdated` - `Microsoft.ApiManagement.GatewayUpdated` - `Microsoft.DataBox.CopyCompleted` - `Microsoft.DataBox.CopyStarted` From e01a75375d52aa3f4f736dcca7b3e12a184574e3 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Fri, 15 Sep 2023 13:00:40 -0700 Subject: [PATCH 28/44] add exports --- sdk/eventgrid/azure-messaging-eventgrid/pom.xml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml index de96f6c58284f..9da17d5dd0b21 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml +++ b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml @@ -56,6 +56,8 @@ --add-opens com.azure.messaging.eventgrid/com.azure.messaging.eventgrid.implementation=ALL-UNNAMED --add-reads com.azure.messaging.eventgrid=com.azure.core.serializer.json.jackson + --add-exports com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED + --add-exports com.azure.core/com.azure.core.implementation=ALL-UNNAMED @@ -118,7 +120,7 @@ com.azure azure-core-test - 1.19.0-beta.1 + 1.21.0-beta.1 test From 7ec717e7b5f4ec56f113090db1a6c5253b634d09 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Fri, 15 Sep 2023 13:06:57 -0700 Subject: [PATCH 29/44] fix version --- sdk/eventgrid/azure-messaging-eventgrid/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml index 9da17d5dd0b21..fe1394700585f 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml +++ b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml @@ -120,7 +120,7 @@ com.azure azure-core-test - 1.21.0-beta.1 + 1.20.0 test From 67c932aca80986c5fa9635f3b7af410a70b9ed71 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Tue, 19 Sep 2023 13:26:48 -0700 Subject: [PATCH 30/44] use unreleased test version --- eng/versioning/version_client.txt | 2 +- .../test/http/TestProxyPlaybackClient.java | 46 ++++++++++++++++--- .../azure-messaging-eventgrid/pom.xml | 2 +- 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/eng/versioning/version_client.txt b/eng/versioning/version_client.txt index e2e0188a3ff1f..8a0a3b3fbae3f 100644 --- a/eng/versioning/version_client.txt +++ b/eng/versioning/version_client.txt @@ -441,7 +441,7 @@ com.azure.tools:azure-sdk-build-tool;1.0.0;1.1.0-beta.1 # note: The unreleased dependencies will not be manipulated with the automatic PR creation code. # In the pom, the version update tag after the version should name the unreleased package and the dependency version: # -unreleased_com.azure:azure-core;1.44.0 +unreleased_com.azure:azure-core-test;dependency-1.21.0-beta.1 # Released Beta dependencies: Copy the entry from above, prepend "beta_", remove the current # version and set the version to the released beta. Released beta dependencies are only valid diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java index 33176ae08d45e..22778b73540ff 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java @@ -14,6 +14,7 @@ import com.azure.core.test.utils.HttpURLConnectionHttpClient; import com.azure.core.test.utils.TestProxyUtils; import com.azure.core.util.Context; +import com.azure.core.util.logging.ClientLogger; import com.azure.core.util.serializer.JacksonAdapter; import com.azure.core.util.serializer.SerializerAdapter; import com.azure.core.util.serializer.SerializerEncoding; @@ -32,6 +33,7 @@ import java.util.List; import java.util.Map; import java.util.Queue; +import java.util.concurrent.TimeUnit; import static com.azure.core.test.implementation.TestingHelpers.X_RECORDING_FILE_LOCATION; import static com.azure.core.test.implementation.TestingHelpers.X_RECORDING_ID; @@ -46,6 +48,7 @@ */ public class TestProxyPlaybackClient implements HttpClient { + private static final ClientLogger LOGGER = new ClientLogger(TestProxyPlaybackClient.class); private final HttpClient client; private final URL proxyUrl; private String xRecordingId; @@ -93,7 +96,7 @@ public Queue startPlayback(File recordFile, Path testClassPath) { throw new RuntimeException(e); } - try (HttpResponse response = client.sendSync(request, Context.NONE)) { + try (HttpResponse response = sendRequestWithRetries(request)) { checkForTestProxyErrors(response); xRecordingId = response.getHeaderValue(X_RECORDING_ID); xRecordingFileLocation = new String(Base64.getUrlDecoder().decode( @@ -127,13 +130,41 @@ public Queue startPlayback(File recordFile, Path testClassPath) { } } + private HttpResponse sendRequestWithRetries(HttpRequest request) { + return sendRequestWithRetries(request, Context.NONE); + } + + private HttpResponse sendRequestWithRetries(HttpRequest request, Context context) { + int retries = 0; + while (true) { + try { + HttpResponse response = client.sendSync(request, Context.NONE); + if (response.getStatusCode() / 100 != 2) { + throw new RuntimeException("Test proxy returned a non-successful status code. " + response.getStatusCode()); + } + return response; + } catch (Exception e) { + retries++; + if(retries >= 3) { + throw e; + } + try { + TimeUnit.SECONDS.sleep(1); + } catch (InterruptedException ex) { + throw new RuntimeException(ex); + } + LOGGER.warning("Retrying request to test proxy. Retry attempt: " + retries); + } + } + } + /** * Stops playback of a test recording. */ public void stopPlayback() { HttpRequest request = new HttpRequest(HttpMethod.POST, proxyUrl + "/playback/stop") .setHeader(X_RECORDING_ID, xRecordingId); - client.sendSync(request, Context.NONE).close(); + sendRequestWithRetries(request); } /** @@ -189,10 +220,11 @@ public HttpResponse sendSync(HttpRequest request, Context context) { */ public void addProxySanitization(List sanitizers) { if (isPlayingBack()) { - HttpRequest request = createAddSanitizersRequest(sanitizers, proxyUrl) - .setHeader(X_RECORDING_ID, xRecordingId); - - client.sendSync(request, Context.NONE).close(); + getSanitizerRequests(sanitizers, proxyUrl) + .forEach(request -> { + request.setHeader(X_RECORDING_ID, xRecordingId); + sendRequestWithRetries(request); + }); } else { this.sanitizers.addAll(sanitizers); } @@ -210,7 +242,7 @@ public void addMatcherRequests(List matchers) { } matcherRequests.forEach(request -> { request.setHeader(X_RECORDING_ID, xRecordingId); - client.sendSync(request, Context.NONE).close(); + sendRequestWithRetries(request); }); } else { this.matchers.addAll(matchers); diff --git a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml index fe1394700585f..67b77b5f6efe8 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml +++ b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml @@ -120,7 +120,7 @@ com.azure azure-core-test - 1.20.0 + 1.21.0-beta.1 test From 4cffdeeaf15caeaaec325e5f42d7364391246c1f Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Tue, 19 Sep 2023 13:45:10 -0700 Subject: [PATCH 31/44] fix tag --- sdk/eventgrid/azure-messaging-eventgrid/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml index 67b77b5f6efe8..61c27a7a7df8b 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/pom.xml +++ b/sdk/eventgrid/azure-messaging-eventgrid/pom.xml @@ -120,7 +120,7 @@ com.azure azure-core-test - 1.21.0-beta.1 + 1.21.0-beta.1 test From 66939e840cb6f7f2eb4159dc51ff9538091e1817 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Tue, 19 Sep 2023 15:33:09 -0700 Subject: [PATCH 32/44] fix tag --- eng/versioning/version_client.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eng/versioning/version_client.txt b/eng/versioning/version_client.txt index 8a0a3b3fbae3f..3367097e25e31 100644 --- a/eng/versioning/version_client.txt +++ b/eng/versioning/version_client.txt @@ -441,7 +441,7 @@ com.azure.tools:azure-sdk-build-tool;1.0.0;1.1.0-beta.1 # note: The unreleased dependencies will not be manipulated with the automatic PR creation code. # In the pom, the version update tag after the version should name the unreleased package and the dependency version: # -unreleased_com.azure:azure-core-test;dependency-1.21.0-beta.1 +unreleased_com.azure:azure-core-test;1.21.0-beta.1 # Released Beta dependencies: Copy the entry from above, prepend "beta_", remove the current # version and set the version to the released beta. Released beta dependencies are only valid From a7ca1f77389e7a5544591e01b9639ef971ceb66d Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Wed, 20 Sep 2023 11:13:09 -0700 Subject: [PATCH 33/44] refactor --- .../test/http/TestProxyPlaybackClient.java | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java index 22778b73540ff..3e9031883dfa6 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java @@ -92,11 +92,7 @@ public Queue startPlayback(File recordFile, Path testClassPath) { SerializerEncoding.JSON)) .setHeader(HttpHeaderName.ACCEPT, "application/json") .setHeader(HttpHeaderName.CONTENT_TYPE, "application/json"); - } catch (IOException e) { - throw new RuntimeException(e); - } - - try (HttpResponse response = sendRequestWithRetries(request)) { + HttpResponse response = sendRequestWithRetries(request); checkForTestProxyErrors(response); xRecordingId = response.getHeaderValue(X_RECORDING_ID); xRecordingFileLocation = new String(Base64.getUrlDecoder().decode( @@ -131,10 +127,6 @@ public Queue startPlayback(File recordFile, Path testClassPath) { } private HttpResponse sendRequestWithRetries(HttpRequest request) { - return sendRequestWithRetries(request, Context.NONE); - } - - private HttpResponse sendRequestWithRetries(HttpRequest request, Context context) { int retries = 0; while (true) { try { @@ -145,19 +137,23 @@ private HttpResponse sendRequestWithRetries(HttpRequest request, Context context return response; } catch (Exception e) { retries++; - if(retries >= 3) { + if (retries >= 3) { throw e; } - try { - TimeUnit.SECONDS.sleep(1); - } catch (InterruptedException ex) { - throw new RuntimeException(ex); - } + sleep(1); LOGGER.warning("Retrying request to test proxy. Retry attempt: " + retries); } } } + private void sleep(int durationInSeconds) { + try { + TimeUnit.SECONDS.sleep(durationInSeconds); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + /** * Stops playback of a test recording. */ From 799c12a24d2e902e9f821d4e060519f2aff08629 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Wed, 20 Sep 2023 11:49:22 -0700 Subject: [PATCH 34/44] fix spring test after sync stack migration --- eng/versioning/version_client.txt | 1 + sdk/spring/spring-cloud-azure-autoconfigure/pom.xml | 2 +- .../eventgrid/AzureEventGridAutoConfigurationTests.java | 3 +-- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/eng/versioning/version_client.txt b/eng/versioning/version_client.txt index 3367097e25e31..1b8c03886c96c 100644 --- a/eng/versioning/version_client.txt +++ b/eng/versioning/version_client.txt @@ -442,6 +442,7 @@ com.azure.tools:azure-sdk-build-tool;1.0.0;1.1.0-beta.1 # In the pom, the version update tag after the version should name the unreleased package and the dependency version: # unreleased_com.azure:azure-core-test;1.21.0-beta.1 +unreleased_com.azure:azure-messaging-eventgrid;4.19.0-beta.1 # Released Beta dependencies: Copy the entry from above, prepend "beta_", remove the current # version and set the version to the released beta. Released beta dependencies are only valid diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/pom.xml b/sdk/spring/spring-cloud-azure-autoconfigure/pom.xml index 26234b0e0558a..153aca9a9115e 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/pom.xml +++ b/sdk/spring/spring-cloud-azure-autoconfigure/pom.xml @@ -177,7 +177,7 @@ com.azure azure-messaging-eventgrid - 4.18.0 + 4.19.0-beta.1 true diff --git a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/implementation/eventgrid/AzureEventGridAutoConfigurationTests.java b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/implementation/eventgrid/AzureEventGridAutoConfigurationTests.java index 3b79fb194c714..bd1644395b61d 100644 --- a/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/implementation/eventgrid/AzureEventGridAutoConfigurationTests.java +++ b/sdk/spring/spring-cloud-azure-autoconfigure/src/test/java/com/azure/spring/cloud/autoconfigure/implementation/eventgrid/AzureEventGridAutoConfigurationTests.java @@ -152,8 +152,7 @@ void clientCanOverride() { assertThat(context).hasBean("myCustomClient"); Object eventClassOfAsyncClient = ReflectionUtils.getField(EventGridPublisherAsyncClient.class, "eventClass", context.getBean(EventGridPublisherAsyncClient.class)); - Object eventGridPublisherAsyncClient = ReflectionUtils.getField(EventGridPublisherClient.class, "asyncClient", context.getBean(EventGridPublisherClient.class)); - Object eventClassOfSyncClient = ReflectionUtils.getField(EventGridPublisherAsyncClient.class, "eventClass", eventGridPublisherAsyncClient); + Object eventClassOfSyncClient = ReflectionUtils.getField(EventGridPublisherClient.class, "eventClass", context.getBean(EventGridPublisherClient.class)); assertEquals(EventGridEvent.class, eventClassOfAsyncClient); assertEquals(BinaryData.class, eventClassOfSyncClient); }); From 507db272032f28d687ca94cc3340185e49303a5b Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Wed, 20 Sep 2023 14:22:46 -0700 Subject: [PATCH 35/44] log the test proxy response on error --- .../java/com/azure/core/test/http/TestProxyPlaybackClient.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java index 3e9031883dfa6..aa7c1e991f3a1 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/http/TestProxyPlaybackClient.java @@ -132,7 +132,8 @@ private HttpResponse sendRequestWithRetries(HttpRequest request) { try { HttpResponse response = client.sendSync(request, Context.NONE); if (response.getStatusCode() / 100 != 2) { - throw new RuntimeException("Test proxy returned a non-successful status code. " + response.getStatusCode()); + throw new RuntimeException("Test proxy returned a non-successful status code. " + + response.getStatusCode() + "; response: " + response.getBodyAsString().block()); } return response; } catch (Exception e) { From baaf27dbfddce10299b66852942fa260059fa584 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Fri, 22 Sep 2023 14:39:44 -0700 Subject: [PATCH 36/44] fix header sanitizer in Service Bus --- .../ServiceBusAdministrationAsyncClientIntegrationTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/administration/ServiceBusAdministrationAsyncClientIntegrationTest.java b/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/administration/ServiceBusAdministrationAsyncClientIntegrationTest.java index f58899b1faf3c..0c213d3ac87f0 100644 --- a/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/administration/ServiceBusAdministrationAsyncClientIntegrationTest.java +++ b/sdk/servicebus/azure-messaging-servicebus/src/test/java/com/azure/messaging/servicebus/administration/ServiceBusAdministrationAsyncClientIntegrationTest.java @@ -92,7 +92,7 @@ class ServiceBusAdministrationAsyncClientIntegrationTest extends TestProxyTestBa static final List TEST_PROXY_REQUEST_MATCHERS; static { - AUTHORIZATION_HEADER = new TestProxySanitizer("SupplementaryAuthorization", "SharedAccessSignature sr=https%3A%2F%2Ffoo.servicebus.windows.net&sig=dummyValue%3D&se=1687267490&skn=dummyKey", TestProxySanitizerType.HEADER); + AUTHORIZATION_HEADER = new TestProxySanitizer("SupplementaryAuthorization", null, "SharedAccessSignature sr=https%3A%2F%2Ffoo.servicebus.windows.net&sig=dummyValue%3D&se=1687267490&skn=dummyKey", TestProxySanitizerType.HEADER); TEST_PROXY_SANITIZERS = Collections.singletonList(AUTHORIZATION_HEADER); final List skippedHeaders = Arrays.asList("ServiceBusDlqSupplementaryAuthorization", "ServiceBusSupplementaryAuthorization"); From 5ad903852a7df351842a79ec5bb8b10bae13658d Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Sat, 23 Sep 2023 19:22:35 -0700 Subject: [PATCH 37/44] fix regex for quantum job tests --- sdk/quantum/azure-quantum-jobs/pom.xml | 4 ++++ .../java/com/azure/quantum/jobs/QuantumClientTestBase.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/sdk/quantum/azure-quantum-jobs/pom.xml b/sdk/quantum/azure-quantum-jobs/pom.xml index aed6cd635105e..dd5033bc4d67a 100644 --- a/sdk/quantum/azure-quantum-jobs/pom.xml +++ b/sdk/quantum/azure-quantum-jobs/pom.xml @@ -39,6 +39,10 @@ false + + --add-exports com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED + --add-opens com.azure.core/com.azure.core.implementation.util=ALL-UNNAMED + diff --git a/sdk/quantum/azure-quantum-jobs/src/test/java/com/azure/quantum/jobs/QuantumClientTestBase.java b/sdk/quantum/azure-quantum-jobs/src/test/java/com/azure/quantum/jobs/QuantumClientTestBase.java index 86c68702db32c..2299df6ca4a22 100644 --- a/sdk/quantum/azure-quantum-jobs/src/test/java/com/azure/quantum/jobs/QuantumClientTestBase.java +++ b/sdk/quantum/azure-quantum-jobs/src/test/java/com/azure/quantum/jobs/QuantumClientTestBase.java @@ -62,7 +62,7 @@ QuantumClientBuilder getClientBuilder(HttpClient httpClient) { customSanitizers.add(new TestProxySanitizer(getResourceGroup(), RESOURCE_GROUP, TestProxySanitizerType.URL)); customSanitizers.add(new TestProxySanitizer(getWorkspaceName(), WORKSPACE, TestProxySanitizerType.URL)); customSanitizers.add(new TestProxySanitizer(getLocation(), LOCATION, TestProxySanitizerType.URL)); - customSanitizers.add(new TestProxySanitizer("(?:\\?(sv|sig|se|srt|ss|sp)=)(?.*)", SANITIZED, TestProxySanitizerType.BODY_REGEX).setGroupForReplace("secret")); + customSanitizers.add(new TestProxySanitizer("(?:\\\\?(sv|sig|se|srt|ss|sp)=)(?.*)", SANITIZED, TestProxySanitizerType.BODY_REGEX).setGroupForReplace("secret")); customSanitizers.add(new TestProxySanitizer("$..sasUri", null, SANITIZED, TestProxySanitizerType.BODY_KEY)); customSanitizers.add(new TestProxySanitizer("$..containerUri", null, SANITIZED, TestProxySanitizerType.BODY_KEY)); customSanitizers.add(new TestProxySanitizer("$..inputDataUri", null, SANITIZED, TestProxySanitizerType.BODY_KEY)); From b29abdd007c0ce1b5bb34f32360f067b81eb3fc9 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Mon, 25 Sep 2023 16:50:21 -0700 Subject: [PATCH 38/44] Put a semaphore around starting the proxy. --- .../java/com/azure/core/test/utils/TestProxyManager.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java index f073570cfaf37..19e407fad16b7 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java @@ -20,6 +20,7 @@ import java.nio.file.Paths; import java.time.Duration; import java.util.Map; +import java.util.concurrent.Semaphore; /** * Manages running the test recording proxy server @@ -28,6 +29,7 @@ public class TestProxyManager { private static final ClientLogger LOGGER = new ClientLogger(TestProxyManager.class); private Process proxy; private final Path testClassPath; + private static final Semaphore semaphore = new Semaphore(1); /** * Construct a {@link TestProxyManager} for controlling the external test proxy. @@ -49,6 +51,7 @@ public TestProxyManager(Path testClassPath) { */ public void startProxy() { try { + semaphore.acquire(); // if we're not running in CI we will check to see if someone has started the proxy, and start one if not. if (runningLocally() && !checkAlive(1, Duration.ofSeconds(1), null)) { String commandLine = Paths.get(TestProxyDownloader.getProxyDirectory().toString(), @@ -98,6 +101,8 @@ public void startProxy() { throw LOGGER.logExceptionAsError(new UncheckedIOException(e)); } catch (InterruptedException e) { throw new RuntimeException(e); + } finally { + semaphore.release(); } } From 531a43c536d372c6c5e0d558857ff67eb0ef0309 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Wed, 27 Sep 2023 12:25:30 -0700 Subject: [PATCH 39/44] make the methods of `TestProxyManager` static and synchronized. --- .../azure/core/test/TestProxyTestBase.java | 22 +----------- .../core/test/utils/TestProxyManager.java | 34 ++++++++----------- 2 files changed, 15 insertions(+), 41 deletions(-) diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/TestProxyTestBase.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/TestProxyTestBase.java index d765d1fa74531..643e8541b2adf 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/TestProxyTestBase.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/TestProxyTestBase.java @@ -5,15 +5,9 @@ import com.azure.core.test.utils.TestProxyManager; import com.azure.core.util.logging.ClientLogger; -import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.TestInfo; -import java.nio.file.Path; -import java.nio.file.Paths; - -import static com.azure.core.test.utils.TestUtils.toURI; - /** * Base class for running live and playback tests using test-proxy */ @@ -30,8 +24,6 @@ public TestProxyTestBase() { super(); } - private static TestProxyManager testProxyManager; - /** * Before tests are executed, determines the test mode by reading the {@code AZURE_TEST_MODE} environment variable. * If it is not set, {@link TestMode#PLAYBACK} @@ -40,20 +32,8 @@ public TestProxyTestBase() { @BeforeAll public static void setupTestProxy(TestInfo testInfo) { testMode = initializeTestMode(); - Path testClassPath = Paths.get(toURI(testInfo.getTestClass().get().getResource(testInfo.getTestClass().get().getSimpleName() + ".class"))); if (isTestProxyEnabled() && (testMode == TestMode.PLAYBACK || testMode == TestMode.RECORD)) { - testProxyManager = new TestProxyManager(testClassPath); - testProxyManager.startProxy(); - } - } - - /** - * Performs cleanup actions after all tests are executed. - */ - @AfterAll - public static void teardownTestProxy() { - if (testProxyManager != null) { - testProxyManager.stopProxy(); + TestProxyManager.startProxy(); } } } diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java index 19e407fad16b7..fc40f29119a18 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java @@ -20,38 +20,34 @@ import java.nio.file.Paths; import java.time.Duration; import java.util.Map; -import java.util.concurrent.Semaphore; + +import static com.azure.core.test.utils.TestUtils.toURI; /** * Manages running the test recording proxy server */ -public class TestProxyManager { +public final class TestProxyManager { private static final ClientLogger LOGGER = new ClientLogger(TestProxyManager.class); - private Process proxy; - private final Path testClassPath; - private static final Semaphore semaphore = new Semaphore(1); + private static Process proxy; + private static final Path TEST_CLASS_PATH = Paths.get(toURI(TestProxyManager.class.getResource(TestProxyManager.class.getSimpleName() + ".class"))); - /** - * Construct a {@link TestProxyManager} for controlling the external test proxy. - * @param testClassPath the test class path - */ - public TestProxyManager(Path testClassPath) { - this.testClassPath = testClassPath; - // This is necessary to stop the proxy when the debugger is stopped. - Runtime.getRuntime().addShutdownHook(new Thread(this::stopProxy)); + static { + Runtime.getRuntime().addShutdownHook(new Thread(TestProxyManager::stopProxy)); if (runningLocally()) { - TestProxyDownloader.installTestProxy(testClassPath); + TestProxyDownloader.installTestProxy(TEST_CLASS_PATH); } } + @Deprecated + private TestProxyManager() { } + /** * Start an instance of the test proxy. * @throws UncheckedIOException There was an issue communicating with the proxy. * @throws RuntimeException There was an issue starting the proxy process. */ - public void startProxy() { + public static synchronized void startProxy() { try { - semaphore.acquire(); // if we're not running in CI we will check to see if someone has started the proxy, and start one if not. if (runningLocally() && !checkAlive(1, Duration.ofSeconds(1), null)) { String commandLine = Paths.get(TestProxyDownloader.getProxyDirectory().toString(), @@ -101,8 +97,6 @@ public void startProxy() { throw LOGGER.logExceptionAsError(new UncheckedIOException(e)); } catch (InterruptedException e) { throw new RuntimeException(e); - } finally { - semaphore.release(); } } @@ -133,7 +127,7 @@ private static boolean checkAlive(int loops, Duration waitTime, Process proxy) t /** * Stop the running instance of the test proxy. */ - public void stopProxy() { + private static void stopProxy() { if (proxy != null && proxy.isAlive()) { proxy.destroy(); } @@ -143,7 +137,7 @@ public void stopProxy() { * Checks the environment variables commonly set in CI to determine if the run is local. * @return True if the run is local. */ - private boolean runningLocally() { + private static boolean runningLocally() { return Configuration.getGlobalConfiguration().get("TF_BUILD") == null && Configuration.getGlobalConfiguration().get("CI") == null; } From 189b6a3ab1e79c951e2482dc628e1fc867b21155 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Thu, 28 Sep 2023 22:29:16 -0700 Subject: [PATCH 40/44] Use the working directory to search for the root of the repo from. --- .../java/com/azure/core/test/utils/TestProxyManager.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java index fc40f29119a18..3003cd39b9699 100644 --- a/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java +++ b/sdk/core/azure-core-test/src/main/java/com/azure/core/test/utils/TestProxyManager.java @@ -21,20 +21,18 @@ import java.time.Duration; import java.util.Map; -import static com.azure.core.test.utils.TestUtils.toURI; - /** * Manages running the test recording proxy server */ public final class TestProxyManager { private static final ClientLogger LOGGER = new ClientLogger(TestProxyManager.class); private static Process proxy; - private static final Path TEST_CLASS_PATH = Paths.get(toURI(TestProxyManager.class.getResource(TestProxyManager.class.getSimpleName() + ".class"))); + private static final Path WORKING_DIRECTORY = Paths.get(System.getProperty("user.dir")); static { Runtime.getRuntime().addShutdownHook(new Thread(TestProxyManager::stopProxy)); if (runningLocally()) { - TestProxyDownloader.installTestProxy(TEST_CLASS_PATH); + TestProxyDownloader.installTestProxy(WORKING_DIRECTORY); } } @@ -53,7 +51,7 @@ public static synchronized void startProxy() { String commandLine = Paths.get(TestProxyDownloader.getProxyDirectory().toString(), TestProxyUtils.getProxyProcessName()).toString(); - Path repoRoot = TestUtils.getRepoRootResolveUntil(testClassPath, "eng"); + Path repoRoot = TestUtils.getRepoRootResolveUntil(WORKING_DIRECTORY, "eng"); // Resolve the path to the repo root 'target' folder and create the folder if it doesn't exist. // This folder will be used to store the 'test-proxy.log' file to enable simpler debugging of Test Proxy From 7d2ed330af66a7c58acb1bc3fdb40015b6fe313a Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Fri, 6 Oct 2023 12:55:08 -0700 Subject: [PATCH 41/44] add missing sanitizer setup --- .../eventgrid/EventGridPublisherImplTests.java | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java index d82bc1295549d..04d4475dabe1b 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java @@ -8,9 +8,6 @@ import com.azure.core.http.policy.AddHeadersPolicy; import com.azure.core.http.policy.RetryPolicy; import com.azure.core.models.CloudEvent; -import com.azure.core.models.CloudEventDataFormat; -import com.azure.core.test.TestBase; -import com.azure.core.util.BinaryData; import com.azure.messaging.eventgrid.implementation.EventGridPublisherClientImpl; import com.azure.messaging.eventgrid.implementation.EventGridPublisherClientImplBuilder; import com.azure.messaging.eventgrid.implementation.models.EventGridEvent; @@ -21,12 +18,6 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; -import java.time.OffsetDateTime; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; public class EventGridPublisherImplTests extends EventGridTestBase { @@ -47,7 +38,8 @@ protected void beforeTest() { } else if (interceptorManager.isRecordMode()) { pipelineBuilder.policies(interceptorManager.getRecordPolicy(), new RetryPolicy()); } - + setupSanitizers(); + } @Override protected void afterTest() { StepVerifier.resetDefaultTimeout(); From 3b8518ea78d11f46c4f2cd5153fbbac446734892 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Fri, 6 Oct 2023 13:13:22 -0700 Subject: [PATCH 42/44] base class has this so it's not needed here --- .../messaging/eventgrid/EventGridPublisherImplTests.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java index 04d4475dabe1b..9ec8a624f4a6a 100644 --- a/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java +++ b/sdk/eventgrid/azure-messaging-eventgrid/src/test/java/com/azure/messaging/eventgrid/EventGridPublisherImplTests.java @@ -40,10 +40,6 @@ protected void beforeTest() { } setupSanitizers(); } - @Override - protected void afterTest() { - StepVerifier.resetDefaultTimeout(); - } @Test public void publishEventGridEventsImpl() { From ceb9d5744e43b2ac35a4ba7b1aacb03c18869702 Mon Sep 17 00:00:00 2001 From: Bill Wert Date: Fri, 6 Oct 2023 13:13:33 -0700 Subject: [PATCH 43/44] removed this in merge accidentally --- eng/versioning/version_client.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/eng/versioning/version_client.txt b/eng/versioning/version_client.txt index 1b8c03886c96c..e3518405fa1bf 100644 --- a/eng/versioning/version_client.txt +++ b/eng/versioning/version_client.txt @@ -441,6 +441,7 @@ com.azure.tools:azure-sdk-build-tool;1.0.0;1.1.0-beta.1 # note: The unreleased dependencies will not be manipulated with the automatic PR creation code. # In the pom, the version update tag after the version should name the unreleased package and the dependency version: # +unreleased_com.azure:azure-core;1.44.0 unreleased_com.azure:azure-core-test;1.21.0-beta.1 unreleased_com.azure:azure-messaging-eventgrid;4.19.0-beta.1 From dca51256eec11dd40eec423ff001f9a64a77f098 Mon Sep 17 00:00:00 2001 From: Srikanta Nagaraja Date: Mon, 9 Oct 2023 23:55:04 -0700 Subject: [PATCH 44/44] pin azure-core version --- sdk/spring/spring-messaging-azure-servicebus/pom.xml | 5 +++++ sdk/spring/spring-messaging-azure-storage-queue/pom.xml | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/sdk/spring/spring-messaging-azure-servicebus/pom.xml b/sdk/spring/spring-messaging-azure-servicebus/pom.xml index 0a1e58c048d65..b1aed4a681118 100644 --- a/sdk/spring/spring-messaging-azure-servicebus/pom.xml +++ b/sdk/spring/spring-messaging-azure-servicebus/pom.xml @@ -52,6 +52,11 @@ azure-messaging-servicebus 7.14.4 + + com.azure + azure-core + 1.44.0 + org.springframework spring-tx diff --git a/sdk/spring/spring-messaging-azure-storage-queue/pom.xml b/sdk/spring/spring-messaging-azure-storage-queue/pom.xml index d360bcb8246aa..e38a90568ea84 100644 --- a/sdk/spring/spring-messaging-azure-storage-queue/pom.xml +++ b/sdk/spring/spring-messaging-azure-storage-queue/pom.xml @@ -51,6 +51,11 @@ azure-storage-queue 12.19.0 + + com.azure + azure-core + 1.44.0 + org.mockito