Skip to content

Commit

Permalink
✅ Improve tests coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
1fabiopereira committed Jun 21, 2022
1 parent da8848a commit e54e1e3
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 158 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
strategy:
fail-fast: false
matrix:
language: [ 'javascript' ]
language: [ 'javascript', 'TypeScript', 'java' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Git checkout
uses: actions/checkout@v2

- name: 'Use Node.js Version 16.x'
- name: Use Node.js Version 16.x
uses: actions/setup-node@v2
with:
node-version: 16.14.x
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/quality.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ jobs:
- name: Git checkout
uses: actions/checkout@v2

- name: 'Use Node.js Version 16.x'
- name: Use Node.js Version 16.x
uses: actions/setup-node@v2
with:
node-version: 16.14.x

- uses: actions/setup-java@v3
- name: Use JDK Version 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ jobs:
node-version: 16.14.x
registry-url: 'https://registry.npmjs.org'

- uses: actions/setup-java@v3
- name: Use JDK Version 11
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: '11'
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
![React Native PDF Extractor Logo](./website/static/img/react-native-pdf-extractor.svg)
<h1 align="center">
<p>
<img src="./website/static/img/react-native-pdf-extractor.svg" alt="React Native PDF Extractor Logo" /><br />
React Native PDF Extractor
</p>
</h1>


## React Native PDF Extractor
<p align="center">
<a href="https://www.npmjs.com/package/react-native-pdf-extractor"><img src="https://img.shields.io/npm/dw/react-native-pdf-extractor.svg?style=flat-square" alt="NPM downloads"></a>
<a href="https://www.npmjs.com/package/react-native-pdf-extractor"><img src="https://img.shields.io/npm/v/react-native-pdf-extractor?style=flat-square" alt="NPM version"></a>
<a href="https://1fabiopereira.github.io/react-native-pdf-extractor/docs/license"><img src="https://img.shields.io/github/license/1fabiopereira/react-native-pdf-extractor?style=flat-square" alt="Package License"></a>
</p>

This library allows you to extract pdfs file data using matches specifics patterns.

Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ dependencies {
// Tests dependencies
testImplementation("junit:junit:4.13.2")
testImplementation("pl.pragmatists:JUnitParams:1.1.1")
testImplementation("io.mockk:mockk:1.12.0")
testImplementation("io.mockk:mockk:1.12.4")
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ open class PdfHandler {

@JvmStatic
fun extract(document: PDDocument?): String? {
if (document === null) return null
val data = PDFTextStripper().getText(document)
return StringHandler.format(data)
if (document === null) return null
val data = PDFTextStripper().getText(document)
return StringHandler.format(data)
}

@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
@@ -1,87 +1,83 @@
package com.reactnativepdfextractor.core

import android.content.ContentResolver
import android.net.Uri

import com.tom_roush.pdfbox.pdmodel.PDDocument
import com.facebook.react.bridge.ReadableArray
import com.reactnativepdfextractor.utils.TestUtils
import com.reactnativepdfextractor.utils.mocks.*
import io.mockk.*

import junitparams.JUnitParamsRunner
import junitparams.Parameters
import org.junit.Assert

import org.junit.After
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

import java.io.InputStream


@RunWith(JUnitParamsRunner::class)
class PdfHandlerTest {

private fun streamMock(): InputStream {
return mockk(relaxed = true)
private val uri = UriMock()
private val log = AndroidLogMock()
private val stream = InputStreamMock()
private val loader = PDDocumentMock()
private val resolver = ContentResolverMock()
private val boxResourceLoader = PDFBoxResourceLoaderMock()

private val localUri = uri.mock()
private val localStream = stream.mock()

@Before
fun setUp() {
MockKAnnotations.init(this)
log.mock()
boxResourceLoader.mock()
}

private fun uriMock(): Uri {
return mockk(relaxed = true)
@After
fun destroy() {
clearAllMocks()
}

// ====================== IS ENCRYPTED ====================== //

@Parameterized.Parameters
fun isEncryptedWithoutExceptionsCases(): Any {
return listOf(
arrayOf(uriMock(), streamMock(), 1, false), // check if PDF file is not encrypted
arrayOf(uriMock(), streamMock(), 0, true), // check if PDF file is encrypted
arrayOf(localUri, localStream, 1, false), // check if PDF file is not encrypted
arrayOf(localUri, localStream, 0, true), // check if PDF file is encrypted
)
}

@Parameterized.Parameters
fun isEncryptedWithExceptionsCases(): Any {
val exception = Exception("File not found.")
val localException = Exception("File not found.")

return listOf(
arrayOf(uriMock(), null, 0, exception), // try check if PDF file is encrypted when STREAM was passed null
arrayOf(null, streamMock(), 0, exception), // try check if PDF file is encrypted when URI was passed null
arrayOf(null, null, 0, exception), // try check if PDF file is encrypted when URI and STREAM was passed null
arrayOf(localUri, null, 0, localException), // try check if PDF file is encrypted when STREAM was passed null
arrayOf(null, localStream, 0, localException), // try check if PDF file is encrypted when URI was passed null
arrayOf(null, null, 0, localException), // try check if PDF file is encrypted when URI and STREAM was passed null
)
}

@Test
@Parameters(method = "isEncryptedWithoutExceptionsCases")
fun isEncryptedWithoutExceptionsCasesTest(uri: Uri?, stream: InputStream?, pages: Int, expected: Boolean) {
val resolver = mockk<ContentResolver>(relaxed = true)
every { resolver.openInputStream(any()) } returns stream

val document = mockk<PDDocument>(relaxed = true)
every { document.numberOfPages } returns pages
every { document.close() } returns Unit

mockkStatic(PDDocument::class)
every { PDDocument.load(stream) } returns document

val result = PdfHandler.isEncrypted(uri, resolver)
loader.update(pages).mock()
val localResolver = resolver.update(stream).mock()
val result = PdfHandler.isEncrypted(uri, localResolver)

Assert.assertEquals(expected, result)
}

@Test
@Parameters(method = "isEncryptedWithExceptionsCases")
fun isEncryptedWithExceptionsCasesTest(uri: Uri?, stream: InputStream?, pages: Int, expected: Exception) {
val resolver = mockk<ContentResolver>(relaxed = true)
every { resolver.openInputStream(any()) } returns stream

val document = mockk<PDDocument>(relaxed = true)
every { document.numberOfPages } returns pages
every { document.close() } returns Unit

mockkStatic(PDDocument::class)
every { PDDocument.load(stream) } returns document

loader.update(pages).mock()
val localResolver = resolver.update(stream).mock()
val exception = Assert.assertThrows(Exception::class.java) {
PdfHandler.isEncrypted(uri, resolver)
PdfHandler.isEncrypted(uri, localResolver)
}

Assert.assertEquals(expected.message, exception.message)
Expand All @@ -92,56 +88,39 @@ class PdfHandlerTest {
@Parameterized.Parameters
fun loadWithoutExceptionsCases(): List<Any> {
return listOf(
arrayOf(uriMock(), streamMock(), null, 1), // try load PDF file when it's not encrypted
arrayOf(uriMock(), streamMock(), "password", 0), // try load PDF file when it's encrypted. (password required)
arrayOf(localUri, localStream, null, 1), // try load PDF file when it's not encrypted
arrayOf(localUri, localStream, "password", 0), // try load PDF file when it's encrypted. (password required)
)
}

@Parameterized.Parameters
fun loadWithExceptionsCases(): List<Any> {
val exception = Exception("Password is required.")
val localException = Exception("Password is required.")

return listOf(
arrayOf(uriMock(), streamMock(), null, exception), // try load PDF file when password is required but was passed null
arrayOf(uriMock(), streamMock(), "", exception), // try load PDF file when password is required but was passed empty
arrayOf(uriMock(), streamMock(), " ", exception), // try load PDF file when password is required but was passed blank
arrayOf(localUri, localStream, null, localException), // try load PDF file when password is required but was passed null
arrayOf(localUri, localStream, "", localException), // try load PDF file when password is required but was passed empty
arrayOf(localUri, localStream, " ", localException), // try load PDF file when password is required but was passed blank
)
}

@Test
@Parameters(method = "loadWithoutExceptionsCases")
fun loadWithoutExceptionsCasesTest(uri: Uri?, stream: InputStream?, password: String?, pages: Int) {
val resolver = mockk<ContentResolver>(relaxed = true)
every { resolver.openInputStream(any()) } returns stream

val document = mockk<PDDocument>(relaxed = true)
every { document.numberOfPages } returns pages
every { document.close() } returns Unit

mockkStatic(PDDocument::class)
every { PDDocument.load(stream) } returns document
every { PDDocument.load(stream, password) } returns document

val result = PdfHandler.load(uri, resolver, password)
val doc = loader.update(pages).mock().getDoc()
val localResolver = resolver.update(stream).mock()
val result = PdfHandler.load(uri, localResolver, password)

Assert.assertEquals(document, result)
Assert.assertEquals(doc, result)
}

@Test
@Parameters(method = "loadWithExceptionsCases")
fun loadWithExceptionsCasesTest(uri: Uri, stream: InputStream, password: String?, expected: Exception) {
val resolver = mockk<ContentResolver>(relaxed = true)
every { resolver.openInputStream(uri) } returns stream

val document = mockk<PDDocument>(relaxed = true)
every { document.numberOfPages } returns 0
every { document.close() } returns Unit

mockkStatic(PDDocument::class)
every { PDDocument.load(stream) } returns document

loader.update(0).mock()
val localResolver = resolver.update(stream).mock()
val exception = Assert.assertThrows(Exception::class.java) {
PdfHandler.load(uri, resolver, password)
PdfHandler.load(uri, localResolver, password)
}

Assert.assertEquals(expected.message, exception.message)
Expand All @@ -152,59 +131,44 @@ class PdfHandlerTest {
@Parameterized.Parameters
fun getNumberOfPagesWithoutExceptionsCases(): Any {
return listOf(
arrayOf(uriMock(), streamMock(), null, 1), // try get number of pages from PDF file when it's not encrypted
arrayOf(uriMock(), streamMock(), "password", 0), // try get number of pages from PDF file when it's encrypted (password required)
arrayOf(localUri, localStream, null, 1), // try get number of pages from PDF file when it's not encrypted
arrayOf(localUri, localStream, "password", 0), // try get number of pages from PDF file when it's encrypted (password required)
)
}

@Test
@Parameters(method = "getNumberOfPagesWithoutExceptionsCases")
fun getNumberOfPagesWithoutExceptionsCasesTest(uri: Uri?, stream: InputStream?, password: String?, pages: Int) {
val resolver = mockk<ContentResolver>(relaxed = true)
every { resolver.openInputStream(any()) } returns stream
loader.update(pages).mock()
val localResolver = resolver.update(stream).mock()
val result = PdfHandler.getNumberOfPages(uri, localResolver, password)

val document = mockk<PDDocument>(relaxed = true)
every { document.numberOfPages } returns pages
every { document.close() } returns Unit
Assert.assertEquals(pages, result)
}

mockkStatic(PDDocument::class)
every { PDDocument.load(stream) } returns document
every { PDDocument.load(stream, password) } returns document
// ====================== GET TEXT ====================== //

val result = PdfHandler.getNumberOfPages(uri, resolver, password)
@Parameterized.Parameters
fun getTextWithoutExceptionsCases(): Any {
val localText = TestUtils.loadText("multiline-unformatted.txt")

Assert.assertEquals(pages, result)
return listOf(
arrayOf(localUri, localStream, null, 1, null, localText), // try get text from PDF file when it's not encrypted without pattern
arrayOf(localUri, localStream, "password", 0, null, localText), // try get text from PDF file when it's encrypted without pattern (password required)
)
}

// ====================== GET TEXT ====================== //
@Test
@Parameters(method = "getTextWithoutExceptionsCases")
fun getTextWithoutExceptionsCasesTest(uri: Uri, stream: InputStream, password: String?, pages: Int, patterns: ReadableArray?, text: String) {
loader.update(pages).mock()
PDFTextStripperMock().update(text).mock()

// @Parameterized.Parameters
// fun getTextWithoutExceptionsCases(): Any? {
// val text = TestUtils.loadText("multiline-unformatted.txt")
//
// return listOf(
// arrayOf(uri, stream, null, 1, null, text), // try get text from PDF file when it's not encrypted without pattern
// arrayOf(uri, stream, "password", 0, null, text), // try get text from PDF file when it's encrypted without pattern (password required)
// )
// }
//
// @Test
// @Parameters(method = "getTextWithoutExceptionsCases")
// fun getTextWithoutExceptionCasesTest(uri: Uri?, stream: InputStream?, password: String?, pages: Int, patterns: ReadableArray?, text: String?) {
// val resolver = mockk<ContentResolver>(relaxed = true)
// every { resolver.openInputStream(any()) } returns stream
//
// val document = mockk<PDDocument>(relaxed = true)
// every { document.numberOfPages } returns pages
// every { document.close() } returns Unit
//
// mockkStatic(PDDocument::class)
// every { PDDocument.load(stream) } returns document
// every { PDDocument.load(stream, password) } returns document
//
// val result = PdfHandler.getText(uri, resolver, patterns, password)
//
// Assert.assertEquals(text, result)
// }
val localResolver = resolver.update(stream).mock()
val result = PdfHandler.getText(uri, localResolver, patterns, password)


Assert.assertEquals(StringHandler.format(text), result)
}
}

Loading

0 comments on commit e54e1e3

Please sign in to comment.