Skip to content

Commit

Permalink
Replace testng with junit5 for gateway-ha module
Browse files Browse the repository at this point in the history
  • Loading branch information
andythsu committed Oct 4, 2023
1 parent a74b0bc commit 12b14c8
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 261 deletions.
6 changes: 6 additions & 0 deletions gateway-ha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.trino.gateway.ha;

import static org.junit.Assert.assertTrue;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import io.trino.gateway.ha.config.DataStoreConfiguration;
Expand All @@ -20,9 +22,11 @@
import okhttp3.RequestBody;
import okhttp3.Response;
import org.javalite.activejdbc.Base;
import org.testng.Assert;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@Slf4j
@TestInstance(Lifecycle.PER_CLASS)
public class HaGatewayTestUtils {
private static final OkHttpClient httpClient = new OkHttpClient();
private static final Random RANDOM = new Random();
Expand Down Expand Up @@ -114,7 +118,7 @@ public static void setUpBackend(
.post(requestBody)
.build();
Response response = httpClient.newCall(request).execute();
Assert.assertTrue(response.isSuccessful());
assertTrue(response.isSuccessful());
}

@Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package io.trino.gateway.ha;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.TestInstance.*;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

@TestInstance(Lifecycle.PER_CLASS)
public class TestGatewayHaMultipleBackend {
public static final String EXPECTED_RESPONSE1 = "{\"id\":\"testId1\"}";
public static final String EXPECTED_RESPONSE2 = "{\"id\":\"testId2\"}";
Expand All @@ -25,7 +29,7 @@ public class TestGatewayHaMultipleBackend {
private final WireMockServer scheduledBackend =
new WireMockServer(WireMockConfiguration.options().port(backend2Port));

@BeforeClass(alwaysRun = true)
@BeforeAll
public void setup() throws Exception {
HaGatewayTestUtils.prepareMockBackend(adhocBackend, "/v1/statement", EXPECTED_RESPONSE1);
HaGatewayTestUtils.prepareMockBackend(scheduledBackend, "/v1/statement", EXPECTED_RESPONSE2);
Expand Down Expand Up @@ -57,7 +61,7 @@ public void testQueryDeliveryToMultipleRoutingGroups() throws Exception {
.post(requestBody)
.build();
Response response1 = httpClient.newCall(request1).execute();
Assert.assertEquals(response1.body().string(), EXPECTED_RESPONSE1);
assertEquals(EXPECTED_RESPONSE1, response1.body().string());

// When X-Trino-Routing-Group is set in header, query should be routed to cluster under the
// routing group
Expand All @@ -68,10 +72,10 @@ public void testQueryDeliveryToMultipleRoutingGroups() throws Exception {
.addHeader("X-Trino-Routing-Group", "scheduled")
.build();
Response response4 = httpClient.newCall(request4).execute();
Assert.assertEquals(response4.body().string(), EXPECTED_RESPONSE2);
assertEquals(EXPECTED_RESPONSE2, response4.body().string());
}

@AfterClass(alwaysRun = true)
@AfterAll
public void cleanup() {
adhocBackend.stop();
scheduledBackend.stop();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
package io.trino.gateway.ha;

import static org.junit.Assert.assertEquals;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;

@TestInstance(Lifecycle.PER_CLASS)
public class TestGatewayHaSingleBackend {
public static final String EXPECTED_RESPONSE = "{\"id\":\"testId\"}";
int backendPort = 20000 + (int) (Math.random() * 1000);
Expand All @@ -21,7 +25,7 @@ public class TestGatewayHaSingleBackend {
new WireMockServer(WireMockConfiguration.options().port(backendPort));
private final OkHttpClient httpClient = new OkHttpClient();

@BeforeClass(alwaysRun = true)
@BeforeAll
public void setup() throws Exception {
HaGatewayTestUtils.prepareMockBackend(backend, "/v1/statement", EXPECTED_RESPONSE);

Expand All @@ -46,10 +50,10 @@ public void testRequestDelivery() throws Exception {
.post(requestBody)
.build();
Response response = httpClient.newCall(request).execute();
Assert.assertEquals(EXPECTED_RESPONSE, response.body().string());
assertEquals(EXPECTED_RESPONSE, response.body().string());
}

@AfterClass(alwaysRun = true)
@AfterAll
public void cleanup() {
backend.stop();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package io.trino.gateway.ha.handler;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.Request;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Test;

@TestInstance(Lifecycle.PER_CLASS)
public class TestQueryIdCachingProxyHandler {

@Test
public void testExtractQueryIdFromUrl() throws IOException {
String[] paths = {
Expand All @@ -24,7 +25,7 @@ public void testExtractQueryIdFromUrl() throws IOException {
"/ui/api/query.html?20200416_160256_03078_6b4yt"};
for (String path : paths) {
String queryId = QueryIdCachingProxyHandler.extractQueryIdIfPresent(path, null);
assertEquals(queryId, "20200416_160256_03078_6b4yt");
assertEquals("20200416_160256_03078_6b4yt", queryId);
}
String[] nonPaths = {
"/ui/api/query/myOtherThing",
Expand All @@ -46,8 +47,8 @@ public void testForwardedHostHeaderOnProxyRequest() throws IOException {
Request proxyRequest = httpClient.newRequest("http://localhost:80");
QueryIdCachingProxyHandler.setForwardedHostHeaderOnProxyRequest(mockServletRequest,
proxyRequest);
Assert.assertEquals(proxyRequest.getHeaders().get("Host"), String.format("%s:%s",
backendServer, backendPort));
assertEquals(String.format("%s:%s",
backendServer, backendPort), proxyRequest.getHeaders().get("Host"));
}

@Test
Expand All @@ -58,12 +59,11 @@ public void testUserFromRequest() throws IOException {
String authHeader = "Basic dGVzdDoxMjPCow==";
Mockito.when(req.getHeader(QueryIdCachingProxyHandler.AUTHORIZATION))
.thenReturn(authHeader);
Assert.assertEquals(QueryIdCachingProxyHandler.getQueryUser(req), "test");
assertEquals("test", QueryIdCachingProxyHandler.getQueryUser(req));

String user = "trino_user";
Mockito.when(req.getHeader(QueryIdCachingProxyHandler.USER_HEADER))
.thenReturn(user);
Assert.assertEquals(QueryIdCachingProxyHandler.getQueryUser(req), user);

assertEquals(user, QueryIdCachingProxyHandler.getQueryUser(req));
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,28 @@
package io.trino.gateway.ha.router;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.TestInstance.*;

import io.trino.gateway.ha.HaGatewayTestUtils;
import io.trino.gateway.ha.config.DataStoreConfiguration;
import io.trino.gateway.ha.config.ProxyBackendConfiguration;
import io.trino.gateway.ha.persistence.JdbcConnectionManager;
import java.io.File;
import java.util.List;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestMethodOrder;

@Test
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(Lifecycle.PER_CLASS)
public class TestHaGatewayManager {
private HaGatewayManager haGatewayManager;

@BeforeClass(alwaysRun = true)
@BeforeAll
public void setUp() {
File baseDir = new File(System.getProperty("java.io.tmpdir"));
File tempH2DbDir = new File(baseDir, "h2db-" + System.currentTimeMillis());
Expand All @@ -28,6 +35,8 @@ public void setUp() {
haGatewayManager = new HaGatewayManager(connectionManager);
}

@Test
@Order(1)
public void testAddBackend() {
ProxyBackendConfiguration backend = new ProxyBackendConfiguration();
backend.setActive(true);
Expand All @@ -36,25 +45,27 @@ public void testAddBackend() {
backend.setProxyTo("adhoc1.trino.gateway.io");
backend.setExternalUrl("adhoc1.trino.gateway.io");
ProxyBackendConfiguration updated = haGatewayManager.addBackend(backend);
Assert.assertEquals(updated, backend);
assertEquals(backend, updated);
}

@Test(dependsOnMethods = {"testAddBackend"})
@Test
@Order(2)
public void testGetBackends() {
List<ProxyBackendConfiguration> backends = haGatewayManager.getAllBackends();
Assert.assertEquals(backends.size(), 1);
assertEquals(1, backends.size());

backends = haGatewayManager.getActiveBackends("adhoc");
Assert.assertEquals(backends.size(), 1);
assertEquals(1, backends.size());

backends = haGatewayManager.getActiveBackends("unknown");
Assert.assertEquals(backends.size(), 0);
assertEquals(0, backends.size());

backends = haGatewayManager.getActiveAdhocBackends();
Assert.assertEquals(backends.size(), 1);
assertEquals(1, backends.size());
}

@Test(dependsOnMethods = {"testGetBackends"})
@Test
@Order(3)
public void testUpdateBackend() {
ProxyBackendConfiguration backend = new ProxyBackendConfiguration();
backend.setActive(false);
Expand All @@ -64,7 +75,7 @@ public void testUpdateBackend() {
backend.setExternalUrl("adhoc1.trino.gateway.io");
haGatewayManager.updateBackend(backend);
List<ProxyBackendConfiguration> backends = haGatewayManager.getActiveBackends("adhoc");
Assert.assertEquals(backends.size(), 1);
assertEquals(1, backends.size());

backend.setActive(false);
backend.setRoutingGroup("etl");
Expand All @@ -73,23 +84,24 @@ public void testUpdateBackend() {
backend.setExternalUrl("adhoc2.trino.gateway.io");
haGatewayManager.updateBackend(backend);
backends = haGatewayManager.getActiveBackends("adhoc");
Assert.assertEquals(backends.size(), 0);
assertEquals(0, backends.size());
backends = haGatewayManager.getAllBackends();
Assert.assertEquals(backends.size(), 2);
Assert.assertEquals(backends.get(1).getRoutingGroup(), "etl");
assertEquals(2, backends.size());
assertEquals("etl", backends.get(1).getRoutingGroup());
}

@Test(dependsOnMethods = {"testUpdateBackend"})
@Test
@Order(4)
public void testDeleteBackend() {
List<ProxyBackendConfiguration> backends = haGatewayManager.getAllBackends();
Assert.assertEquals(backends.size(), 2);
Assert.assertEquals(backends.get(1).getRoutingGroup(), "etl");
assertEquals(2, backends.size());
assertEquals("etl", backends.get(1).getRoutingGroup());
haGatewayManager.deleteBackend(backends.get(0).getName());
backends = haGatewayManager.getAllBackends();
Assert.assertEquals(backends.size(), 1);
assertEquals(1, backends.size());
}

@AfterClass(alwaysRun = true)
@AfterAll
public void cleanUp() {
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
package io.trino.gateway.ha.router;

import static org.junit.jupiter.api.TestInstance.*;

import io.trino.gateway.ha.HaGatewayTestUtils;
import io.trino.gateway.ha.config.DataStoreConfiguration;
import io.trino.gateway.ha.config.ProxyBackendConfiguration;
import io.trino.gateway.ha.persistence.JdbcConnectionManager;
import java.io.File;

import lombok.extern.slf4j.Slf4j;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;

@Slf4j
@Test
@TestInstance(Lifecycle.PER_CLASS)
public class TestHaRoutingManager {
RoutingManager haRoutingManager;
GatewayBackendManager backendManager;
QueryHistoryManager historyManager;

@BeforeClass(alwaysRun = true)
@BeforeAll
public void setUp() {
File baseDir = new File(System.getProperty("java.io.tmpdir"));
File tempH2DbDir = new File(baseDir, "h2db-" + System.currentTimeMillis());
Expand Down
Loading

0 comments on commit 12b14c8

Please sign in to comment.