Skip to content

Commit

Permalink
HTM-1229: implement delete
Browse files Browse the repository at this point in the history
  • Loading branch information
mprins committed Oct 7, 2024
1 parent 8170ae6 commit 7e25298
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.quartz.impl.matchers.GroupMatcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -33,6 +34,7 @@
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;

/**
* Admin controller for controlling the task scheduler. Not to be used to create new tasks, adding
Expand Down Expand Up @@ -197,7 +199,7 @@ public ResponseEntity<?> stopJob(@PathVariable UUID uuid) {
description =
"This will remove the job from the scheduler and delete all information about the job")
@DeleteMapping(
path = "${tailormap-api.admin.base-path}/tasks/{uuid}",
path = "${tailormap-api.admin.base-path}/tasks/{type}/{uuid}",
produces = MediaType.APPLICATION_JSON_VALUE)
@ApiResponse(
responseCode = "404",
Expand All @@ -207,8 +209,17 @@ public ResponseEntity<?> stopJob(@PathVariable UUID uuid) {
mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(example = "{\"message\":\"Job does not exist\"}")))
@ApiResponse(responseCode = "204", description = "Job is deleted")
public ResponseEntity<?> delete(@PathVariable UUID uuid) {
logger.debug("Deleted job {}", uuid);
public ResponseEntity<?> delete(@PathVariable String type, @PathVariable UUID uuid)
throws SchedulerException {

JobKey jobKey =
scheduler.getJobKeys(GroupMatcher.groupEquals(type)).stream()
.filter(jobkey -> jobkey.getName().equals(uuid.toString()))
.findFirst()
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));

boolean succes = scheduler.deleteJob(jobKey);
logger.info("Job {}:{} deletion {}", type, uuid, (succes ? "succeeded" : "failed"));

return ResponseEntity.noContent().build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ void listTasksForExistingDummyType() throws Exception {
@WithMockUser(
username = "tm-admin",
authorities = {Group.ADMIN})
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
void listTasksForNonExistentType() throws Exception {
mockMvc
.perform(
Expand Down Expand Up @@ -164,15 +165,47 @@ void stopTask() throws Exception {
.andExpect(status().isAccepted());
}

@Test
@WithMockUser(
username = "tm-admin",
authorities = {Group.ADMIN})
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
void deleteNonExistentTask() throws Exception {
mockMvc
.perform(
delete(
adminBasePath + "/tasks/dummy/{uuid}", /*does not exist*/
"6308d26e-fe1e-4268-bb28-20db2cd06914")
.accept(MediaType.APPLICATION_JSON))
// .andDo(print())
.andExpect(status().isNotFound());
}

@Test
@WithMockUser(
username = "tm-admin",
authorities = {Group.ADMIN})
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
void deleteTask() throws Exception {
MvcResult result =
mockMvc
.perform(
get(adminBasePath + "/tasks")
.queryParam("type", "dummy")
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.tasks").isArray())
.andExpect(jsonPath("$.tasks.length()").value(2))
.andReturn();

final String validUUID =
JsonPath.read(result.getResponse().getContentAsString(), "$.tasks[0].uuid");

mockMvc
.perform(
delete(adminBasePath + "/tasks/{uuid}", "6308d26e-fe1e-4268-bb28-20db2cd06914")
delete(adminBasePath + "/tasks/dummy/{uuid}", /*does not exist*/ validUUID)
.accept(MediaType.APPLICATION_JSON))
// .andDo(print())
.andExpect(status().isNoContent());
Expand Down

0 comments on commit 7e25298

Please sign in to comment.