Skip to content

Commit

Permalink
#55 - Tests refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
iakunin committed May 14, 2020
1 parent 1dc72e4 commit f362f49
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 283 deletions.
16 changes: 14 additions & 2 deletions src/test/java/dev/iakunin/codexiabot/AbstractIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.github.database.rider.spring.api.DBRider;
import com.github.javafaker.Faker;
import dev.iakunin.codexiabot.util.PostgreSQLContainer;
import dev.iakunin.codexiabot.util.WireMockServer;
import lombok.extern.slf4j.Slf4j;
import net.dean.jraw.RedditClient;
import org.junit.jupiter.api.AfterEach;
import org.kohsuke.github.GitHub;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -26,7 +28,8 @@
@TestPropertySource(properties = {
"app.scheduling.enable=false",
"spring.liquibase.enabled=false",
"spring.main.allow-bean-definition-overriding=true"
"spring.main.allow-bean-definition-overriding=true",
"app.github.service.lines-of-code.delay=0",
})
abstract public class AbstractIntegrationTest {

Expand Down Expand Up @@ -58,8 +61,17 @@ public void initialize(ConfigurableApplicationContext configurableApplicationCon
),
"app.database.name=" + PostgreSQLContainer.getInstance().getDatabaseName(),
"spring.datasource.username=" + PostgreSQLContainer.getInstance().getUsername(),
"spring.datasource.password=" + PostgreSQLContainer.getInstance().getPassword()
"spring.datasource.password=" + PostgreSQLContainer.getInstance().getPassword(),
"app.codexia.base-url=" + WireMockServer.getInstance().baseUrl() + "/codexia",
"app.codetabs.base-url=" + WireMockServer.getInstance().baseUrl() + "/codetabs",
"app.hackernews.base-url=" + WireMockServer.getInstance().baseUrl() + "/hackernews"
).applyTo(configurableApplicationContext.getEnvironment());
}
}

