26
26
import com .google .genai .types .Content ;
27
27
import com .google .genai .types .FinishReason ;
28
28
import com .google .genai .types .FunctionCall ;
29
+ import com .google .genai .types .GenerateContentResponseUsageMetadata ;
29
30
import com .google .genai .types .Part ;
30
31
import java .util .Optional ;
31
32
import org .junit .Before ;
@@ -61,6 +62,12 @@ private Content createSampleFunctionCallContent(String functionName) {
61
62
public void testSerializationAndDeserialization_allFieldsPresent ()
62
63
throws JsonProcessingException {
63
64
Content sampleContent = createSampleContent ("Hello, world!" );
65
+ GenerateContentResponseUsageMetadata usageMetadata =
66
+ GenerateContentResponseUsageMetadata .builder ()
67
+ .promptTokenCount (10 )
68
+ .candidatesTokenCount (20 )
69
+ .totalTokenCount (30 )
70
+ .build ();
64
71
LlmResponse originalResponse =
65
72
LlmResponse .builder ()
66
73
.content (sampleContent )
@@ -69,6 +76,7 @@ public void testSerializationAndDeserialization_allFieldsPresent()
69
76
.errorCode (new FinishReason ("ERR_123" ))
70
77
.errorMessage (Optional .of ("An error occurred." ))
71
78
.interrupted (Optional .of (true ))
79
+ .usageMetadata (usageMetadata )
72
80
.build ();
73
81
74
82
String json = originalResponse .toJson ();
@@ -83,6 +91,10 @@ public void testSerializationAndDeserialization_allFieldsPresent()
83
91
assertThat (jsonNode .get ("errorCode" ).asText ()).isEqualTo ("ERR_123" );
84
92
assertThat (jsonNode .get ("errorMessage" ).asText ()).isEqualTo ("An error occurred." );
85
93
assertThat (jsonNode .get ("interrupted" ).asBoolean ()).isTrue ();
94
+ assertThat (jsonNode .has ("usageMetadata" )).isTrue ();
95
+ assertThat (jsonNode .get ("usageMetadata" ).get ("promptTokenCount" ).asInt ()).isEqualTo (10 );
96
+ assertThat (jsonNode .get ("usageMetadata" ).get ("candidatesTokenCount" ).asInt ()).isEqualTo (20 );
97
+ assertThat (jsonNode .get ("usageMetadata" ).get ("totalTokenCount" ).asInt ()).isEqualTo (30 );
86
98
87
99
LlmResponse deserializedResponse = LlmResponse .fromJsonString (json , LlmResponse .class );
88
100
@@ -93,6 +105,7 @@ public void testSerializationAndDeserialization_allFieldsPresent()
93
105
assertThat (deserializedResponse .errorCode ()).hasValue (new FinishReason ("ERR_123" ));
94
106
assertThat (deserializedResponse .errorMessage ()).hasValue ("An error occurred." );
95
107
assertThat (deserializedResponse .interrupted ()).hasValue (true );
108
+ assertThat (deserializedResponse .usageMetadata ()).hasValue (usageMetadata );
96
109
}
97
110
98
111
@ Test
@@ -108,6 +121,7 @@ public void testSerializationAndDeserialization_optionalFieldsEmpty()
108
121
.errorCode (Optional .empty ())
109
122
.errorMessage (Optional .empty ())
110
123
.interrupted (Optional .empty ())
124
+ .usageMetadata (Optional .empty ())
111
125
.build ();
112
126
113
127
String json = originalResponse .toJson ();
@@ -122,6 +136,7 @@ public void testSerializationAndDeserialization_optionalFieldsEmpty()
122
136
assertThat (jsonNode .has ("errorCode" )).isFalse ();
123
137
assertThat (jsonNode .has ("errorMessage" )).isFalse ();
124
138
assertThat (jsonNode .has ("interrupted" )).isFalse ();
139
+ assertThat (jsonNode .has ("usageMetadata" )).isFalse ();
125
140
126
141
LlmResponse deserializedResponse = LlmResponse .fromJsonString (json , LlmResponse .class );
127
142
@@ -133,6 +148,7 @@ public void testSerializationAndDeserialization_optionalFieldsEmpty()
133
148
assertThat (deserializedResponse .errorCode ()).isEmpty ();
134
149
assertThat (deserializedResponse .errorMessage ()).isEmpty ();
135
150
assertThat (deserializedResponse .interrupted ()).isEmpty ();
151
+ assertThat (deserializedResponse .usageMetadata ()).isEmpty ();
136
152
}
137
153
138
154
@ Test
@@ -146,7 +162,8 @@ public void testDeserialization_optionalFieldsNullInJson() throws JsonProcessing
146
162
+ "\" turnComplete\" : true,"
147
163
+ "\" errorCode\" : null,"
148
164
+ "\" errorMessage\" : null,"
149
- + "\" interrupted\" : null"
165
+ + "\" interrupted\" : null,"
166
+ + "\" usageMetadata\" : null"
150
167
+ "}" ;
151
168
152
169
LlmResponse deserializedResponse = LlmResponse .fromJsonString (jsonWithNulls , LlmResponse .class );
@@ -160,6 +177,7 @@ public void testDeserialization_optionalFieldsNullInJson() throws JsonProcessing
160
177
assertThat (deserializedResponse .errorCode ()).isEmpty ();
161
178
assertThat (deserializedResponse .errorMessage ()).isEmpty ();
162
179
assertThat (deserializedResponse .interrupted ()).isEmpty ();
180
+ assertThat (deserializedResponse .usageMetadata ()).isEmpty ();
163
181
}
164
182
165
183
@ Test
@@ -185,6 +203,7 @@ public void testDeserialization_someOptionalFieldsMissingSomePresent()
185
203
assertThat (jsonNode .get ("errorCode" ).asText ()).isEqualTo ("FATAL_ERROR" );
186
204
assertThat (jsonNode .has ("errorMessage" )).isFalse ();
187
205
assertThat (jsonNode .has ("interrupted" )).isFalse ();
206
+ assertThat (jsonNode .has ("usageMetadata" )).isFalse ();
188
207
189
208
LlmResponse deserializedResponse = LlmResponse .fromJsonString (json , LlmResponse .class );
190
209
assertThat (deserializedResponse ).isEqualTo (originalResponse );
@@ -197,5 +216,6 @@ public void testDeserialization_someOptionalFieldsMissingSomePresent()
197
216
assertThat (deserializedResponse .errorCode ()).hasValue (new FinishReason ("FATAL_ERROR" ));
198
217
assertThat (deserializedResponse .errorMessage ()).isEmpty ();
199
218
assertThat (deserializedResponse .interrupted ()).isEmpty ();
219
+ assertThat (deserializedResponse .usageMetadata ()).isEmpty ();
200
220
}
201
221
}
0 commit comments