Skip to content

Commit

Permalink
Add new clear key (and sub queue) by queue type endpoint.
Browse files Browse the repository at this point in the history
Add tests for new endpoint.
  • Loading branch information
Kilemonn committed Oct 6, 2023
1 parent 90b0e33 commit 3b7c325
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ open class MessageQueueController : HasLogger
return ResponseEntity.ok(messageQueue.keys(includeEmpty ?: true))
}

@Operation(summary = "Delete a keys or all keys, in turn clearing that sub queue.", description = "Delete the sub queue that matches the provided key. If no key is provided, all sub queues will be cleared.")
@DeleteMapping(ENDPOINT_KEYS, produces = [MediaType.APPLICATION_JSON_VALUE])
@ApiResponse(responseCode = "204", description = "Successfully cleared the sub queue(s) with the provided key, or all sub queues if the key is null.")
fun deleteKeys(@Parameter(`in` = ParameterIn.QUERY, required = false, description = "The queue type to clear the sub queue of. If it is not provided, all sub queues will be cleared.")
@RequestParam(required = false, name = RestParameters.QUEUE_TYPE) queueType: String?): ResponseEntity<Void>
{
if (queueType != null)
{
messageQueue.clearForType(queueType)
}
else
{
messageQueue.clear()
}
return ResponseEntity.noContent().build()
}

/**
* A [GetMapping] endpoint which retrieves all the stored [QueueMessage]s that are currently available in the [MultiQueue].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -861,6 +861,9 @@ class MessageQueueControllerTest
.andReturn()
}

/**
* Ensure that the [CorrelationIdFilter] will generate a random Correlation ID when one is not provided and that it is returned in the [MessageResponse].
*/
@Test
fun testCorrelationId_randomIdOnSuccess()
{
Expand All @@ -877,6 +880,9 @@ class MessageQueueControllerTest
Assertions.assertEquals(UUID.fromString(messageResponse.correlationId).toString(), messageResponse.correlationId)
}

/**
* Ensure that the [CorrelationIdFilter] will use the same correlationID that is provided is used and returned in the [MessageResponse].
*/
@Test
fun testCorrelationId_providedId()
{
Expand All @@ -894,6 +900,9 @@ class MessageQueueControllerTest
Assertions.assertEquals(correlationId, messageResponse.correlationId)
}

/**
* Ensure that the [CorrelationIdFilter] will generate a random Correlation ID is generated on error and returned in the response.
*/
@Test
fun testCorrelationId_randomIdOnError()
{
Expand All @@ -916,6 +925,61 @@ class MessageQueueControllerTest
Assertions.assertEquals(correlationId, UUID.fromString(correlationId as String).toString())
}

/**
* Ensure that [MessageQueueController.deleteKeys] will only delete keys by the specified [RestParameters.QUEUE_TYPE] when it is provided and that other sub queues are not cleared.
*/
@Test
fun testDeleteKeys_singleKey()
{
val subQueue1 = "testDeleteKeys_singleKey1"
var message = createQueueMessage(subQueue1)
Assertions.assertTrue(multiQueue.add(message))
message = createQueueMessage(subQueue1)
Assertions.assertTrue(multiQueue.add(message))
message = createQueueMessage(subQueue1)
Assertions.assertTrue(multiQueue.add(message))

Assertions.assertEquals(3, multiQueue.size)

val subQueue2 = "testDeleteKeys_singleKey2"
message = createQueueMessage(subQueue2)
Assertions.assertTrue(multiQueue.add(message))
message = createQueueMessage(subQueue2)
Assertions.assertTrue(multiQueue.add(message))

Assertions.assertEquals(5, multiQueue.size)

Assertions.assertTrue(multiQueue.keys().contains(subQueue1))
Assertions.assertTrue(multiQueue.keys().contains(subQueue2))

mockMvc.perform(delete(MessageQueueController.MESSAGE_QUEUE_BASE_PATH + "/" + MessageQueueController.ENDPOINT_KEYS)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.param(RestParameters.QUEUE_TYPE, subQueue1))
.andExpect(MockMvcResultMatchers.status().isNoContent)

Assertions.assertEquals(2, multiQueue.size)
Assertions.assertFalse(multiQueue.keys().contains(subQueue1))
Assertions.assertTrue(multiQueue.keys().contains(subQueue2))
}

/**
* Ensure that [MessageQueueController.deleteKeys] will only delete all keys/queues when the provided [RestParameters.QUEUE_TYPE] is `null`.
*/
@Test
fun testDeleteKeys_allKeys()
{
val (messages, types) = initialiseMapWithEntries()
Assertions.assertEquals(messages.size, multiQueue.size)
types.forEach { type -> Assertions.assertTrue(multiQueue.keys().contains(type)) }

mockMvc.perform(delete(MessageQueueController.MESSAGE_QUEUE_BASE_PATH + "/" + MessageQueueController.ENDPOINT_KEYS)
.contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(MockMvcResultMatchers.status().isNoContent)

Assertions.assertTrue(multiQueue.isEmpty())
types.forEach { type -> Assertions.assertFalse(multiQueue.keys().contains(type)) }
}

/**
* A helper method which creates `4` [QueueMessage] objects and inserts them into the [MultiQueue].
*
Expand Down

0 comments on commit 3b7c325

Please sign in to comment.