From e445fe2e6f073f67d5f1514e0875a58441ce545a Mon Sep 17 00:00:00 2001 From: Julien Lengrand-Lambert Date: Thu, 1 May 2025 13:27:54 +0200 Subject: [PATCH] Fix Junie bug, adds tests --- .../kotlin/nl/lengrand/opengraphkt/Parser.kt | 10 +- .../nl/lengrand/opengraphkt/ParserTest.kt | 132 ++++++++++++++++++ 2 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/nl/lengrand/opengraphkt/ParserTest.kt diff --git a/src/main/kotlin/nl/lengrand/opengraphkt/Parser.kt b/src/main/kotlin/nl/lengrand/opengraphkt/Parser.kt index 1c93bcc..3f6d3b6 100644 --- a/src/main/kotlin/nl/lengrand/opengraphkt/Parser.kt +++ b/src/main/kotlin/nl/lengrand/opengraphkt/Parser.kt @@ -21,8 +21,14 @@ data class OpenGraph( // TODO : Continue with more ){ + /** + * The very minimum requirement for OpenGraph is a set of 4 properties + */ fun isValid(): Boolean { - return title != null && type != null && image != null && url != null + return tags.find { it.property == "title" } != null && + tags.find { it.property == "url" } != null && + tags.find { it.property == "image" } != null && + tags.find { it.property == "type" } != null } } @@ -59,4 +65,4 @@ class Parser { url ) } -} \ No newline at end of file +} diff --git a/src/test/kotlin/nl/lengrand/opengraphkt/ParserTest.kt b/src/test/kotlin/nl/lengrand/opengraphkt/ParserTest.kt new file mode 100644 index 0000000..07efa72 --- /dev/null +++ b/src/test/kotlin/nl/lengrand/opengraphkt/ParserTest.kt @@ -0,0 +1,132 @@ +package nl.lengrand.opengraphkt + +import nl.lengrand.opengraphkt.nl.lengrand.opengraphkt.DocumentFetcher +import org.jsoup.nodes.Document +import org.junit.jupiter.api.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertNotNull +import kotlin.test.assertTrue + +class ParserTest { + + private val parser = Parser() + private val fetcher = DocumentFetcher() + + // Sample HTML with all required OpenGraph tags + private val completeHtml = """ + + + + Open Graph Example + + + + + + + + +

Example Page

+ + + """.trimIndent() + + // Sample HTML with missing required OpenGraph tags + private val incompleteHtml = """ + + + + Open Graph Example + + + + +

Example Page

+ + + """.trimIndent() + + // Sample HTML with no OpenGraph tags + private val noOgHtml = """ + + + + No Open Graph Example + + +

Example Page

+ + + """.trimIndent() + + @Test + fun `test extractOpenGraphTags with complete OpenGraph tags`() { + val document = fetcher.fromString(completeHtml) + val openGraph = parser.extractOpenGraphTags(document) + + // Verify that all required properties are extracted correctly + assertEquals("The Rock", openGraph.title) + assertEquals("video.movie", openGraph.type) + assertEquals("https://example.com/the-rock", openGraph.url) + assertEquals("https://example.com/rock.jpg", openGraph.image) + + // Verify that the OpenGraph object is valid + assertTrue(openGraph.isValid()) + + // Verify that all tags are extracted + assertEquals(6, openGraph.tags.size) + + // Verify specific tag content + val descriptionTag = openGraph.tags.find { it.property == "description" } + assertNotNull(descriptionTag) + assertEquals("An action movie about a rock", descriptionTag.content) + + val siteNameTag = openGraph.tags.find { it.property == "site_name" } + assertNotNull(siteNameTag) + assertEquals("Example Movies", siteNameTag.content) + } + + @Test + fun `test extractOpenGraphTags with incomplete OpenGraph tags`() { + val document = fetcher.fromString(incompleteHtml) + val openGraph = parser.extractOpenGraphTags(document) + + // Verify that the available property is extracted correctly + assertEquals("The Rock", openGraph.title) + + // Verify that missing properties are empty strings + assertEquals("", openGraph.type) + assertEquals("", openGraph.url) + assertEquals("", openGraph.image) + + // Verify that the OpenGraph object is not valid due to missing required properties + assertFalse(openGraph.isValid()) + + // Verify that only the available tags are extracted + assertEquals(2, openGraph.tags.size) + + // Verify specific tag content + val descriptionTag = openGraph.tags.find { it.property == "description" } + assertNotNull(descriptionTag) + assertEquals("An action movie about a rock", descriptionTag.content) + } + + @Test + fun `test extractOpenGraphTags with no OpenGraph tags`() { + val document = fetcher.fromString(noOgHtml) + val openGraph = parser.extractOpenGraphTags(document) + + // Verify that all properties are empty strings + assertEquals("", openGraph.title) + assertEquals("", openGraph.type) + assertEquals("", openGraph.url) + assertEquals("", openGraph.image) + + // Verify that the OpenGraph object is not valid + assertFalse(openGraph.isValid()) + + // Verify that no tags are extracted + assertEquals(0, openGraph.tags.size) + } +}