Refactor change config, add error response loging, add additonal error code

This commit is contained in:
Mariusz Pacek committed 2018-03-12 22:09:46 +01:00
1 parent f3f42628ea
commit 1e9d7e69aa
9 files changed
+46 -40

No files matched your search

+8 -8
View File
@@ -13,6 +13,10 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<ratpack.version>1.5.4</ratpack.version>
<slf4j-api.version>1.7.25</slf4j-api.version>
<junit.version>4.12</junit.version>
<wiremock.version>2.15.0</wiremock.version>
<jackson-annotations.version>2.9.4</jackson-annotations.version>
<jmh-core.version>1.20</jmh-core.version>
</properties>
<dependencies>
@@ -26,7 +30,6 @@
<artifactId>ratpack-test</artifactId>
<version>${ratpack.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
@@ -35,16 +38,13 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.tomakehurst/wiremock -->
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock</artifactId>
<version>2.15.0</version>
<type>pom</type>
<scope>test</scope>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-annotations.version}</version>
</dependency>
</dependencies>
@@ -3,13 +3,12 @@ package edu.allegro.exercise;
public enum Constant {
APP_CONFIG("/app"),
GITHUB_USER_AGENT_HEADER("githubUserAgent"),
GITHUB_ACCEPT_HEADER("githubAcept"),
GITHUB_ACCEPT_HEADER("githubAccept"),
GITHUB_REPOSITORY_OWNER_INFO("githubResourceV3"),
ACCEPT("Accept"),
USER_AGENT("User-Agent"),
CONFIG_YAML("config.yaml"),
GITHUB_USER("githubUser"),
GITHUB_PASSWORD("githubPassword"), CLIENT_POOL_SIZE("clientPoolSize"), CLIENT_TIMEOUT("clientTimeout");
CLIENT_POOL_SIZE("clientPoolSize"), CLIENT_TIMEOUT("clientTimeout");
private final String prop;
@@ -3,8 +3,10 @@ package edu.allegro.exercise;
import edu.allegro.exercise.model.github.ErrorResponse;
public enum ResultCodes {
BAD_REQUEST(400, 500, "Internal error - Bad github request"),
NOT_FOUND(404, 404, "Not found - Given repository or owner doesn't exist");
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");
private final int statusCode;
private final int internalCode;
@@ -6,6 +6,8 @@ import edu.allegro.exercise.ResultCodes;
import edu.allegro.exercise.model.github.ErrorResponse;
import edu.allegro.exercise.model.github.UserRepoInfo;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ratpack.exec.Promise;
import ratpack.func.Action;
import ratpack.func.Function;
@@ -21,6 +23,7 @@ import java.util.Optional;
import static ratpack.jackson.Jackson.json;
public class GetUserRepoInfoService {
private static final Logger log = LoggerFactory.getLogger(GetUserRepoInfoService.class);
private static final String OWNER = "owner";
private static final String REPO = "repo";
private static final String PATH = ":" + OWNER + "/:" + REPO;
@@ -50,7 +53,7 @@ public class GetUserRepoInfoService {
}
private boolean ifSuccess(ReceivedResponse response) {
return response.getStatusCode() == HttpResponseStatus.OK.code();
return response.getStatusCode() < HttpResponseStatus.BAD_REQUEST.code();
}
private Function<ReceivedResponse, Optional<ReceivedResponse>> handleOK() {
@@ -59,6 +62,7 @@ public class GetUserRepoInfoService {
private Function<ReceivedResponse, Optional<ReceivedResponse>> handleNOK(Context ctx) {
return response -> {
logErrorResponse(response);
ErrorResponse errorResponse = ResultCodes.fromCode(response.getStatusCode());
ctx.getResponse().status(errorResponse.getStatusCode());
ctx.render(json(errorResponse));
@@ -66,6 +70,12 @@ public class GetUserRepoInfoService {
};
}
private void logErrorResponse(ReceivedResponse response) {
log.info(response.getHeaders().asMultiValueMap().toString());
log.info(response.getStatus().toString());
log.info(response.getBody().getText());
}
private String getToken(Context ctx, String token) {
return ctx.getPathTokens().get(token);
}
@@ -12,13 +12,13 @@ import ratpack.http.internal.NettyHeadersBackedHeaders;
import java.net.URI;
import java.net.URISyntaxException;
import java.time.Duration;
import java.util.Base64;
import java.util.Map;
import static edu.allegro.exercise.Constant.*;
public class RepositoryInfo {
public static final String GITHUB_USER_BASIC = "githubUserBasic";
private static final String AUTHORIZATION = "Authorization";
private static final String BASIC = "Basic ";
private final HttpClient client;
@@ -44,12 +44,9 @@ public class RepositoryInfo {
}
public Promise<ReceivedResponse> getInfo(String owner, String repo) throws URISyntaxException {
StringBuilder urlBuilder = new StringBuilder(resourceLink)
.append(owner)
.append("/")
.append(repo);
String url = resourceLink + owner + "/" + repo;
return client.get(new URI(urlBuilder.toString()),
return client.get(new URI(url),
this::setHeaders);
}
@@ -68,12 +65,9 @@ public class RepositoryInfo {
}
private void setAuthorizationHeader(Map<String, Object> configuration, DefaultHttpHeaders defaultHttpHeaders) {
String githubUser = (String) configuration.get(GITHUB_USER.getName());
String githubPassword = (String) configuration.get(GITHUB_PASSWORD.getName());
if (!StringUtil.isNullOrEmpty(githubPassword) && !StringUtil.isNullOrEmpty(githubPassword)) {
String credentials = githubUser + ":" + githubPassword;
byte[] encode = Base64.getEncoder().encode(credentials.getBytes());
defaultHttpHeaders.set(AUTHORIZATION, BASIC + new String(encode));
String pass = (String) configuration.get(GITHUB_USER_BASIC);
if (!StringUtil.isNullOrEmpty(pass)) {
defaultHttpHeaders.set(AUTHORIZATION, BASIC + pass);
}
}
}
@@ -1,8 +1,8 @@
package edu.allegro.exercise.model.github;
public class ErrorResponse {
private int StatusCode;
private String StatusDescription;
private final int StatusCode;
private final String StatusDescription;
public ErrorResponse(int statusCode, String statusDescription) {
StatusCode = statusCode;
+2 -3
View File
@@ -8,6 +8,5 @@ app:
clientTimeout: 10
githubResourceV3: https://api.github.com/repos/
githubUserAgent: Allegro-exercise
githubAcept: application/vnd.github.v3+json
githubUser: sinu0
githubPassword: myk700i
githubAccept: application/vnd.github.v3+json
githubUserBasic: YWxsZWdyb0V4ZXJjaXNlOkNpSHVuQmFIaWR1andpNg==
@@ -6,14 +6,18 @@ import java.util.Map;
public class GetUserRepoInfoTest {
private static final Map<String, Object> CONFIGURATION = new HashMap<String, Object>() {{
put("githubResourceV3", "https://api.github.com/repos/%s/%s");
put("githubResourceV3", "https://api.github.com/repos/");
put("githubUserAgent", "Allegro-exercise");
put("githubAcept", "application/vnd.github.v3+json");
put("githubAccept", "application/vnd.github.v3+json");
put("githubUserBasic", "YWxsZWdyb0V4ZXJjaXNlOkNpSHVuQmFIaWR1andpNg==");
put("clientPoolSize", 10);
put("clientTimeout", 10);
}};
public static GetUserRepoInfoService userRepotTest() throws Exception {
return new GetUserRepoInfoService(CONFIGURATION);
public static GetUserRepoInfoService userRepoTest() throws Exception {
RepositoryInfo info = new RepositoryInfo(CONFIGURATION);
return new GetUserRepoInfoService(info);
}
@@ -1,5 +1,6 @@
package edu.allegro.exercise.handlers;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.junit.*;
import ratpack.http.client.ReceivedResponse;
@@ -13,13 +14,12 @@ public class IntegrationTest {
private static final String GITHUB_OWNER = "sinu0";
private static final String GIHTUB_REPO = "SiSPD";
private static final String GIHTUB_REPO_NOK = "SiSPDs";
@Rule
public WireMockRule wireMockRule = new WireMockRule(8089);
private EmbeddedApp app;
@Before
public void setUp() throws Exception {
GetUserRepoInfoService getUserRepoInfoService = GetUserRepoInfoTest.userRepotTest();
GetUserRepoInfoService getUserRepoInfoService = GetUserRepoInfoTest.userRepoTest();
app = EmbeddedApp.fromServer(RatpackServer.of(ratpackServerSpec ->
ratpackServerSpec
.serverConfig(ServerConfigBuilder::build)
@@ -65,7 +65,5 @@ public class IntegrationTest {
Assert.assertTrue("Response should be nok for owner:" + GITHUB_OWNER + " and for repo: " + GIHTUB_REPO_NOK, receivedResponse.getStatusCode() != HttpResponseStatus.OK.code());
}
private void performanceTest() {
}
}