diff --git a/pom.xml b/pom.xml index 104bd05..3125158 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,10 @@ UTF-8 1.5.4 1.7.25 + 4.12 + 2.15.0 + 2.9.4 + 1.20 @@ -26,7 +30,6 @@ ratpack-test ${ratpack.version} - org.apache.logging.log4j log4j-slf4j-impl @@ -35,16 +38,13 @@ junit junit - RELEASE + ${junit.version} test - - com.github.tomakehurst - wiremock - 2.15.0 - pom - test + com.fasterxml.jackson.core + jackson-annotations + ${jackson-annotations.version} diff --git a/src/main/java/edu/allegro/exercise/Constant.java b/src/main/java/edu/allegro/exercise/Constant.java index 3db1110..e0f1513 100644 --- a/src/main/java/edu/allegro/exercise/Constant.java +++ b/src/main/java/edu/allegro/exercise/Constant.java @@ -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; diff --git a/src/main/java/edu/allegro/exercise/ResultCodes.java b/src/main/java/edu/allegro/exercise/ResultCodes.java index 1d4743e..62ad7e3 100644 --- a/src/main/java/edu/allegro/exercise/ResultCodes.java +++ b/src/main/java/edu/allegro/exercise/ResultCodes.java @@ -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; diff --git a/src/main/java/edu/allegro/exercise/handlers/GetUserRepoInfoService.java b/src/main/java/edu/allegro/exercise/handlers/GetUserRepoInfoService.java index eee2417..2896edb 100644 --- a/src/main/java/edu/allegro/exercise/handlers/GetUserRepoInfoService.java +++ b/src/main/java/edu/allegro/exercise/handlers/GetUserRepoInfoService.java @@ -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> handleOK() { @@ -59,6 +62,7 @@ public class GetUserRepoInfoService { private Function> 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); } diff --git a/src/main/java/edu/allegro/exercise/handlers/RepositoryInfo.java b/src/main/java/edu/allegro/exercise/handlers/RepositoryInfo.java index 17c0fff..7277a98 100644 --- a/src/main/java/edu/allegro/exercise/handlers/RepositoryInfo.java +++ b/src/main/java/edu/allegro/exercise/handlers/RepositoryInfo.java @@ -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 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 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); } } } diff --git a/src/main/java/edu/allegro/exercise/model/github/ErrorResponse.java b/src/main/java/edu/allegro/exercise/model/github/ErrorResponse.java index f536d00..f3a9dd9 100644 --- a/src/main/java/edu/allegro/exercise/model/github/ErrorResponse.java +++ b/src/main/java/edu/allegro/exercise/model/github/ErrorResponse.java @@ -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; diff --git a/src/main/resources/config.yaml b/src/main/resources/config.yaml index 98965a1..afb5ac9 100644 --- a/src/main/resources/config.yaml +++ b/src/main/resources/config.yaml @@ -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 \ No newline at end of file + githubAccept: application/vnd.github.v3+json + githubUserBasic: YWxsZWdyb0V4ZXJjaXNlOkNpSHVuQmFIaWR1andpNg== \ No newline at end of file diff --git a/src/test/java/edu/allegro/exercise/handlers/GetUserRepoInfoTest.java b/src/test/java/edu/allegro/exercise/handlers/GetUserRepoInfoTest.java index 48b35fa..9c24733 100644 --- a/src/test/java/edu/allegro/exercise/handlers/GetUserRepoInfoTest.java +++ b/src/test/java/edu/allegro/exercise/handlers/GetUserRepoInfoTest.java @@ -6,14 +6,18 @@ import java.util.Map; public class GetUserRepoInfoTest { private static final Map CONFIGURATION = new HashMap() {{ - 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); } diff --git a/src/test/java/edu/allegro/exercise/handlers/IntegrationTest.java b/src/test/java/edu/allegro/exercise/handlers/IntegrationTest.java index 5e97a84..2d490f6 100644 --- a/src/test/java/edu/allegro/exercise/handlers/IntegrationTest.java +++ b/src/test/java/edu/allegro/exercise/handlers/IntegrationTest.java @@ -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() { - } }