diff --git a/src/test/groovy/it/move2/agent/application/JobFixture.groovy b/src/test/groovy/it/move2/agent/application/JobFixture.groovy new file mode 100644 index 0000000..a549bf6 --- /dev/null +++ b/src/test/groovy/it/move2/agent/application/JobFixture.groovy @@ -0,0 +1,26 @@ +package it.move2.agent.application + +import it.move2.job.EmploymentType +import it.move2.job.JobOffer +import it.move2.job.Skill + +class JobFixture { + + static jobOffer(args = [:]) { + new JobOffer( + 'id' in args.keySet() ? args.id as String : 'test-id', + 'title' in args.keySet() ? args.title as String : 'test-title', + 'companyName' in args.keySet() ? args.companyName as String : 'test-company', + 'experience' in args.keySet() ? args.experience as String : 'test-experience', + 'skills' in args.keySet() ? args.skills as List : [], + 'employmentType' in args.keySet() ? args.employmentType as List : [], + 'remote' in args.keySet() ? args.remote as boolean : true + ) + } + + static List createListJobOffers(int count) { + return (1..count).collect { _ -> + jobOffer() + } + } +} \ No newline at end of file diff --git a/src/test/groovy/it/move2/agent/application/PublisherTest.groovy b/src/test/groovy/it/move2/agent/application/PublisherTest.groovy new file mode 100644 index 0000000..d45062e --- /dev/null +++ b/src/test/groovy/it/move2/agent/application/PublisherTest.groovy @@ -0,0 +1,36 @@ +package it.move2.agent.application + +import com.fasterxml.jackson.databind.ObjectMapper +import it.move2.agent.configuration.PublisherConfiguration +import it.move2.agent.ports.Queue +import it.move2.job.JobOffer +import spock.lang.Specification +import spock.lang.Subject + +class PublisherTest extends Specification { + + private PublisherConfiguration configuration + private ObjectMapper objectMapper + private Queue queue + + @Subject + Publisher publisher + + def setup() { + configuration = new PublisherConfiguration(2) + objectMapper = new ObjectMapper() + queue = Mock(Queue) + publisher = new Publisher(configuration, objectMapper, queue) + } + + def "publish should chunk job offers and publish them to the queue"() { + given: + List jobOffers = JobFixture.createListJobOffers(10) + + when: + publisher.publish(jobOffers) + + then: + 5 * queue.pub(_) + } +} diff --git a/src/test/groovy/it/move2/agent/application/UpdaterTest.groovy b/src/test/groovy/it/move2/agent/application/UpdaterTest.groovy new file mode 100644 index 0000000..c2f40d7 --- /dev/null +++ b/src/test/groovy/it/move2/agent/application/UpdaterTest.groovy @@ -0,0 +1,62 @@ +package it.move2.agent.application + +import com.fasterxml.jackson.databind.ObjectMapper +import it.move2.agent.configuration.PublisherConfiguration +import it.move2.agent.ports.JobBoard +import it.move2.agent.ports.Queue +import spock.lang.Specification +import spock.lang.Subject + +class UpdaterTest extends Specification { + private JobBoard jobBoard1 + private JobBoard jobBoard2 + private Queue queue + private ObjectMapper objectMapper + private Publisher publisher + + @Subject + private Updater updater + + def setup() { + jobBoard1 = Mock() + jobBoard2 = Mock() + queue = Mock() + objectMapper = new ObjectMapper() + publisher = new Publisher(new PublisherConfiguration(2), objectMapper, queue) + + updater = new Updater([jobBoard1, jobBoard2], publisher) + } + + def "should publish jobs from all job boards"() { + given: + def jobs1 = [JobFixture.jobOffer(), JobFixture.jobOffer()] + def jobs2 = [JobFixture.jobOffer(), JobFixture.jobOffer()] + + jobBoard1.getJobs() >> jobs1 + jobBoard2.getJobs() >> jobs2 + + when: + updater.execute() + + then: + noExceptionThrown() + + and: + 2 * queue.pub(_) >> {} + } + + def "should not publish jobs when error occurs"() { + given: + def jobs1 = [JobFixture.jobOffer(), JobFixture.jobOffer()] + + jobBoard1.getJobs() >> jobs1 + jobBoard2.getJobs() >> { throw new RuntimeException("Test error message") } + + when: + updater.execute() + + then: + thrown(RuntimeException) + 0 * queue.pub(_) >> {} + } +} \ No newline at end of file