2323
2424import java .io .IOException ;
2525import java .util .HashMap ;
26+ import java .util .HashSet ;
2627import java .util .List ;
2728import java .util .Map ;
2829import java .util .Set ;
3132import static org .opensearch .flowframework .common .CommonValue .WORKFLOW_RESOURCE_TYPE ;
3233import static org .opensearch .flowframework .common .CommonValue .WORKFLOW_STATE_RESOURCE_TYPE ;
3334
35+ /**
36+ * Tests flow-framework resources: workflow and workflow_state with resource sharing enabled
37+ */
3438public class FlowFrameworkResourceSharingRestApiIT extends FlowFrameworkRestTestCase {
3539
3640 String aliceUser = "alice" ;
@@ -61,6 +65,8 @@ public class FlowFrameworkResourceSharingRestApiIT extends FlowFrameworkRestTest
6165 // If the suite is launched without the flag, just skip these tests cleanly.
6266 private final boolean skipTests = !isResourceSharingFeatureEnabled ();
6367
68+ private final Set <String > workflowIdsToCleanup = new HashSet <>();
69+
6470 @ Before
6571 public void setupSecureTests () throws IOException {
6672 if (skipTests ) {
@@ -126,6 +132,24 @@ public void tearDownSecureTests() throws IOException {
126132 if (skipTests ) {
127133 return ;
128134 }
135+
136+ // Clean up any workflows created by this test class
137+ for (String workflowId : workflowIdsToCleanup ) {
138+ try {
139+ // Owner (alice) can always delete the workflow
140+ deleteWorkflow (aliceClient , workflowId );
141+ } catch (ResponseException e ) {
142+ // Ignore 404 / already-deleted cases
143+ if (e .getResponse ().getStatusLine ().getStatusCode () != RestStatus .NOT_FOUND .getStatus ()) {
144+ logger .warn ("Non-404 error while cleaning up workflow {}" , workflowId , e );
145+ }
146+ } catch (Exception e ) {
147+ logger .warn ("Failed to clean up workflow {}" , workflowId , e );
148+ }
149+ }
150+ workflowIdsToCleanup .clear ();
151+
152+ // Now close clients and delete users/roles
129153 aliceClient .close ();
130154 bobClient .close ();
131155 catClient .close ();
@@ -142,15 +166,21 @@ public void tearDownSecureTests() throws IOException {
142166 deleteUser (lionUser );
143167 }
144168
145- public void testWorkflowVisibilityAndSearch_withResourceSharingEnabled () throws Exception {
169+ private String createWorkflowAndTrack (RestClient restClient , Template template ) throws Exception {
170+ Response created = createWorkflow (restClient , template );
171+ assertEquals (RestStatus .CREATED , TestHelpers .restStatus (created ));
172+ String workflowId = (String ) entityAsMap (created ).get (WORKFLOW_ID );
173+ workflowIdsToCleanup .add (workflowId );
174+ return workflowId ;
175+ }
176+
177+ public void testWorkflowVisibilityAndSearch () throws Exception {
146178 if (skipTests ) {
147179 logger .info ("Skipping test - resource sharing not enabled" );
148180 return ;
149181 }
150182 Template template = TestHelpers .createTemplateFromFile ("register-deploylocalsparseencodingmodel.json" );
151- Response created = createWorkflow (aliceClient , template );
152- assertEquals (RestStatus .CREATED , TestHelpers .restStatus (created ));
153- String workflowId = (String ) entityAsMap (created ).get (WORKFLOW_ID );
183+ String workflowId = createWorkflowAndTrack (aliceClient , template );
154184
155185 // Unshared → cat/admin cannot GET
156186 ResponseException ex = expectThrows (ResponseException .class , () -> getWorkflow (catClient , workflowId ));
@@ -225,14 +255,12 @@ public void testWorkflowVisibilityAndSearch_withResourceSharingEnabled() throws
225255 waitForWorkflowRevokeNonVisibility (workflowId , catClient );
226256 }
227257
228- public void testWorkflowUpdate_withResourceSharingEnabled () throws Exception {
258+ public void testWorkflowUpdate () throws Exception {
229259 if (skipTests ) {
230260 return ;
231261 }
232262 Template template = TestHelpers .createTemplateFromFile ("register-deploylocalsparseencodingmodel.json" );
233- Response created = createWorkflow (aliceClient , template );
234- assertEquals (RestStatus .CREATED , TestHelpers .restStatus (created ));
235- String workflowId = (String ) entityAsMap (created ).get (WORKFLOW_ID );
263+ String workflowId = createWorkflowAndTrack (aliceClient , template );
236264
237265 // Unshared → admin/fish/cat cannot update
238266 ResponseException ex = expectThrows (ResponseException .class , () -> updateWorkflow (client (), workflowId , template ));
@@ -278,14 +306,12 @@ public void testWorkflowUpdate_withResourceSharingEnabled() throws Exception {
278306 assertEquals (RestStatus .FORBIDDEN .getStatus (), elkDenied .getResponse ().getStatusLine ().getStatusCode ());
279307 }
280308
281- public void testProvisionDeprovision_withResourceSharingEnabled () throws Exception {
309+ public void testProvisionDeprovision () throws Exception {
282310 if (skipTests ) {
283311 return ;
284312 }
285313 Template template = TestHelpers .createTemplateFromFile ("register-deploylocalsparseencodingmodel.json" );
286- Response created = createWorkflow (aliceClient , template );
287- assertEquals (RestStatus .CREATED , TestHelpers .restStatus (created ));
288- String workflowId = (String ) entityAsMap (created ).get (WORKFLOW_ID );
314+ String workflowId = createWorkflowAndTrack (aliceClient , template );
289315
290316 // Unshared → cat cannot provision/deprovision
291317 ResponseException ex = expectThrows (ResponseException .class , () -> provisionWorkflow (catClient , workflowId ));
@@ -334,14 +360,12 @@ public void testProvisionDeprovision_withResourceSharingEnabled() throws Excepti
334360 assertEquals (RestStatus .FORBIDDEN .getStatus (), elkDenied .getResponse ().getStatusLine ().getStatusCode ());
335361 }
336362
337- public void testDeleteWorkflow_withResourceSharingEnabled () throws Exception {
363+ public void testDeleteWorkflow () throws Exception {
338364 if (skipTests ) {
339365 return ;
340366 }
341367 Template template = TestHelpers .createTemplateFromFile ("register-deploylocalsparseencodingmodel.json" );
342- Response created = createWorkflow (aliceClient , template );
343- assertEquals (RestStatus .CREATED , TestHelpers .restStatus (created ));
344- String workflowId = (String ) entityAsMap (created ).get (WORKFLOW_ID );
368+ String workflowId = createWorkflowAndTrack (aliceClient , template );
345369
346370 // Unshared → cat/admin cannot delete
347371 ResponseException ex = expectThrows (ResponseException .class , () -> deleteWorkflow (catClient , workflowId ));
@@ -362,15 +386,13 @@ public void testDeleteWorkflow_withResourceSharingEnabled() throws Exception {
362386 assertEquals (RestStatus .OK , TestHelpers .restStatus (del ));
363387 }
364388
365- public void testWorkflowStateVisibilityAndSearch_withResourceSharingEnabled () throws Exception {
389+ public void testWorkflowStateVisibilityAndSearch () throws Exception {
366390 if (skipTests ) {
367391 return ;
368392 }
369393 // Create a workflow (state doc gets created/managed by FF)
370394 Template template = TestHelpers .createTemplateFromFile ("register-deploylocalsparseencodingmodel.json" );
371- Response created = createWorkflow (aliceClient , template );
372- assertEquals (RestStatus .CREATED , TestHelpers .restStatus (created ));
373- String workflowId = (String ) entityAsMap (created ).get (WORKFLOW_ID );
395+ String workflowId = createWorkflowAndTrack (aliceClient , template );
374396
375397 // Unshared state → cat/admin cannot read status or find it
376398 ResponseException ex = expectThrows (ResponseException .class , () -> getWorkflowStatus (catClient , workflowId , false ));
0 commit comments