Skip to content

Commit

Permalink
Introduce Openlib
Browse files Browse the repository at this point in the history
  • Loading branch information
koenighotze committed Oct 24, 2023
1 parent fa640a0 commit 1f116b3
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,24 @@ class AuthorsGroup(
cascade = [MERGE, PERSIST],
fetch = EAGER
)
var authors: MutableSet<Author>,
var authors: MutableSet<Author> = mutableSetOf(),
@Id
var id: AuthorsGroupId? = null,
) {
companion object {
fun randomId() = randomUUID().toString()
}

fun join(author: Author): AuthorsGroup {
author.authorsGroup.add(this)
authors.add(author)
fun withAuthors(authors: Set<Author>): AuthorsGroup {
this.authors.clear()
authors.forEach { author -> this.withAuthor(author) }

return this
}

fun withBook(book: Book): AuthorsGroup {
books.add(book)
book.authorsGroup = this
fun withAuthor(author: Author): AuthorsGroup {
author.authorsGroup.add(this)
authors.add(author)

return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Book(
cascade = [MERGE, PERSIST],
fetch = EAGER,
optional = false
) var authorsGroup: AuthorsGroup? = null,
) var authorsGroup: AuthorsGroup = AuthorsGroup(authors = mutableSetOf(), id = AuthorsGroupId()),
@Basic
@Convert(converter = ISBNStringConverter::class)
@Column(nullable = true, length = 36)
Expand All @@ -36,8 +36,9 @@ class Book(
fun randomId() = randomUUID().toString()
}

fun withAuthorsGroup(newAuthorsGroup: AuthorsGroup?): Book {
newAuthorsGroup?.withBook(this)
fun withAuthorsGroup(newAuthorsGroup: AuthorsGroup): Book {
authorsGroup = newAuthorsGroup
authorsGroup.books.add(this)

return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ data class OpenLibraryBook(
} else
null

fun toBook(authorsGroup: AuthorsGroup? = null) = Book(
fun toBook(authorsGroup: AuthorsGroup) = Book(
id = randomId(),
isbn = isbn,
title = title ?: "unknown",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.koenighotze.bodleian.bookcatalog.openlibrary

import org.koenighotze.bodleian.bookcatalog.entity.Author
import org.koenighotze.bodleian.bookcatalog.entity.AuthorsGroup
import org.koenighotze.bodleian.bookcatalog.entity.Book
import org.koenighotze.bodleian.bookcatalog.entity.ISBN
import org.koenighotze.bodleian.bookcatalog.entity.*
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import org.springframework.web.client.RestTemplate
Expand Down Expand Up @@ -40,7 +37,6 @@ class OpenLibraryService(private val resilientOpenLibraryRestTemplate: RestTempl
OpenLibraryBook::class.java
) // TODO: handle exception


if (null == openLibraryBookResponse.body) {
return empty()
}
Expand All @@ -53,9 +49,9 @@ class OpenLibraryService(private val resilientOpenLibraryRestTemplate: RestTempl

val book =
openLibraryBookResponse.body!!.toBook(
authorsGroup = if (null == authorKeys) null else AuthorsGroup(
authorsGroup = if (null == authorKeys) AuthorsGroup(id = AuthorsGroupId()) else AuthorsGroup(
authors = authorKeys,
id = AuthorsGroup.randomId()
id = AuthorsGroupId()
)
)
return of(book)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.koenighotze.bodleian.IntegrationTest
import org.koenighotze.bodleian.bookcatalog.DomainTestDataHelper.randomBook
import org.koenighotze.bodleian.bookcatalog.entity.Book
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.testcontainers.service.connection.ServiceConnection
import org.testcontainers.containers.PostgreSQLContainer
import org.testcontainers.junit.jupiter.Container

@IntegrationTest
class BookRepositoryIT(@Autowired var repository: BookRepository) {
private val knownBook = randomBook()
private lateinit var knownBook: Book

companion object {
@Container
Expand All @@ -22,6 +23,7 @@ class BookRepositoryIT(@Autowired var repository: BookRepository) {

@BeforeEach
fun setupBooks() {
knownBook = randomBook()
repository.save(knownBook)
}

Expand All @@ -36,7 +38,7 @@ class BookRepositoryIT(@Autowired var repository: BookRepository) {
fun `The repository returns the book, author group and the authors`() {
val book = repository.findById(knownBook.id!!)

assertThat(book.get().authorsGroup!!.authors).isNotEmpty()
assertThat(book.get().authorsGroup.authors).isNotEmpty()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import java.util.*
import java.util.UUID.randomUUID

object DomainTestDataHelper {
fun randomBook() = Book(
isbn = ISBN(randomUUID().toString()),
title = "Random Title ${randomUUID()}",
authorsGroup = randomAuthorsGroup(),
id = Book.randomId()
)
fun randomBook(): Book {
return Book(
isbn = ISBN(randomUUID().toString()),
title = "Random Title ${randomUUID()}",
id = Book.randomId()
).withAuthorsGroup(randomAuthorsGroup())
}

fun randomAuthorsGroup() = AuthorsGroup(id = AuthorsGroup.randomId(), authors = randomAuthors())
fun randomAuthorsGroup() = AuthorsGroup(id = AuthorsGroup.randomId()).withAuthors(
randomAuthors()
)

fun randomAuthors() = 1.until(Random().nextInt(1, 4)).map { randomAuthor() }.toMutableSet()

Expand Down

0 comments on commit 1f116b3

Please sign in to comment.