Skip to content

Commit

Permalink
improve error msg obscure when connect url port miss
Browse files Browse the repository at this point in the history
  • Loading branch information
camilesing committed Sep 12, 2021
1 parent 920aa06 commit c55bc39
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 123 deletions.
180 changes: 95 additions & 85 deletions src/main/java/org/influxdb/InfluxDBFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,103 +6,113 @@
import okhttp3.OkHttpClient;
import org.influxdb.impl.Preconditions;

import java.net.*;
import java.util.Objects;


/**
* A Factory to create a instance of a InfluxDB Database adapter.
*
* @author stefan.majer [at] gmail.com
*
*/
public enum InfluxDBFactory {
INSTANCE;
INSTANCE;

/**
* Create a connection to a InfluxDB.
*
* @param url the url to connect to.
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url) {
Preconditions.checkNonEmptyString(url, "url");
checkUrl(url);
return new InfluxDBImpl(url, null, null, new OkHttpClient.Builder());
}

/**
* Create a connection to a InfluxDB.
*
* @param url the url to connect to.
* @param username the username which is used to authorize against the influxDB instance.
* @param password the password for the username which is used to authorize against the influxDB
* instance.
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url, final String username, final String password) {
Preconditions.checkNonEmptyString(url, "url");
Preconditions.checkNonEmptyString(username, "username");
checkUrl(url);
return new InfluxDBImpl(url, username, password, new OkHttpClient.Builder());
}

/**
* Create a connection to a InfluxDB.
*
* @param url the url to connect to.
* @param client the HTTP client to use
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url, final OkHttpClient.Builder client) {
Preconditions.checkNonEmptyString(url, "url");
Objects.requireNonNull(client, "client");
checkUrl(url);
return new InfluxDBImpl(url, null, null, client);
}

/**
* Create a connection to a InfluxDB.
*
* @param url
* the url to connect to.
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url) {
Preconditions.checkNonEmptyString(url, "url");
return new InfluxDBImpl(url, null, null, new OkHttpClient.Builder());
}
/**
* Create a connection to a InfluxDB.
*
* @param url the url to connect to.
* @param username the username which is used to authorize against the influxDB instance.
* @param password the password for the username which is used to authorize against the influxDB
* instance.
* @param client the HTTP client to use
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url, final String username, final String password,
final OkHttpClient.Builder client) {
checkUrl(url);
return connect(url, username, password, client, ResponseFormat.JSON);
}

/**
* Create a connection to a InfluxDB.
*
* @param url
* the url to connect to.
* @param username
* the username which is used to authorize against the influxDB instance.
* @param password
* the password for the username which is used to authorize against the influxDB
* instance.
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url, final String username, final String password) {
Preconditions.checkNonEmptyString(url, "url");
Preconditions.checkNonEmptyString(username, "username");
return new InfluxDBImpl(url, username, password, new OkHttpClient.Builder());
}
/**
* Create a connection to a InfluxDB.
*
* @param url the url to connect to.
* @param username the username which is used to authorize against the influxDB instance.
* @param password the password for the username which is used to authorize against the influxDB
* instance.
* @param client the HTTP client to use
* @param responseFormat The {@code ResponseFormat} to use for response from InfluxDB server
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url, final String username, final String password,
final OkHttpClient.Builder client, final ResponseFormat responseFormat) {
Preconditions.checkNonEmptyString(url, "url");
Preconditions.checkNonEmptyString(username, "username");
Objects.requireNonNull(client, "client");
checkUrl(url);
return new InfluxDBImpl(url, username, password, client, responseFormat);
}

/**
* Create a connection to a InfluxDB.
*
* @param url
* the url to connect to.
* @param client
* the HTTP client to use
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url, final OkHttpClient.Builder client) {
Preconditions.checkNonEmptyString(url, "url");
Objects.requireNonNull(client, "client");
return new InfluxDBImpl(url, null, null, client);
}
/**
* Check url is legal
*/
private static void checkUrl(final String url) {
String colon = ":";
if (!url.contains(colon) || url.endsWith(colon)) {
throw new IllegalArgumentException(String.format("The url [%s] port cannot be null", url));
}

/**
* Create a connection to a InfluxDB.
*
* @param url
* the url to connect to.
* @param username
* the username which is used to authorize against the influxDB instance.
* @param password
* the password for the username which is used to authorize against the influxDB
* instance.
* @param client
* the HTTP client to use
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url, final String username, final String password,
final OkHttpClient.Builder client) {
return connect(url, username, password, client, ResponseFormat.JSON);
}
try {
URL urlObj = new URL(url);
if (-1 == urlObj.getPort()) {
throw new IllegalArgumentException(String.format("The url [%s] port cannot be null", url));
}
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}

/**
* Create a connection to a InfluxDB.
*
* @param url
* the url to connect to.
* @param username
* the username which is used to authorize against the influxDB instance.
* @param password
* the password for the username which is used to authorize against the influxDB
* instance.
* @param client
* the HTTP client to use
* @param responseFormat
* The {@code ResponseFormat} to use for response from InfluxDB server
* @return a InfluxDB adapter suitable to access a InfluxDB.
*/
public static InfluxDB connect(final String url, final String username, final String password,
final OkHttpClient.Builder client, final ResponseFormat responseFormat) {
Preconditions.checkNonEmptyString(url, "url");
Preconditions.checkNonEmptyString(username, "username");
Objects.requireNonNull(client, "client");
return new InfluxDBImpl(url, username, password, client, responseFormat);
}
}
}
89 changes: 51 additions & 38 deletions src/test/java/org/influxdb/InfluxDBFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,59 @@
* Test the InfluxDB Factory API.
*
* @author fujian1115 [at] gmail.com
*
*/
@RunWith(JUnitPlatform.class)
public class InfluxDBFactoryTest {

/**
* Test for a {@link InfluxDBFactory #connect(String)}.
*/
@Test
public void testShouldNotUseBasicAuthWhenCreateInfluxDBInstanceWithoutUserNameAndPassword() {
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true));
verifyInfluxDBInstance(influxDB);
}

