Skip to content

Commit

Permalink
Check if url has https protocol scheme and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Chaho12 authored and andythsu committed Oct 7, 2023
1 parent 358fc6c commit de1db88
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
6 changes: 6 additions & 0 deletions gateway-ha/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@
<scope>test</scope>
</dependency>

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

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public ClusterStats monitor(ProxyBackendConfiguration backend) {
jdbcUrl = String
.format("jdbc:trino://%s:%s/system",
parsedUrl.getHost(),
parsedUrl.getPort() == -1 ? 80 : parsedUrl.getPort()
parsedUrl.getPort() == -1 ? parsedUrl.getDefaultPort() : parsedUrl.getPort()
);
} catch (MalformedURLException e) {
log.error("could not parse backend url {} ", url);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package io.trino.gateway.ha.clustermonitor;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import io.trino.gateway.ha.config.BackendStateConfiguration;
import io.trino.gateway.ha.config.ProxyBackendConfiguration;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;

@Slf4j
@org.junit.jupiter.api.TestInstance(org.junit.jupiter.api.TestInstance.Lifecycle.PER_CLASS)
public class TestClusterStatsJdbcMonitor {
ClusterStatsJdbcMonitor clusterStatsJdbcMonitor;
@Mock
private Connection con;
@Mock
private PreparedStatement stmt;
@Mock
private ResultSet rs;
private java.util.Properties properties;

@BeforeEach
public void initMocks() {
log.info("initializing test");
MockitoAnnotations.openMocks(this);
}

@AfterAll
public void resetMocks() {
log.info("resetting mocks");
Mockito.reset(con);
Mockito.reset(stmt);
Mockito.reset(rs);
}

@BeforeAll
public void setUp() {
BackendStateConfiguration backendStateConfiguration = new BackendStateConfiguration();
backendStateConfiguration.setUsername("Trino");
properties = new java.util.Properties();
properties.setProperty("user", backendStateConfiguration.getUsername());
properties.setProperty("password", backendStateConfiguration.getPassword());
properties.setProperty("SSL", String.valueOf(backendStateConfiguration.getSsl()));
clusterStatsJdbcMonitor = new ClusterStatsJdbcMonitor(backendStateConfiguration);
}

private static Stream<Arguments> provideSchemeAndPort(){
return Stream.of(
Arguments.of("https", "90", "jdbc:trino://trino.example.com:90/system"),
Arguments.of("http", "90", "jdbc:trino://trino.example.com:90/system"),
Arguments.of("https", "", "jdbc:trino://trino.example.com:443/system"),
Arguments.of("http", "", "jdbc:trino://trino.example.com:80/system")
);
}
@ParameterizedTest
@MethodSource("provideSchemeAndPort")
public void testProtocol(String scheme, String port, String expectedJdbcUrl) throws java.sql.SQLException {
try(MockedStatic<DriverManager> m = Mockito.mockStatic(java.sql.DriverManager.class)) {
m.when(() -> DriverManager.getConnection(anyString(), any())).thenReturn(con);
when(con.prepareStatement(anyString())).thenReturn(stmt);
when(stmt.executeQuery()).thenReturn(rs);
ProxyBackendConfiguration proxyBackend = new ProxyBackendConfiguration();
proxyBackend.setProxyTo(String.format("%s://trino.example.com:%s", scheme, port));
proxyBackend.setName("abc");

clusterStatsJdbcMonitor.monitor(proxyBackend);

m.verify(() -> DriverManager.getConnection(expectedJdbcUrl, properties));
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private void addMockBackends() {
proxyBackend.setProxyTo(backend + ".trino.example.com");
proxyBackend.setExternalUrl("trino.example.com");
backendManager.addBackend(proxyBackend);
//set backend as healthyti start with
//set backend as healthy start with
haRoutingManager.upateBackEndHealth(backend, true);
}

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.0</version>
<version>${dep.junit.version}</version>
<scope>test</scope>
</dependency>

Expand Down

0 comments on commit de1db88

Please sign in to comment.