Skip to content

Commit

Permalink
add recorded tests for egv2
Browse files Browse the repository at this point in the history
  • Loading branch information
billwert committed Nov 17, 2023
1 parent 1cbdb4d commit 2727f34
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
2 changes: 1 addition & 1 deletion sdk/eventgrid/azure-messaging-eventgrid/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "java",
"TagPrefix": "java/eventgrid/azure-messaging-eventgrid",
"Tag": "java/eventgrid/azure-messaging-eventgrid_7bb36a1579"
"Tag": "java/eventgrid/azure-messaging-eventgrid_49976f9afc"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.azure.core.http.HttpClient;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.models.CloudEvent;
import com.azure.core.models.CloudEventDataFormat;
import com.azure.core.util.BinaryData;
Expand All @@ -20,26 +21,46 @@
import com.azure.messaging.eventgrid.models.RenewCloudEventLocksResult;
import com.azure.messaging.eventgrid.models.RenewLockOptions;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import reactor.test.StepVerifier;

import java.time.Duration;
import java.util.Arrays;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;


@Disabled("recording for new feature not done yet")
class EventGridClientTest extends EventGridTestBase {



public static final String TOPIC_NAME = Configuration.getGlobalConfiguration().get(EVENTGRID_TOPIC_NAME);
public static final String EVENT_SUBSCRIPTION_NAME = Configuration.getGlobalConfiguration().get(EVENTGRID_EVENT_SUBSCRIPTION_NAME);

private EventGridClientBuilder asyncBuilder;
private EventGridClientBuilder syncBuilder;

@Override
protected void beforeTest() {
asyncBuilder = buildClientBuilder();
syncBuilder = buildClientBuilder();

if (interceptorManager.isPlaybackMode()) {
asyncBuilder.httpClient(buildAssertingClient(interceptorManager.getPlaybackClient(), false));
syncBuilder.httpClient(buildAssertingClient(interceptorManager.getPlaybackClient(), true));
} else { // both record and live will use these clients
asyncBuilder.httpClient(buildAssertingClient(HttpClient.createDefault(), false));
syncBuilder.httpClient(buildAssertingClient(HttpClient.createDefault(), true));
}

if (interceptorManager.isRecordMode()) {
asyncBuilder.addPolicy(interceptorManager.getRecordPolicy())
.retryPolicy(new RetryPolicy());
syncBuilder.addPolicy(interceptorManager.getRecordPolicy())
.retryPolicy(new RetryPolicy());
}

setupSanitizers();
}

EventGridClientBuilder buildClientBuilder() {
return new EventGridClientBuilder()
Expand All @@ -50,32 +71,26 @@ EventGridClientBuilder buildClientBuilder() {
.credential(getKey(EVENTGRID_KEY));
}

EventGridClient buildClient() {
return buildClientBuilder().buildClient();
EventGridClient buildSyncClient() {
return syncBuilder.buildClient();
}

EventGridAsyncClient buildAsyncClient() {
return buildClientBuilder().buildAsyncClient();
return asyncBuilder.buildAsyncClient();
}


@Test
void publishCloudEventSync() {
EventGridClient client = buildClient();
EventGridClient client = buildSyncClient();
client.publishCloudEvent(TOPIC_NAME, getCloudEvent());
}

@Test
void publishCloudEventBinaryModeSync() {
EventGridClient client = buildClient();
EventGridClient client = buildSyncClient();
CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent",
BinaryData.fromObject(new HashMap<String, String>() {
{
put("Field1", "Value1");
put("Field2", "Value2");
put("Field3", "Value3");
}
}), CloudEventDataFormat.JSON, "text/plain")
BinaryData.fromString("MyCoolString"), CloudEventDataFormat.BYTES, "text/plain")
.setSubject("Test")
.setTime(testResourceNamer.now())
.setId(testResourceNamer.randomUuid());
Expand All @@ -84,13 +99,13 @@ void publishCloudEventBinaryModeSync() {

@Test
void publishBatchOfCloudEventsSync() {
EventGridClient client = buildClient();
EventGridClient client = buildSyncClient();
client.publishCloudEvents(TOPIC_NAME, Arrays.asList(getCloudEvent(), getCloudEvent()));
}

@Test
void receiveBatchOfCloudEventsSync() {
EventGridClient client = buildClient();
EventGridClient client = buildSyncClient();
client.publishCloudEvent(TOPIC_NAME, getCloudEvent());
ReceiveResult result = client.receiveCloudEvents(TOPIC_NAME, EVENT_SUBSCRIPTION_NAME, 10, Duration.ofSeconds(10));
assertNotNull(result);
Expand All @@ -99,7 +114,7 @@ void receiveBatchOfCloudEventsSync() {

@Test
void acknowledgeBatchOfCloudEventsSync() {
EventGridClient client = buildClient();
EventGridClient client = buildSyncClient();
client.publishCloudEvent(TOPIC_NAME, getCloudEvent());
ReceiveResult receiveResult = client.receiveCloudEvents(TOPIC_NAME, EVENT_SUBSCRIPTION_NAME, 1, Duration.ofSeconds(10));
AcknowledgeOptions acknowledgeOptions = new AcknowledgeOptions(Arrays.asList(receiveResult.getValue().get(0).getBrokerProperties().getLockToken()));
Expand All @@ -110,7 +125,7 @@ void acknowledgeBatchOfCloudEventsSync() {

@Test
void releaseBatchOfCloudEventsSync() {
EventGridClient client = buildClient();
EventGridClient client = buildSyncClient();
client.publishCloudEvent(TOPIC_NAME, getCloudEvent());
ReceiveResult receiveResult = client.receiveCloudEvents(TOPIC_NAME, EVENT_SUBSCRIPTION_NAME, 1, Duration.ofSeconds(10));
ReleaseOptions releaseOptions = new ReleaseOptions(Arrays.asList(receiveResult.getValue().get(0).getBrokerProperties().getLockToken()));
Expand All @@ -121,7 +136,7 @@ void releaseBatchOfCloudEventsSync() {

@Test
void releaseBatchOfCloudEventsWithDelaySync() {
EventGridClient client = buildClient();
EventGridClient client = buildSyncClient();
client.publishCloudEvent(TOPIC_NAME, getCloudEvent());
ReceiveResult receiveResult = client.receiveCloudEvents(TOPIC_NAME, EVENT_SUBSCRIPTION_NAME, 1, Duration.ofSeconds(10));
ReleaseOptions releaseOptions = new ReleaseOptions(Arrays.asList(receiveResult.getValue().get(0).getBrokerProperties().getLockToken()));
Expand All @@ -132,7 +147,7 @@ void releaseBatchOfCloudEventsWithDelaySync() {

@Test
void rejectBatchOfCloudEventsSync() {
EventGridClient client = buildClient();
EventGridClient client = buildSyncClient();
client.publishCloudEvent(TOPIC_NAME, getCloudEvent());
ReceiveResult receiveResult = client.receiveCloudEvents(TOPIC_NAME, EVENT_SUBSCRIPTION_NAME, 1, Duration.ofSeconds(10));
RejectOptions rejectOptions = new RejectOptions(Arrays.asList(receiveResult.getValue().get(0).getBrokerProperties().getLockToken()));
Expand All @@ -143,7 +158,7 @@ void rejectBatchOfCloudEventsSync() {

@Test
void renewBatchOfEventsSync() {
EventGridClient client = buildClient();
EventGridClient client = buildSyncClient();
client.publishCloudEvent(TOPIC_NAME, getCloudEvent());
ReceiveResult receiveResult = client.receiveCloudEvents(TOPIC_NAME, EVENT_SUBSCRIPTION_NAME, 1, Duration.ofSeconds(10));
RenewLockOptions options = new RenewLockOptions(Arrays.asList(receiveResult.getValue().get(0).getBrokerProperties().getLockToken()));
Expand All @@ -164,13 +179,7 @@ void publishCloudEvent() {
void publishCloudEventBinaryMode() {
EventGridAsyncClient client = buildAsyncClient();
CloudEvent event = new CloudEvent("/microsoft/testEvent", "Microsoft.MockPublisher.TestEvent",
BinaryData.fromObject(new HashMap<String, String>() {
{
put("Field1", "Value1");
put("Field2", "Value2");
put("Field3", "Value3");
}
}), CloudEventDataFormat.JSON, "text/plain")
BinaryData.fromString("MyCoolString"), CloudEventDataFormat.BYTES, "text/plain")
.setSubject("Test")
.setTime(testResourceNamer.now())
.setId(testResourceNamer.randomUuid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +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.test.http.AssertingHttpClientBuilder;
import com.azure.core.util.BinaryData;
import com.azure.core.util.Context;
import com.azure.core.util.serializer.JacksonAdapter;
Expand Down Expand Up @@ -514,16 +513,4 @@ 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
package com.azure.messaging.eventgrid;

import com.azure.core.credential.AzureKeyCredential;
import com.azure.core.http.HttpClient;
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.http.AssertingHttpClientBuilder;
import com.azure.core.test.models.TestProxySanitizer;
import com.azure.core.test.models.TestProxySanitizerType;
import com.azure.core.util.BinaryData;
Expand Down Expand Up @@ -140,7 +142,7 @@ String getEndpoint(String liveEnvName) {
if (interceptorManager.isPlaybackMode()) {
return DUMMY_ENDPOINT;
}
String endpoint = System.getenv(liveEnvName);
String endpoint = Configuration.getGlobalConfiguration().get(liveEnvName);
assertNotNull(endpoint, "System environment variable " + liveEnvName + " is null");
return endpoint;
}
Expand All @@ -154,4 +156,15 @@ AzureKeyCredential getKey(String liveEnvName) {
return key;
}

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();
}

}

0 comments on commit 2727f34

Please sign in to comment.