Created
February 16, 2017 21:17
-
-
Save rogerzxu/9b68addb0165637944bd0322e308a65a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package echo360.searchindexer.service | |
import echo360.search.model._ | |
import echo360.searchindexer.fixtures.{SearchDataFixtures, SearchIndexEventFixtures} | |
import echo360.searchindexer.util.SearchDataException | |
import echo360.test.EchoMatchers | |
import org.mockito.Mockito._ | |
import org.scalatest.mockito.MockitoSugar | |
import org.scalatest.{FlatSpec, Matchers} | |
import scala.concurrent.duration.Duration | |
import scala.concurrent.{Await, Future} | |
class SearchIndexEventConsumerSpec extends FlatSpec | |
with Matchers | |
with EchoMatchers | |
with MockitoSugar | |
with SearchDataFixtures | |
with SearchIndexEventFixtures { | |
"process" should "process valid CreateOrUpdate SearchIndexEvent" in new SearchIndexEventConsumerTest { | |
givenSuccessfulDataRetrieval | |
searchIndexEventConsumer.process(indexQuestionRecord) | |
verify(indexerService, times(1)).indexObj(any, any) | |
} | |
it should "process valid Delete SearchIndexEvent" in new SearchIndexEventConsumerTest { | |
searchIndexEventConsumer.process(deleteQuestionRecord) | |
verify(indexerService, times(1)).deleteObj(any) | |
} | |
it should "not process unrecognized json message" in new SearchIndexEventConsumerTest { | |
searchIndexEventConsumer.process(unrecognizedJsonRecord) | |
verify(indexerService, times(0)).indexObj(any, any) | |
} | |
it should "not process invalid json message" in new SearchIndexEventConsumerTest { | |
searchIndexEventConsumer.process(invalidJsonRecord) | |
verify(indexerService, times(0)).indexObj(any, any) | |
} | |
"processSyncEvent" should "index question given question data" in new SearchIndexEventConsumerTest { | |
givenSuccessfulDataRetrieval | |
Await.ready(searchIndexEventConsumer.processSyncEvent(indexQuestionEvent), Duration(5, "seconds")) | |
verify(searchDataService, times(1)).getQuestion(anyString) | |
verify(indexerService, times(1)).indexObj(any, any[Question]) | |
} | |
it should "index user given user data" in new SearchIndexEventConsumerTest { | |
givenSuccessfulDataRetrieval | |
Await.ready(searchIndexEventConsumer.processSyncEvent(indexUserEvent), Duration(5, "seconds")) | |
verify(searchDataService, times(1)).getUser(anyString) | |
verify(indexerService, times(1)).indexObj(any, any[User]) | |
} | |
it should "index lesson given lesson data" in new SearchIndexEventConsumerTest { | |
givenSuccessfulDataRetrieval | |
Await.ready(searchIndexEventConsumer.processSyncEvent(indexLessonEvent), Duration(5, "seconds")) | |
verify(searchDataService, times(1)).getLesson(anyString) | |
verify(indexerService, times(1)).indexObj(any, any[Lesson]) | |
} | |
it should "index course given course data" in new SearchIndexEventConsumerTest { | |
givenSuccessfulDataRetrieval | |
Await.ready(searchIndexEventConsumer.processSyncEvent(indexCourseEvent), Duration(5, "seconds")) | |
verify(searchDataService, times(1)).getCourse(anyString) | |
verify(indexerService, times(1)).indexObj(any, any[Course]) | |
} | |
it should "index presentation given presentation data" in new SearchIndexEventConsumerTest { | |
givenSuccessfulDataRetrieval | |
Await.ready(searchIndexEventConsumer.processSyncEvent(indexPresentationEvent), Duration(5, "seconds")) | |
verify(searchDataService, times(1)).getPresentation(anyString) | |
verify(indexerService, times(1)).indexObj(any, any[Presentation]) | |
} | |
it should "index note given note data" in new SearchIndexEventConsumerTest { | |
givenSuccessfulDataRetrieval | |
Await.ready(searchIndexEventConsumer.processSyncEvent(indexNoteEvent), Duration(5, "seconds")) | |
verify(searchDataService, times(1)).getNote(anyString) | |
verify(indexerService, times(1)).indexObj(any, any[Note]) | |
} | |
it should "index video given video data" in new SearchIndexEventConsumerTest { | |
givenSuccessfulDataRetrieval | |
Await.ready(searchIndexEventConsumer.processSyncEvent(indexVideoEvent), Duration(5, "seconds")) | |
verify(searchDataService, times(1)).getVideo(anyString) | |
verify(indexerService, times(1)).indexObj(any, any[Video]) | |
} | |
it should "not index obj if data is not found" in new SearchIndexEventConsumerTest { | |
givenUnsuccessfulDataRetrieval | |
Await.ready(searchIndexEventConsumer.processSyncEvent(indexQuestionEvent), Duration(5, "seconds")) | |
verify(indexerService, times(0)).indexObj(any, any) | |
} | |
it should "delete index for deletion event" in new SearchIndexEventConsumerTest { | |
Await.ready(searchIndexEventConsumer.processSyncEvent(deleteQuestionEvent), Duration(5, "seconds")) | |
verify(indexerService, times(1)).deleteObj(any) | |
} | |
it should "catch SearchDataExceptions and not index" in new SearchIndexEventConsumerTest { | |
givenFailedDataRetrieval | |
Await.ready(searchIndexEventConsumer.processSyncEvent(indexQuestionEvent), Duration(5, "seconds")) | |
verify(indexerService, times(0)).indexObj(any, any) | |
} | |
trait SearchIndexEventConsumerTest { | |
val indexerService = mock[IndexerService] | |
val searchDataService = mock[SearchDataService] | |
val searchIndexEventConsumer = new SearchIndexEventConsumer(indexerService, searchDataService) | |
def givenSuccessfulDataRetrieval = { | |
when(searchDataService.getQuestion(anyString)).thenReturn(Future.successful(Some(question))) | |
when(searchDataService.getUser(anyString)).thenReturn(Future.successful(Some(user))) | |
when(searchDataService.getLesson(anyString)).thenReturn(Future.successful(Some(lesson))) | |
when(searchDataService.getCourse(anyString)).thenReturn(Future.successful(Some(course))) | |
when(searchDataService.getPresentation(anyString)).thenReturn(Future.successful(Some(presentation))) | |
when(searchDataService.getNote(anyString)).thenReturn(Future.successful(Some(noteData))) | |
when(searchDataService.getVideo(anyString)).thenReturn(Future.successful(Some(video))) | |
} | |
def givenFailedDataRetrieval = { | |
val ex = SearchDataException("failed to get search data", new Exception("cause")) | |
when(searchDataService.getQuestion(anyString)).thenReturn(Future.failed(ex)) | |
when(searchDataService.getUser(anyString)).thenReturn(Future.failed(ex)) | |
when(searchDataService.getLesson(anyString)).thenReturn(Future.failed(ex)) | |
when(searchDataService.getCourse(anyString)).thenReturn(Future.failed(ex)) | |
when(searchDataService.getPresentation(anyString)).thenReturn(Future.failed(ex)) | |
when(searchDataService.getNote(anyString)).thenReturn(Future.failed(ex)) | |
when(searchDataService.getVideo(anyString)).thenReturn(Future.failed(ex)) | |
} | |
def givenUnsuccessfulDataRetrieval = { | |
when(searchDataService.getQuestion(anyString)).thenReturn(Future.successful(None)) | |
when(searchDataService.getUser(anyString)).thenReturn(Future.successful(None)) | |
when(searchDataService.getLesson(anyString)).thenReturn(Future.successful(None)) | |
when(searchDataService.getCourse(anyString)).thenReturn(Future.successful(None)) | |
when(searchDataService.getPresentation(anyString)).thenReturn(Future.successful(None)) | |
when(searchDataService.getNote(anyString)).thenReturn(Future.successful(None)) | |
when(searchDataService.getVideo(anyString)).thenReturn(Future.successful(None)) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment