@Test
void handlesMultipleSsldPendingFirmwareUpdatesWithoutFailure() throws Exception {

    final String deviceIdentification = "Test-SSLD-1";
    final Ssld ssld = new Ssld(deviceIdentification);
    final String correlationUidMostRecentPendingFirmwareUpdate = "correlation-uid-most-recent";
    final long mostRecentCreationMillis = System.currentTimeMillis();
    final SsldPendingFirmwareUpdate olderPendingFirmwareUpdate1 = this.anSsldPendingFirmwareUpdate(134562345L,
            new Date(mostRecentCreationMillis - 3_000_000_000L), deviceIdentification, "correlation-uid-1");
    final SsldPendingFirmwareUpdate olderPendingFirmwareUpdate2 = this.anSsldPendingFirmwareUpdate(227587L,
            new Date(mostRecentCreationMillis - 604_800_000L), deviceIdentification, "correlation-uid-2");
    final SsldPendingFirmwareUpdate olderPendingFirmwareUpdate3 = this.anSsldPendingFirmwareUpdate(308943152L,
            new Date(mostRecentCreationMillis - 123L), deviceIdentification, "correlation-uid-3");
    final SsldPendingFirmwareUpdate mostRecentPendingFirmwareUpdate = this.anSsldPendingFirmwareUpdate(4459483L,
            new Date(mostRecentCreationMillis), deviceIdentification,
            correlationUidMostRecentPendingFirmwareUpdate);
    final Organisation organisation = new Organisation(
            mostRecentPendingFirmwareUpdate.getOrganisationIdentification(), "Organisation", "ORG",
            PlatformFunctionGroup.USER);
    when(this.ssldPendingFirmwareUpdateRepository.findByDeviceIdentification(deviceIdentification))
            .thenReturn(Arrays.asList(olderPendingFirmwareUpdate1, olderPendingFirmwareUpdate2,
                    mostRecentPendingFirmwareUpdate, olderPendingFirmwareUpdate3));
    when(this.deviceDomainService.searchActiveDevice(eq(deviceIdentification), any(ComponentType.class)))
            .thenReturn(ssld);
    when(this.organisationDomainService.searchOrganisation(organisation.getOrganisationIdentification()))
            .thenReturn(organisation);
    this.firmwareManagementService.handleSsldPendingFirmwareUpdate(deviceIdentification);
    /*
     * Verify the older pending firmware updates are deleted. This appears
     * to be a reasonable way to deal with multiple records being present.
     * The most recent pending update should not be deleted at this point,
     * as it is important for the way the firmware version response will be
     * treated later-on in a more complete firmware update scenario than the
     * fragment seen here in this unit test.
     *
     * The check is here to confirm the code works as it was meant to be
     * implemented. Not so much as a definitive specification as how it
     * should work.
     */
    final ArgumentCaptor<SsldPendingFirmwareUpdate> pendingUpdateCaptor = ArgumentCaptor
            .forClass(SsldPendingFirmwareUpdate.class);
    verify(this.ssldPendingFirmwareUpdateRepository, atLeastOnce()).delete(pendingUpdateCaptor.capture());
    final List<SsldPendingFirmwareUpdate> deletedPendingUpdates = pendingUpdateCaptor.getAllValues();
    assertThat(deletedPendingUpdates).containsExactlyInAnyOrder(olderPendingFirmwareUpdate1,
            olderPendingFirmwareUpdate2, olderPendingFirmwareUpdate3);
    /*
     * Check that a get firmware version message is sent for the most recent
     * pending firmware update.
     */
    this.assertFirmwareVersionRequested(organisation.getOrganisationIdentification(), deviceIdentification,
            correlationUidMostRecentPendingFirmwareUpdate);
}