Skip to content

Commit

Permalink
Modify to use switch statements.
Browse files Browse the repository at this point in the history
  • Loading branch information
conniey committed Jul 13, 2023
1 parent 1c40150 commit c1c0043
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ static String getConnectionString() {
}

static RetryOptions getRetryOptions() {
return new RetryOptions(new FixedDelayOptions(0, Duration.ofSeconds(20)));
return new RetryOptions(new FixedDelayOptions(3, Duration.ofSeconds(20)));
}

static HttpClient buildAsyncAssertingClient(HttpClient httpClient) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,21 @@ protected void beforeTest() {
WebPubSubServiceClientBuilder builder = new WebPubSubServiceClientBuilder()
.connectionString(TestUtils.getConnectionString())
.retryOptions(TestUtils.getRetryOptions())
.httpClient(HttpClient.createDefault())
.hub(TestUtils.HUB_NAME);

if (getTestMode() == TestMode.PLAYBACK) {
builder.httpClient(buildAsyncAssertingClient(interceptorManager.getPlaybackClient()));
} else if (getTestMode() == TestMode.RECORD) {
builder.addPolicy(interceptorManager.getRecordPolicy());
}
switch (getTestMode()) {
case LIVE:
builder.httpClient(HttpClient.createDefault());
break;
case RECORD:
builder.httpClient(HttpClient.createDefault())
.addPolicy(interceptorManager.getRecordPolicy());
break;
case PLAYBACK:
builder.httpClient(interceptorManager.getPlaybackClient());
break;
default:
throw new IllegalStateException("Unknown test mode. " + getTestMode());

this.client = builder.buildAsyncClient();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,27 @@ public class WebPubSubServiceClientTests extends TestProxyTestBase {

@Override
protected void beforeTest() {
WebPubSubServiceClientBuilder webPubSubServiceClientBuilder = new WebPubSubServiceClientBuilder()
WebPubSubServiceClientBuilder builder = new WebPubSubServiceClientBuilder()
.connectionString(TestUtils.getConnectionString())
.retryOptions(TestUtils.getRetryOptions())
.httpClient(HttpClient.createDefault())
.hub(TestUtils.HUB_NAME);

if (getTestMode() == TestMode.PLAYBACK) {
webPubSubServiceClientBuilder.httpClient(interceptorManager.getPlaybackClient());
} else if (getTestMode() == TestMode.RECORD) {
webPubSubServiceClientBuilder.addPolicy(interceptorManager.getRecordPolicy());
switch (getTestMode()) {
case LIVE:
builder.httpClient(HttpClient.createDefault());
break;
case RECORD:
builder.httpClient(HttpClient.createDefault())
.addPolicy(interceptorManager.getRecordPolicy());
break;
case PLAYBACK:
builder.httpClient(interceptorManager.getPlaybackClient());
break;
default:
throw new IllegalStateException("Unknown test mode. " + getTestMode());
}

this.client = webPubSubServiceClientBuilder
this.client = builder
.buildClient();
}

Expand All @@ -63,8 +71,8 @@ private static void assertResponse(Response<?> response, int expectedCode) {
@Test
public void testBroadcastString() {
assertResponse(client.sendToAllWithResponse(
BinaryData.fromString("Hello World - Broadcast test!"),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202);
BinaryData.fromString("Hello World - Broadcast test!"),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202);
}

@Test
Expand All @@ -79,8 +87,8 @@ public void testBroadcastStringWithFilter() {
public void testBroadcastBytes() {
byte[] bytes = "Hello World - Broadcast test!".getBytes();
assertResponse(client.sendToAllWithResponse(
BinaryData.fromBytes(bytes),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "application/octet-stream"))), 202);
BinaryData.fromBytes(bytes),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "application/octet-stream"))), 202);
}

@Test
Expand All @@ -89,7 +97,7 @@ public void testSendToUserString() {

assertResponse(client.sendToUserWithResponse("test_user",
message,
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202);
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202);

assertResponse(client.sendToUserWithResponse("test_user",
message, WebPubSubContentType.TEXT_PLAIN, message.getLength(),
Expand Down Expand Up @@ -128,15 +136,15 @@ public void testSendToUserStringWithFilter() {
@Test
public void testSendToUserBytes() {
assertResponse(client.sendToUserWithResponse("test_user",
BinaryData.fromBytes("Hello World!".getBytes(StandardCharsets.UTF_8)),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "application/octet-stream"))), 202);
BinaryData.fromBytes("Hello World!".getBytes(StandardCharsets.UTF_8)),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "application/octet-stream"))), 202);
}

