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 ;
1718import com .azure .core .util .polling .PollerFlux ;
1819import org .junit .jupiter .api .Test ;
1920import reactor .core .publisher .Mono ;
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,16 @@ 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 completed unsuccessfully with status: " + pollResponse .getStatus ());
6261 }
62+ return pollResponse .getValue ();
6363 }).block (); // block() is used here for testing; in production, use subscribe()
64+
65+ AnalysisResult result = operationStatus .getResult ();
6466 // END:ContentUnderstandingAnalyzeInvoiceAsync
6567
6668 // BEGIN:Assertion_ContentUnderstandingAnalyzeInvoiceAsync
@@ -74,6 +76,36 @@ public void testAnalyzeInvoiceAsync() {
7476 System .out .println ("Analysis result contains " + result .getContents ().size () + " content(s)" );
7577 // END:Assertion_ContentUnderstandingAnalyzeInvoiceAsync
7678
79+ // BEGIN:ContentUnderstandingAnalyzeInvoiceUsageAsync
80+ // Get usage details from the operation status
81+ UsageDetails usage = operationStatus .getUsage ();
82+ if (usage != null ) {
83+ System .out .println ("\n Usage Details:" );
84+ if (usage .getDocumentPagesStandard () != null ) {
85+ System .out .println (" Document pages (standard): " + usage .getDocumentPagesStandard ());
86+ }
87+ if (usage .getContextualizationTokens () != null ) {
88+ System .out .println (" Contextualization tokens: " + usage .getContextualizationTokens ());
89+ }
90+ Map <String , Integer > tokens = usage .getTokens ();
91+ if (tokens != null && !tokens .isEmpty ()) {
92+ System .out .println (" Model tokens:" );
93+ for (Map .Entry <String , Integer > entry : tokens .entrySet ()) {
94+ System .out .println (" " + entry .getKey () + ": " + entry .getValue ());
95+ }
96+ }
97+ }
98+ // END:ContentUnderstandingAnalyzeInvoiceUsageAsync
99+
100+ // BEGIN:Assertion_ContentUnderstandingAnalyzeInvoiceUsageAsync
101+ assertNotNull (usage , "Usage details should not be null" );
102+ assertNotNull (usage .getDocumentPagesStandard (), "Document pages (standard) should not be null" );
103+ assertTrue (usage .getDocumentPagesStandard () > 0 , "Document pages (standard) should be positive" );
104+ assertNotNull (usage .getTokens (), "Tokens should not be null" );
105+ assertTrue (!usage .getTokens ().isEmpty (), "Tokens should not be empty" );
106+ System .out .println ("Usage details validated successfully" );
107+ // END:Assertion_ContentUnderstandingAnalyzeInvoiceUsageAsync
108+
77109 // BEGIN:ContentUnderstandingExtractInvoiceFieldsAsync
78110 // Get the document content (invoices are documents)
79111 AnalysisContent firstContent = result .getContents ().get (0 );
0 commit comments