1515import org .opensearch .action .search .SearchResponse ;
1616import org .opensearch .client .Response ;
1717import org .opensearch .client .ResponseException ;
18+ import org .opensearch .core .rest .RestStatus ;
1819import org .opensearch .rest .RestRequest ;
1920
2021public class RestMLModelTenantAwareIT extends MLCommonsTenantAwareRestTestCase {
@@ -32,7 +33,14 @@ public void testModelCRUD() throws Exception {
3233 Map <String , Object > map = responseToMap (response );
3334 assertTrue (map .containsKey (CONNECTOR_ID ));
3435 String connectorId = map .get (CONNECTOR_ID ).toString ();
35- // Create a second connector from other tenant
36+ // Create a second connector with same tenant for deletion tests later
37+ response = makeRequest (createConnectorRequest , POST , CONNECTORS_PATH + "_create" );
38+ assertOK (response );
39+ map = responseToMap (response );
40+ assertTrue (map .containsKey (CONNECTOR_ID ));
41+ String secondConnectorId = map .get (CONNECTOR_ID ).toString ();
42+
43+ // Create a third connector from other tenant
3644 createConnectorRequest = getRestRequestWithHeadersAndContent (otherTenantId , COHERE_CONNECTOR_BLUEPRINT );
3745 response = makeRequest (createConnectorRequest , POST , CONNECTORS_PATH + "_create" );
3846 assertOK (response );
@@ -308,6 +316,17 @@ public void testModelCRUD() throws Exception {
308316 assertEquals (MISSING_TENANT_REASON , getErrorReasonFromResponseMap (map ));
309317 }
310318
319+ // Try to delete connector while model exists
320+ ResponseException ex = assertThrows (
321+ ResponseException .class ,
322+ () -> makeRequest (tenantRequest , DELETE , CONNECTORS_PATH + connectorId )
323+ );
324+ response = ex .getResponse ();
325+ assertEquals (RestStatus .CONFLICT .getStatus (), response .getStatusLine ().getStatusCode ());
326+ // but it should work with the connector not in a model
327+ response = makeRequest (tenantRequest , DELETE , CONNECTORS_PATH + secondConnectorId );
328+ assertOK (response );
329+
311330 // Now actually do the deletions. Same result whether multi-tenancy is enabled.
312331 // Verify still exists
313332 response = makeRequest (tenantRequest , GET , MODELS_PATH + modelId );
@@ -320,7 +339,7 @@ public void testModelCRUD() throws Exception {
320339 assertEquals (modelId , map .get (DOC_ID ).toString ());
321340
322341 // Verify the deletion
323- ResponseException ex = assertThrows (ResponseException .class , () -> makeRequest (tenantRequest , GET , MODELS_PATH + modelId ));
342+ ex = assertThrows (ResponseException .class , () -> makeRequest (tenantRequest , GET , MODELS_PATH + modelId ));
324343 response = ex .getResponse ();
325344 assertNotFound (response );
326345 map = responseToMap (response );
0 commit comments