Skip to content

Commit

Permalink
Update Web Pub Sub test recordings (#35848)
Browse files Browse the repository at this point in the history
* Add opens for testing so it does not fail.

* Add bicep script so resources are created for live tests.

* Consolidate common properties.

* Do not record test token generation because there are no network calls.

* Update WebPubSubServiceClientTests

* Add async client tests.

* Update pom.xml to remove suppression for line coverage.

* Extends from TestProxyTestBase.

* Do not record non-network tests.
  • Loading branch information
conniey authored Jul 13, 2023
1 parent 1df8b88 commit 92529dc
Show file tree
Hide file tree
Showing 33 changed files with 500 additions and 608 deletions.
6 changes: 6 additions & 0 deletions sdk/webpubsub/azure-messaging-webpubsub/assets.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"AssetsRepo": "Azure/azure-sdk-assets",
"AssetsRepoPrefixPath": "java",
"TagPrefix": "java/webpubsub/azure-messaging-webpubsub",
"Tag": "java/webpubsub/azure-messaging-webpubsub_2c7e99b063"
}
7 changes: 0 additions & 7 deletions sdk/webpubsub/azure-messaging-webpubsub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,6 @@
<tag>HEAD</tag>
</scm>

<properties>
<!-- Once full testing is added this should either be removed or increased -->
<jacoco.min.linecoverage>0.10</jacoco.min.linecoverage>
<jacoco.min.branchcoverage>0.10</jacoco.min.branchcoverage>
</properties>

<dependencies>
<dependency>
<groupId>com.azure</groupId>
Expand Down Expand Up @@ -135,7 +129,6 @@
<rules>
<bannedDependencies>
<includes>
<include>com.azure:*</include>
<include>com.nimbusds:nimbus-jose-jwt:[9.31]</include> <!-- {x-include-update;com.nimbusds:nimbus-jose-jwt;external_dependency} -->
</includes>
</bannedDependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ Mono<Response<BinaryData>> 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<String> expectedRoles, List<String> expectedGroups) throws ParseException {
WebPubSubServiceClient client = new WebPubSubServiceClientBuilder()
.hub("test")

WebPubSubServiceClient client = builder
.connectionString(connectionString)
.buildClient();

WebPubSubClientAccessToken authenticationToken = client.getClientAccessToken(tokenOptions);

assertNotNull(authenticationToken.getToken());
Expand All @@ -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<String> expectedRoles, List<String> expectedGroups) throws ParseException {
String endpoint = Configuration.getGlobalConfiguration()
.get("WEB_PUB_SUB_ENDPOINT", "http://testendpoint.webpubsubdev.azure.com");
String expectedUrlPrefix, String expectedSubject, List<String> expectedRoles, List<String> 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);
Expand Down
Loading

0 comments on commit 92529dc

Please sign in to comment.