* Checking result of service
* Added additional error code
This commit is contained in:
Mariusz Pacek committed 2018-03-17 17:40:26 +01:00
1 parent 66ef0e4504
commit 794e14cf64
9 files changed
+86 -23

No files matched your search

+6
View File
@@ -17,6 +17,7 @@
<wiremock.version>2.15.0</wiremock.version>
<jackson-annotations.version>2.9.4</jackson-annotations.version>
<jmh-core.version>1.20</jmh-core.version>
<jsonassert.version>1.5.0</jsonassert.version>
</properties>
<dependencies>
@@ -59,6 +60,11 @@
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.skyscreamer</groupId>
<artifactId>jsonassert</artifactId>
<version>${jsonassert.version}</version>
</dependency>
</dependencies>
<build>
@@ -6,7 +6,8 @@ public enum ResultCodes {
BAD_REQUEST(400, 500, "Github client: Bad request"),
NOT_FOUND(404, 404, "Github client: Given repository or owner doesn't exist"),
FORBIDDEN(403, 403, "Github client: API rate limit exceeded"),
TIMEOUT(504, 504, "Githoub client: connection timeout");
TIMEOUT(504, 504, "Githoub client: connection timeout"),
BAD_GATEWAY(503, 503, "Githoub client: connection timeout");
private final int statusCode;
@@ -7,9 +7,10 @@ import ratpack.handling.Context;
import ratpack.http.client.HttpClientReadTimeoutException;
import ratpack.path.InvalidPathEncodingException;
import java.net.ConnectException;
import static edu.allegro.exercise.handlers.HandlersUtils.handleError;
import static io.netty.handler.codec.http.HttpResponseStatus.GATEWAY_TIMEOUT;
import static io.netty.handler.codec.http.HttpResponseStatus.INTERNAL_SERVER_ERROR;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
public class ErrorHandler implements ServerErrorHandler {
private static final Logger log = LoggerFactory.getLogger(ErrorHandler.class);
@@ -18,6 +19,8 @@ public class ErrorHandler implements ServerErrorHandler {
public void error(Context context, Throwable throwable) throws Exception {
if (throwable instanceof HttpClientReadTimeoutException) {
handleError(context, GATEWAY_TIMEOUT.code());
} else if (throwable instanceof ConnectException) {
handleError(context, SERVICE_UNAVAILABLE.code());
}
handleError(context, INTERNAL_SERVER_ERROR.code());
log.error("Internal error occur: ", throwable);
+1 -1
View File
@@ -6,7 +6,7 @@ server:
app:
clientPoolSize: 20
clientTimeout: 10
githubResourceV3: https://api.github.com/repos/
githubResourceV3: https://api.github.com
githubUserAgent: Allegro-exercise
githubAccept: application/vnd.github.v3+json
githubUserBasic: YWxsZWdyb0V4ZXJjaXNlOkNpSHVuQmFIaWR1andpNg==
@@ -1,6 +1,7 @@
package edu.allegro.exercise;
import edu.allegro.exercise.model.github.ErrorResponse;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import java.util.HashSet;
@@ -14,6 +15,7 @@ public class ResultCodesTest {
public void errorResponseShouldBe404Test() {
ErrorResponse errorResponse = ResultCodes.fromCode(404);
assertTrue("Github error server 404 should be mapped on 404 error response", errorResponse.getStatusCode() == 404);
assertTrue("And description should not be empty", StringUtils.isNotEmpty(errorResponse.getStatusDescription()));
}
@@ -21,6 +23,7 @@ public class ResultCodesTest {
public void errorResponseShouldBe400Test() {
ErrorResponse errorResponse = ResultCodes.fromCode(403);
assertTrue("Github error server 403 should be mapped on 400 error response", errorResponse.getStatusCode() == 403);
assertTrue("And description should not be empty", StringUtils.isNotEmpty(errorResponse.getStatusDescription()));
}
@@ -28,6 +31,7 @@ public class ResultCodesTest {
public void errorResponseShouldBe500Test() {
ErrorResponse errorResponse = ResultCodes.fromCode(500);
assertTrue("Github error server 500 should be mapped on 500 error response", errorResponse.getStatusCode() == 500);
assertTrue("And description should not be empty", StringUtils.isNotEmpty(errorResponse.getStatusDescription()));
}
@Test
@@ -40,6 +44,7 @@ public class ResultCodesTest {
if (!notUsed.contains(i)) {
ErrorResponse errorResponse = ResultCodes.fromCode(i);
assertTrue("For error code: " + i + " ErrorResponse should be 500", errorResponse.getStatusCode() == 500);
assertTrue("And description should not be empty", StringUtils.isNotEmpty(errorResponse.getStatusDescription()));
}
}
}
@@ -1,9 +1,13 @@
package edu.allegro.exercise.handlers;
import org.apache.logging.log4j.core.util.IOUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
public class GetUserRepoInfoTest {
public class HandlerUtilTest {
private static final Map<String, Object> CONFIGURATION = new HashMap<String, Object>() {{
put("githubResourceV3", "http://localhost:1111");
@@ -21,4 +25,9 @@ public class GetUserRepoInfoTest {
}
public static String getFileFromResourceAsString(String name) throws IOException {
InputStreamReader reader = new InputStreamReader(Thread.currentThread().getContextClassLoader().getResourceAsStream(name));
return IOUtils.toString(reader);
}
}
@@ -1,10 +1,12 @@
package edu.allegro.exercise.handlers;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
import com.github.tomakehurst.wiremock.common.Slf4jNotifier;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.slf4j.Logger;
@@ -15,16 +17,19 @@ import ratpack.error.internal.DefaultProductionErrorHandler;
import ratpack.http.client.ReceivedResponse;
import ratpack.registry.Registry;
import ratpack.server.RatpackServer;
import ratpack.server.ServerConfigBuilder;
import ratpack.test.embed.EmbeddedApp;
import ratpack.test.http.TestHttpClient;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
import static org.junit.Assert.assertTrue;
import static org.skyscreamer.jsonassert.JSONAssert.assertEquals;
public class IntegrationTest {
private static final Logger log = LoggerFactory.getLogger(IntegrationTest.class);
private static final String GITHUB_OWNER = "sinu0";
private static final String GIHTUB_REPO = "SiSPD";
private static final String GIHTUB_REPO_NOK = "SiSPDs";
private static final String GIHTUB_REPO_NOT_FOUND = "SiSPDs";
private static final String GITHUB_REPO_FORBIDDEN = "Forbidden";
private static final String GITHUB_REPO_TIMEOUT = "timeout";
private final EmbeddedApp app;
@@ -34,13 +39,17 @@ public class IntegrationTest {
public IntegrationTest() throws Exception {
rule.start();
GetUserRepoInfoService getUserRepoInfoService = GetUserRepoInfoTest.userRepoTest();
GetUserRepoInfoService getUserRepoInfoService = HandlerUtilTest.userRepoTest();
app = EmbeddedApp.fromServer(RatpackServer.of(ratpackServerSpec ->
ratpackServerSpec
.serverConfig(ServerConfigBuilder::build)
.serverConfig(conf -> conf
.threads(1)
)
.registry(Registry.builder()
.add(ClientErrorHandler.class, new DefaultProductionErrorHandler())
.add(ServerErrorHandler.class, new ErrorHandler())
.with(registrySpec ->
registrySpec.add(new ObjectMapper().registerModule(new Jdk8Module())))
.build())
.handlers(getUserRepoInfoService.create())));
}
@@ -55,47 +64,63 @@ public class IntegrationTest {
@Test
public void shouldReturnForbiddenTest() throws Exception {
app.test(httpClient -> {
ReceivedResponse receivedResponse = httpClient.get(String.format("/repositories/%s/%s", GITHUB_OWNER, GITHUB_REPO_FORBIDDEN));
Assert.assertTrue("Response should be nok for owner: " + GITHUB_OWNER + " and for repo: " + GITHUB_REPO_FORBIDDEN, receivedResponse.getStatusCode() == HttpResponseStatus.FORBIDDEN.code());
ReceivedResponse receivedResponse = whenClientCall(httpClient, GITHUB_REPO_FORBIDDEN);
assertTrue("Response should be FORBIDDEN for owner: " + GITHUB_OWNER + " and for repo: " + GITHUB_REPO_FORBIDDEN, receivedResponse.getStatusCode() == HttpResponseStatus.FORBIDDEN.code());
assertTrue("And description should not be empty", StringUtils.isNotEmpty(receivedResponse.getBody().getText()));
});
}
@Test
public void shouldReturnOKInfoAboutOwnerRepoTest() throws Exception {
app.test(httpClient -> {
ReceivedResponse receivedResponse = httpClient.get(String.format("/repositories/%s/%s", GITHUB_OWNER, GIHTUB_REPO));
String jsonExpected = HandlerUtilTest.getFileFromResourceAsString("jsonExpected.json");
ReceivedResponse receivedResponse = whenClientCall(httpClient, GIHTUB_REPO);
String response = receivedResponse.getBody().getText();
logResponse(receivedResponse);
Assert.assertTrue("Response should be ok for owner: " + GITHUB_OWNER + " and for repo: " + GIHTUB_REPO, receivedResponse.getStatusCode() == HttpResponseStatus.OK.code());
assertTrue("Response should be OK for owner: " + GITHUB_OWNER + " and for repo: " + GIHTUB_REPO, receivedResponse.getStatusCode() == HttpResponseStatus.OK.code());
assertTrue("And description should not be empty", StringUtils.isNotEmpty(receivedResponse.getBody().getText()));
assertEquals(jsonExpected, response, true);
});
}
@Test
public void forNotExistingRepoShouldReturn404Test() throws Exception {
app.test(httpClient -> {
ReceivedResponse receivedResponse = httpClient.get(String.format("/repositories/%s/%s", GITHUB_OWNER, GIHTUB_REPO_NOK));
Assert.assertTrue("Response should be nok for owner: " + GITHUB_OWNER + " and for repo: " + GIHTUB_REPO_NOK, receivedResponse.getStatusCode() == HttpResponseStatus.NOT_FOUND.code());
ReceivedResponse receivedResponse = whenClientCall(httpClient, GIHTUB_REPO_NOT_FOUND);
assertTrue("Response should be NOT_FOUND for owner: " + GITHUB_OWNER + " and for repo: " + GIHTUB_REPO_NOT_FOUND, receivedResponse.getStatusCode() == HttpResponseStatus.NOT_FOUND.code());
assertTrue("And description should not be empty", StringUtils.isNotEmpty(receivedResponse.getBody().getText()));
});
}
@Test
public void whenGithubShutdownShouldReturn500OnGithubUnavailabilityTest() throws Exception {
public void whenGithubShutdownShouldReturn503OnGithubUnavailabilityTest() throws Exception {
rule.stop();
app.test(httpClient -> {
ReceivedResponse receivedResponse = httpClient.get(String.format("/repositories/%s/%s", GITHUB_OWNER, GIHTUB_REPO_NOK));
Assert.assertTrue("Response should be nok for owner: " + GITHUB_OWNER + " and for repo: " + GIHTUB_REPO_NOK, receivedResponse.getStatusCode() == HttpResponseStatus.INTERNAL_SERVER_ERROR.code());
ReceivedResponse receivedResponse = whenClientCall(httpClient, GIHTUB_REPO);
assertTrue("Response should be for owner: " + GITHUB_OWNER + " and for repo: " + GIHTUB_REPO, receivedResponse.getStatusCode() == HttpResponseStatus.SERVICE_UNAVAILABLE.code());
assertTrue("And description should not be empty", StringUtils.isNotEmpty(receivedResponse.getBody().getText()));
});
}
@Test
public void whenGithubIsVerySlowShouldReturnTimeout() throws Exception {
app.test(httpClient -> {
ReceivedResponse receivedResponse = httpClient.get(String.format("/repositories/%s/%s", GITHUB_OWNER, GITHUB_REPO_TIMEOUT));
logResponse(receivedResponse);
Assert.assertTrue("Response should be 504 for owner: " + GITHUB_OWNER + " and for repo: " + GITHUB_REPO_TIMEOUT, receivedResponse.getStatusCode() == HttpResponseStatus.GATEWAY_TIMEOUT.code());
ReceivedResponse receivedResponse = whenClientCall(httpClient, GITHUB_REPO_TIMEOUT);
assertTrue("Response should be 504 for owner: " + GITHUB_OWNER + " and for repo: " + GITHUB_REPO_TIMEOUT, receivedResponse.getStatusCode() == HttpResponseStatus.GATEWAY_TIMEOUT.code());
assertTrue("And description should not be empty", StringUtils.isNotEmpty(receivedResponse.getBody().getText()));
});
}
private ReceivedResponse whenClientCall(TestHttpClient httpClient, String gihtubRepo) {
return httpClient.get(String.format("/repositories/%s/%s", GITHUB_OWNER, gihtubRepo));
}
private void logResponse(ReceivedResponse response) {
log.info(response.getHeaders().asMultiValueMap().toString());
log.info(response.getStatus().toString());
+7
View File
@@ -0,0 +1,7 @@
{
"full_name": "sinu0/SiSPD",
"description": "Symulacja i sterowanie procesów dyskretnych - projekt.",
"created_at": "2012-10-22T16:19:46Z",
"clone_url": "https://github.com/sinu0/SiSPD.git",
"stargazers_count": 0
}
+7
View File
@@ -0,0 +1,7 @@
{
"full_name": "sinu0/scribe-java",
"description": "Simple OAuth library for Java",
"created_at": "2012-08-18T13:17:18Z",
"clone_url": "https://github.com/sinu0/scribe-java.git",
"stargazers_count": 0
}