FirmwareManagementService extends AbstractService { public void handleSsldPendingFirmwareUpdate(final String deviceIdentification) { final List<SsldPendingFirmwareUpdate> ssldPendingFirmwareUpdates = this.ssldPendingFirmwareUpdateRepository .findByDeviceIdentification(deviceIdentification); if (CollectionUtils.isEmpty(ssldPendingFirmwareUpdates)) { return; } final SsldPendingFirmwareUpdate ssldPendingFirmwareUpdate; if (ssldPendingFirmwareUpdates.size() == 1) { ssldPendingFirmwareUpdate = ssldPendingFirmwareUpdates.get(0); } else { LOGGER.warn("Found multiple pending firmware update records for SSLD: {}", ssldPendingFirmwareUpdates); ssldPendingFirmwareUpdate = this.getMostRecentSsldPendingFirmwareUpdate(ssldPendingFirmwareUpdates) .orElseThrow( () -> new AssertionError("No most recent pending firmware update from a non-empty list")); this.deleteOutdatedSsldPendingFirmwareUpdates(ssldPendingFirmwareUpdates, ssldPendingFirmwareUpdate); } final String organisationIdentification = ssldPendingFirmwareUpdate.getOrganisationIdentification(); final String correlationUid = ssldPendingFirmwareUpdate.getCorrelationUid(); LOGGER.info( "Handling SSLD pending firmware update for device identification: {}, organisation identification: {} and correlation UID: {}.", deviceIdentification, organisationIdentification, correlationUid); try { final int messagePriority = MessagePriorityEnum.DEFAULT.getPriority(); this.getFirmwareVersion(organisationIdentification, deviceIdentification, correlationUid, DeviceFunction.GET_FIRMWARE_VERSION.name(), messagePriority, this.getFirmwareVersionDelay); } catch (final FunctionalException e) { LOGGER.error("Caught exception when calling get firmware version", e); } } FirmwareManagementService(); void updateFirmware(final CorrelationIds ids,
            final FirmwareUpdateMessageDataContainer firmwareUpdateMessageDataContainer, final Long scheduleTime,
            final String messageType, final int messagePriority); void handleSsldPendingFirmwareUpdate(final String deviceIdentification); void getFirmwareVersion(@Identification final String organisationIdentification,
            @Identification final String deviceIdentification, final String correlationUid, final String messageType,
            final int messagePriority); void getFirmwareVersion(@Identification final String organisationIdentification,
            @Identification final String deviceIdentification, final String correlationUid, final String messageType,
            final int messagePriority, final Long delay); void handleGetFirmwareVersionResponse(final List<FirmwareVersionDto> firmwareVersionsDto,
            final CorrelationIds ids, final String messageType, final int messagePriority,
            final ResponseMessageResultType deviceResult, final OsgpException exception); List<FirmwareVersion> checkFirmwareHistoryForVersion(final String deviceId,
            final List<FirmwareVersion> firmwareVersions); void tryToAddDeviceFirmwareFile(final String deviceIdentification,
            final List<FirmwareVersion> firmwareVersionsNotCurrent); void switchFirmware(final String organisationIdentification, final String deviceIdentification,
            final String correlationUid, final String messageType, final int messagePriority, final String version);  }
@Test void handlesZeroSsldPendingFirmwareUpdatesDoingNothing() { final String deviceIdentification = "Test-SSLD-1"; when(this.ssldPendingFirmwareUpdateRepository.findByDeviceIdentification(deviceIdentification)) .thenReturn(Collections.emptyList()); this.firmwareManagementService.handleSsldPendingFirmwareUpdate(deviceIdentification); verify(this.ssldPendingFirmwareUpdateRepository).findByDeviceIdentification(deviceIdentification); verifyNoMoreInteractions(this.ssldPendingFirmwareUpdateRepository); verifyNoInteractions(this.osgpCoreRequestMessageSender); }
@Test void handlesOneSsldPendingFirmwareUpdateRetrievingFirmwareVersion() throws Exception { final String deviceIdentification = "Test-SSLD-1"; final Ssld ssld = new Ssld(deviceIdentification); final String correlationUid = "correlation-uid-pending-firmware-update"; final SsldPendingFirmwareUpdate ssldPendingFirmwareUpdate = this.anSsldPendingFirmwareUpdate(1L, new Date(), deviceIdentification, correlationUid); final Organisation organisation = new Organisation(ssldPendingFirmwareUpdate.getOrganisationIdentification(), "Organisation", "ORG", PlatformFunctionGroup.USER); when(this.ssldPendingFirmwareUpdateRepository.findByDeviceIdentification(deviceIdentification)) .thenReturn(Collections.singletonList(ssldPendingFirmwareUpdate)); when(this.deviceDomainService.searchActiveDevice(eq(deviceIdentification), any(ComponentType.class))) .thenReturn(ssld); when(this.organisationDomainService.searchOrganisation(organisation.getOrganisationIdentification())) .thenReturn(organisation); this.firmwareManagementService.handleSsldPendingFirmwareUpdate(deviceIdentification); verify(this.ssldPendingFirmwareUpdateRepository, never()).delete(ssldPendingFirmwareUpdate); this.assertFirmwareVersionRequested(organisation.getOrganisationIdentification(), deviceIdentification, correlationUid); }
@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); 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); this.assertFirmwareVersionRequested(organisation.getOrganisationIdentification(), deviceIdentification, correlationUidMostRecentPendingFirmwareUpdate); }