diff --git a/opengraphkt/src/test/kotlin/fr/lengrand/opengraphkt/GeneratorTest.kt b/opengraphkt/src/test/kotlin/fr/lengrand/opengraphkt/GeneratorTest.kt index 9382694..0728573 100644 --- a/opengraphkt/src/test/kotlin/fr/lengrand/opengraphkt/GeneratorTest.kt +++ b/opengraphkt/src/test/kotlin/fr/lengrand/opengraphkt/GeneratorTest.kt @@ -4,12 +4,48 @@ import org.junit.jupiter.api.Test import java.net.URI import java.time.OffsetDateTime import kotlin.test.assertEquals +import kotlin.test.assertFalse import kotlin.test.assertTrue class GeneratorTest { private val generator = Generator() + @Test + fun `test generate with empty metadata`() { + val data = Data( + tags = emptyList(), + title = null, + type = null, + url = null, + description = null, + siteName = null, + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = emptyList(), + videos = emptyList(), + audios = emptyList(), + article = null, + profile = null, + book = null, + musicSong = null, + musicAlbum = null, + musicPlaylist = null, + musicRadioStation = null, + videoMovie = null, + videoEpisode = null + ) + + val html = generator.generate(data) + + // Verify that all basic metadata tags are generated correctly + assertFalse(html.contains("")) assertTrue(generatedHtml.contains("")) } + + @Test + fun `test generate with custom type`() { + // Create a Data object with a custom type that doesn't match any specific case in the when statement + val data = Data( + tags = emptyList(), + title = "Custom Type Test", + type = "custom.type", + url = URI("https://example.com/custom").toURL(), + description = "Custom type description", + siteName = "Test Site", + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = emptyList(), + videos = emptyList(), + audios = emptyList(), + article = null, + profile = null, + book = null, + musicSong = null, + musicAlbum = null, + musicPlaylist = null, + musicRadioStation = null, + videoMovie = null, + videoEpisode = null + ) + + val html = generator.generate(data) + + // Verify that basic metadata is included but no type-specific tags + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + + // Verify that no type-specific tags are included + assertFalse(html.contains("og:article:")) + assertFalse(html.contains("og:profile:")) + assertFalse(html.contains("og:book:")) + assertFalse(html.contains("og:music:")) + assertFalse(html.contains("og:video:")) + } + + @Test + fun `test partial image properties`() { + // Create a Data object with images that have some null properties + val data = Data( + tags = emptyList(), + title = "Partial Image Test", + type = "website", + url = URI("https://example.com").toURL(), + description = null, + siteName = null, + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = listOf( + Image( + url = "https://example.com/image1.jpg", + secureUrl = null, // Testing null secureUrl + type = "image/jpeg", + width = null, // Testing null width + height = 600, + alt = "Test Image 1" + ), + Image( + url = "https://example.com/image2.png", + secureUrl = "https://secure.example.com/image2.png", + type = null, // Testing null type + width = 1024, + height = null, // Testing null height + alt = null // Testing null alt + ) + ), + videos = emptyList(), + audios = emptyList(), + article = null, + profile = null, + book = null, + musicSong = null, + musicAlbum = null, + musicPlaylist = null, + musicRadioStation = null, + videoMovie = null, + videoEpisode = null + ) + + val html = generator.generate(data) + + // Verify basic metadata tags + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + + // Verify image1 tags - should have url, type, height, and alt but not secureUrl or width + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + + // Verify image2 tags - should have url, secureUrl, and width but not type, height, or alt + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + + // Count the total number of image-related tags to ensure no unexpected tags are present + val expectedImageTagCount = 7 // 2 og:image + 1 secure_url + 1 type + 1 width + 1 height + 1 alt + val actualImageTagCount = html.split("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + + // Verify video1 tags - should have url, type, height, and duration but not secureUrl or width + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + + // Verify video2 tags - should have url, secureUrl, and width but not type, height, or duration + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + + // Count the total number of video-related tags to ensure no unexpected tags are present + val expectedVideoTagCount = 7 // 2 og:video + 1 secure_url + 1 type + 1 width + 1 height + 1 duration + val actualVideoTagCount = html.split("")) + assertFalse(html.contains("og:audio:secure_url\" content=\"https://example.com/audio1.mp3")) + assertTrue(html.contains("")) + + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertFalse(html.contains("og:audio:type\" content=\"\" />")) + } + + @Test + fun `test partial article properties`() { + // Create a Data object with an article that has some null properties + val data = Data( + tags = emptyList(), + title = "Partial Article Test", + type = "article", + url = URI("https://example.com/article").toURL(), + description = null, + siteName = null, + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = emptyList(), + videos = emptyList(), + audios = emptyList(), + article = Article( + publishedTime = OffsetDateTime.parse("2023-01-01T00:00:00Z"), + modifiedTime = null, // Testing null modifiedTime + expirationTime = OffsetDateTime.parse("2023-12-31T23:59:59Z"), + authors = listOf("Author 1"), + section = null, // Testing null section + tags = listOf("tag1", "tag2") + ), + profile = null, + book = null, + musicSong = null, + musicAlbum = null, + musicPlaylist = null, + musicRadioStation = null, + videoMovie = null, + videoEpisode = null + ) + + val html = generator.generate(data) + + // Verify that only non-null article properties generate tags + assertTrue(html.contains("")) + assertFalse(html.contains("og:article:modified_time")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertFalse(html.contains("og:article:section")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + } + + @Test + fun `test partial profile properties`() { + // Create a Data object with a profile that has some null properties + val data = Data( + tags = emptyList(), + title = "Partial Profile Test", + type = "profile", + url = URI("https://example.com/profile").toURL(), + description = null, + siteName = null, + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = emptyList(), + videos = emptyList(), + audios = emptyList(), + article = null, + profile = Profile( + firstName = "John", + lastName = null, // Testing null lastName + username = "johndoe", + gender = null // Testing null gender + ), + book = null, + musicSong = null, + musicAlbum = null, + musicPlaylist = null, + musicRadioStation = null, + videoMovie = null, + videoEpisode = null + ) + + val html = generator.generate(data) + + // Verify that only non-null profile properties generate tags + assertTrue(html.contains("")) + assertFalse(html.contains("og:profile:last_name")) + assertTrue(html.contains("")) + assertFalse(html.contains("og:profile:gender")) + } + + @Test + fun `test partial book properties`() { + // Create a Data object with a book that has some null properties + val data = Data( + tags = emptyList(), + title = "Partial Book Test", + type = "book", + url = URI("https://example.com/book").toURL(), + description = null, + siteName = null, + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = emptyList(), + videos = emptyList(), + audios = emptyList(), + article = null, + profile = null, + book = Book( + authors = listOf("Author 1"), + isbn = null, // Testing null isbn + releaseDate = OffsetDateTime.parse("2023-01-01T00:00:00Z"), + tags = emptyList() // Testing empty tags list + ), + musicSong = null, + musicAlbum = null, + musicPlaylist = null, + musicRadioStation = null, + videoMovie = null, + videoEpisode = null + ) + + val html = generator.generate(data) + + // Verify that only non-null book properties generate tags + assertTrue(html.contains("")) + assertFalse(html.contains("og:book:isbn")) + assertTrue(html.contains("")) + assertFalse(html.contains("og:book:tag")) + } + + @Test + fun `test partial music song properties`() { + // Create a Data object with a music song that has some null properties + val data = Data( + tags = emptyList(), + title = "Partial Music Song Test", + type = "music.song", + url = URI("https://example.com/song").toURL(), + description = null, + siteName = null, + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = emptyList(), + videos = emptyList(), + audios = emptyList(), + article = null, + profile = null, + book = null, + musicSong = MusicSong( + duration = 240, + album = null, // Testing null album + albumDisc = 1, + albumTrack = null, // Testing null albumTrack + musician = emptyList() // Testing empty musician list + ), + musicAlbum = null, + musicPlaylist = null, + musicRadioStation = null, + videoMovie = null, + videoEpisode = null + ) + + val html = generator.generate(data) + + // Verify that only non-null music song properties generate tags + assertTrue(html.contains("")) + assertFalse(html.contains("")) + assertFalse(html.contains("")) + assertFalse(html.contains("og:music:song:disc")) + assertTrue(html.contains("")) + assertFalse(html.contains("og:music:musician")) + assertFalse(html.contains("og:music:release_date")) + } + + @Test + fun `test partial music playlist properties`() { + // Create a Data object with a music playlist that has some null properties + val data = Data( + tags = emptyList(), + title = "Partial Music Playlist Test", + type = "music.playlist", + url = URI("https://example.com/playlist").toURL(), + description = null, + siteName = null, + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = emptyList(), + videos = emptyList(), + audios = emptyList(), + article = null, + profile = null, + book = null, + musicSong = null, + musicAlbum = null, + musicPlaylist = MusicPlaylist( + songs = emptyList(), // Testing empty songs list + songDisc = null, // Testing null songDisc + songTrack = null, // Testing null songTrack + creator = "Playlist Creator" + ), + musicRadioStation = null, + videoMovie = null, + videoEpisode = null + ) + + val html = generator.generate(data) + + // Verify that only non-null music playlist properties generate tags + assertFalse(html.contains("og:music:song")) + assertFalse(html.contains("og:music:song:disc")) + assertFalse(html.contains("og:music:song:track")) + assertTrue(html.contains("")) + } + + @Test + fun `test partial music radio station properties`() { + // Create a Data object with a music radio station that has null properties + val data = Data( + tags = emptyList(), + title = "Partial Music Radio Station Test", + type = "music.radio_station", + url = URI("https://example.com/radio").toURL(), + description = null, + siteName = null, + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = emptyList(), + videos = emptyList(), + audios = emptyList(), + article = null, + profile = null, + book = null, + musicSong = null, + musicAlbum = null, + musicPlaylist = null, + musicRadioStation = MusicRadioStation( + creator = null // Testing null creator + ), + videoMovie = null, + videoEpisode = null + ) + + val html = generator.generate(data) + + // Verify that no music radio station properties generate tags when they're null + assertFalse(html.contains("og:music:creator")) + } + + @Test + fun `test partial video movie properties`() { + // Create a Data object with a video movie that has some null properties + val data = Data( + tags = emptyList(), + title = "Partial Video Movie Test", + type = "video.movie", + url = URI("https://example.com/movie").toURL(), + description = null, + siteName = null, + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = emptyList(), + videos = emptyList(), + audios = emptyList(), + article = null, + profile = null, + book = null, + musicSong = null, + musicAlbum = null, + musicPlaylist = null, + musicRadioStation = null, + videoMovie = VideoMovie( + actors = emptyList(), // Testing empty actors list + director = listOf("Director"), + writer = emptyList(), // Testing empty writers list + duration = null, // Testing null duration + releaseDate = OffsetDateTime.parse("2023-01-01T00:00:00Z"), + tags = listOf("action") + ), + videoEpisode = null + ) + + val html = generator.generate(data) + + // Verify that only non-null video movie properties generate tags + assertFalse(html.contains("og:video:actor")) + assertTrue(html.contains("")) + assertFalse(html.contains("og:video:writer")) + assertFalse(html.contains("og:video:duration")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + } + + @Test + fun `test partial video episode properties`() { + // Create a Data object with a video episode that has some null properties + val data = Data( + tags = emptyList(), + title = "Partial Video Episode Test", + type = "video.episode", + url = URI("https://example.com/episode").toURL(), + description = null, + siteName = null, + determiner = null, + locale = null, + localeAlternate = emptyList(), + images = emptyList(), + videos = emptyList(), + audios = emptyList(), + article = null, + profile = null, + book = null, + musicSong = null, + musicAlbum = null, + musicPlaylist = null, + musicRadioStation = null, + videoMovie = null, + videoEpisode = VideoEpisode( + actors = listOf("Actor 1"), + director = emptyList(), // Testing empty directors list + writer = listOf("Writer 1"), + duration = 45, + releaseDate = null, // Testing null releaseDate + tags = emptyList(), // Testing empty tags list + series = null // Testing null series + ) + ) + + val html = generator.generate(data) + + // Verify that only non-null video episode properties generate tags + assertTrue(html.contains("")) + assertFalse(html.contains("og:video:director")) + assertTrue(html.contains("")) + assertTrue(html.contains("")) + assertFalse(html.contains("og:video:release_date")) + assertFalse(html.contains("og:video:tag")) + assertFalse(html.contains("og:video:series")) + } }