@Test
public void testShouldNotUseBasicAuthWhenCreateInfluxDBInstanceWithUserNameAndWithoutPassword() {
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "admin", null);
verifyInfluxDBInstance(influxDB);
}

private void verifyInfluxDBInstance(InfluxDB influxDB) {
Assertions.assertNotNull(influxDB);
Pong pong = influxDB.ping();
Assertions.assertNotNull(pong);
Assertions.assertNotEquals(pong.getVersion(), "unknown");
}

/**
* Test for a {@link InfluxDBFactory #connect(String, okhttp3.OkHttpClient.Builder)}.
*/
@Test
public void testCreateInfluxDBInstanceWithClientAndWithoutUserNameAndPassword() {
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), new OkHttpClient.Builder());
verifyInfluxDBInstance(influxDB);
}

@Test
public void testShouldThrowIllegalArgumentWithInvalidUrl() {
Assertions.assertThrows(IllegalArgumentException.class,() -> {
InfluxDBFactory.connect("invalidUrl");
});
}
@Test
public void testUrlNotContainsColon() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP());
});
}

@Test
public void testUrlEndWithColon() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":");
});
}

/**
* Test for a {@link InfluxDBFactory #connect(String)}.
*/
@Test
public void testShouldNotUseBasicAuthWhenCreateInfluxDBInstanceWithoutUserNameAndPassword() {
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true));
verifyInfluxDBInstance(influxDB);
}

@Test
public void testShouldNotUseBasicAuthWhenCreateInfluxDBInstanceWithUserNameAndWithoutPassword() {
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), "admin", null);
verifyInfluxDBInstance(influxDB);
}

private void verifyInfluxDBInstance(InfluxDB influxDB) {
Assertions.assertNotNull(influxDB);
Pong pong = influxDB.ping();
Assertions.assertNotNull(pong);
Assertions.assertNotEquals(pong.getVersion(), "unknown");
}

/**
* Test for a {@link InfluxDBFactory #connect(String, okhttp3.OkHttpClient.Builder)}.
*/
@Test
public void testCreateInfluxDBInstanceWithClientAndWithoutUserNameAndPassword() {
InfluxDB influxDB = InfluxDBFactory.connect("http://" + TestUtils.getInfluxIP() + ":" + TestUtils.getInfluxPORT(true), new OkHttpClient.Builder());
verifyInfluxDBInstance(influxDB);
}

@Test
public void testShouldThrowIllegalArgumentWithInvalidUrl() {
Assertions.assertThrows(IllegalArgumentException.class, () -> {
InfluxDBFactory.connect("invalidUrl");
});
}
}

0 comments on commit c55bc39

Please sign in to comment.