Skip to content

Commit

Permalink
GH-458 Cover FailureFacade with unit test and fix joinToString method…
Browse files Browse the repository at this point in the history
…s usage
  • Loading branch information
dzikoysk committed Aug 14, 2021
1 parent a8d823d commit c8fd2dc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,8 @@ package com.reposilite.failure

import net.dzikoysk.dynamiclogger.Journalist
import net.dzikoysk.dynamiclogger.Logger
import panda.std.function.ThrowingRunnable
import panda.utilities.ArrayUtils
import panda.utilities.StringUtils
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ExecutorService

class FailureFacade internal constructor(private val journalist: Journalist) : Journalist {

Expand All @@ -32,13 +29,14 @@ class FailureFacade internal constructor(private val journalist: Journalist) : J
logger.error(id)
logger.exception(throwable)

exceptions
.add(arrayOf(
exceptions.add(
arrayOf(
"failure $id",
throwException(throwable)
)
.joinToString { System.lineSeparator() }
.trim())
.joinToString(separator = System.lineSeparator())
.trim()
)
}

private fun throwException(throwable: Throwable?): String {
Expand All @@ -48,27 +46,9 @@ class FailureFacade internal constructor(private val journalist: Journalist) : J

return arrayOf(
" by ${throwable.javaClass.simpleName}: ${throwable.message}",
" at " + ArrayUtils.get(throwable.stackTrace, 0)
.map { it.toString() }
.orElseGet("<unknown stacktrace>"),
" at " + (throwable.stackTrace.getOrNull(0) ?: "<unknown stacktrace>"),
throwException(throwable.cause)
).joinToString { System.lineSeparator() }
}

fun <E : Exception?> ofChecked(runnable: ThrowingRunnable<E>): Runnable {
return Runnable { run(runnable) }
}

fun <E : Exception?> executeChecked(service: ExecutorService, runnable: ThrowingRunnable<E>) {
service.execute { run(runnable) }
}

private fun run(runnable: ThrowingRunnable<*>) {
try {
runnable.run()
} catch (exception: Exception) {
throwException("Exception occurred during the task execution", exception)
}
).joinToString(separator = System.lineSeparator())
}

fun hasFailures() =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.reposilite.failure

import net.dzikoysk.dynamiclogger.backend.InMemoryLogger
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test

internal class FailureFacadeTest {

private val failureFacade = FailureFacade(InMemoryLogger())

@Test
fun `should store failure`() {
// given: an exception with a message
val message = "Unlucky"
val exception = RuntimeException(message)

// when: an error has been registered in failure facade
failureFacade.throwException("PATH /com/reposilite", exception)

// then: service properly registered thrown exception
assertTrue(failureFacade.hasFailures())
assertTrue(failureFacade.getFailures().iterator().next().contains(message))
}

}

0 comments on commit c8fd2dc

Please sign in to comment.