Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log video creation #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/main/tv/codely/mooc/api/MoocApiApp.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ object MoocApiApp {

val container = new EntryPointDependencyContainer(
new UserModuleDependencyContainer(sharedDependencies.doobieDbConnection, sharedDependencies.messagePublisher),
new VideoModuleDependencyContainer(sharedDependencies.doobieDbConnection, sharedDependencies.messagePublisher)
new VideoModuleDependencyContainer(sharedDependencies.doobieDbConnection, sharedDependencies.messagePublisher, sharedDependencies.logger)
)

val routes = new Routes(container)
Expand Down
3 changes: 2 additions & 1 deletion app/test/tv/codely/HttpSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ abstract class HttpSpec extends WordSpec with Matchers with ScalaFutures with Sc
)
protected val videoDependencies = new VideoModuleDependencyContainer(
sharedDependencies.doobieDbConnection,
sharedDependencies.messagePublisher
sharedDependencies.messagePublisher,
sharedDependencies.logger
)(sharedDependencies.executionContext)

private val routes = new Routes(new EntryPointDependencyContainer(userDependencies, videoDependencies))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import tv.codely.mooc.shared.domain.user.UserId
import tv.codely.mooc.shared.infrastructure.marshaller.DomainEventsMarshaller.MessageMarshaller
import tv.codely.mooc.video.domain._
import tv.codely.shared.domain.bus.MessagePublisher
import tv.codely.shared.domain.logger.Logger

