@@ -218,4 +218,62 @@ record = _gZkClient.readData(path);
218218 put ("k2" , "v2" );
219219 }}, record .getMapField ("map" ));
220220 }
221+
222+ @ Test ()
223+ public void testUpsertWithOptionalCreate_CreateWhenNotExists () {
224+ // Test: Create node when it doesn't exist and allowCreate is true
225+ String path = PropertyPathBuilder .instanceConfig (clusterName , "id10" );
226+ ZNRecord record = new ZNRecord ("id10" );
227+ record .setSimpleField ("key1" , "value1" );
228+
229+ // Node doesn't exist yet
230+ AssertJUnit .assertFalse (_gZkClient .exists (path ));
231+
232+ // Create with allowCreate=true, persistent=true
233+ ZKUtil .upsertWithOptionalCreate (_gZkClient , path , record , true , true , true );
234+
235+ // Verify node was created
236+ AssertJUnit .assertTrue (_gZkClient .exists (path ));
237+ ZNRecord result = _gZkClient .readData (path );
238+ AssertJUnit .assertEquals ("id10" , result .getId ());
239+ AssertJUnit .assertEquals ("value1" , result .getSimpleField ("key1" ));
240+ }
241+
242+ @ Test ()
243+ public void testUpsertWithOptionalCreate_NoCreateWhenNotExists () {
244+ // Test: Don't create node when it doesn't exist and allowCreate is false
245+ String path = PropertyPathBuilder .instanceConfig (clusterName , "id11" );
246+ ZNRecord record = new ZNRecord ("id11" );
247+ record .setSimpleField ("key1" , "value1" );
248+
249+ // Node doesn't exist yet
250+ AssertJUnit .assertFalse (_gZkClient .exists (path ));
251+
252+ // Try to update with allowCreate=false
253+ ZKUtil .upsertWithOptionalCreate (_gZkClient , path , record , true , true , false );
254+
255+ // Verify node was NOT created
256+ AssertJUnit .assertFalse (_gZkClient .exists (path ));
257+ }
258+
259+ @ Test ()
260+ public void testUpsertWithOptionalCreate_UpdateExistingWithAllowCreateFalse () {
261+ // Test: Update should work even when allowCreate=false if node exists
262+ String path = PropertyPathBuilder .instanceConfig (clusterName , "id15" );
263+
264+ // Create initial node
265+ ZNRecord initial = new ZNRecord ("id15" );
266+ initial .setSimpleField ("key1" , "value1" );
267+ _gZkClient .createPersistent (path , initial );
268+
269+ // Update with allowCreate=false (should still work since node exists)
270+ ZNRecord update = new ZNRecord ("id15" );
271+ update .setSimpleField ("key2" , "value2" );
272+ ZKUtil .upsertWithOptionalCreate (_gZkClient , path , update , true , true , false );
273+
274+ // Verify update happened
275+ ZNRecord result = _gZkClient .readData (path );
276+ AssertJUnit .assertEquals ("value1" , result .getSimpleField ("key1" ));
277+ AssertJUnit .assertEquals ("value2" , result .getSimpleField ("key2" ));
278+ }
221279}
0 commit comments