@AfterEach
// @todo: check all child classes
void after() {
WireMockServer.getInstance().resetAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
AbstractIntegrationTest.TestConfig.class,
GithubConfig.class,
})
@ContextConfiguration(initializers = CodexiaParserIntegrationTest.Initializer.class)
public class CodexiaParserIntegrationTest extends AbstractIntegrationTest {

@Autowired
Expand Down Expand Up @@ -103,18 +102,4 @@ public void deletedProject() {

codexiaParser.run();
}

@AfterEach
void after() {
WireMockServer.getInstance().resetAll();
}

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"app.codexia.base-url=" + WireMockServer.getInstance().baseUrl() + "/codexia"
).applyTo(applicationContext.getEnvironment());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(initializers = ProjectsHealthCheckIntegrationTest.Initializer.class)
public class ProjectsHealthCheckIntegrationTest extends AbstractIntegrationTest {

@Autowired
Expand All @@ -42,7 +41,7 @@ public void noActiveProjectsInRepo() {
public void twoActiveProjectsInRepoButDeletedInCodexia() {
WireMockServer.stub(
new Stub(
new Request(WireMock.urlPathMatching("/p/\\d+\\.json")),
new Request(WireMock.urlPathMatching("/codexia/p/\\d+\\.json")),
new Response(
new ResourceOf(
"wiremock/codexia/cron/projects-health-check/twoActiveProjectsInRepoButDeletedInCodexia.json"
Expand All @@ -53,18 +52,4 @@ public void twoActiveProjectsInRepoButDeletedInCodexia() {

projectsHealthCheck.run();
}

@AfterEach
void after() {
WireMockServer.getInstance().resetAll();
}

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"app.codexia.base-url=" + WireMockServer.getInstance().baseUrl()
).applyTo(applicationContext.getEnvironment());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@
import dev.iakunin.codexiabot.util.wiremock.Request;
import dev.iakunin.codexiabot.util.wiremock.Response;
import dev.iakunin.codexiabot.util.wiremock.Stub;
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.http.HttpStatus;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(initializers = ResendErroneousReviewsIntegrationTest.Initializer.class)
public class ResendErroneousReviewsIntegrationTest extends AbstractIntegrationTest {

@Autowired
Expand Down Expand Up @@ -53,7 +47,7 @@ public void noErroneous() {
public void oneErroneous() {
WireMockServer.stub(
new Stub(
new Request(RequestMethod.POST, WireMock.urlPathMatching("/p/\\d+/post")),
new Request(RequestMethod.POST, WireMock.urlPathMatching("/codexia/p/\\d+/post")),
new Response("Review successfully sent")
)
);
Expand All @@ -70,7 +64,7 @@ public void oneErroneous() {
public void oneSuccessfulOneErroneous() {
WireMockServer.stub(
new Stub(
new Request(RequestMethod.POST, WireMock.urlPathMatching("/p/\\d+/post")),
new Request(RequestMethod.POST, WireMock.urlPathMatching("/codexia/p/\\d+/post")),
new Response("Review successfully sent")
)
);
Expand All @@ -87,7 +81,7 @@ public void oneSuccessfulOneErroneous() {
public void twoErroneous() {
WireMockServer.stub(
new Stub(
new Request(RequestMethod.POST, WireMock.urlPathMatching("/p/\\d+/post")),
new Request(RequestMethod.POST, WireMock.urlPathMatching("/codexia/p/\\d+/post")),
new Response("Review successfully sent")
)
);
Expand All @@ -104,25 +98,11 @@ public void twoErroneous() {
public void reviewSentWith500() {
WireMockServer.stub(
new Stub(
new Request(RequestMethod.POST, WireMock.urlPathMatching("/p/\\d+/post")),
new Request(RequestMethod.POST, WireMock.urlPathMatching("/codexia/p/\\d+/post")),
new Response(HttpStatus.INTERNAL_SERVER_ERROR.value())
)
);

resendErroneousReviews.run();
}

@AfterEach
void after() {
WireMockServer.getInstance().resetAll();
}

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"app.codexia.base-url=" + WireMockServer.getInstance().baseUrl()
).applyTo(applicationContext.getEnvironment());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@
import dev.iakunin.codexiabot.util.wiremock.Request;
import dev.iakunin.codexiabot.util.wiremock.Response;
import dev.iakunin.codexiabot.util.wiremock.Stub;
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.http.HttpStatus;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(initializers = ResendReviewsUntilDuplicatedIntegrationTest.Initializer.class)
public class ResendReviewsUntilDuplicatedIntegrationTest extends AbstractIntegrationTest {

@Autowired
Expand Down Expand Up @@ -63,25 +57,11 @@ public void oneSuccessfulNotificationWithDuplicatedCode() {
public void happyPath() {
WireMockServer.stub(
new Stub(
new Request(RequestMethod.POST, WireMock.urlPathMatching("/p/\\d+/post")),
new Request(RequestMethod.POST, WireMock.urlPathMatching("/codexia/p/\\d+/post")),
new Response(HttpStatus.NOT_FOUND.value(), "Review already exists.")
)
);

cron.run();
}

@AfterEach
void after() {
WireMockServer.getInstance().resetAll();
}

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"app.codexia.base-url=" + WireMockServer.getInstance().baseUrl()
).applyTo(applicationContext.getEnvironment());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@
import dev.iakunin.codexiabot.util.wiremock.Request;
import dev.iakunin.codexiabot.util.wiremock.Response;
import dev.iakunin.codexiabot.util.wiremock.Stub;
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.http.HttpStatus;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(initializers = SendReviewsIntegrationTest.Initializer.class)
public class SendReviewsIntegrationTest extends AbstractIntegrationTest {

@Autowired
Expand All @@ -43,7 +37,7 @@ public void noReviewsToSend() {
public void reviewSuccessfullySent() {
WireMockServer.stub(
new Stub(
new Request(RequestMethod.POST, WireMock.urlPathMatching("/p/\\d+/post")),
new Request(RequestMethod.POST, WireMock.urlPathMatching("/codexia/p/\\d+/post")),
new Response()
)
);
Expand All @@ -60,7 +54,7 @@ public void reviewSuccessfullySent() {
public void reviewSentWithDuplicate_responseBodyExists() {
WireMockServer.stub(
new Stub(
new Request(RequestMethod.POST, WireMock.urlPathMatching("/p/\\d+/post")),
new Request(RequestMethod.POST, WireMock.urlPathMatching("/codexia/p/\\d+/post")),
new Response(HttpStatus.NOT_FOUND.value(), "Review already exists")
)
);
Expand All @@ -77,7 +71,7 @@ public void reviewSentWithDuplicate_responseBodyExists() {
public void reviewSentWithDuplicate_responseBodyEmpty() {
WireMockServer.stub(
new Stub(
new Request(RequestMethod.POST, WireMock.urlPathMatching("/p/\\d+/post")),
new Request(RequestMethod.POST, WireMock.urlPathMatching("/codexia/p/\\d+/post")),
new Response(HttpStatus.NOT_FOUND.value())
)
);
Expand All @@ -94,25 +88,11 @@ public void reviewSentWithDuplicate_responseBodyEmpty() {
public void reviewSentWith500() {
WireMockServer.stub(
new Stub(
new Request(RequestMethod.POST, WireMock.urlPathMatching("/p/\\d+/post")),
new Request(RequestMethod.POST, WireMock.urlPathMatching("/codexia/p/\\d+/post")),
new Response(HttpStatus.INTERNAL_SERVER_ERROR.value())
)
);

sendReviews.run();
}

@AfterEach
void after() {
WireMockServer.getInstance().resetAll();
}

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"app.codexia.base-url=" + WireMockServer.getInstance().baseUrl()
).applyTo(applicationContext.getEnvironment());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
AbstractIntegrationTest.TestConfig.class,
GithubConfig.class,
})
@ContextConfiguration(initializers = GithubModuleCreateRepoIntegrationTest.Initializer.class)
public class GithubModuleCreateRepoIntegrationTest extends AbstractIntegrationTest {

@Autowired
Expand Down Expand Up @@ -160,13 +159,4 @@ public void notFoundInGithub() throws IOException {
void after() {
WireMockServer.getInstance().resetAll();
}

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"app.codexia.base-url=" + WireMockServer.getInstance().baseUrl() + "/codexia"
).applyTo(applicationContext.getEnvironment());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,10 @@
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.http.HttpStatus;
import org.springframework.test.context.ContextConfiguration;

@ContextConfiguration(initializers = CodexiaIntegrationTest.Initializer.class)
public class CodexiaIntegrationTest extends AbstractIntegrationTest {

@Autowired
Expand All @@ -43,7 +37,7 @@ public void emptyDatabase() {
public void processedRepo() {
WireMockServer.stub(
new Stub(
new Request(WireMock.urlPathEqualTo("/loc")),
new Request(WireMock.urlPathEqualTo("/codetabs/loc")),
new Response(
new ResourceOf("wiremock/github/cron/stat/loc/happyPath.json")
)
Expand All @@ -62,7 +56,7 @@ public void processedRepo() {
public void happyPath() {
WireMockServer.stub(
new Stub(
new Request(WireMock.urlPathEqualTo("/loc")),
new Request(WireMock.urlPathEqualTo("/codetabs/loc")),
new Response(
new ResourceOf("wiremock/github/cron/stat/loc/happyPath.json")
)
Expand All @@ -81,7 +75,7 @@ public void happyPath() {
public void tooManyRequests() {
WireMockServer.stub(
new Stub(
new Request(WireMock.urlPathEqualTo("/loc")),
new Request(WireMock.urlPathEqualTo("/codetabs/loc")),
new Response(HttpStatus.TOO_MANY_REQUESTS.value())
)
);
Expand All @@ -98,26 +92,11 @@ public void tooManyRequests() {
public void codetabsException() {
WireMockServer.stub(
new Stub(
new Request(WireMock.urlPathEqualTo("/loc")),
new Request(WireMock.urlPathEqualTo("/codetabs/loc")),
new Response(HttpStatus.INTERNAL_SERVER_ERROR.value())
)
);

linesOfCode.run();
}

@AfterEach
void after() {
WireMockServer.getInstance().resetAll();
}

static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
TestPropertyValues.of(
"app.codetabs.base-url=" + WireMockServer.getInstance().baseUrl(),
"app.github.service.lines-of-code.delay=0"
).applyTo(applicationContext.getEnvironment());
}
}
}
Loading

0 comments on commit f362f49

Please sign in to comment.