Skip to content

Commit

Permalink
pin ktlint to 1.5.0
Browse files Browse the repository at this point in the history
verinice-veo#1116
  • Loading branch information
jj-sn committed Dec 5, 2024
1 parent 57bb2c2 commit bdacc34
Show file tree
Hide file tree
Showing 23 changed files with 86 additions and 68 deletions.
14 changes: 8 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ tasks.register("restTest", Test::class.java) {
includeTestsMatching("org.veo.accounts.rest.*")
}

inputs.property("veoAccountsBaseUrl") {
System.getenv("VEO_ACCOUNTS_RESTTEST_BASEURL")
}.optional(true)
inputs
.property("veoAccountsBaseUrl") {
System.getenv("VEO_ACCOUNTS_RESTTEST_BASEURL")
}.optional(true)

systemProperties(
System.getProperties().mapKeys { it.key as String }.filterKeys {
Expand All @@ -128,10 +129,10 @@ spotless {
replaceRegex("Excessive line breaks", "\n{3,}", "\n\n")
}
kotlin {
ktlint()
ktlint("1.5.0")
}
kotlinGradle {
ktlint()
ktlint("1.5.0")
}
json {
target("**/*.json")
Expand All @@ -144,7 +145,8 @@ spotless {
file: File,
): String {
val om = ObjectMapper()
return om.writer()
return om
.writer()
.with(DefaultPrettyPrinter().apply { indentArraysWith(SYSTEM_LINEFEED_INSTANCE) })
.writeValueAsString(om.readValue(rawUnix, Map::class.java))
}
Expand Down
4 changes: 1 addition & 3 deletions src/main/kotlin/org/veo/accounts/ExceptionHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class ExceptionHandler {
private fun handle(
message: String?,
status: HttpStatus,
): ResponseEntity<String> {
return ResponseEntity<String>(message, status)
}
): ResponseEntity<String> = ResponseEntity<String>(message, status)

private fun getParsingErrorMessage(ex: HttpMessageNotReadableException): String? =
ex.cause
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/org/veo/accounts/Role.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*/
package org.veo.accounts

enum class Role(val roleName: String) {
enum class Role(
val roleName: String,
) {
CREATE("account:create"),
READ("account:read"),
UPDATE("account:update"),
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/org/veo/accounts/Validate.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ fun Any.validate() =
}
}

class ValidationException(message: String) : Exception(message) {
class ValidationException(
message: String,
) : Exception(message) {
constructor(violations: Collection<ConstraintViolation<Any>>) : this(
violations.joinToString("; ") {
"Invalid ${it.rootBeanClass.simpleName}: ${it.message}"
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/org/veo/accounts/WebSecurity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ class WebSecurity(
jwtAuthenticationConverter =
JwtAuthenticationConverter().apply {
setJwtGrantedAuthoritiesConverter { jwt ->
jwt.getClaimAsMap("resource_access")
jwt
.getClaimAsMap("resource_access")
?.get(keycloakServiceClientName)
?.let { it as Map<*, *> }
?.get("roles")
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/org/veo/accounts/auth/ParseAccount.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,7 @@ private fun Authentication.getVeoClient(): VeoClientId =

private fun Authentication.token(): Jwt = (this as JwtAuthenticationToken).token

private data class AuthenticatedAccountImpl(override val id: AccountId, override val veoClient: VeoClientId) : AuthenticatedAccount
private data class AuthenticatedAccountImpl(
override val id: AccountId,
override val veoClient: VeoClientId,
) : AuthenticatedAccount
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ package org.veo.accounts.dtos.response

import org.veo.accounts.dtos.AccountId

class AccountCreatedDto(val id: AccountId)
class AccountCreatedDto(
val id: AccountId,
)
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,7 @@ import org.springframework.http.HttpStatus
* Supertype for exceptions which are mapped with an HTTP status code. Messages are supposed to be presented to the
* HTTP client, so this exception should not contain confidential information.
*/
abstract class AbstractMappedException(msg: String, val status: HttpStatus) : Exception(msg)
abstract class AbstractMappedException(
msg: String,
val status: HttpStatus,
) : Exception(msg)
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ package org.veo.accounts.exceptions

import org.springframework.http.HttpStatus.CONFLICT

class ConflictException(msg: String) : AbstractMappedException(msg, CONFLICT)
class ConflictException(
msg: String,
) : AbstractMappedException(msg, CONFLICT)
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ package org.veo.accounts.exceptions

import org.springframework.http.HttpStatus.FORBIDDEN

class ExceedingMaxUsersException(maxUsers: Int) :
AbstractMappedException(
class ExceedingMaxUsersException(
maxUsers: Int,
) : AbstractMappedException(
"Your veo license only allows up to $maxUsers enabled account(s)",
FORBIDDEN,
)
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ package org.veo.accounts.exceptions

import org.springframework.http.HttpStatus.FORBIDDEN

class ForbiddenOperationException(msg: String) : AbstractMappedException(msg, FORBIDDEN)
class ForbiddenOperationException(
msg: String,
) : AbstractMappedException(msg, FORBIDDEN)
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ package org.veo.accounts.exceptions

import org.springframework.http.HttpStatus.NOT_FOUND

class ResourceNotFoundException(message: String = "Resource not found") : AbstractMappedException(message, NOT_FOUND)
class ResourceNotFoundException(
message: String = "Resource not found",
) : AbstractMappedException(message, NOT_FOUND)
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ package org.veo.accounts.exceptions

import org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY

class UnprocessableDtoException(msg: String) : AbstractMappedException(msg, UNPROCESSABLE_ENTITY)
class UnprocessableDtoException(
msg: String,
) : AbstractMappedException(msg, UNPROCESSABLE_ENTITY)
33 changes: 13 additions & 20 deletions src/main/kotlin/org/veo/accounts/keycloak/AccountService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ class AccountService(
facade.perform {
// Self-management is not supported
if (id == authAccount.id) throw ResourceNotFoundException()
users().get(id.toString())
users()
.get(id.toString())
.let { userResource ->
try {
userResource.toRepresentation()
} catch (_: NotFoundException) {
throw ResourceNotFoundException()
}
}
.also { loadGroups(it) }
}.also { loadGroups(it) }
.apply { if (!groups.contains(authAccount.veoClient.groupName)) throw ResourceNotFoundException() }
}

Expand Down Expand Up @@ -108,8 +108,7 @@ class AccountService(
if (enabled.value) {
checkMaxUsersNotExhausted(authAccount)
}
}
.let { dtoToUser(it, authAccount) }
}.let { dtoToUser(it, authAccount) }
.also { log.info { "Creating new account ${it.username} in ${authAccount.veoClient}" } }
.let { createAccount(it) }
}
Expand All @@ -133,8 +132,7 @@ class AccountService(
if (!isEnabled && dto.enabled.value) {
checkMaxUsersNotExhausted(authAccount)
}
}
.also { log.info { "Updating account ${it.username} in ${authAccount.veoClient}" } }
}.also { log.info { "Updating account ${it.username} in ${authAccount.veoClient}" } }
.apply { update(dto) }
.also {
try {
Expand All @@ -145,20 +143,18 @@ class AccountService(
}
throw ex
}
}
.also { user ->
}.also { user ->
dto.groups
.groupNames
.filter { !user.groups.contains(it) }
.forEach { users().get(user.id).joinGroup(getGroupId(it)) }
}
.also { user ->
AssignableGroupSet.byGroupNames(user.groups)
}.also { user ->
AssignableGroupSet
.byGroupNames(user.groups)
.values
.filter { !dto.groups.values.contains(it) }
.forEach { users().get(user.id).leaveGroup(getGroupId(it.groupName)) }
}
.run { if (!isEmailVerified) sendEmail(id.toString()) }
}.run { if (!isEmailVerified) sendEmail(id.toString()) }
}

fun deleteAccount(
Expand Down Expand Up @@ -195,8 +191,7 @@ class AccountService(
name = client.groupName
singleAttribute("maxUnits", maxUnits.toString())
singleAttribute("maxUsers", maxUsers.toString())
}
.let { groups().add(it) }
}.let { groups().add(it) }
.run {
if (!HttpStatusCode.valueOf(status).is2xxSuccessful) {
log.error { "Failed to create veo client group $client, unexpected status code $status" }
Expand Down Expand Up @@ -227,8 +222,7 @@ class AccountService(
.apply {
maxUnits?.let { singleAttribute("maxUnits", it.toString()) }
maxUsers?.let { singleAttribute("maxUsers", it.toString()) }
}
.let { groups().group(it.id).update(it) }
}.let { groups().group(it.id).update(it) }
}

fun deleteClient(client: VeoClientId) =
Expand Down Expand Up @@ -364,8 +358,7 @@ class AccountService(
.also { log.debug { "Determined email actions for user $accountId: $it" } }
.also { log.debug { "Mailing keycloak client: $userAuthKeycloakClient" } }
.also { log.debug { "Mailing actions redirect URL: $mailActionsRedirectUrl" } }
.let {
actions ->
.let { actions ->
if (mailingEnabled) users().get(accountId).executeActionsEmail(userAuthKeycloakClient, mailActionsRedirectUrl, actions)
}

Expand Down
6 changes: 5 additions & 1 deletion src/main/kotlin/org/veo/accounts/messaging/Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@
*/
package org.veo.accounts.messaging

class Message(val routingKey: String, val id: Long, val content: String)
class Message(
val routingKey: String,
val id: Long,
val content: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ class MessageSubscriber(

private fun handleClientChange(content: JsonNode) {
val client =
content.get("clientId")
content
.get("clientId")
.asText()
.let { UUID.fromString(it) }
.let { VeoClientId(it) }
Expand Down
12 changes: 3 additions & 9 deletions src/test/kotlin/org/veo/accounts/CastingFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,8 @@ package org.veo.accounts

// Convenience functions for casting things in deserialized JSON response bodies.

fun Any?.asMap(): MutableMap<String, Any> {
return this as MutableMap<String, Any>
}
fun Any?.asMap(): MutableMap<String, Any> = this as MutableMap<String, Any>

fun Any?.asNestedMap(): MutableMap<String, MutableMap<String, Any>> {
return this as MutableMap<String, MutableMap<String, Any>>
}
fun Any?.asNestedMap(): MutableMap<String, MutableMap<String, Any>> = this as MutableMap<String, MutableMap<String, Any>>

fun Any?.asListOfMaps(): MutableList<MutableMap<String, Any>> {
return this as MutableList<MutableMap<String, Any>>
}
fun Any?.asListOfMaps(): MutableList<MutableMap<String, Any>> = this as MutableList<MutableMap<String, Any>>
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ class TestAccountService(
isEnabled = true
isEmailVerified = true
groups = listOf(group.path)
}
.let { users().create(it) }
}.let { users().create(it) }
.getHeaderString("Location")
.substringAfterLast('/')
.also { assignRoles(it, roles) }
Expand Down Expand Up @@ -94,7 +93,8 @@ class TestAccountService(

fun getUsername(accountId: String): String =
facade.perform {
users().get(accountId)
users()
.get(accountId)
.toRepresentation()
.username
}
Expand All @@ -113,7 +113,8 @@ class TestAccountService(
private fun RealmResource.assignRoles(
accountId: String,
roles: List<Role>,
) = users().get(accountId)
) = users()
.get(accountId)
.roles()
.clientLevel(clientId)
.apply { add(listAvailable().filter { roles.map(Role::roleName).contains(it.name) }) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class MessageSubscriberTest {

@Test
fun `listeners don't return anything`() {
MessageSubscriber::class.functions
MessageSubscriber::class
.functions
.filter { it.annotations.filterIsInstance<RabbitListener>().isNotEmpty() }
.forEach { it.returnType.toStr() shouldBe "kotlin.Unit" }
}
Expand Down
6 changes: 3 additions & 3 deletions src/test/kotlin/org/veo/accounts/rest/AbstractRestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,7 @@ abstract class AbstractRestTest {
"type" to "CREATION",
),
) { findGroup(groupName) shouldNotBe null }
}
.also { createdVeoClients.add(it) }
}.also { createdVeoClients.add(it) }

protected fun createManager(
group: VeoClientId,
Expand Down Expand Up @@ -230,7 +229,8 @@ abstract class AbstractRestTest {
headers: Map<String, List<String>> = emptyMap(),
expectedStatus: Int?,
): Response =
testRestTemplate.exchange(buildUrl(uri), method, buildHttpEntity(body, headers, authAccountId), String::class.java)
testRestTemplate
.exchange(buildUrl(uri), method, buildHttpEntity(body, headers, authAccountId), String::class.java)
.apply { expectedStatus?.let { statusCode.value() shouldBe it } }
.let { Response(it) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class AccountManagementRestTest : AbstractRestTest() {
}

// and found in the list
get("/", managerId).bodyAsListOfMaps
get("/", managerId)
.bodyAsListOfMaps
.first { it["id"] == accountId }
.also { hans ->
hans["username"] shouldBe "$prefix-hans"
Expand Down
9 changes: 4 additions & 5 deletions src/test/kotlin/org/veo/accounts/rest/MaxUsersRestTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,7 @@ class MaxUsersRestTest : AbstractRestTest() {
else -> throw IllegalStateException()
}
}
}
.forEach { it.await() }
}.forEach { it.await() }

// then the maximum amount of enabled accounts has not been exceeded
failedAttempts shouldBe 18
Expand Down Expand Up @@ -175,12 +174,12 @@ class MaxUsersRestTest : AbstractRestTest() {
else -> throw IllegalStateException()
}
}
}
.forEach { it.await() }
}.forEach { it.await() }

// then the maximum amount of enabled accounts has not been exceeded
failedAttempts shouldBe 18
get("/", managerId).bodyAsListOfMaps
get("/", managerId)
.bodyAsListOfMaps
.filter { it["enabled"] == true }
.size shouldBe 2
}
Expand Down
6 changes: 4 additions & 2 deletions src/test/kotlin/org/veo/accounts/rest/TestAuthenticator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ internal class TestAuthenticator(
password: String,
): String =
userTokenCache.computeIfAbsent(username) {
HttpClientBuilder.create()
HttpClientBuilder
.create()
.apply { proxyHost?.let { setProxy(HttpHost(it, proxyPort)) } }
.build()
.use {
AuthzClient.create(Configuration(keycloakUrl, realm, clientName, mapOf("secret" to clientSecret), it))
AuthzClient
.create(Configuration(keycloakUrl, realm, clientName, mapOf("secret" to clientSecret), it))
.obtainAccessToken(username, password)
.token
}
Expand Down

0 comments on commit bdacc34

Please sign in to comment.