Skip to content

Commit 651ce86

Browse files
author
Changjian Wang
committed
Refactor Content Understanding Samples to Support API Key and Default Azure Credential Authentication
- Updated Sample03_AnalyzeInvoice, Sample04_CreateAnalyzer, Sample05_CreateClassifier, Sample06_GetAnalyzer, Sample07_ListAnalyzers, Sample08_UpdateAnalyzer, Sample09_DeleteAnalyzer, Sample10_AnalyzeConfigs, Sample11_AnalyzeReturnRawJson, Sample12_GetResultFile, Sample13_DeleteResult, Sample14_CopyAnalyzer, Sample15_GrantCopyAuth, and Sample16_CreateAnalyzerWithLabels to include logic for initializing the Content Understanding client with either an API key or the Default Azure Credential. - Added assertions to verify client initialization in each sample. - Improved code readability and maintainability by consolidating client creation logic.
1 parent 5fefcc3 commit 651ce86

17 files changed

+517
-142
lines changed

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample00_ConfigureDefaults.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,16 @@
77
import com.azure.ai.contentunderstanding.ContentUnderstandingClient;
88
import com.azure.ai.contentunderstanding.ContentUnderstandingClientBuilder;
99
import com.azure.ai.contentunderstanding.models.ContentUnderstandingDefaults;
10+
import com.azure.core.credential.AzureKeyCredential;
1011
import com.azure.core.http.rest.RequestOptions;
1112
import com.azure.core.http.rest.Response;
1213
import com.azure.core.util.BinaryData;
1314
import com.azure.core.util.Configuration;
1415
import com.azure.identity.DefaultAzureCredentialBuilder;
1516
import org.junit.jupiter.api.Test;
1617

