Skip to content

Commit

Permalink
FDP-2638: merge main formatted
Browse files Browse the repository at this point in the history
Signed-off-by: Loes Immens <[email protected]>
  • Loading branch information
loesimmens committed Oct 15, 2024
1 parent aba6429 commit c7b4ecf
Show file tree
Hide file tree
Showing 17 changed files with 33 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice
Expand Down Expand Up @@ -45,8 +45,7 @@ class WebIntegrationTest {
fun firmwareFileUploadTest() {
// arrange
val testFile = ClassPathResource(name).file
val consumer =
createKafkaConsumer(embeddedKafkaBroker, kafkaProducerProperties.firmware.topic)
val consumer = createKafkaConsumer(embeddedKafkaBroker, kafkaProducerProperties.firmware.topic)

// act
val response = uploadFile(testFile)
Expand All @@ -61,8 +60,7 @@ class WebIntegrationTest {
}

fun uploadFile(file: File): ResponseEntity<String> {
val headers: HttpHeaders =
HttpHeaders().apply { contentType = MediaType.MULTIPART_FORM_DATA }
val headers: HttpHeaders = HttpHeaders().apply { contentType = MediaType.MULTIPART_FORM_DATA }

val body: LinkedMultiValueMap<String, Any> =
LinkedMultiValueMap<String, Any>().apply { add("file", FileSystemResource(file)) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.config
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.config
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.config

import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties(prefix = "config.web-server")
data class WebServerProperties(val port: Int)
@ConfigurationProperties(prefix = "config.web-server") data class WebServerProperties(val port: Int)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.controller
Expand All @@ -19,17 +19,11 @@ import org.springframework.web.bind.annotation.RestController

@RestController
@RequestMapping("/web/api/firmware")
class FirmwareController(
val firmwareService: FirmwareService,
val firmwareProducerService: FirmwareProducerService
) {
class FirmwareController(val firmwareService: FirmwareService, val firmwareProducerService: FirmwareProducerService) {
private val logger = KotlinLogging.logger {}

@PostMapping("/{name}")
fun post(
@NonNull @PathVariable name: String,
@NonNull @RequestBody firmware: FirmwareDTO
): ResponseEntity<String> {
fun post(@NonNull @PathVariable name: String, @NonNull @RequestBody firmware: FirmwareDTO): ResponseEntity<String> {
try {
logger.debug { "Processing firmware file with name $name" }
val firmwares = firmwareService.processFirmware(firmware)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.service
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.firmware.dto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.firmware.exception
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.firmware.mapper
Expand Down Expand Up @@ -32,8 +32,7 @@ class FirmwareMapper(private val firmwareRepository: FirmwareRepository) {
return firmware
}

private fun getFirmwareVersionFromName(name: String) =
name.substringAfter("#TO#").substringBefore(".txt")
private fun getFirmwareVersionFromName(name: String) = name.substringAfter("#TO#").substringBefore(".txt")

private fun getPreviousFirmwareIdFromName(name: String): UUID? {
if (!name.contains("#FROM#")) {
Expand All @@ -44,16 +43,14 @@ class FirmwareMapper(private val firmwareRepository: FirmwareRepository) {
if (previousFirmware != null) {
return previousFirmware.id
} else {
throw FirmwareException(
"Previous firmware with version $previousFirmwareVersion does not exist")
throw FirmwareException("Previous firmware with version $previousFirmwareVersion does not exist")
}
}

private fun mapLinesToPackets(dtoPackets: List<String>, firmware: Firmware) =
dtoPackets.mapIndexed { index, line -> mapLineToPacket(index, line, firmware) }

private fun mapLineToPacket(index: Int, line: String, firmware: Firmware) =
FirmwarePacket(firmware, index, line)
private fun mapLineToPacket(index: Int, line: String, firmware: Firmware) = FirmwarePacket(firmware, index, line)

fun mapEntitiesToFirmwares(firmwareEntities: List<Firmware>): Firmwares {
val firmwares = firmwareEntities.map { firmware -> mapEntityToSchema(firmware) }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.firmware.service
Expand All @@ -12,6 +12,5 @@ class FirmwareServiceTest {
private val firmwareRepository = mock<FirmwareRepository>()
private val firmwarePacketRepository = mock<FirmwarePacketRepository>()
private val firmwareMapper = mock<FirmwareMapper>()
private val firmwareService =
FirmwareService(firmwareRepository, firmwarePacketRepository, firmwareMapper)
private val firmwareService = FirmwareService(firmwareRepository, firmwarePacketRepository, firmwareMapper)
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.firmware.mapper
Expand Down Expand Up @@ -28,8 +28,7 @@ class FirmwareMapperTest {
@Test
fun mapFirmwareDTOToEntity() {
val firmwareDTO = testFirmwareDTO()
val previousFirmware =
Firmware(previousUUID, name, previousVersion, UUID.randomUUID(), mutableListOf())
val previousFirmware = Firmware(previousUUID, name, previousVersion, UUID.randomUUID(), mutableListOf())
whenever(firmwareRepository.findByVersion(previousVersion)).thenReturn(previousFirmware)

val result = firmwareMapper.mapFirmwareDTOToEntity(firmwareDTO)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.http
Expand All @@ -22,15 +22,11 @@ class HttpClient(private val webClient: RestClient) {

@Throws(HttpClientErrorException::class, HttpServerErrorException::class)
fun postFirmware(firmware: FirmwareWebDTO): ResponseEntity<String> {
logger.debug {
"Posting firmware with name ${firmware.name} and ${firmware.packets.size} packets"
}
logger.debug { "Posting firmware with name ${firmware.name} and ${firmware.packets.size} packets" }

try {
val response = executeFirmwareRequest(firmware)
logger.debug {
"Posted message with name ${firmware.name}, resulting response: $response"
}
logger.debug { "Posted message with name ${firmware.name}, resulting response: $response" }
return response
} catch (e: Exception) {
logger.warn(e) { "Error received while posting message with name ${firmware.name}" }
Expand All @@ -40,10 +36,5 @@ class HttpClient(private val webClient: RestClient) {

@Throws(HttpClientErrorException::class, HttpServerErrorException::class)
private fun executeFirmwareRequest(firmware: FirmwareWebDTO): ResponseEntity<String> =
webClient
.post()
.uri("$FIRMWARE_API/${firmware.name}")
.body(firmware)
.retrieve()
.toEntity<String>()
webClient.post().uri("$FIRMWARE_API/${firmware.name}").body(firmware).retrieve().toEntity<String>()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.http.configuration
Expand Down Expand Up @@ -32,6 +32,5 @@ class WebClientConfiguration(private val httpProps: HttpProperties) {
}
.build()

private fun requestFactory() =
JdkClientHttpRequestFactory().apply { setReadTimeout(httpProps.connectionTimeout) }
private fun requestFactory() = JdkClientHttpRequestFactory().apply { setReadTimeout(httpProps.connectionTimeout) }
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.http.configuration.properties
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.web
Expand All @@ -21,10 +21,7 @@ class FirmwareWebController(val firmwareWebService: FirmwareWebService) {
}

@PostMapping("/web/firmware")
fun handleFileUpload(
@RequestPart("file") file: MultipartFile,
redirectAttributes: RedirectAttributes
): String {
fun handleFileUpload(@RequestPart("file") file: MultipartFile, redirectAttributes: RedirectAttributes): String {
if (file.originalFilename.isNullOrEmpty()) {
redirectAttributes.setMessage("No file provided")
return redirectUrl
Expand All @@ -38,8 +35,7 @@ class FirmwareWebController(val firmwareWebService: FirmwareWebService) {
}
try {
val processedPackets = firmwareWebService.processFirmwareFile(file)
redirectAttributes.setMessage(
"Successfully processed $processedPackets firmware packets")
redirectAttributes.setMessage("Successfully processed $processedPackets firmware packets")
} catch (e: Exception) {
redirectAttributes.setMessage("Failed to process file: ${e.message}")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.web
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
// SPDX-FileCopyrightText: Copyright Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0
package org.gxf.crestdeviceservice.web
Expand Down

0 comments on commit c7b4ecf

Please sign in to comment.