diff --git a/src/test/java/dev/iakunin/codexiabot/hackernews/cron/GapsFillerIntegrationTest.java b/src/test/java/dev/iakunin/codexiabot/hackernews/cron/GapsFillerIntegrationTest.java index 7189c6bd..c9c35c13 100644 --- a/src/test/java/dev/iakunin/codexiabot/hackernews/cron/GapsFillerIntegrationTest.java +++ b/src/test/java/dev/iakunin/codexiabot/hackernews/cron/GapsFillerIntegrationTest.java @@ -1,13 +1,94 @@ package dev.iakunin.codexiabot.hackernews.cron; +import com.github.database.rider.core.api.dataset.DataSet; +import com.github.database.rider.core.api.dataset.ExpectedDataSet; import dev.iakunin.codexiabot.AbstractIntegrationTest; +import dev.iakunin.codexiabot.util.WireMockServer; +import dev.iakunin.codexiabot.util.wiremock.Request; +import dev.iakunin.codexiabot.util.wiremock.Response; +import dev.iakunin.codexiabot.util.wiremock.Stub; +import org.cactoos.io.ResourceOf; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.util.TestPropertyValues; +import org.springframework.context.ApplicationContextInitializer; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.test.context.ContextConfiguration; +@ContextConfiguration(initializers = GapsFillerIntegrationTest.Initializer.class) public class GapsFillerIntegrationTest extends AbstractIntegrationTest { @Autowired private GapsFiller gapsFiller; - // @todo #10 Implement GapsFillerIntegrationTest + @Test + @DataSet( + value = "db-rider/hackernews/cron/gaps-filler/initial/emptyDatabase.yml", + cleanBefore = true, cleanAfter = true + ) + @ExpectedDataSet("db-rider/hackernews/cron/gaps-filler/expected/emptyDatabase.yml") + public void emptyDatabase() { + gapsFiller.run(); + } + @Test + @DataSet( + value = "db-rider/hackernews/cron/gaps-filler/initial/oneMissing.yml", + cleanBefore = true, cleanAfter = true + ) + @ExpectedDataSet("db-rider/hackernews/cron/gaps-filler/expected/oneMissing.yml") + public void oneMissing() { + WireMockServer.stub( + new Stub( + new Request("/item/2.json"), + new Response( + new ResourceOf("wiremock/hackernews/cron/gaps-filler/2.json") + ) + ) + ); + + gapsFiller.run(); + } + + @Test + @DataSet( + value = "db-rider/hackernews/cron/gaps-filler/initial/twoMissing.yml", + cleanBefore = true, cleanAfter = true + ) + @ExpectedDataSet("db-rider/hackernews/cron/gaps-filler/expected/twoMissing.yml") + public void twoMissing() { + WireMockServer.stub( + new Stub( + new Request("/item/2.json"), + new Response( + new ResourceOf("wiremock/hackernews/cron/gaps-filler/2.json") + ) + ) + ); + WireMockServer.stub( + new Stub( + new Request("/item/4.json"), + new Response( + new ResourceOf("wiremock/hackernews/cron/gaps-filler/4.json") + ) + ) + ); + + gapsFiller.run(); + } + + @AfterEach + void after() { + WireMockServer.getInstance().resetAll(); + } + + static class Initializer implements ApplicationContextInitializer { + @Override + public void initialize(ConfigurableApplicationContext applicationContext) { + TestPropertyValues.of( + "app.hackernews.base-url=" + WireMockServer.getInstance().baseUrl() + ).applyTo(applicationContext.getEnvironment()); + } + } } diff --git a/src/test/resources/db-rider/hackernews/cron/gaps-filler/expected/emptyDatabase.yml b/src/test/resources/db-rider/hackernews/cron/gaps-filler/expected/emptyDatabase.yml new file mode 100644 index 00000000..598ffa20 --- /dev/null +++ b/src/test/resources/db-rider/hackernews/cron/gaps-filler/expected/emptyDatabase.yml @@ -0,0 +1 @@ +hackernews_item: [] diff --git a/src/test/resources/db-rider/hackernews/cron/gaps-filler/expected/oneMissing.yml b/src/test/resources/db-rider/hackernews/cron/gaps-filler/expected/oneMissing.yml new file mode 100644 index 00000000..0f0dcf81 --- /dev/null +++ b/src/test/resources/db-rider/hackernews/cron/gaps-filler/expected/oneMissing.yml @@ -0,0 +1,25 @@ +hackernews_item: + - + external_id: 1 + type: story + by: test1author + title: Title for test1 + url: test 1 url + time: 2009-01-15 05:56:17 + deleted: false + - + external_id: 3 + type: story + by: test3author + title: Title for test3 + url: test 3 url + time: 2009-01-15 05:58:12 + deleted: false + - + external_id: 2 + type: story + by: test2author + title: Title for test2 + url: test 2 url + time: 2009-01-15 06:12:49 + deleted: false diff --git a/src/test/resources/db-rider/hackernews/cron/gaps-filler/expected/twoMissing.yml b/src/test/resources/db-rider/hackernews/cron/gaps-filler/expected/twoMissing.yml new file mode 100644 index 00000000..c886bc80 --- /dev/null +++ b/src/test/resources/db-rider/hackernews/cron/gaps-filler/expected/twoMissing.yml @@ -0,0 +1,41 @@ +hackernews_item: + - + external_id: 1 + type: story + by: test1author + title: Title for test1 + url: test 1 url + time: 2009-01-15 05:56:17 + deleted: false + - + external_id: 3 + type: story + by: test3author + title: Title for test3 + url: test 3 url + time: 2009-01-15 05:58:12 + deleted: false + - + external_id: 5 + type: story + by: test5author + title: Title for test5 + url: test 5 url + time: 2009-01-15 06:32:52 + deleted: false + - + external_id: 2 + type: story + by: test2author + title: Title for test2 + url: test 2 url + time: 2009-01-15 06:12:49 + deleted: false + - + external_id: 4 + type: story + by: test4author + title: Title for test4 + url: test 4 url + time: 2009-01-15 06:12:53 + deleted: false diff --git a/src/test/resources/db-rider/hackernews/cron/gaps-filler/initial/emptyDatabase.yml b/src/test/resources/db-rider/hackernews/cron/gaps-filler/initial/emptyDatabase.yml new file mode 100644 index 00000000..598ffa20 --- /dev/null +++ b/src/test/resources/db-rider/hackernews/cron/gaps-filler/initial/emptyDatabase.yml @@ -0,0 +1 @@ +hackernews_item: [] diff --git a/src/test/resources/db-rider/hackernews/cron/gaps-filler/initial/oneMissing.yml b/src/test/resources/db-rider/hackernews/cron/gaps-filler/initial/oneMissing.yml new file mode 100644 index 00000000..71e5df0c --- /dev/null +++ b/src/test/resources/db-rider/hackernews/cron/gaps-filler/initial/oneMissing.yml @@ -0,0 +1,17 @@ +hackernews_item: + - + external_id: 1 + type: story + by: test1author + title: Title for test1 + url: test 1 url + time: 2009-01-15 05:56:17 + deleted: false + - + external_id: 3 + type: story + by: test3author + title: Title for test3 + url: test 3 url + time: 2009-01-15 05:58:12 + deleted: false diff --git a/src/test/resources/db-rider/hackernews/cron/gaps-filler/initial/twoMissing.yml b/src/test/resources/db-rider/hackernews/cron/gaps-filler/initial/twoMissing.yml new file mode 100644 index 00000000..37288ac6 --- /dev/null +++ b/src/test/resources/db-rider/hackernews/cron/gaps-filler/initial/twoMissing.yml @@ -0,0 +1,25 @@ +hackernews_item: + - + external_id: 1 + type: story + by: test1author + title: Title for test1 + url: test 1 url + time: 2009-01-15 05:56:17 + deleted: false + - + external_id: 3 + type: story + by: test3author + title: Title for test3 + url: test 3 url + time: 2009-01-15 05:58:12 + deleted: false + - + external_id: 5 + type: story + by: test5author + title: Title for test5 + url: test 5 url + time: 2009-01-15 06:32:52 + deleted: false diff --git a/src/test/resources/wiremock/hackernews/cron/gaps-filler/2.json b/src/test/resources/wiremock/hackernews/cron/gaps-filler/2.json new file mode 100644 index 00000000..f36420c0 --- /dev/null +++ b/src/test/resources/wiremock/hackernews/cron/gaps-filler/2.json @@ -0,0 +1,11 @@ +{ + "by": "test2author", + "id": 2, + "text": "Some awesome text for test 2.", + "title": "Title for test2", + "url": "test 2 url", + "time": 1231999969, + "type": "story", + "descendants": 0, + "score": 2 +} diff --git a/src/test/resources/wiremock/hackernews/cron/gaps-filler/4.json b/src/test/resources/wiremock/hackernews/cron/gaps-filler/4.json new file mode 100644 index 00000000..7581b84c --- /dev/null +++ b/src/test/resources/wiremock/hackernews/cron/gaps-filler/4.json @@ -0,0 +1,11 @@ +{ + "by": "test4author", + "id": 4, + "text": "Some awesome text for test 4.", + "title": "Title for test4", + "url": "test 4 url", + "time": 1231999973, + "type": "story", + "descendants": 30, + "score": 55 +}