Skip to content

Commit

Permalink
fixes initialization of PdfTextTool in pdf-text-spring-boot-starter (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
codyfrehr committed Apr 21, 2024
1 parent 62788e1 commit 52bf568
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package io.xpdf.api.common.util
import io.kotest.assertions.throwables.shouldThrowWithMessage
import io.kotest.matchers.shouldBe
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.unmockkAll
import io.xpdf.api.common.exception.XpdfRuntimeException
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
Expand All @@ -41,7 +41,7 @@ class XpdfUtilsTest {

@AfterEach
fun afterEach() {
unmockkStatic(XpdfUtils::class)
unmockkAll()

// reset system properties that test may have altered
System.setProperty("java.io.tmpdir", javaTmpDir)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,12 @@
package io.xpdf.api.pdftext.autoconfigure;

import io.xpdf.api.pdftext.PdfTextTool;
import io.xpdf.api.pdftext.util.PdfTextUtils;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

import java.nio.file.Path;

/**
* An autoconfiguration for {@link PdfTextTool} with configuration properties defined in {@link PdfTextToolProperties}.
*
Expand All @@ -39,17 +36,9 @@ public class PdfTextToolAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public PdfTextTool pdfTextTool(PdfTextToolProperties pdfTextToolProperties) {
Path executablePath = pdfTextToolProperties.getExecutablePath() == null
? PdfTextUtils.getPdfTextExecutablePath()
: pdfTextToolProperties.getExecutablePath();

Integer timeoutSeconds = pdfTextToolProperties.getTimeoutSeconds() == null
? PdfTextUtils.getPdfTextTimeoutSeconds()
: pdfTextToolProperties.getTimeoutSeconds();

return PdfTextTool.builder()
.executableFile(executablePath.toFile())
.timeoutSeconds(timeoutSeconds)
.executableFile(pdfTextToolProperties.getExecutablePath() != null ? pdfTextToolProperties.getExecutablePath().toFile() : null)
.timeoutSeconds(pdfTextToolProperties.getTimeoutSeconds())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
package io.xpdf.api.pdftext.autoconfigure

import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.*
import io.xpdf.api.common.util.XpdfUtils
import io.xpdf.api.pdftext.PdfTextTool
import io.xpdf.api.pdftext.util.PdfTextUtils
import org.apache.commons.io.FileUtils
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand All @@ -32,26 +32,37 @@ import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import java.io.File
import java.nio.file.Path
import java.nio.file.Paths

class PdfTextToolAutoConfigurationTest {

private var context: AnnotationConfigApplicationContext = AnnotationConfigApplicationContext()

companion object {
private val pdfTextToolBean = mockk<PdfTextTool>()

@JvmStatic
@AfterAll
fun afterAll() {
FileUtils.deleteQuietly(XpdfUtils.getXpdfTempPath().toFile())
}
}

@BeforeEach
fun beforeEach() {
context = AnnotationConfigApplicationContext()
}

@AfterEach
fun afterEach() {
unmockkAll()
context.close()
}

@Test
fun `should autoconfigure pdf text tool from properties`() {
fun `should autoconfigure pdf text tool with properties`() {
// given
val executableFile: File = Paths.get(System.getProperty("java.io.tmpdir"), "executableName").toFile().apply {
val executableFile: File = XpdfUtils.getXpdfTempPath().resolve("executableName").toFile().apply {
mkdirs()
createNewFile()
deleteOnExit()
}
Expand All @@ -73,7 +84,7 @@ class PdfTextToolAutoConfigurationTest {
}

@Test
fun `should autoconfigure pdf text tool from xpdf utils`() {
fun `should autoconfigure pdf text tool without properties`() {
// given
val executableFile = mockk<File> {
every { exists() } returns true
Expand Down Expand Up @@ -110,10 +121,6 @@ class PdfTextToolAutoConfigurationTest {
context.getBean(PdfTextTool::class.java) shouldBe pdfTextToolBean
}

companion object {
private val pdfTextToolBean = mockk<PdfTextTool>()
}

@Configuration
open class PdfTextToolConfig {
@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ protected File initializeTextFile(PdfTextRequest request) throws IOException {
}

// create output directory if not exists
textFile.getParentFile().mkdir();
textFile.getParentFile().mkdirs();

return textFile;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,22 @@
*/
package io.xpdf.api.pdftext

import io.cucumber.java.After
import io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME
import io.xpdf.api.common.util.XpdfUtils
import org.apache.commons.io.FileUtils
import org.junit.platform.suite.api.ConfigurationParameter
import org.junit.platform.suite.api.SelectClasspathResource
import org.junit.platform.suite.api.Suite

@Suite
@SelectClasspathResource("io/xpdf/api/pdftext")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "io.xpdf.api.pdftext" )
class PdfTextToolCucumberIT
class PdfTextToolCucumberIT {

@After
fun after() {
FileUtils.deleteQuietly(XpdfUtils.getXpdfTempPath().toFile())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,33 @@ import io.kotest.matchers.shouldBe
import io.kotest.matchers.shouldNotBe
import io.kotest.matchers.string.shouldMatch
import io.xpdf.api.common.exception.XpdfException
import io.xpdf.api.common.util.XpdfUtils
import io.xpdf.api.pdftext.options.PdfTextEncoding
import io.xpdf.api.pdftext.options.PdfTextEndOfLine
import io.xpdf.api.pdftext.options.PdfTextFormat
import io.xpdf.api.pdftext.util.PdfTextUtils
import org.apache.commons.io.FileUtils
import org.springframework.test.context.event.annotation.AfterTestClass
import java.io.File
import java.nio.file.Paths

class PdfTextToolCucumberSteps {

private var toolDto: PdfTextToolDto? = null
private var requestDto: PdfTextRequestDto? = null
private var optionsDto: PdfTextOptionsDto? = null
private var nativeOptionsDto: NativeOptionsDto? = null
private var response: PdfTextResponse? = null
private var exception: XpdfException? = null

companion object {
@JvmStatic
@AfterTestClass
fun afterAll() {
FileUtils.deleteQuietly(XpdfUtils.getXpdfTempPath().toFile())
}
}

@Given("a PdfTextTool")
fun `a PdfTextTool`() {
toolDto = PdfTextToolDto(null, null)
Expand All @@ -59,7 +70,7 @@ class PdfTextToolCucumberSteps {
@Given("a PdfTextTool with {int} second timeout and dynamic executable file")
fun `a PdfTextTool with TIMEOUT_SECONDS second timeout and dynamic executable file`(timeoutSeconds: Int) {
val executableResourceStream = this::class.java.classLoader.getResourceAsStream(PdfTextUtils.getPdfTextExecutableResourceName())!!
val executableFile = Paths.get(System.getProperty("java.io.tmpdir")).resolve("some.exe").toFile()
val executableFile = XpdfUtils.getXpdfTempPath().resolve("some.exe").toFile()
FileUtils.copyInputStreamToFile(executableResourceStream, executableFile)
executableFile.setExecutable(true)

Expand All @@ -79,7 +90,7 @@ class PdfTextToolCucumberSteps {
@Given("a PdfTextRequest with pdf file {word} and dynamic text file")
fun `a PdfTextRequest with pdf file PDF_FILE_NAME and dynamic text file`(pdfFileName: String) {
val pdfFile = File(this.javaClass.classLoader.getResource("pdfs/${pdfFileName}")!!.toURI())
val txtFile = Paths.get(System.getProperty("java.io.tmpdir")).resolve("some.txt").toFile()
val txtFile = XpdfUtils.getXpdfTempPath().resolve("some.txt").toFile()

this.requestDto = PdfTextRequestDto(pdfFile, txtFile)
}
Expand Down Expand Up @@ -146,7 +157,7 @@ class PdfTextToolCucumberSteps {
@DataTableType
fun pdfTextRequestDtoTransformer(row: Map<String, String?>) = PdfTextRequestDto(
File(this.javaClass.classLoader.getResource("pdfs/${row["pdfFile"]!!}")!!.toURI()),
row["textFile"]?.let { Paths.get(System.getProperty("java.io.tmpdir")).resolve(it).toFile() },
row["textFile"]?.let { XpdfUtils.getXpdfTempPath().resolve(it).toFile() },
)

@DataTableType
Expand Down
44 changes: 16 additions & 28 deletions pdf-text-api/src/test/kotlin/io/xpdf/api/pdftext/PdfTextToolTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldMatch
import io.mockk.*
import io.xpdf.api.common.exception.*
import io.xpdf.api.common.util.XpdfUtils
import io.xpdf.api.pdftext.options.PdfTextEncoding
import io.xpdf.api.pdftext.options.PdfTextEndOfLine
import io.xpdf.api.pdftext.options.PdfTextFormat
import io.xpdf.api.pdftext.util.PdfTextUtils
import org.apache.commons.io.FileUtils
import org.apache.commons.io.IOUtils
import org.junit.jupiter.api.AfterAll
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.junit.jupiter.params.ParameterizedTest
Expand All @@ -51,6 +54,19 @@ class PdfTextToolTest {

private val pdfTextTool = PdfTextTool.builder().build()

companion object {
@JvmStatic
@AfterAll
fun afterAll() {
FileUtils.deleteQuietly(XpdfUtils.getXpdfTempPath().toFile())
}
}

@AfterEach
fun afterEach() {
unmockkAll()
}

@Test
fun `should initialize and copy executable to local system`() {
// given
Expand All @@ -68,9 +84,6 @@ class PdfTextToolTest {

// then
verify { FileUtils.copyInputStreamToFile(any(), any()) }

unmockkStatic(PdfTextUtils::class)
unmockkStatic(FileUtils::class)
}

@Test
Expand All @@ -89,9 +102,6 @@ class PdfTextToolTest {

// then
verify(exactly = 0) { FileUtils.copyInputStreamToFile(any(), any()) }

unmockkStatic(PdfTextUtils::class)
unmockkStatic(FileUtils::class)
}

@Test
Expand Down Expand Up @@ -123,8 +133,6 @@ class PdfTextToolTest {
shouldThrowWithMessage<XpdfRuntimeException>("Unable to locate executable in project resources") {
PdfTextTool.builder().build()
}

unmockkStatic(PdfTextUtils::class)
}

@Test
Expand All @@ -143,9 +151,6 @@ class PdfTextToolTest {
shouldThrowWithMessage<XpdfRuntimeException>("Unable to copy executable from resources to local system") {
PdfTextTool.builder().build()
}

unmockkStatic(PdfTextUtils::class)
unmockkStatic(FileUtils::class)
}

@Test
Expand All @@ -161,8 +166,6 @@ class PdfTextToolTest {
shouldThrowWithMessage<XpdfRuntimeException>("Unable to set execute permissions on executable") {
PdfTextTool.builder().build()
}

unmockkStatic(PdfTextUtils::class)
}

@Test
Expand Down Expand Up @@ -203,8 +206,6 @@ class PdfTextToolTest {

// then
result.timeoutSeconds shouldBe 99

unmockkStatic(PdfTextUtils::class)
}

@Test
Expand Down Expand Up @@ -257,9 +258,6 @@ class PdfTextToolTest {
capturedOutput.all shouldContain "Invocation completed; exit code: 0, standard output: standardOutput"
capturedOutput.all shouldContain "Invocation succeeded"
capturedOutput.all shouldContain "Process finished"

unmockkConstructor(ProcessBuilder::class)
unmockkStatic(IOUtils::class)
}

@ParameterizedTest
Expand Down Expand Up @@ -313,9 +311,6 @@ class PdfTextToolTest {
capturedOutput.all shouldContain "Invocation failed; error output: errorOutput"
capturedOutput.all shouldContain "Process failed; exception message: $message"
capturedOutput.all shouldContain "Process finished"

unmockkConstructor(ProcessBuilder::class)
unmockkStatic(IOUtils::class)
}

@Test
Expand Down Expand Up @@ -356,9 +351,6 @@ class PdfTextToolTest {
capturedOutput.all shouldContain "Invocation timed out"
capturedOutput.all shouldContain "Process failed; exception message: Timeout reached before process could finish"
capturedOutput.all shouldContain "Process finished"

unmockkConstructor(ProcessBuilder::class)
unmockkStatic(IOUtils::class)
}

@Test
Expand Down Expand Up @@ -509,10 +501,6 @@ class PdfTextToolTest {
pdfTextTool.initializeTextFile(request) shouldBe textFile

verify { Paths.get("outPath", textFileName) }

unmockkStatic(PdfTextUtils::class)
unmockkStatic(UUID::class)
unmockkStatic(Paths::class)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ package io.xpdf.api.pdftext.util
import io.kotest.matchers.shouldBe
import io.mockk.every
import io.mockk.mockkStatic
import io.mockk.unmockkStatic
import io.mockk.unmockkAll
import io.xpdf.api.common.util.XpdfUtils
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.BeforeEach
Expand All @@ -38,8 +38,7 @@ class PdfTextUtilsTest {

@AfterEach
fun afterEach() {
unmockkStatic(XpdfUtils::class)
unmockkStatic(PdfTextUtils::class)
unmockkAll()
}

@Test
Expand Down

0 comments on commit 52bf568

Please sign in to comment.