18+
import static org.junit.jupiter.api.Assertions.*;
19+
1720
/**
1821
* Test class demonstrating how to configure and manage default settings for Content Understanding service.
1922
* This test shows:
@@ -25,11 +28,28 @@ public class Sample00_ConfigureDefaults {
2528

2629
@Test
2730
public void testConfigureDefaults() {
28-
// Create the Content Understanding client
29-
ContentUnderstandingClient client
30-
= new ContentUnderstandingClientBuilder().credential(new DefaultAzureCredentialBuilder().build())
31-
.endpoint(Configuration.getGlobalConfiguration().get("CONTENTUNDERSTANDING_ENDPOINT"))
32-
.buildClient();
31+
// BEGIN: com.azure.ai.contentunderstanding.buildClient
32+
String endpoint = Configuration.getGlobalConfiguration().get("CONTENTUNDERSTANDING_ENDPOINT");
33+
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
34+
35+
// Build the client with appropriate authentication
36+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder()
37+
.endpoint(endpoint);
38+
39+
ContentUnderstandingClient client;
40+
if (key != null && !key.trim().isEmpty()) {
41+
// Use API key authentication
42+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
43+
} else {
44+
// Use default Azure credential (for managed identity, Azure CLI, etc.)
45+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
46+
}
47+
// END: com.azure.ai.contentunderstanding.buildClient
48+
49+
// Verify client initialization
50+
assertNotNull(endpoint, "CONTENTUNDERSTANDING_ENDPOINT environment variable should be set");
51+
assertFalse(endpoint.trim().isEmpty(), "Endpoint should not be empty");
52+
assertNotNull(client, "Client should be successfully created");
3353

3454
// Step 1: Get current defaults
3555
System.out.println("Getting current default configuration...");

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample01_AnalyzeBinary.java

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
import com.azure.ai.contentunderstanding.models.DocumentTable;
1414
import com.azure.ai.contentunderstanding.models.DocumentTableCell;
1515
import com.azure.ai.contentunderstanding.models.MediaContent;
16+
import com.azure.core.credential.AzureKeyCredential;
1617
import com.azure.core.util.BinaryData;
1718
import com.azure.core.util.Configuration;
1819
import com.azure.core.util.polling.SyncPoller;
1920
import com.azure.identity.DefaultAzureCredentialBuilder;
2021
import org.junit.jupiter.api.Assertions;
2122
import org.junit.jupiter.api.Test;
2223

24+
import static org.junit.jupiter.api.Assertions.*;
25+
2326
import java.io.IOException;
2427
import java.nio.file.Files;
2528
import java.nio.file.Path;
@@ -40,12 +43,28 @@ public class Sample01_AnalyzeBinary {
4043

4144
@Test
4245
public void testAnalyzeBinaryAsync() throws IOException {
43-
// Create the Content Understanding client
46+
// BEGIN: com.azure.ai.contentunderstanding.buildClient
4447
String endpoint = Configuration.getGlobalConfiguration().get("CONTENTUNDERSTANDING_ENDPOINT");
45-
ContentUnderstandingClient client
46-
= new ContentUnderstandingClientBuilder().credential(new DefaultAzureCredentialBuilder().build())
47-
.endpoint(endpoint)
48-
.buildClient();
48+
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
49+
50+
// Build the client with appropriate authentication
51+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder()
52+
.endpoint(endpoint);
53+
54+
ContentUnderstandingClient client;
55+
if (key != null && !key.trim().isEmpty()) {
56+
// Use API key authentication
57+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
58+
} else {
59+
// Use default Azure credential (for managed identity, Azure CLI, etc.)
60+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
61+
}
62+
// END: com.azure.ai.contentunderstanding.buildClient
63+
64+
// Verify client initialization
65+
assertNotNull(endpoint, "CONTENTUNDERSTANDING_ENDPOINT environment variable should be set");
66+
assertFalse(endpoint.trim().isEmpty(), "Endpoint should not be empty");
67+
assertNotNull(client, "Client should be successfully created");
4968

5069
// Load the sample file
5170
String filePath = "src/test/resources/sample_invoice.pdf";
@@ -157,6 +176,11 @@ public void testAnalyzeBinaryAsync() throws IOException {
157176
tableCounter++;
158177
}
159178
}
179+
} else {
180+
// Content is not DocumentContent - verify it's MediaContent
181+
Assertions.assertTrue(content instanceof MediaContent,
182+
"Content should be MediaContent when not DocumentContent");
183+
System.out.println("Content is MediaContent (not document-specific), skipping document properties");
160184
}
161185
// END:ContentUnderstandingAccessDocumentProperties
162186

@@ -263,9 +287,12 @@ public void testAnalyzeBinaryAsync() throws IOException {
263287

264288
System.out.println("All document properties validated successfully");
265289
} else {
266-
System.out.println("Content is not DocumentContent type, " + "skipping document-specific validations");
267-
System.out.println("⚠️ Expected DocumentContent but got "
268-
+ (content != null ? content.getClass().getSimpleName() : "null"));
290+
// Content is not DocumentContent - validate alternative types
291+
Assertions.assertTrue(content instanceof MediaContent,
292+
"Content should be MediaContent when not DocumentContent, but got "
293+
+ (content != null ? content.getClass().getSimpleName() : "null"));
294+
System.out.println("Content is not DocumentContent type, skipping document-specific validations");
295+
System.out.println("⚠️ Content type: " + content.getClass().getSimpleName() + " (MediaContent validated)");
269296
}
270297
// END:Assertion_ContentUnderstandingAccessDocumentProperties
271298
}

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample02_AnalyzeUrl.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
import com.azure.ai.contentunderstanding.models.DocumentTable;
1515
import com.azure.ai.contentunderstanding.models.DocumentTableCell;
1616
import com.azure.ai.contentunderstanding.models.MediaContent;
17+
import com.azure.core.credential.AzureKeyCredential;
1718
import com.azure.core.util.Configuration;
1819
import com.azure.core.util.polling.SyncPoller;
1920
import com.azure.identity.DefaultAzureCredentialBuilder;
2021
import org.junit.jupiter.api.Assertions;
2122
import org.junit.jupiter.api.Test;
2223

24+
import static org.junit.jupiter.api.Assertions.*;
25+
2326
import java.util.Arrays;
2427
import java.util.HashSet;
2528
import java.util.Set;
@@ -36,12 +39,28 @@ public class Sample02_AnalyzeUrl {
3639

3740
@Test
3841
public void testAnalyzeUrlAsync() {
39-
// Create the Content Understanding client
42+
// BEGIN: com.azure.ai.contentunderstanding.buildClient
4043
String endpoint = Configuration.getGlobalConfiguration().get("CONTENTUNDERSTANDING_ENDPOINT");
41-
ContentUnderstandingClient client
42-
= new ContentUnderstandingClientBuilder().credential(new DefaultAzureCredentialBuilder().build())
43-
.endpoint(endpoint)
44-
.buildClient();
44+
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
45+
46+
// Build the client with appropriate authentication
47+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder()
48+
.endpoint(endpoint);
49+
50+
ContentUnderstandingClient client;
51+
if (key != null && !key.trim().isEmpty()) {
52+
// Use API key authentication
53+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
54+
} else {
55+
// Use default Azure credential (for managed identity, Azure CLI, etc.)
56+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
57+
}
58+
// END: com.azure.ai.contentunderstanding.buildClient
59+
60+
// Verify client initialization
61+
assertNotNull(endpoint, "CONTENTUNDERSTANDING_ENDPOINT environment variable should be set");
62+
assertFalse(endpoint.trim().isEmpty(), "Endpoint should not be empty");
63+
assertNotNull(client, "Client should be successfully created");
4564

4665
// BEGIN:ContentUnderstandingAnalyzeUrlAsync
4766
// Using a publicly accessible sample file from Azure-Samples GitHub repository
@@ -125,6 +144,11 @@ public void testAnalyzeUrlAsync() {
125144
tableCounter++;
126145
}
127146
}
147+
} else {
148+
// Content is not DocumentContent - verify it's MediaContent
149+
Assertions.assertTrue(content instanceof MediaContent,
150+
"Content should be MediaContent when not DocumentContent");
151+
System.out.println("Content is MediaContent (not document-specific), skipping document properties");
128152
}
129153

130154
Assertions.assertNotNull(content, "Content should not be null for document properties validation");
@@ -225,9 +249,12 @@ public void testAnalyzeUrlAsync() {
225249

226250
System.out.println("All document properties validated successfully");
227251
} else {
252+
// Content is not DocumentContent - validate alternative types
253+
Assertions.assertTrue(content instanceof MediaContent,
254+
"Content should be MediaContent when not DocumentContent, but got "
255+
+ (content != null ? content.getClass().getSimpleName() : "null"));
228256
System.out.println("⚠️ Content is not DocumentContent type, skipping document-specific validations");
229-
System.out.println("⚠️ Expected DocumentContent but got "
230-
+ (content != null ? content.getClass().getSimpleName() : "null"));
257+
System.out.println("⚠️ Content type: " + content.getClass().getSimpleName() + " (MediaContent validated)");
231258
}
232259
}
233260
}

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample03_AnalyzeInvoice.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
import com.azure.ai.contentunderstanding.models.NumberField;
1818
import com.azure.ai.contentunderstanding.models.ObjectField;
1919
import com.azure.ai.contentunderstanding.models.StringField;
20+
import com.azure.core.credential.AzureKeyCredential;
2021
import com.azure.core.util.Configuration;
2122
import com.azure.core.util.polling.SyncPoller;
2223
import com.azure.identity.DefaultAzureCredentialBuilder;
2324
import org.junit.jupiter.api.Assertions;
2425
import org.junit.jupiter.api.Test;
2526

27+
import static org.junit.jupiter.api.Assertions.*;
28+
2629
import java.util.Arrays;
2730
import java.util.List;
2831

@@ -39,12 +42,28 @@ public class Sample03_AnalyzeInvoice {
3942

4043
@Test
4144
public void testAnalyzeInvoiceAsync() {
42-
// Create the Content Understanding client
45+
// BEGIN: com.azure.ai.contentunderstanding.buildClient
4346
String endpoint = Configuration.getGlobalConfiguration().get("CONTENTUNDERSTANDING_ENDPOINT");
44-
ContentUnderstandingClient client
45-
= new ContentUnderstandingClientBuilder().credential(new DefaultAzureCredentialBuilder().build())
46-
.endpoint(endpoint)
47-
.buildClient();
47+
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
48+
49+
// Build the client with appropriate authentication
50+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder()
51+
.endpoint(endpoint);
52+
53+
ContentUnderstandingClient client;
54+
if (key != null && !key.trim().isEmpty()) {
55+
// Use API key authentication
56+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
57+
} else {
58+
// Use default Azure credential (for managed identity, Azure CLI, etc.)
59+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
60+
}
61+
// END: com.azure.ai.contentunderstanding.buildClient
62+
63+
// Verify client initialization
64+
assertNotNull(endpoint, "CONTENTUNDERSTANDING_ENDPOINT environment variable should be set");
65+
assertFalse(endpoint.trim().isEmpty(), "Endpoint should not be empty");
66+
assertNotNull(client, "Client should be successfully created");
4867

4968
// BEGIN:ContentUnderstandingAnalyzeInvoice
5069
// Using a publicly accessible sample file from Azure-Samples GitHub repository
@@ -218,6 +237,10 @@ public void testAnalyzeInvoiceAsync() {
218237
+ " to " + docContent.getEndPageNumber());
219238

220239
System.out.println("All invoice fields validated successfully");
240+
} else {
241+
// This should not happen given the assertTrue above, but handle it for completeness
242+
Assertions.fail("Content type validation failed: expected DocumentContent but got "
243+
+ (content != null ? content.getClass().getSimpleName() : "null"));
221244
}
222245
// END:Assertion_ContentUnderstandingExtractInvoiceFields
223246
}

sdk/contentunderstanding/azure-ai-contentunderstanding/src/test/java/com/azure/ai/contentunderstanding/generated/Sample04_CreateAnalyzer.java

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,18 @@ public void cleanup() {
5252
if (createdAnalyzerId != null) {
5353
try {
5454
String endpoint = Configuration.getGlobalConfiguration().get("CONTENTUNDERSTANDING_ENDPOINT");
55-
ContentUnderstandingClient client
56-
= new ContentUnderstandingClientBuilder().credential(new DefaultAzureCredentialBuilder().build())
57-
.endpoint(endpoint)
58-
.buildClient();
55+
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
56+
57+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder()
58+
.endpoint(endpoint);
59+
60+
ContentUnderstandingClient client;
61+
if (key != null && !key.trim().isEmpty()) {
62+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
63+
} else {
64+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
65+
}
66+
5967
client.deleteAnalyzer(createdAnalyzerId);
6068
System.out.println("Analyzer '" + createdAnalyzerId + "' deleted successfully.");
6169
} catch (Exception e) {
@@ -66,12 +74,28 @@ public void cleanup() {
6674

6775
@Test
6876
public void testCreateAnalyzerAsync() {
69-
// Create the Content Understanding client
77+
// BEGIN: com.azure.ai.contentunderstanding.buildClient
7078
String endpoint = Configuration.getGlobalConfiguration().get("CONTENTUNDERSTANDING_ENDPOINT");
71-
ContentUnderstandingClient client
72-
= new ContentUnderstandingClientBuilder().credential(new DefaultAzureCredentialBuilder().build())
73-
.endpoint(endpoint)
74-
.buildClient();
79+
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
80+
81+
// Build the client with appropriate authentication
82+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder()
83+
.endpoint(endpoint);
84+
85+
ContentUnderstandingClient client;
86+
if (key != null && !key.trim().isEmpty()) {
87+
// Use API key authentication
88+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
89+
} else {
90+
// Use default Azure credential (for managed identity, Azure CLI, etc.)
91+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
92+
}
93+
// END: com.azure.ai.contentunderstanding.buildClient
94+
95+
// Verify client initialization
96+
assertNotNull(endpoint, "CONTENTUNDERSTANDING_ENDPOINT environment variable should be set");
97+
assertFalse(endpoint.trim().isEmpty(), "Endpoint should not be empty");
98+
assertNotNull(client, "Client should be successfully created");
7599

76100
// BEGIN:ContentUnderstandingCreateAnalyzer
77101
// Generate a unique analyzer ID
@@ -272,10 +296,19 @@ public void testCreateAnalyzerAsync() {
272296
public void testUseCustomAnalyzerAsync() {
273297
// Create the Content Understanding client
274298
String endpoint = Configuration.getGlobalConfiguration().get("CONTENTUNDERSTANDING_ENDPOINT");
275-
ContentUnderstandingClient client
276-
= new ContentUnderstandingClientBuilder().credential(new DefaultAzureCredentialBuilder().build())
277-
.endpoint(endpoint)
278-
.buildClient();
299+
String key = System.getenv("AZURE_CONTENT_UNDERSTANDING_KEY");
300+
301+
ContentUnderstandingClientBuilder builder = new ContentUnderstandingClientBuilder()
302+
.endpoint(endpoint);
303+
304+
ContentUnderstandingClient client;
305+
if (key != null && !key.trim().isEmpty()) {
306+
client = builder.credential(new AzureKeyCredential(key)).buildClient();
307+
} else {
308+
client = builder.credential(new DefaultAzureCredentialBuilder().build()).buildClient();
309+
}
310+
311+
assertNotNull(client, "Client should be successfully created");
279312

280313
// First create an analyzer
281314
String analyzerId = "test_analyzer_" + UUID.randomUUID().toString().replace("-", "");

0 commit comments

Comments
 (0)