Skip to content

Commit

Permalink
Migrate code away from anonymous class using initializers
Browse files Browse the repository at this point in the history
While it's possible to create a pre-populated collection using `new
CollectionClass() { { ... add elements ... } }`, it is not the idiomatic
way to do it.
  • Loading branch information
findepi committed Jan 16, 2022
1 parent 57cfebe commit 53596c3
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 71 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,7 @@ public void testAllLoggingDisabledShouldTimeout()
server.enqueue(new MockResponse()
.setResponseCode(200));

EventListener querylogListener = factory.create(new HashMap<>() {{
put("http-event-listener.connect-ingest-uri", server.url("/").toString());
}});
EventListener querylogListener = factory.create(Map.of("http-event-listener.connect-ingest-uri", server.url("/").toString()));
querylogListener.queryCreated(null);
querylogListener.queryCompleted(null);
querylogListener.splitCompleted(null);
Expand All @@ -235,12 +233,11 @@ public void testAllLoggingDisabledShouldTimeout()
public void testAllLoggingEnabledShouldSendCorrectEvent()
throws Exception
{
EventListener querylogListener = factory.create(new HashMap<>() {{
put("http-event-listener.log-created", "true");
put("http-event-listener.log-completed", "true");
put("http-event-listener.log-split", "true");
put("http-event-listener.connect-ingest-uri", server.url("/").toString());
}});
EventListener querylogListener = factory.create(Map.of(
"http-event-listener.log-created", "true",
"http-event-listener.log-completed", "true",
"http-event-listener.log-split", "true",
"http-event-listener.connect-ingest-uri", server.url("/").toString()));

server.enqueue(new MockResponse().setResponseCode(200));
server.enqueue(new MockResponse().setResponseCode(200));
Expand All @@ -260,10 +257,9 @@ public void testAllLoggingEnabledShouldSendCorrectEvent()
public void testContentTypeDefaultHeaderShouldAlwaysBeSet()
throws Exception
{
EventListener querylogListener = factory.create(new HashMap<>(){{
put("http-event-listener.connect-ingest-uri", server.url("/").toString());
put("http-event-listener.log-completed", "true");
}});
EventListener querylogListener = factory.create(Map.of(
"http-event-listener.connect-ingest-uri", server.url("/").toString(),
"http-event-listener.log-completed", "true"));

server.enqueue(new MockResponse().setResponseCode(200));

Expand All @@ -276,20 +272,18 @@ public void testContentTypeDefaultHeaderShouldAlwaysBeSet()
public void testHttpHeadersShouldBePresent()
throws Exception
{
EventListener querylogListener = factory.create(new HashMap<>(){{
put("http-event-listener.connect-ingest-uri", server.url("/").toString());
put("http-event-listener.log-completed", "true");
put("http-event-listener.connect-http-headers", "Authorization: Trust Me!, Cache-Control: no-cache");
}});
EventListener querylogListener = factory.create(Map.of(
"http-event-listener.connect-ingest-uri", server.url("/").toString(),
"http-event-listener.log-completed", "true",
"http-event-listener.connect-http-headers", "Authorization: Trust Me!, Cache-Control: no-cache"));

server.enqueue(new MockResponse().setResponseCode(200));

querylogListener.queryCompleted(queryCompleteEvent);

checkRequest(server.takeRequest(5, TimeUnit.SECONDS), new HashMap<>() {{
put("Authorization", "Trust Me!");
put("Cache-Control", "no-cache");
}}, queryCompleteEvent);
checkRequest(server.takeRequest(5, TimeUnit.SECONDS), Map.of(
"Authorization", "Trust Me!",
"Cache-Control", "no-cache"), queryCompleteEvent);
}

@Test
Expand All @@ -299,12 +293,11 @@ public void testHttpsEnabledShouldUseTLSv13()
setupServerTLSCertificate();
server.enqueue(new MockResponse().setResponseCode(200));

EventListener querylogListener = factory.create(new HashMap<>(){{
put("http-event-listener.connect-ingest-uri", server.url("/").toString());
put("http-event-listener.log-completed", "true");
put("http-event-listener.http-client.key-store-path", "src/test/resources/trino-httpquery-test.p12");
put("http-event-listener.http-client.key-store-password", "testing-ssl");
}});
EventListener querylogListener = factory.create(Map.of(
"http-event-listener.connect-ingest-uri", server.url("/").toString(),
"http-event-listener.log-completed", "true",
"http-event-listener.http-client.key-store-path", "src/test/resources/trino-httpquery-test.p12",
"http-event-listener.http-client.key-store-password", "testing-ssl"));
querylogListener.queryCompleted(queryCompleteEvent);

RecordedRequest recordedRequest = server.takeRequest(5, TimeUnit.SECONDS);
Expand All @@ -322,12 +315,11 @@ public void testDifferentCertificatesShouldNotSendRequest()
setupServerTLSCertificate();
server.enqueue(new MockResponse().setResponseCode(200));

EventListener querylogListener = factory.create(new HashMap<>(){{
put("http-event-listener.connect-ingest-uri", server.url("/").toString());
put("http-event-listener.log-completed", "true");
put("http-event-listener.http-client.key-store-path", "src/test/resources/trino-httpquery-test2.p12");
put("http-event-listener.http-client.key-store-password", "testing-ssl");
}});
EventListener querylogListener = factory.create(Map.of(
"http-event-listener.connect-ingest-uri", server.url("/").toString(),
"http-event-listener.log-completed", "true",
"http-event-listener.http-client.key-store-path", "src/test/resources/trino-httpquery-test2.p12",
"http-event-listener.http-client.key-store-password", "testing-ssl"));
querylogListener.queryCompleted(queryCompleteEvent);

RecordedRequest recordedRequest = server.takeRequest(5, TimeUnit.SECONDS);
Expand All @@ -341,12 +333,11 @@ public void testNoServerCertificateShouldNotSendRequest()
{
server.enqueue(new MockResponse().setResponseCode(200));

EventListener querylogListener = factory.create(new HashMap<>(){{
put("http-event-listener.connect-ingest-uri", new URL("https", server.getHostName(), server.getPort(), "/").toString());
put("http-event-listener.log-completed", "true");
put("http-event-listener.http-client.key-store-path", "src/test/resources/trino-httpquery-test.p12");
put("http-event-listener.http-client.key-store-password", "testing-ssl");
}});
EventListener querylogListener = factory.create(Map.of(
"http-event-listener.connect-ingest-uri", new URL("https", server.getHostName(), server.getPort(), "/").toString(),
"http-event-listener.log-completed", "true",
"http-event-listener.http-client.key-store-path", "src/test/resources/trino-httpquery-test.p12",
"http-event-listener.http-client.key-store-password", "testing-ssl"));
querylogListener.queryCompleted(queryCompleteEvent);

RecordedRequest recordedRequest = server.takeRequest(5, TimeUnit.SECONDS);
Expand All @@ -358,11 +349,10 @@ public void testNoServerCertificateShouldNotSendRequest()
public void testServer500ShouldRetry()
throws Exception
{
EventListener querylogListener = factory.create(new HashMap<>(){{
put("http-event-listener.connect-ingest-uri", server.url("/").toString());
put("http-event-listener.log-completed", "true");
put("http-event-listener.connect-retry-count", "1");
}});
EventListener querylogListener = factory.create(Map.of(
"http-event-listener.connect-ingest-uri", server.url("/").toString(),
"http-event-listener.log-completed", "true",
"http-event-listener.connect-retry-count", "1"));

server.enqueue(new MockResponse().setResponseCode(500));
server.enqueue(new MockResponse().setResponseCode(200));
Expand All @@ -377,11 +367,10 @@ public void testServer500ShouldRetry()
public void testServer400ShouldRetry()
throws Exception
{
EventListener querylogListener = factory.create(new HashMap<>(){{
put("http-event-listener.connect-ingest-uri", server.url("/").toString());
put("http-event-listener.log-completed", "true");
put("http-event-listener.connect-retry-count", "1");
}});
EventListener querylogListener = factory.create(Map.of(
"http-event-listener.connect-ingest-uri", server.url("/").toString(),
"http-event-listener.log-completed", "true",
"http-event-listener.connect-retry-count", "1"));

server.enqueue(new MockResponse().setResponseCode(400));
server.enqueue(new MockResponse().setResponseCode(200));
Expand All @@ -396,11 +385,10 @@ public void testServer400ShouldRetry()
public void testServerDisconnectShouldRetry()
throws Exception
{
EventListener querylogListener = factory.create(new HashMap<>(){{
put("http-event-listener.connect-ingest-uri", server.url("/").toString());
put("http-event-listener.log-completed", "true");
put("http-event-listener.connect-retry-count", "1");
}});
EventListener querylogListener = factory.create(Map.of(
"http-event-listener.connect-ingest-uri", server.url("/").toString(),
"http-event-listener.log-completed", "true",
"http-event-listener.connect-retry-count", "1"));

server.enqueue(new MockResponse().setSocketPolicy(SocketPolicy.DISCONNECT_AT_START));
server.enqueue(new MockResponse().setResponseCode(200));
Expand All @@ -415,10 +403,9 @@ public void testServerDisconnectShouldRetry()
public void testServerDelayDoesNotBlock()
throws Exception
{
EventListener querylogListener = factory.create(new HashMap<>(){{
put("http-event-listener.connect-ingest-uri", server.url("/").toString());
put("http-event-listener.log-completed", "true");
}});
EventListener querylogListener = factory.create(Map.of(
"http-event-listener.connect-ingest-uri", server.url("/").toString(),
"http-event-listener.log-completed", "true"));

server.enqueue(new MockResponse().setResponseCode(200).setHeadersDelay(5, TimeUnit.SECONDS));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import io.airlift.units.Duration;
import org.testng.annotations.Test;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -47,17 +46,16 @@ public void testDefaults()
public void testExplicitPropertyMappings()
throws Exception
{
Map<String, String> properties = new HashMap<>() {{
put("http-event-listener.log-created", "true");
put("http-event-listener.log-completed", "true");
put("http-event-listener.log-split", "true");
put("http-event-listener.connect-ingest-uri", "http://example.com:8080/api");
put("http-event-listener.connect-http-headers", "Authorization: Trust Me, Cache-Control: no-cache");
put("http-event-listener.connect-retry-count", "2");
put("http-event-listener.connect-retry-delay", "101s");
put("http-event-listener.connect-backoff-base", "1.5");
put("http-event-listener.connect-max-delay", "10m");
}};
Map<String, String> properties = Map.of(
"http-event-listener.log-created", "true",
"http-event-listener.log-completed", "true",
"http-event-listener.log-split", "true",
"http-event-listener.connect-ingest-uri", "http://example.com:8080/api",
"http-event-listener.connect-http-headers", "Authorization: Trust Me, Cache-Control: no-cache",
"http-event-listener.connect-retry-count", "2",
"http-event-listener.connect-retry-delay", "101s",
"http-event-listener.connect-backoff-base", "1.5",
"http-event-listener.connect-max-delay", "10m");

HttpEventListenerConfig expected = new HttpEventListenerConfig()
.setLogCompleted(true)
Expand Down

0 comments on commit 53596c3

Please sign in to comment.