Skip to content

Commit

Permalink
Feature + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasCAI-mlv committed Oct 9, 2024
1 parent b4c25a0 commit 871f67f
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public class ResourceQuotaController extends NamespacedResourceController {
* List quotas by namespace.
*
* @param namespace The namespace
* @param name The name parameter
* @return A list of quotas
*/
@Get
Expand All @@ -59,7 +60,7 @@ public List<ResourceQuotaResponse> list(String namespace, @QueryValue(defaultVal
* @param namespace The name
* @param quota The quota name
* @return A quota
* @deprecated use list(String, String name) instead.
* @deprecated use {@link #list(String, String)} instead.
*/
@Get("/{quota}")
@Deprecated(since = "1.12.0")
Expand Down Expand Up @@ -115,12 +116,49 @@ public HttpResponse<ResourceQuota> apply(String namespace, @Body @Valid Resource
return formatHttpResponse(resourceQuotaService.create(quota), status);
}

/**
* Delete quotas.
*
* @param namespace The namespace
* @param name The name parameter
* @param dryrun Is dry run mode or not?
* @return An HTTP response
*/
@Delete
@Status(HttpStatus.NO_CONTENT)
public HttpResponse<Void> bulkDelete(String namespace, @QueryValue(defaultValue = "*") String name,
@QueryValue(defaultValue = "false") boolean dryrun) {

List<ResourceQuota> resourceQuotas = resourceQuotaService.findByWildcardName(namespace, name);

if (resourceQuotas.isEmpty()) {
return HttpResponse.notFound();
}

if (dryrun) {
return HttpResponse.noContent();
}

resourceQuotas.forEach(resourceQuota -> {
sendEventLog(
resourceQuota,
ApplyStatus.deleted,
resourceQuota.getSpec(),
null,
EMPTY_STRING
);
resourceQuotaService.delete(resourceQuota);
});

return HttpResponse.noContent();
}

/**
* Delete a quota.
*
* @param namespace The namespace
* @param name The resource quota
* @param dryrun Is dry run mode or not ?
* @param dryrun Is dry run mode or not?
* @return An HTTP response
*/
@Delete("/{name}{?dryrun}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,19 +321,19 @@ void shouldApplyUpdated() {
}

@Test
void shouldDeleteWhenNotFound() {
void shouldNotDeleteQuotaWhenNotFound() {
when(resourceQuotaService.findByName("test", "quota")).thenReturn(Optional.empty());
HttpResponse<Void> actual = resourceQuotaController.delete("test", "quota", false);
assertEquals(HttpStatus.NOT_FOUND, actual.getStatus());
verify(resourceQuotaService, never()).delete(ArgumentMatchers.any());
}

@Test
void shouldDeleteWhenDryRun() {
void shouldNotDeleteQuotaWhenDryRun() {
ResourceQuota resourceQuota = ResourceQuota.builder()
.metadata(Metadata.builder()
.cluster("local")
.name("created-quota")
.name("quota")
.build())
.spec(Map.of("count/topics", "3"))
.build();
Expand All @@ -345,11 +345,11 @@ void shouldDeleteWhenDryRun() {
}

@Test
void shouldDelete() {
void shouldDeleteQuota() {
ResourceQuota resourceQuota = ResourceQuota.builder()
.metadata(Metadata.builder()
.cluster("local")
.name("created-quota")
.name("quota")
.build())
.spec(Map.of("count/topics", "3"))
.build();
Expand All @@ -364,4 +364,49 @@ void shouldDelete() {
assertEquals(HttpStatus.NO_CONTENT, actual.getStatus());
verify(resourceQuotaService).delete(resourceQuota);
}

@Test
void shouldNotBulkDeleteQuotaWhenNotFound() {
when(resourceQuotaService.findByWildcardName("test", "quota*")).thenReturn(List.of());
HttpResponse<Void> actual = resourceQuotaController.bulkDelete("test", "quota*", false);
assertEquals(HttpStatus.NOT_FOUND, actual.getStatus());
verify(resourceQuotaService, never()).delete(ArgumentMatchers.any());
}

@Test
void shouldNotBulkDeleteQuotaWhenDryRun() {
ResourceQuota resourceQuota1 = ResourceQuota.builder()
.metadata(Metadata.builder()
.cluster("local")
.name("quota1")
.build())
.spec(Map.of("count/topics", "3"))
.build();

when(resourceQuotaService.findByWildcardName("test", "quota*")).thenReturn(List.of(resourceQuota1));
HttpResponse<Void> actual = resourceQuotaController.bulkDelete("test", "quota*", true);
assertEquals(HttpStatus.NO_CONTENT, actual.getStatus());
verify(resourceQuotaService, never()).delete(ArgumentMatchers.any());
}

@Test
void shouldBulkDeleteQuota() {
ResourceQuota resourceQuota = ResourceQuota.builder()
.metadata(Metadata.builder()
.cluster("local")
.name("quota")
.build())
.spec(Map.of("count/topics", "3"))
.build();

when(resourceQuotaService.findByWildcardName("test", "quota*")).thenReturn(List.of(resourceQuota));
when(securityService.username()).thenReturn(Optional.of("test-user"));
when(securityService.hasRole(ResourceBasedSecurityRule.IS_ADMIN)).thenReturn(false);
doNothing().when(applicationEventPublisher).publishEvent(any());
doNothing().when(resourceQuotaService).delete(resourceQuota);

HttpResponse<Void> actual = resourceQuotaController.bulkDelete("test", "quota*", false);
assertEquals(HttpStatus.NO_CONTENT, actual.getStatus());
verify(resourceQuotaService).delete(resourceQuota);
}
}

0 comments on commit 871f67f

Please sign in to comment.