1414import com .azure .ai .contentunderstanding .models .AnalysisContent ;
1515import com .azure .ai .contentunderstanding .models .ContentObjectField ;
1616import com .azure .ai .contentunderstanding .models .DocumentSource ;
17+ import com .azure .ai .contentunderstanding .models .UsageDetails ;
18+ import com .azure .core .util .polling .LongRunningOperationStatus ;
1719import com .azure .core .util .polling .PollerFlux ;
1820import org .junit .jupiter .api .Test ;
19- import reactor .core .publisher .Mono ;
2021
2122import static org .junit .jupiter .api .Assertions .assertEquals ;
2223import static org .junit .jupiter .api .Assertions .assertNotNull ;
2526
2627import java .util .Arrays ;
2728import java .util .List ;
29+ import java .util .Map ;
2830
2931/**
3032 * Async sample demonstrating how to analyze invoices using Content Understanding service.
@@ -51,16 +53,19 @@ public void testAnalyzeInvoiceAsync() {
5153 PollerFlux <ContentAnalyzerAnalyzeOperationStatus , AnalysisResult > operation
5254 = contentUnderstandingAsyncClient .beginAnalyze ("prebuilt-invoice" , Arrays .asList (input ));
5355
54- // Use reactive pattern: chain operations using flatMap
56+ // Wait for the operation to complete and get the operation status
5557 // In a real application, you would use subscribe() instead of block()
56- AnalysisResult result = operation .last ().flatMap (pollResponse -> {
57- if (pollResponse .getStatus ().isComplete ()) {
58- return pollResponse .getFinalResult ();
59- } else {
60- return Mono .error (
61- new RuntimeException ("Polling completed unsuccessfully with status: " + pollResponse .getStatus ()));
58+ ContentAnalyzerAnalyzeOperationStatus operationStatus = operation .last ().map (pollResponse -> {
59+ if (!pollResponse .getStatus ().isComplete ()) {
60+ throw new RuntimeException ("Polling did not complete: " + pollResponse .getStatus ());
6261 }
62+ if (pollResponse .getStatus () != LongRunningOperationStatus .SUCCESSFULLY_COMPLETED ) {
63+ throw new RuntimeException ("Operation failed with status: " + pollResponse .getStatus ());
64+ }
65+ return pollResponse .getValue ();
6366 }).block (); // block() is used here for testing; in production, use subscribe()
67+
68+ AnalysisResult result = operationStatus .getResult ();
6469 // END:ContentUnderstandingAnalyzeInvoiceAsync
6570
6671 // BEGIN:Assertion_ContentUnderstandingAnalyzeInvoiceAsync
@@ -74,6 +79,36 @@ public void testAnalyzeInvoiceAsync() {
7479 System .out .println ("Analysis result contains " + result .getContents ().size () + " content(s)" );
7580 // END:Assertion_ContentUnderstandingAnalyzeInvoiceAsync
7681
82+ // BEGIN:ContentUnderstandingAnalyzeInvoiceUsageAsync
83+ // Get usage details from the operation status
84+ UsageDetails usage = operationStatus .getUsage ();
85+ if (usage != null ) {
86+ System .out .println ("\n Usage Details:" );
87+ if (usage .getDocumentPagesStandard () != null ) {
88+ System .out .println (" Document pages (standard): " + usage .getDocumentPagesStandard ());
89+ }
90+ if (usage .getContextualizationTokens () != null ) {
91+ System .out .println (" Contextualization tokens: " + usage .getContextualizationTokens ());
92+ }
93+ Map <String , Integer > tokens = usage .getTokens ();
94+ if (tokens != null && !tokens .isEmpty ()) {
95+ System .out .println (" Model tokens:" );
96+ for (Map .Entry <String , Integer > entry : tokens .entrySet ()) {
97+ System .out .println (" " + entry .getKey () + ": " + entry .getValue ());
98+ }
99+ }
100+ }
101+ // END:ContentUnderstandingAnalyzeInvoiceUsageAsync
102+
103+ // BEGIN:Assertion_ContentUnderstandingAnalyzeInvoiceUsageAsync
104+ assertNotNull (usage , "Usage details should not be null" );
105+ assertNotNull (usage .getDocumentPagesStandard (), "Document pages (standard) should not be null" );
106+ assertTrue (usage .getDocumentPagesStandard () > 0 , "Document pages (standard) should be positive" );
107+ assertNotNull (usage .getTokens (), "Tokens should not be null" );
108+ assertTrue (!usage .getTokens ().isEmpty (), "Tokens should not be empty" );
109+ System .out .println ("Usage details validated successfully" );
110+ // END:Assertion_ContentUnderstandingAnalyzeInvoiceUsageAsync
111+
77112 // BEGIN:ContentUnderstandingExtractInvoiceFieldsAsync
78113 // Get the document content (invoices are documents)
79114 AnalysisContent firstContent = result .getContents ().get (0 );
0 commit comments