final class VideoCreator(repository: VideoRepository, publisher: MessagePublisher) {
final class VideoCreator(repository: VideoRepository, publisher: MessagePublisher, logger: Logger) {
def create(
id: VideoId,
title: VideoTitle,
Expand All @@ -17,6 +18,7 @@ final class VideoCreator(repository: VideoRepository, publisher: MessagePublishe

repository.save(video)

logger.info("Video created")
publisher.publish(VideoCreated(video))(MessageMarshaller)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ import scala.concurrent.Future

final class VideosSearcher(repository: VideoRepository) {
def all(): Future[Seq[Video]] = repository.all()

def findByTermInTitle(term: String): Future[Seq[Video]] = repository.findByTermInTitle(term)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ import scala.concurrent.Future
trait VideoRepository {
def all(): Future[Seq[Video]]

def findByTermInTitle(term: String): Future[Seq[Video]]

def save(video: Video): Future[Unit]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import tv.codely.mooc.video.application.create.VideoCreator
import tv.codely.mooc.video.application.search.VideosSearcher
import tv.codely.mooc.video.domain.VideoRepository
import tv.codely.mooc.video.infrastructure.repository.DoobieMySqlVideoRepository
import tv.codely.shared.domain.logger.Logger
import scala.concurrent.ExecutionContext

import tv.codely.shared.domain.bus.MessagePublisher

final class VideoModuleDependencyContainer(
doobieDbConnection: DoobieDbConnection,
messagePublisher: MessagePublisher
messagePublisher: MessagePublisher,
logger: Logger,
)(implicit executionContext: ExecutionContext) {
val repository: VideoRepository = new DoobieMySqlVideoRepository(doobieDbConnection)

val videosSearcher: VideosSearcher = new VideosSearcher(repository)
val videoCreator: VideoCreator = new VideoCreator(repository, messagePublisher)
val videoCreator: VideoCreator = new VideoCreator(repository, messagePublisher, logger)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ final class DoobieMySqlVideoRepository(db: DoobieDbConnection)(implicit executio
override def all(): Future[Seq[Video]] =
db.read(sql"SELECT video_id, title, duration_in_seconds, category, creator_id FROM videos".query[Video].to[Seq])

override def findByTermInTitle(term: String) =
db.read(
sql"SELECT video_id, title, duration_in_seconds, category, creator_id FROM videos WHERE title LIKE CONCAT('%', $term, '%')"
.query[Video]
.to[Seq])

override def save(video: Video): Future[Unit] =
sql"INSERT INTO videos(video_id, title, duration_in_seconds, category, creator_id) VALUES (${video.id}, ${video.title}, ${video.duration}, ${video.category}, ${video.creatorId})".update.run
.transact(db.transactor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import tv.codely.mooc.video.infrastructure.dependency_injection.VideoModuleDepen
import tv.codely.shared.infrastructure.integration.IntegrationTestCase

protected[video] trait VideoIntegrationTestCase extends IntegrationTestCase {
private val container = new VideoModuleDependencyContainer(doobieDbConnection, messagePublisher)
private val container = new VideoModuleDependencyContainer(doobieDbConnection, messagePublisher, logger)

protected val repository: VideoRepository = container.repository
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import tv.codely.mooc.shared.infrastructure.marshaller.DomainEventsMarshaller.Me
import tv.codely.mooc.video.domain.{VideoCreatedMother, VideoMother}
import tv.codely.mooc.video.infrastructure.repository.VideoRepositoryMock
import tv.codely.shared.infrastructure.rabbitmq.MessagePublisherMock
import tv.codely.shared.infrastructure.logger.LoggerMock
import tv.codely.shared.infrastructure.unit.UnitTestCase

final class VideoCreatorShould extends UnitTestCase with VideoRepositoryMock with MessagePublisherMock {
private val creator = new VideoCreator(repository, messagePublisher)
final class VideoCreatorShould extends UnitTestCase with VideoRepositoryMock with MessagePublisherMock with LoggerMock {
private val creator = new VideoCreator(repository, messagePublisher, logger)

"save a video" in {
val video = VideoMother.random
Expand All @@ -17,6 +18,8 @@ final class VideoCreatorShould extends UnitTestCase with VideoRepositoryMock wit

publisherShouldPublish(videoCreated)(MessageMarshaller)

loggerShouldInfo()

creator.create(video.id, video.title, video.duration, video.category, video.creatorId).shouldBe(())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,14 @@ final class VideosSearcherShould extends UnitTestCase with VideoRepositoryMock {

searcher.all().futureValue shouldBe existingVideos
}

"search all videos that contain a term in title" in {
val matchingVideo = VideoMother.random
val anotherMatchingVideo = VideoMother.random
val matchingVideos = Seq(matchingVideo, anotherMatchingVideo)
val term = "Term"
repositoryShouldFindByTermInTitle(term, matchingVideos)

searcher.findByTermInTitle(term).futureValue shouldBe matchingVideos
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tv.codely.mooc.video.infrastructure.repository

import tv.codely.mooc.video.VideoIntegrationTestCase
import tv.codely.mooc.video.domain.VideoMother
import tv.codely.mooc.video.domain.VideoTitleMother
import doobie.implicits._
import org.scalatest.BeforeAndAfterEach

Expand Down Expand Up @@ -29,4 +30,16 @@ final class DoobieMySqlVideoRepositoryShould extends VideoIntegrationTestCase wi

repository.all().futureValue shouldBe videos
}

"search all videos that contain a term in title" in {
val term = "Term"
val videoMatch = VideoMother.random.copy(title = VideoTitleMother("This has a Term inside"))
val videoNoMatch = VideoMother.random.copy(title = VideoTitleMother("This has not a ? inside"))

repository.save(videoMatch).futureValue
repository.save(videoNoMatch).futureValue

repository.findByTermInTitle(term).futureValue should contain(videoMatch)
repository.findByTermInTitle(term).futureValue should not contain (videoNoMatch)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ protected[video] trait VideoRepositoryMock extends MockFactory {
(repository.all _)
.expects()
.returning(Future.successful(videos))

protected def repositoryShouldFindByTermInTitle(term: String, videos: Seq[Video]): Unit =
(repository.findByTermInTitle _)
.expects(term)
.returning(Future.successful(videos))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package tv.codely.shared.infrastructure.logger

import org.scalamock.scalatest.MockFactory
import tv.codely.shared.domain.logger.Logger
import tv.codely.shared.infrastructure.unit.UnitTestCase

trait LoggerMock extends MockFactory {
this: UnitTestCase => // Make mandatory to also extend UnitTestCase in order to avoid using mocks in any other kind of test.

protected val logger: Logger = mock[Logger]

protected def loggerShouldInfo(): Unit =
(logger.info(_: String, _: Map[String, Any]))
.expects(*, *)
.returning(Unit)
}