@Test
public void testSendToConnectionString() {
assertResponse(client.sendToConnectionWithResponse("test_connection",
BinaryData.fromString("Hello World!"),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202);
BinaryData.fromString("Hello World!"),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202);
}

@Test
Expand All @@ -150,33 +158,33 @@ public void testSendToConnectionStringWithFilter() {
@Test
public void testSendToConnectionBytes() {
assertResponse(client.sendToConnectionWithResponse("test_connection",
BinaryData.fromBytes("Hello World!".getBytes(StandardCharsets.UTF_8)),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "application/octet-stream"))), 202);
BinaryData.fromBytes("Hello World!".getBytes(StandardCharsets.UTF_8)),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "application/octet-stream"))), 202);
}

@Test
public void testSendToConnectionJson() {
assertResponse(client.sendToConnectionWithResponse("test_connection",
BinaryData.fromString("{\"data\": true}"),
new RequestOptions()
.addRequestCallback(request -> request.getHeaders().set("Content-Type", "application/json"))), 202);
BinaryData.fromString("{\"data\": true}"),
new RequestOptions()
.addRequestCallback(request -> request.getHeaders().set("Content-Type", "application/json"))), 202);
}

@Test
public void testSendToAllJson() {
RequestOptions requestOptions = new RequestOptions().addRequestCallback(request -> request.getHeaders().set(
"Content-Type", "application/json"));
"Content-Type", "application/json"));

assertResponse(client.sendToAllWithResponse(BinaryData.fromString("{\"boolvalue\": true}"),
requestOptions), 202);
requestOptions), 202);
assertResponse(client.sendToAllWithResponse(BinaryData.fromString("{\"stringvalue\": \"testingwebpubsub\"}"),
requestOptions), 202);
requestOptions), 202);

assertResponse(client.sendToAllWithResponse(BinaryData.fromString("{\"intvalue\": 25}"),
requestOptions), 202);
requestOptions), 202);

assertResponse(client.sendToAllWithResponse(BinaryData.fromString("{\"floatvalue\": 55.4}"),
requestOptions), 202);
requestOptions), 202);
}

@Test
Expand Down Expand Up @@ -219,39 +227,39 @@ public void testGetAuthenticationToken() throws ParseException {
@Test
public void testRemoveNonExistentUserFromGroup() {
assertResponse(client.removeUserFromGroupWithResponse("java",
"testRemoveNonExistentUserFromGroup", new RequestOptions()), 204);
"testRemoveNonExistentUserFromGroup", new RequestOptions()), 204);
}

@Test
public void testSendMessageToGroup() {
assertResponse(client.sendToGroupWithResponse("java",
BinaryData.fromString("Hello World!"),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202);
BinaryData.fromString("Hello World!"),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202);
}

@Test
public void testAadCredential() {
WebPubSubServiceClientBuilder webPubSubServiceClientBuilder = new WebPubSubServiceClientBuilder()
.endpoint(TestUtils.getEndpoint())
.httpClient(HttpClient.createDefault())
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.hub("test");
.endpoint(TestUtils.getEndpoint())
.httpClient(HttpClient.createDefault())
.httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
.hub("test");

if (getTestMode() == TestMode.PLAYBACK) {
webPubSubServiceClientBuilder.httpClient(interceptorManager.getPlaybackClient())
.connectionString(TestUtils.getConnectionString());
.connectionString(TestUtils.getConnectionString());
} else if (getTestMode() == TestMode.RECORD) {
webPubSubServiceClientBuilder.addPolicy(interceptorManager.getRecordPolicy())
.credential(new DefaultAzureCredentialBuilder().build());
.credential(new DefaultAzureCredentialBuilder().build());
} else if (getTestMode() == TestMode.LIVE) {
webPubSubServiceClientBuilder.credential(new DefaultAzureCredentialBuilder().build());
}

this.client = webPubSubServiceClientBuilder.buildClient();

assertResponse(client.sendToUserWithResponse("test_user",
BinaryData.fromString("Hello World!"),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202);
BinaryData.fromString("Hello World!"),
new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202);
}

@Test
Expand Down

0 comments on commit c1c0043

Please sign in to comment.