From d59e09e8aef8dd6b1c659009d5276626ba1a02b0 Mon Sep 17 00:00:00 2001 From: Chia-Ping Tsai Date: Sat, 13 May 2023 20:39:05 +0800 Subject: [PATCH] [WEB] rewrite http.Response by java 17 record --- .../common/http/HttpExecutorBuilder.java | 5 +- .../org/astraea/common/http/Response.java | 36 ++----------- .../connector/ConnectorClientBuilderTest.java | 13 +---- .../org/astraea/common/http/ResponseTest.java | 54 +------------------ 4 files changed, 9 insertions(+), 99 deletions(-) diff --git a/common/src/main/java/org/astraea/common/http/HttpExecutorBuilder.java b/common/src/main/java/org/astraea/common/http/HttpExecutorBuilder.java index 3aa8b7f377..b361b83f12 100644 --- a/common/src/main/java/org/astraea/common/http/HttpExecutorBuilder.java +++ b/common/src/main/java/org/astraea/common/http/HttpExecutorBuilder.java @@ -63,8 +63,9 @@ private CompletionStage> send(HttpRequest request, TypeRef ty if (r.statusCode() < 400) { if (typeRef != null && r.body() == null) throw new IllegalStateException("There is no body!!!"); - if (typeRef == null) return Response.of(null, r.statusCode()); - return Response.of(jsonConverter.fromJson(r.body(), typeRef), r.statusCode()); + if (typeRef == null) return new Response<>(r.statusCode()); + return new Response<>( + jsonConverter.fromJson(r.body(), typeRef), r.statusCode()); } if (r.body() == null || r.body().isBlank()) throw new HttpRequestException(r.statusCode()); diff --git a/common/src/main/java/org/astraea/common/http/Response.java b/common/src/main/java/org/astraea/common/http/Response.java index 2c644e738d..ae5d3006cb 100644 --- a/common/src/main/java/org/astraea/common/http/Response.java +++ b/common/src/main/java/org/astraea/common/http/Response.java @@ -16,39 +16,9 @@ */ package org.astraea.common.http; -public interface Response { +public record Response(T body, int statusCode) { - int statusCode(); - - T body(); - - static Response of(int code) { - return new Response<>() { - - @Override - public int statusCode() { - return code; - } - - @Override - public Void body() { - return null; - } - }; - } - - static Response of(T body, int code) { - return new Response<>() { - - @Override - public int statusCode() { - return code; - } - - @Override - public T body() { - return body; - } - }; + public Response(int code) { + this(null, code); } } diff --git a/common/src/test/java/org/astraea/common/connector/ConnectorClientBuilderTest.java b/common/src/test/java/org/astraea/common/connector/ConnectorClientBuilderTest.java index 7ebaec387e..d2472a359a 100644 --- a/common/src/test/java/org/astraea/common/connector/ConnectorClientBuilderTest.java +++ b/common/src/test/java/org/astraea/common/connector/ConnectorClientBuilderTest.java @@ -46,18 +46,7 @@ void testExecutor() throws MalformedURLException, ExecutionException, Interrupte var httpExecutor = Mockito.mock(HttpExecutor.class); Mockito.when(httpExecutor.get(Mockito.any(), Mockito.eq(TypeRef.set(String.class)))) .thenReturn( - CompletableFuture.completedFuture( - new Response<>() { - @Override - public int statusCode() { - return 200; - } - - @Override - public Set body() { - return Set.of("SpecialConnectorName"); - } - })); + CompletableFuture.completedFuture(new Response<>(Set.of("SpecialConnectorName"), 200))); var connectorClient = ConnectorClient.builder() diff --git a/common/src/test/java/org/astraea/common/http/ResponseTest.java b/common/src/test/java/org/astraea/common/http/ResponseTest.java index 59fd118d3a..3aa84a6b4c 100644 --- a/common/src/test/java/org/astraea/common/http/ResponseTest.java +++ b/common/src/test/java/org/astraea/common/http/ResponseTest.java @@ -18,69 +18,19 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import java.net.URI; -import java.net.http.HttpClient.Version; -import java.net.http.HttpHeaders; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.util.Optional; -import javax.net.ssl.SSLSession; import org.junit.jupiter.api.Test; class ResponseTest { @Test void testStatusCode() { - var response = Response.of("aa", 10); + var response = new Response<>("aa", 10); assertEquals(10, response.statusCode()); } @Test void testBody() { - var response = Response.of("bodyValue", 10); + var response = new Response<>("bodyValue", 10); assertEquals("bodyValue", response.body()); } - - static class EmptyResponseImpl implements HttpResponse { - - @Override - public int statusCode() { - return 0; - } - - @Override - public HttpRequest request() { - return null; - } - - @Override - public Optional> previousResponse() { - return Optional.empty(); - } - - @Override - public HttpHeaders headers() { - return null; - } - - @Override - public String body() { - return null; - } - - @Override - public Optional sslSession() { - return Optional.empty(); - } - - @Override - public URI uri() { - return null; - } - - @Override - public Version version() { - return null; - } - } }