diff --git a/sdk/webpubsub/azure-messaging-webpubsub/assets.json b/sdk/webpubsub/azure-messaging-webpubsub/assets.json new file mode 100644 index 0000000000000..3fea42f2794a1 --- /dev/null +++ b/sdk/webpubsub/azure-messaging-webpubsub/assets.json @@ -0,0 +1,6 @@ +{ + "AssetsRepo": "Azure/azure-sdk-assets", + "AssetsRepoPrefixPath": "java", + "TagPrefix": "java/webpubsub/azure-messaging-webpubsub", + "Tag": "java/webpubsub/azure-messaging-webpubsub_2c7e99b063" +} diff --git a/sdk/webpubsub/azure-messaging-webpubsub/pom.xml b/sdk/webpubsub/azure-messaging-webpubsub/pom.xml index b4909e725cd2e..67a791be01d76 100644 --- a/sdk/webpubsub/azure-messaging-webpubsub/pom.xml +++ b/sdk/webpubsub/azure-messaging-webpubsub/pom.xml @@ -32,12 +32,6 @@ HEAD - - - 0.10 - 0.10 - - com.azure @@ -135,7 +129,6 @@ - com.azure:* com.nimbusds:nimbus-jose-jwt:[9.31] diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/main/java/com/azure/messaging/webpubsub/WebPubSubServiceAsyncClient.java b/sdk/webpubsub/azure-messaging-webpubsub/src/main/java/com/azure/messaging/webpubsub/WebPubSubServiceAsyncClient.java index 67e024dd0e665..e22ed76f37910 100644 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/main/java/com/azure/messaging/webpubsub/WebPubSubServiceAsyncClient.java +++ b/sdk/webpubsub/azure-messaging-webpubsub/src/main/java/com/azure/messaging/webpubsub/WebPubSubServiceAsyncClient.java @@ -114,7 +114,6 @@ Mono> generateClientTokenWithResponse(RequestOptions reques return this.serviceClient.generateClientTokenWithResponseAsync(hub, requestOptions); } - /** * Broadcast content inside request body to all the connected client connections. * @param message The payload body. diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/TestUtils.java b/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/TestUtils.java new file mode 100644 index 0000000000000..7a4b7e5b641b8 --- /dev/null +++ b/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/TestUtils.java @@ -0,0 +1,43 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.messaging.webpubsub; + +import com.azure.core.http.HttpClient; +import com.azure.core.http.policy.FixedDelayOptions; +import com.azure.core.http.policy.RetryOptions; +import com.azure.core.test.http.AssertingHttpClientBuilder; +import com.azure.core.util.Configuration; + +import java.time.Duration; + +/** + * Common properties used in testing. + */ +final class TestUtils { + static final String HUB_NAME = "Hub"; + + static String getEndpoint() { + return Configuration.getGlobalConfiguration() + .get("WEB_PUB_SUB_ENDPOINT", "http://testendpoint.webpubsubdev.azure.com"); + } + + static String getConnectionString() { + return Configuration.getGlobalConfiguration() + .get("WEB_PUB_SUB_CONNECTION_STRING", "Endpoint=https://testendpoint.webpubsubdev.azure.com;AccessKey=LoremIpsumDolorSitAmetConsectetur;Version=1.0;"); + } + + static RetryOptions getRetryOptions() { + return new RetryOptions(new FixedDelayOptions(3, Duration.ofSeconds(20))); + } + + static HttpClient buildAsyncAssertingClient(HttpClient httpClient) { + return new AssertingHttpClientBuilder(httpClient) + .assertAsync() + .skipRequest((httpRequest, context) -> false) + .build(); + } + + private TestUtils() { + } +} diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/TokenGenerationTest.java b/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/TokenGenerationTest.java index 9c2426756f8f3..c66331757d629 100644 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/TokenGenerationTest.java +++ b/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/TokenGenerationTest.java @@ -6,9 +6,8 @@ import com.azure.core.http.HttpClient; import com.azure.core.http.policy.HttpLogDetailLevel; import com.azure.core.http.policy.HttpLogOptions; -import com.azure.core.test.TestBase; +import com.azure.core.test.TestProxyTestBase; import com.azure.core.test.annotation.DoNotRecord; -import com.azure.core.util.Configuration; import com.azure.identity.DefaultAzureCredentialBuilder; import com.azure.messaging.webpubsub.models.GetClientAccessTokenOptions; import com.azure.messaging.webpubsub.models.WebPubSubClientAccessToken; @@ -33,16 +32,32 @@ * Unit tests for {@link WebPubSubServiceAsyncClient#getClientAccessToken(GetClientAccessTokenOptions) * getAuthenticationToken} method. */ -public class TokenGenerationTest extends TestBase { +public class TokenGenerationTest extends TestProxyTestBase { + + private WebPubSubServiceClientBuilder builder; + + @Override + protected void beforeTest() { + builder = new WebPubSubServiceClientBuilder() + .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)) + .retryOptions(TestUtils.getRetryOptions()) + .hub(TestUtils.HUB_NAME); + } + + /** + * Testing token generation with connection string. + */ @ParameterizedTest @MethodSource("getTokenOptions") + @DoNotRecord public void testTokenGeneration(GetClientAccessTokenOptions tokenOptions, String connectionString, String expectedUrlPrefix, String expectedSubject, List expectedRoles, List expectedGroups) throws ParseException { - WebPubSubServiceClient client = new WebPubSubServiceClientBuilder() - .hub("test") + + WebPubSubServiceClient client = builder .connectionString(connectionString) .buildClient(); + WebPubSubClientAccessToken authenticationToken = client.getClientAccessToken(tokenOptions); assertNotNull(authenticationToken.getToken()); @@ -53,23 +68,22 @@ public void testTokenGeneration(GetClientAccessTokenOptions tokenOptions, String assertEquals(expectedSubject, jwtClaimsSet.getSubject()); assertEquals(expectedRoles, jwtClaimsSet.getClaim("role")); assertEquals(expectedGroups, jwtClaimsSet.getClaim("webpubsub.group")); - } @ParameterizedTest @MethodSource("getTokenOptions") @DoNotRecord(skipInPlayback = true) public void testTokenGenerationFromAadCredential(GetClientAccessTokenOptions tokenOptions, String connectionString, - String expectedUrlPrefix, String expectedSubject, - List expectedRoles, List expectedGroups) throws ParseException { - String endpoint = Configuration.getGlobalConfiguration() - .get("WEB_PUB_SUB_ENDPOINT", "http://testendpoint.webpubsubdev.azure.com"); + String expectedUrlPrefix, String expectedSubject, List expectedRoles, List expectedGroups) + throws ParseException { + + String endpoint = TestUtils.getEndpoint(); + WebPubSubServiceClientBuilder webPubSubServiceClientBuilder = new WebPubSubServiceClientBuilder() .endpoint(endpoint) .httpClient(HttpClient.createDefault()) - .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)) .credential(new DefaultAzureCredentialBuilder().build()) - .hub("test"); + .hub(TestUtils.HUB_NAME); WebPubSubServiceClient client = webPubSubServiceClientBuilder.buildClient(); WebPubSubClientAccessToken authenticationToken = client.getClientAccessToken(tokenOptions); diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/WebPubSubServiceAsyncClientTests.java b/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/WebPubSubServiceAsyncClientTests.java new file mode 100644 index 0000000000000..2f4bbb1b9dfc9 --- /dev/null +++ b/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/WebPubSubServiceAsyncClientTests.java @@ -0,0 +1,318 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.messaging.webpubsub; + +import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpHeaderName; +import com.azure.core.http.policy.HttpLogDetailLevel; +import com.azure.core.http.policy.HttpLogOptions; +import com.azure.core.http.rest.RequestOptions; +import com.azure.core.http.rest.Response; +import com.azure.core.test.TestMode; +import com.azure.core.test.TestProxyTestBase; +import com.azure.core.test.annotation.DoNotRecord; +import com.azure.core.util.BinaryData; +import com.azure.identity.DefaultAzureCredentialBuilder; +import com.azure.messaging.webpubsub.models.GetClientAccessTokenOptions; +import com.azure.messaging.webpubsub.models.WebPubSubContentType; +import com.azure.messaging.webpubsub.models.WebPubSubPermission; +import com.nimbusds.jwt.JWT; +import com.nimbusds.jwt.JWTClaimsSet; +import com.nimbusds.jwt.JWTParser; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import reactor.core.publisher.Mono; +import reactor.test.StepVerifier; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.text.ParseException; +import java.time.Duration; + +import static com.azure.messaging.webpubsub.TestUtils.buildAsyncAssertingClient; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; +import static org.junit.jupiter.api.Assumptions.assumeTrue; + +public class WebPubSubServiceAsyncClientTests extends TestProxyTestBase { + private static final Duration TIMEOUT = Duration.ofSeconds(10); + private static final String USER_ID = "test_user"; + private static final RequestOptions REQUEST_OPTIONS_TEXT = new RequestOptions() + .addRequestCallback(request -> { + request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"); + }); + private static final RequestOptions REQUEST_OPTIONS_STREAM = new RequestOptions() + .addRequestCallback(request -> { + request.setHeader(HttpHeaderName.CONTENT_TYPE, "application/octet-stream"); + }); + + private static final BinaryData MESSAGE = BinaryData.fromString("Hello World - Broadcast test!"); + + private WebPubSubServiceAsyncClient client; + + @Override + protected void beforeTest() { + WebPubSubServiceClientBuilder builder = new WebPubSubServiceClientBuilder() + .connectionString(TestUtils.getConnectionString()) + .retryOptions(TestUtils.getRetryOptions()) + .hub(TestUtils.HUB_NAME); + + 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(); + } + + private static void assertResponse(Mono> operation) { + StepVerifier.create(operation) + .assertNext(response -> { + assertNotNull(response); + assertEquals(202, response.getStatusCode()); + }) + .expectComplete() + .verify(TIMEOUT); + } + + @Test + public void testBroadcastString() { + assertResponse(client.sendToAllWithResponse(MESSAGE, REQUEST_OPTIONS_TEXT)); + } + + @Test + public void testBroadcastStringWithFilter() { + RequestOptions requestOptions = new RequestOptions() + .setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain") + .addQueryParam("filter", "userId ne 'user1'"); + + assertResponse(client.sendToAllWithResponse(MESSAGE, requestOptions)); + } + + @Test + public void testBroadcastBytes() { + byte[] bytes = "Hello World - Broadcast test!".getBytes(); + + assertResponse(client.sendToAllWithResponse(BinaryData.fromBytes(bytes), REQUEST_OPTIONS_STREAM)); + } + + @Test + public void testSendToUserString() throws IOException { + assertResponse(client.sendToUserWithResponse(USER_ID, MESSAGE, REQUEST_OPTIONS_TEXT)); + + assertResponse(client.sendToUserWithResponse(USER_ID, MESSAGE, WebPubSubContentType.TEXT_PLAIN, + MESSAGE.getLength(), null)); + + try (ByteArrayInputStream messageStream = new ByteArrayInputStream(MESSAGE.toBytes())) { + assertResponse(client.sendToUserWithResponse(USER_ID, BinaryData.fromStream(messageStream), + WebPubSubContentType.APPLICATION_OCTET_STREAM, MESSAGE.getLength(), null)); + } + } + + @Test + public void testSendToUserStringWithFilter() throws IOException { + RequestOptions requestOptions = new RequestOptions() + .addQueryParam("filter", "userId ne 'user1'"); + + assertResponse(client.sendToUserWithResponse(USER_ID, MESSAGE, REQUEST_OPTIONS_TEXT)); + + assertResponse(client.sendToUserWithResponse(USER_ID, MESSAGE, WebPubSubContentType.TEXT_PLAIN, + MESSAGE.getLength(), null)); + + try (ByteArrayInputStream messageStream = new ByteArrayInputStream(MESSAGE.toBytes())) { + assertResponse(client.sendToUserWithResponse(USER_ID, BinaryData.fromStream(messageStream), + WebPubSubContentType.APPLICATION_OCTET_STREAM, MESSAGE.getLength(), requestOptions)); + } + } + + @Test + public void testSendToUserBytes() { + final BinaryData binaryData = BinaryData.fromBytes("Hello World!".getBytes(StandardCharsets.UTF_8)); + + assertResponse(client.sendToUserWithResponse(USER_ID, binaryData, REQUEST_OPTIONS_STREAM)); + } + + @Test + public void testSendToConnectionString() { + assertResponse(client.sendToConnectionWithResponse("test_connection", MESSAGE, + REQUEST_OPTIONS_TEXT)); + } + + @Test + public void testSendToConnectionStringWithFilter() { + final RequestOptions filter = new RequestOptions() + .setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain") + .addQueryParam("filter", "userId ne 'user1'"); + + assertResponse(client.sendToConnectionWithResponse("test_connection", MESSAGE, filter)); + } + + @Test + public void testSendToConnectionBytes() { + final BinaryData binaryData = BinaryData.fromBytes("Hello World!".getBytes(StandardCharsets.UTF_8)); + + assertResponse(client.sendToConnectionWithResponse("test_connection", binaryData, + REQUEST_OPTIONS_STREAM)); + } + + @Test + public void testSendToConnectionJson() { + RequestOptions requestOptions = new RequestOptions() + .addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "application/json")); + + assertResponse(client.sendToConnectionWithResponse("test_connection", + BinaryData.fromString("{\"data\": true}"), requestOptions)); + } + + @Test + public void testSendToAllJson() { + RequestOptions requestOptions = new RequestOptions() + .addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "application/json")); + + + assertResponse(client.sendToAllWithResponse(BinaryData.fromString("{\"boolvalue\": true}"), + requestOptions)); + + assertResponse(client.sendToAllWithResponse(BinaryData.fromString("{\"stringvalue\": \"testingwebpubsub\"}"), + requestOptions)); + + assertResponse(client.sendToAllWithResponse(BinaryData.fromString("{\"intvalue\": 25}"), + requestOptions)); + + assertResponse(client.sendToAllWithResponse(BinaryData.fromString("{\"floatvalue\": 55.4}"), + requestOptions)); + } + + @Test + public void testRemoveNonExistentUserFromHub() { + StepVerifier.create(client.removeUserFromAllGroupsWithResponse("testRemoveNonExistentUserFromHub", new RequestOptions())) + .assertNext(response -> { + assertEquals(204, response.getStatusCode()); + }) + .expectComplete() + .verify(TIMEOUT); + } + + @Test + public void testRemoveConnectionFromAllGroup() { + StepVerifier.create(client.removeConnectionFromAllGroupsWithResponse("test_connection", new RequestOptions())) + .assertNext(response -> { + assertEquals(204, response.getStatusCode()); + }) + .expectComplete() + .verify(TIMEOUT); + } + + @Test + @DoNotRecord(skipInPlayback = true) + public void testGetAuthenticationToken() { + StepVerifier.create(client.getClientAccessToken(new GetClientAccessTokenOptions())) + .assertNext(token -> { + Assertions.assertNotNull(token); + Assertions.assertNotNull(token.getToken()); + Assertions.assertNotNull(token.getUrl()); + + assertTrue(token.getUrl().startsWith("wss://")); + assertTrue(token.getUrl().contains(".webpubsub.azure.com/client/hubs/")); + + String authToken = token.getToken(); + JWT jwt; + try { + jwt = JWTParser.parse(authToken); + } catch (ParseException e) { + fail("Unable to parse auth token: " + authToken + " exception: ", e); + return; + } + + JWTClaimsSet claimsSet; + try { + claimsSet = jwt.getJWTClaimsSet(); + } catch (ParseException e) { + fail("Unable to parse claims: " + authToken + " exception: ", e); + return; + } + + assertNotNull(claimsSet); + assertNotNull(claimsSet.getAudience()); + assertFalse(claimsSet.getAudience().isEmpty()); + + String aud = claimsSet.getAudience().iterator().next(); + assertTrue(aud.contains(".webpubsub.azure.com/client/hubs/")); + }) + .expectComplete() + .verify(TIMEOUT); + + } + + @Test + public void testRemoveNonExistentUserFromGroup() { + StepVerifier.create(client.removeUserFromGroupWithResponse("java", + "testRemoveNonExistentUserFromGroup", new RequestOptions()), 204); + } + + @Test + public void testSendMessageToGroup() { + StepVerifier.create(client.sendToGroupWithResponse("java", + BinaryData.fromString("Hello World!"), + new RequestOptions().addRequestCallback(request -> request.getHeaders() + .set("Content-Type", "text/plain"))), 202); + } + + @Test + public void testAadCredential() { + WebPubSubServiceClientBuilder builder = new WebPubSubServiceClientBuilder() + .endpoint(TestUtils.getEndpoint()) + .httpClient(HttpClient.createDefault()) + .httpLogOptions(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)) + .hub("test"); + + if (getTestMode() == TestMode.PLAYBACK) { + builder.httpClient(buildAsyncAssertingClient(interceptorManager.getPlaybackClient())) + .connectionString(TestUtils.getConnectionString()); + } else { + builder.credential(new DefaultAzureCredentialBuilder().build()); + } + + if (getTestMode() == TestMode.RECORD) { + builder.addPolicy(interceptorManager.getRecordPolicy()); + } + + final WebPubSubServiceAsyncClient asyncClient = builder.buildAsyncClient(); + + assertResponse(asyncClient.sendToUserWithResponse(USER_ID, MESSAGE, REQUEST_OPTIONS_TEXT)); + } + + @Test + public void testCheckPermission() { + assumeTrue(getTestMode() == TestMode.PLAYBACK, "This requires real " + + "connection id that is created when a client connects to Web PubSub service. So, run this in PLAYBACK " + + "mode only."); + + RequestOptions requestOptions = new RequestOptions() + .addQueryParam("targetName", "java"); + + StepVerifier.create(client.checkPermissionWithResponse(WebPubSubPermission.SEND_TO_GROUP, "71xtjgThROOJ6DsVY3xbBw2ef45fd11", + requestOptions)) + .assertNext(response -> { + assertEquals(200, response.getStatusCode()); + assertTrue(response.getValue()); + }) + .expectComplete() + .verify(TIMEOUT); + } +} diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/WebPubSubServiceClientTests.java b/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/WebPubSubServiceClientTests.java index 97ceffc069d9b..1edd59e5c1b58 100644 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/WebPubSubServiceClientTests.java +++ b/sdk/webpubsub/azure-messaging-webpubsub/src/test/java/com/azure/messaging/webpubsub/WebPubSubServiceClientTests.java @@ -4,15 +4,15 @@ package com.azure.messaging.webpubsub; import com.azure.core.http.HttpClient; +import com.azure.core.http.HttpHeaderName; import com.azure.core.http.policy.HttpLogDetailLevel; import com.azure.core.http.policy.HttpLogOptions; import com.azure.core.http.rest.RequestOptions; import com.azure.core.http.rest.Response; -import com.azure.core.test.TestBase; import com.azure.core.test.TestMode; +import com.azure.core.test.TestProxyTestBase; import com.azure.core.test.annotation.DoNotRecord; import com.azure.core.util.BinaryData; -import com.azure.core.util.Configuration; import com.azure.identity.DefaultAzureCredentialBuilder; import com.azure.messaging.webpubsub.models.GetClientAccessTokenOptions; import com.azure.messaging.webpubsub.models.WebPubSubClientAccessToken; @@ -22,9 +22,7 @@ import com.nimbusds.jwt.JWTClaimsSet; import com.nimbusds.jwt.JWTParser; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledIfEnvironmentVariable; import java.io.ByteArrayInputStream; import java.nio.charset.StandardCharsets; @@ -32,59 +30,49 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assumptions.assumeTrue; -public class WebPubSubServiceClientTests extends TestBase { - private static final String DEFAULT_CONNECTION_STRING = - "Endpoint=https://example.com;AccessKey=dummykey;Version=1.0;"; - private static final String CONNECTION_STRING = Configuration.getGlobalConfiguration() - .get("WEB_PUB_SUB_CS", DEFAULT_CONNECTION_STRING); - private static final String ENDPOINT = Configuration.getGlobalConfiguration() - .get("WEB_PUB_SUB_ENDPOINT", "https://srnagar-wps-pubsub.webpubsub.azure.com"); +public class WebPubSubServiceClientTests extends TestProxyTestBase { private WebPubSubServiceClient client; - private WebPubSubServiceAsyncClient asyncClient; - @BeforeEach - public void setup() { - WebPubSubServiceClientBuilder webPubSubServiceClientBuilder = new WebPubSubServiceClientBuilder() - .connectionString(CONNECTION_STRING) - .httpClient(HttpClient.createDefault()) - .hub("test"); - - if (getTestMode() == TestMode.PLAYBACK) { - webPubSubServiceClientBuilder.httpClient(interceptorManager.getPlaybackClient()); - } else if (getTestMode() == TestMode.RECORD) { - webPubSubServiceClientBuilder.addPolicy(interceptorManager.getRecordPolicy()); + @Override + protected void beforeTest() { + WebPubSubServiceClientBuilder builder = new WebPubSubServiceClientBuilder() + .connectionString(TestUtils.getConnectionString()) + .retryOptions(TestUtils.getRetryOptions()) + .hub(TestUtils.HUB_NAME); + + 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(); - - this.asyncClient = webPubSubServiceClientBuilder - .buildAsyncClient(); } - private void assertResponse(Response response, int expectedCode) { + private static void assertResponse(Response response, int expectedCode) { assertNotNull(response); assertEquals(expectedCode, response.getStatusCode()); } - /***************************************************************************************************************** - * Sync Tests - WebPubSubServiceClient - ****************************************************************************************************************/ - - @Test - public void assertClientNotNull() { - assertNotNull(client); - } - @Test public void testBroadcastString() { assertResponse(client.sendToAllWithResponse( - BinaryData.fromString("Hello World - Broadcast test!"), - new RequestOptions().addRequestCallback(request -> request.getHeaders() - .set("Content-Type", "text/plain"))), 202); + BinaryData.fromString("Hello World - Broadcast test!"), + new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202); } @Test @@ -99,9 +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.getHeaders() - .set("Content-Type", "application/octet-stream"))), 202); + BinaryData.fromBytes(bytes), + new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "application/octet-stream"))), 202); } @Test @@ -110,8 +97,7 @@ public void testSendToUserString() { assertResponse(client.sendToUserWithResponse("test_user", message, - new RequestOptions().addRequestCallback(request -> request.getHeaders() - .set("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(), @@ -132,8 +118,7 @@ public void testSendToUserStringWithFilter() { assertResponse(client.sendToUserWithResponse("test_user", message, - new RequestOptions().addRequestCallback(request -> request.getHeaders() - .set("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(), @@ -151,17 +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.getHeaders() - .set("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.getHeaders() - .set("Content-Type", "text/plain"))), 202); + BinaryData.fromString("Hello World!"), + new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202); } @Test @@ -175,34 +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.getHeaders() - .set("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 @@ -242,38 +224,33 @@ public void testGetAuthenticationToken() throws ParseException { Assertions.assertTrue(aud.contains(".webpubsub.azure.com/client/hubs/")); } - /***************************************************************************************************************** - * Sync Tests - WebPubSubGroup - ****************************************************************************************************************/ - @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.getHeaders() - .set("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(ENDPOINT) - .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(CONNECTION_STRING); + .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()); } @@ -281,16 +258,16 @@ public void testAadCredential() { this.client = webPubSubServiceClientBuilder.buildClient(); assertResponse(client.sendToUserWithResponse("test_user", - BinaryData.fromString("Hello World!"), - new RequestOptions().addRequestCallback(request -> request.getHeaders() - .set("Content-Type", "text/plain"))), 202); + BinaryData.fromString("Hello World!"), + new RequestOptions().addRequestCallback(request -> request.setHeader(HttpHeaderName.CONTENT_TYPE, "text/plain"))), 202); } @Test - @DisabledIfEnvironmentVariable(named = "AZURE_TEST_MODE", matches = "LIVE", disabledReason = "This requires real " - + "connection id that is created when a client connects to Web PubSub service. So, run this in PLAYBACK " - + "mode only.") public void testCheckPermission() { + assumeTrue(getTestMode() == TestMode.PLAYBACK, "This requires real " + + "connection id that is created when a client connects to Web PubSub service. So, run this in PLAYBACK " + + "mode only."); + RequestOptions requestOptions = new RequestOptions() .addQueryParam("targetName", "java"); boolean permission = client.checkPermissionWithResponse(WebPubSubPermission.SEND_TO_GROUP, "71xtjgThROOJ6DsVY3xbBw2ef45fd11", diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[1].json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[1].json deleted file mode 100644 index ba5f37f8f8555..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[1].json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "networkCallRecords" : [ ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[2].json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[2].json deleted file mode 100644 index ba5f37f8f8555..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[2].json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "networkCallRecords" : [ ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[3].json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[3].json deleted file mode 100644 index ba5f37f8f8555..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[3].json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "networkCallRecords" : [ ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[4].json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[4].json deleted file mode 100644 index ba5f37f8f8555..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[4].json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "networkCallRecords" : [ ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[5].json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[5].json deleted file mode 100644 index ba5f37f8f8555..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[5].json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "networkCallRecords" : [ ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[6].json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[6].json deleted file mode 100644 index ba5f37f8f8555..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[6].json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "networkCallRecords" : [ ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[7].json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[7].json deleted file mode 100644 index ba5f37f8f8555..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/TokenGenerationTest.testTokenGeneration[7].json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "networkCallRecords" : [ ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.assertClientNotNull.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.assertClientNotNull.json deleted file mode 100644 index ba5f37f8f8555..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.assertClientNotNull.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "networkCallRecords" : [ ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testAadCredential.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testAadCredential.json deleted file mode 100644 index be8462b72ff3a..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testAadCredential.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/users/test_user/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "text/plain" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:48 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testBroadcastBytes.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testBroadcastBytes.json deleted file mode 100644 index 85fdbda0c7a6e..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testBroadcastBytes.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "application/octet-stream" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testBroadcastString.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testBroadcastString.json deleted file mode 100644 index cbb41cb5b66ad..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testBroadcastString.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "text/plain" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testBroadcastStringWithFilter.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testBroadcastStringWithFilter.json deleted file mode 100644 index 9a383e6bd596d..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testBroadcastStringWithFilter.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/:send?api-version=2022-11-01&filter=userId%20ne%20%27user1%27", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "text/plain" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 09:03:40 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testCheckPermission.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testCheckPermission.json deleted file mode 100644 index 7a9628b6b59ca..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testCheckPermission.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "HEAD", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/permissions/sendToGroup/connections/71xtjgThROOJ6DsVY3xbBw2ef45fd11?api-version=2022-11-01&targetName=java", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.0.0-beta.6 (14.0.2; Windows 10; 10.0)" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "200", - "Date" : "Fri, 12 Nov 2021 01:49:48 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testRemoveConnectionFromAllGroup.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testRemoveConnectionFromAllGroup.json deleted file mode 100644 index 55821dbe68f4e..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testRemoveConnectionFromAllGroup.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "DELETE", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/connections/test_connection/groups?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0 (17.0.4.1; Windows 11; 10.0)" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "204", - "Date" : "Wed, 04 Jan 2023 02:33:39 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testRemoveNonExistentUserFromGroup.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testRemoveNonExistentUserFromGroup.json deleted file mode 100644 index 06c370da57538..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testRemoveNonExistentUserFromGroup.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "DELETE", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/users/testRemoveNonExistentUserFromGroup/groups/java?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "204", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testRemoveNonExistentUserFromHub.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testRemoveNonExistentUserFromHub.json deleted file mode 100644 index 00c194ea0d89c..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testRemoveNonExistentUserFromHub.json +++ /dev/null @@ -1,19 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "DELETE", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/users/testRemoveNonExistentUserFromHub/groups?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)" - }, - "Response" : { - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "204", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendMessageToGroup.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendMessageToGroup.json deleted file mode 100644 index c2d146a684e99..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendMessageToGroup.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/groups/java/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "text/plain" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToAllJson.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToAllJson.json deleted file mode 100644 index 1059019684f10..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToAllJson.json +++ /dev/null @@ -1,72 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "application/json" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - }, { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "application/json" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:47 GMT" - }, - "Exception" : null - }, { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "application/json" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:47 GMT" - }, - "Exception" : null - }, { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "application/json" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:47 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionBytes.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionBytes.json deleted file mode 100644 index 4fe609fcc2274..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionBytes.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/connections/test_connection/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "application/octet-stream" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionJson.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionJson.json deleted file mode 100644 index 1fad8ddb13cee..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionJson.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/connections/test_connection/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "application/json" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionString.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionString.json deleted file mode 100644 index 325aac47374e0..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionString.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/connections/test_connection/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "text/plain" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionStringWithFilter.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionStringWithFilter.json deleted file mode 100644 index 7f21a4279c310..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToConnectionStringWithFilter.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/connections/test_connection/:send?api-version=2022-11-01&filter=userId%20ne%20%27user1%27", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "text/plain" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 09:03:41 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToUserBytes.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToUserBytes.json deleted file mode 100644 index 9737da8c2aa8e..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToUserBytes.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/users/test_user/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "application/octet-stream" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToUserString.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToUserString.json deleted file mode 100644 index 5d04df9479576..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToUserString.json +++ /dev/null @@ -1,55 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/users/test_user/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "text/plain" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:46 GMT" - }, - "Exception" : null - }, { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/users/test_user/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "text/plain" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:47 GMT" - }, - "Exception" : null - }, { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/users/test_user/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "application/octet-stream" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 05:59:47 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToUserStringWithFilter.json b/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToUserStringWithFilter.json deleted file mode 100644 index c7b75c2e513df..0000000000000 --- a/sdk/webpubsub/azure-messaging-webpubsub/src/test/resources/session-records/WebPubSubServiceClientTests.testSendToUserStringWithFilter.json +++ /dev/null @@ -1,55 +0,0 @@ -{ - "networkCallRecords" : [ { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/users/test_user/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "text/plain" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 09:03:40 GMT" - }, - "Exception" : null - }, { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/users/test_user/:send?api-version=2022-11-01", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "text/plain" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 09:03:41 GMT" - }, - "Exception" : null - }, { - "Method" : "POST", - "Uri" : "https://REDACTED.webpubsub.azure.com/api/hubs/test/users/test_user/:send?api-version=2022-11-01&filter=userId%20ne%20%27user1%27", - "Headers" : { - "User-Agent" : "azsdk-java-azure-messaging-webpubsub/1.2.0-beta.1 (17.0.4.1; Windows 11; 10.0)", - "Content-Type" : "application/octet-stream" - }, - "Response" : { - "content-length" : "0", - "Strict-Transport-Security" : "max-age=15724800; includeSubDomains", - "api-supported-versions" : "2022-11-01", - "Connection" : "keep-alive", - "retry-after" : "0", - "StatusCode" : "202", - "Date" : "Thu, 10 Nov 2022 09:03:41 GMT" - }, - "Exception" : null - } ], - "variables" : [ ] -} \ No newline at end of file diff --git a/sdk/webpubsub/test-resources.bicep b/sdk/webpubsub/test-resources.bicep index 2250946806c02..48e4f2baaf3cc 100644 --- a/sdk/webpubsub/test-resources.bicep +++ b/sdk/webpubsub/test-resources.bicep @@ -1,3 +1,6 @@ +@description('The base resource name.') +param baseName string = '${resourceGroup().name}' + @description('The tenant id to which the application and resources belong.') param tenantId string = '72f988bf-86f1-41af-91ab-2d7cd011db47' @@ -7,15 +10,52 @@ param testApplicationId string @description('This is the object id of the service principal used to run tests.') param testApplicationOid string +@secure() @description('The application client secret used to run tests.') param testApplicationSecret string -var contributorRoleId = '/subscriptions/${subscription().subscriptionId}/providers/Microsoft.Authorization/roleDefinitions/b24988ac-6180-42a0-ab88-20f7382dd24c' +@description('Location of the resource.') +param location string = resourceGroup().location + +var webPubSubName = '${baseName}-e2e' + +// Find role id by heading to the Web Pub Sub resource, selecting Access Control (IAM), Roles, choose the Role, +// then click on View under Details and check out the JSON. +var webPubSubContributorRoleId = subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '12cf5a90-567b-43ae-8102-96cf46c7d9b4') + +resource webPubSub 'Microsoft.SignalRService/webPubSub@2021-10-01' = { + name: webPubSubName + location: location + sku: { + name: 'Standard_S1' + tier: 'Standard' + capacity: 1 + } + identity: { + type: 'None' + } + properties: { + tls: { + clientCertEnabled: false + } + networkACLs: { + defaultAction: 'Deny' + publicNetwork: { + allow: [ 'ServerConnection', 'ClientConnection', 'RESTAPI', 'Trace' ] + } + privateEndpoints: [] + } + publicNetworkAccess: 'Enabled' + disableLocalAuth: false + disableAadAuth: false + } +} -resource contributorRoleId_name 'Microsoft.Authorization/roleAssignments@2022-04-01' = { - name: guid('contributorRoleId${resourceGroup().name}') +resource webPubSubContributor 'Microsoft.Authorization/roleAssignments@2020-04-01-preview' = { + name: guid('contributor', webPubSubName) + scope: webPubSub properties: { - roleDefinitionId: contributorRoleId + roleDefinitionId: webPubSubContributorRoleId principalId: testApplicationOid } } @@ -25,3 +65,5 @@ output AZURE_CLIENT_ID string = testApplicationId output AZURE_CLIENT_SECRET string = testApplicationSecret output AZURE_SUBSCRIPTION_ID string = subscription().subscriptionId output AZURE_RESOURCE_GROUP_NAME string = resourceGroup().name +output WEB_PUB_SUB_CONNECTION_STRING string = webPubSub.listKeys().primaryConnectionString +output WEB_PUB_SUB_ENDPOINT string = 'https://${webPubSub.properties.hostName}'