From 065851824a238d695f93a63aa131ab03bdcb62fb Mon Sep 17 00:00:00 2001 From: dernasherbrezon Date: Fri, 9 Feb 2024 19:33:14 +0000 Subject: [PATCH] remove ddns support ddns is not reliable. better use vpn to access remote instances --- src/main/java/ru/r2cloud/R2Cloud.java | 7 - src/main/java/ru/r2cloud/ddns/DDNSClient.java | 62 -------- src/main/java/ru/r2cloud/ddns/DDNSType.java | 7 - .../ru/r2cloud/ddns/ExternalIpClient.java | 52 ------- src/main/java/ru/r2cloud/ddns/NoIPTask.java | 86 ------------ .../java/ru/r2cloud/ddns/noip/NoIpClient.java | 71 ---------- .../ru/r2cloud/ddns/noip/NoIpException.java | 15 -- .../ru/r2cloud/ddns/noip/RetryException.java | 16 --- .../java/ru/r2cloud/util/Configuration.java | 9 -- .../r2cloud/web/api/configuration/DDNS.java | 132 ------------------ src/main/resources/config-common.properties | 2 - .../ru/r2cloud/ddns/ExternalIpClientTest.java | 47 ------- .../ru/r2cloud/ddns/noip/NoIpClientTest.java | 72 ---------- src/test/java/ru/r2cloud/it/DdnsTest.java | 60 -------- .../java/ru/r2cloud/it/util/RestClient.java | 36 ----- .../resources/config-common-test.properties | 3 - 16 files changed, 677 deletions(-) delete mode 100644 src/main/java/ru/r2cloud/ddns/DDNSClient.java delete mode 100644 src/main/java/ru/r2cloud/ddns/DDNSType.java delete mode 100644 src/main/java/ru/r2cloud/ddns/ExternalIpClient.java delete mode 100644 src/main/java/ru/r2cloud/ddns/NoIPTask.java delete mode 100644 src/main/java/ru/r2cloud/ddns/noip/NoIpClient.java delete mode 100644 src/main/java/ru/r2cloud/ddns/noip/NoIpException.java delete mode 100644 src/main/java/ru/r2cloud/ddns/noip/RetryException.java delete mode 100644 src/main/java/ru/r2cloud/web/api/configuration/DDNS.java delete mode 100644 src/test/java/ru/r2cloud/ddns/ExternalIpClientTest.java delete mode 100644 src/test/java/ru/r2cloud/ddns/noip/NoIpClientTest.java delete mode 100644 src/test/java/ru/r2cloud/it/DdnsTest.java diff --git a/src/main/java/ru/r2cloud/R2Cloud.java b/src/main/java/ru/r2cloud/R2Cloud.java index 71351553..a5f96a58 100644 --- a/src/main/java/ru/r2cloud/R2Cloud.java +++ b/src/main/java/ru/r2cloud/R2Cloud.java @@ -20,7 +20,6 @@ import ru.r2cloud.cloud.LeoSatDataClient; import ru.r2cloud.cloud.LeoSatDataService; import ru.r2cloud.cloud.SatnogsClient; -import ru.r2cloud.ddns.DDNSClient; import ru.r2cloud.device.Device; import ru.r2cloud.device.DeviceManager; import ru.r2cloud.device.LoraAtBleDevice; @@ -78,7 +77,6 @@ import ru.r2cloud.web.api.PresentationMode; import ru.r2cloud.web.api.TLE; import ru.r2cloud.web.api.configuration.Configured; -import ru.r2cloud.web.api.configuration.DDNS; import ru.r2cloud.web.api.configuration.General; import ru.r2cloud.web.api.configuration.Integrations; import ru.r2cloud.web.api.observation.ObservationList; @@ -113,7 +111,6 @@ public class R2Cloud { private final Authenticator auth; private final Metrics metrics; private final AutoUpdate autoUpdate; - private final DDNSClient ddnsClient; private final SatelliteDao satelliteDao; private final TleDao tleDao; private final Housekeeping houseKeeping; @@ -156,7 +153,6 @@ public R2Cloud(Configuration props, Clock clock) { predict = new PredictOreKit(props); auth = new Authenticator(props); autoUpdate = new AutoUpdate(props); - ddnsClient = new DDNSClient(props); satelliteDao = new SatelliteDao(props); tleDao = new TleDao(props); signed = new SignedURL(props, clock); @@ -239,7 +235,6 @@ public R2Cloud(Configuration props, Clock clock) { index(new MetricsController(signed, metrics)); index(new Overview(deviceManager)); index(new General(props, autoUpdate)); - index(new DDNS(props, ddnsClient)); index(new TLE(satelliteDao, tleDao)); index(new Integrations(props)); index(new ObservationSpectrogram(resultDao, spectogramService, signed)); @@ -258,7 +253,6 @@ public R2Cloud(Configuration props, Clock clock) { } public void start() { - ddnsClient.start(); decoderService.start(); houseKeeping.start(); // device manager should start after tle (it uses TLE to schedule @@ -283,7 +277,6 @@ public void stop() { deviceManager.stop(); houseKeeping.stop(); decoderService.stop(); - ddnsClient.stop(); } public static void main(String[] args) { diff --git a/src/main/java/ru/r2cloud/ddns/DDNSClient.java b/src/main/java/ru/r2cloud/ddns/DDNSClient.java deleted file mode 100644 index e9f2c399..00000000 --- a/src/main/java/ru/r2cloud/ddns/DDNSClient.java +++ /dev/null @@ -1,62 +0,0 @@ -package ru.r2cloud.ddns; - -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import ru.r2cloud.util.Configuration; -import ru.r2cloud.util.NamingThreadFactory; -import ru.r2cloud.util.Util; - -public class DDNSClient { - - private static final Logger LOG = LoggerFactory.getLogger(DDNSClient.class); - - private final Configuration config; - - private ScheduledExecutorService executor; - - public DDNSClient(Configuration config) { - this.config = config; - } - - public synchronized void start() { - if (executor != null) { - return; - } - String typeStr = config.getProperty("ddns.type"); - if (typeStr == null || typeStr.trim().length() == 0) { - return; - } - Runnable task; - try { - DDNSType type = DDNSType.valueOf(typeStr); - switch (type) { - case NONE: - LOG.info("ddns is disabled"); - return; - case NOIP: - task = new NoIPTask(config); - LOG.info("ddns provider is no-ip"); - break; - default: - throw new IllegalArgumentException("unsupported ddns provider: " + type); - } - } catch (Exception e) { - LOG.error("unable to configure ddns", e); - return; - } - - executor = Executors.newScheduledThreadPool(1, new NamingThreadFactory("ddns-updater")); - executor.scheduleAtFixedRate(task, 0, config.getLong("ddns.interval.seconds"), TimeUnit.SECONDS); - } - - public synchronized void stop() { - Util.shutdown(executor, config.getThreadPoolShutdownMillis()); - executor = null; - } - -} diff --git a/src/main/java/ru/r2cloud/ddns/DDNSType.java b/src/main/java/ru/r2cloud/ddns/DDNSType.java deleted file mode 100644 index bf829062..00000000 --- a/src/main/java/ru/r2cloud/ddns/DDNSType.java +++ /dev/null @@ -1,7 +0,0 @@ -package ru.r2cloud.ddns; - -public enum DDNSType { - - NONE, NOIP - -} diff --git a/src/main/java/ru/r2cloud/ddns/ExternalIpClient.java b/src/main/java/ru/r2cloud/ddns/ExternalIpClient.java deleted file mode 100644 index 05b2e035..00000000 --- a/src/main/java/ru/r2cloud/ddns/ExternalIpClient.java +++ /dev/null @@ -1,52 +0,0 @@ -package ru.r2cloud.ddns; - -import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpClient.Redirect; -import java.net.http.HttpClient.Version; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.net.http.HttpResponse.BodyHandlers; -import java.time.Duration; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import ru.r2cloud.R2Cloud; - -final class ExternalIpClient { - - private static final Logger LOG = LoggerFactory.getLogger(ExternalIpClient.class); - - private final String host; - private final HttpClient httpclient; - - public ExternalIpClient(String host) { - this.host = host; - this.httpclient = HttpClient.newBuilder().version(Version.HTTP_2).followRedirects(Redirect.NORMAL).connectTimeout(Duration.ofMinutes(1L)).build(); - } - - public String getExternalIp() { - HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(host)); - builder.timeout(Duration.ofMinutes(1L)); - builder.header("User-Agent", R2Cloud.getVersion() + " leosatdata.com"); - HttpResponse response; - - try { - response = httpclient.send(builder.GET().build(), BodyHandlers.ofString()); - } catch (IOException e) { - LOG.error("unable to get an external ip", e); - return null; - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - return null; - } - if (response.statusCode() != 200) { - LOG.error("unable to get an external ip: {}", response.statusCode()); - return null; - } - return response.body().trim(); - } - -} diff --git a/src/main/java/ru/r2cloud/ddns/NoIPTask.java b/src/main/java/ru/r2cloud/ddns/NoIPTask.java deleted file mode 100644 index b4d8b9f5..00000000 --- a/src/main/java/ru/r2cloud/ddns/NoIPTask.java +++ /dev/null @@ -1,86 +0,0 @@ -package ru.r2cloud.ddns; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import ru.r2cloud.ddns.noip.NoIpClient; -import ru.r2cloud.ddns.noip.NoIpException; -import ru.r2cloud.ddns.noip.RetryException; -import ru.r2cloud.util.Configuration; - -public class NoIPTask implements Runnable { - - private static final Logger LOG = LoggerFactory.getLogger(NoIPTask.class); - - private final Configuration config; - private final NoIpClient client; - private final ExternalIpClient externalIpClient; - private final String username; - private final String password; - private final String domainName; - - private String currentExternalIp; - private boolean fatalError = false; - private Long retryAfter = null; - - public NoIPTask(Configuration config) throws Exception { - this.config = config; - username = getAndValidate(config, "ddns.noip.username"); - password = getAndValidate(config, "ddns.noip.password"); - domainName = getAndValidate(config, "ddns.noip.domain"); - client = new NoIpClient("https://dynupdate.no-ip.com", username, password); - externalIpClient = new ExternalIpClient("http://checkip.amazonaws.com"); - currentExternalIp = config.getProperty("ddns.ip"); - retryAfter = config.getLong("ddns.retry.after.millis"); - } - - @Override - public void run() { - if (fatalError) { - return; - } - // skip update due to ddns server error. - // see the protocol at https://www.noip.com/integrate/response - if (retryAfter != null) { - if (retryAfter > System.currentTimeMillis()) { - return; - } else { - config.remove("ddns.retry.after.millis"); - config.update(); - retryAfter = null; - } - } - - String externalIp = externalIpClient.getExternalIp(); - if (currentExternalIp != null && currentExternalIp.equals(externalIp)) { - return; - } - LOG.info("ip has changed. old: {} new: {}", currentExternalIp, externalIp); - try { - currentExternalIp = client.update(domainName); - LOG.info("ddns ip updated: {}", currentExternalIp); - config.setProperty("ddns.ip", currentExternalIp); - config.update(); - } catch (NoIpException e) { - LOG.error("unable to update ddns record", e); - fatalError = true; - } catch (RetryException e) { - LOG.info("no-ip failure. retry after: {}", e.getRetryTimeout()); - retryAfter = System.currentTimeMillis() + e.getRetryTimeout(); - // save it to show in UI - config.setProperty("ddns.retry.after.millis", retryAfter); - config.update(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } - - private static String getAndValidate(Configuration config, String name) throws Exception { - String username = config.getProperty(name); - if (username == null || username.trim().length() == 0) { - throw new IllegalArgumentException(name + " cannot be empty"); - } - return username; - } - -} diff --git a/src/main/java/ru/r2cloud/ddns/noip/NoIpClient.java b/src/main/java/ru/r2cloud/ddns/noip/NoIpClient.java deleted file mode 100644 index 6e0c2618..00000000 --- a/src/main/java/ru/r2cloud/ddns/noip/NoIpClient.java +++ /dev/null @@ -1,71 +0,0 @@ -package ru.r2cloud.ddns.noip; - -import java.io.IOException; -import java.net.URI; -import java.net.http.HttpClient; -import java.net.http.HttpClient.Redirect; -import java.net.http.HttpClient.Version; -import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.net.http.HttpResponse.BodyHandlers; -import java.nio.charset.StandardCharsets; -import java.time.Duration; -import java.util.Base64; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import ru.r2cloud.R2Cloud; - -public class NoIpClient { - - private static final Logger LOG = LoggerFactory.getLogger(NoIpClient.class); - private static final long RETRY_TIMEOUT = TimeUnit.MINUTES.toMillis(30); - - private final HttpClient httpclient; - private final String hostname; - private final String username; - private final String password; - - public NoIpClient(String hostname, String username, String password) { - this.hostname = hostname; - this.username = username; - this.password = password; - this.httpclient = HttpClient.newBuilder().version(Version.HTTP_2).followRedirects(Redirect.NORMAL).connectTimeout(Duration.ofMinutes(1L)).build(); - } - - public String update(String domain) throws NoIpException, RetryException, InterruptedException { - HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(hostname + "/nic/update?hostname=" + domain)); - builder.timeout(Duration.ofMinutes(1L)); - builder.header("User-Agent", R2Cloud.getVersion() + " leosatdata.com"); - builder.header("Authorization", "Basic " + Base64.getEncoder().encodeToString((username + ":" + password).getBytes(StandardCharsets.US_ASCII))); - HttpResponse response; - try { - response = httpclient.send(builder.GET().build(), BodyHandlers.ofString()); - } catch (IOException e) { - throw new NoIpException("unable to update", e); - } - if (response.statusCode() != 200) { - if (LOG.isErrorEnabled()) { - LOG.error("unable to update ddns: {}", response.body()); - } - throw new NoIpException("invalid status code: " + response.statusCode()); - } - String body = response.body(); - if (body.startsWith("good") || body.startsWith("nochg")) { - int index = body.indexOf(' '); - if (index != -1) { - return body.substring(index + 1); - } - throw new NoIpException("unable to get ip from the response: " + body); - } else if ("nohost".equals(body) || "badauth".equals(body) || "badagent".equals(body) || "!donator".equals(body) || "abuse".equals(body)) { - throw new NoIpException("fatal error detected: " + body); - } else if ("911".equals(body)) { - throw new RetryException(RETRY_TIMEOUT); - } else { - throw new NoIpException("unknown response code: " + body); - } - } - -} diff --git a/src/main/java/ru/r2cloud/ddns/noip/NoIpException.java b/src/main/java/ru/r2cloud/ddns/noip/NoIpException.java deleted file mode 100644 index 8dd4662f..00000000 --- a/src/main/java/ru/r2cloud/ddns/noip/NoIpException.java +++ /dev/null @@ -1,15 +0,0 @@ -package ru.r2cloud.ddns.noip; - -public class NoIpException extends Exception { - - private static final long serialVersionUID = -2202052437185248771L; - - public NoIpException(String message) { - super(message); - } - - public NoIpException(String message, Exception e) { - super(message, e); - } - -} diff --git a/src/main/java/ru/r2cloud/ddns/noip/RetryException.java b/src/main/java/ru/r2cloud/ddns/noip/RetryException.java deleted file mode 100644 index dee7c512..00000000 --- a/src/main/java/ru/r2cloud/ddns/noip/RetryException.java +++ /dev/null @@ -1,16 +0,0 @@ -package ru.r2cloud.ddns.noip; - -public class RetryException extends Exception { - - private static final long serialVersionUID = -7456809427495492302L; - - private final long retryTimeout; - - public RetryException(long retryTimeout) { - this.retryTimeout = retryTimeout; - } - - public long getRetryTimeout() { - return retryTimeout; - } -} diff --git a/src/main/java/ru/r2cloud/util/Configuration.java b/src/main/java/ru/r2cloud/util/Configuration.java index 53846d37..5b475686 100644 --- a/src/main/java/ru/r2cloud/util/Configuration.java +++ b/src/main/java/ru/r2cloud/util/Configuration.java @@ -25,7 +25,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import ru.r2cloud.ddns.DDNSType; import ru.r2cloud.model.DemodulatorType; import ru.r2cloud.model.DeviceConfiguration; import ru.r2cloud.model.Modulation; @@ -558,14 +557,6 @@ public void setList(String name, List urls) { setProperty(name, str.toString()); } - public DDNSType getDdnsType(String name) { - String str = getProperty(name); - if (str == null || str.trim().length() == 0) { - return null; - } - return DDNSType.valueOf(str); - } - public void remove(String name) { synchronized (changedProperties) { changedProperties.add(name); diff --git a/src/main/java/ru/r2cloud/web/api/configuration/DDNS.java b/src/main/java/ru/r2cloud/web/api/configuration/DDNS.java deleted file mode 100644 index 6063130f..00000000 --- a/src/main/java/ru/r2cloud/web/api/configuration/DDNS.java +++ /dev/null @@ -1,132 +0,0 @@ -package ru.r2cloud.web.api.configuration; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.eclipsesource.json.JsonObject; - -import fi.iki.elonen.NanoHTTPD.IHTTPSession; -import ru.r2cloud.ddns.DDNSClient; -import ru.r2cloud.ddns.DDNSType; -import ru.r2cloud.util.Configuration; -import ru.r2cloud.web.AbstractHttpController; -import ru.r2cloud.web.BadRequest; -import ru.r2cloud.web.ModelAndView; -import ru.r2cloud.web.Success; -import ru.r2cloud.web.ValidationResult; -import ru.r2cloud.web.WebServer; -import ru.r2cloud.web.api.Messages; - -public class DDNS extends AbstractHttpController { - - private static final String TYPE_PROPERTY_NAME = "ddns.type"; - private static final String DOMAIN_PROPERTY_NAME = "ddns.noip.domain"; - private static final String PASSWORD_PROPERTY_NAME = "ddns.noip.password"; - private static final String USERNAME_PROPERTY_NAME = "ddns.noip.username"; - - private static final String PASSWORD_PARAMETER = "password"; - private static final String USERNAME_PARAMETER = "username"; - private static final String DOMAIN_PARAMETER = "domain"; - - private static final Logger LOG = LoggerFactory.getLogger(DDNS.class); - - private final Configuration config; - private final DDNSClient ddnsClient; - - public DDNS(Configuration config, DDNSClient ddnsClient) { - this.config = config; - this.ddnsClient = ddnsClient; - } - - @Override - public ModelAndView doGet(IHTTPSession session) { - ModelAndView result = new ModelAndView(); - JsonObject entity = new JsonObject(); - String username = config.getProperty(USERNAME_PROPERTY_NAME); - if (username != null) { - entity.add(USERNAME_PARAMETER, username); - } - String password = config.getProperty(PASSWORD_PROPERTY_NAME); - if (password != null) { - entity.add(PASSWORD_PARAMETER, password); - } - String domain = config.getProperty(DOMAIN_PROPERTY_NAME); - if (domain != null) { - entity.add(DOMAIN_PARAMETER, domain); - } - entity.add("type", config.getDdnsType(TYPE_PROPERTY_NAME).toString()); - String currentIp = config.getProperty("ddns.ip"); - if (currentIp != null) { - entity.add("currentIp", currentIp); - } - result.setData(entity.toString()); - return result; - } - - @Override - public ModelAndView doPost(JsonObject request) { - String typeStr = WebServer.getString(request, "type"); - String username = request.getString(USERNAME_PARAMETER, null); - String password = request.getString(PASSWORD_PARAMETER, null); - String domain = request.getString(DOMAIN_PARAMETER, null); - - ValidationResult errors = new ValidationResult(); - if (typeStr == null) { - errors.put("type", Messages.CANNOT_BE_EMPTY); - LOG.info("unable to save: {}", errors); - return new BadRequest(errors); - } - - DDNSType type; - try { - type = DDNSType.valueOf(typeStr); - } catch (Exception e) { - LOG.info("unknown ddns type: {}", typeStr, e); - errors.put("type", "unknown type"); - return new BadRequest(errors); - } - - switch (type) { - case NOIP: - if (username == null || username.trim().length() == 0) { - errors.put(USERNAME_PARAMETER, Messages.CANNOT_BE_EMPTY); - } - if (password == null || password.trim().length() == 0) { - errors.put(PASSWORD_PARAMETER, Messages.CANNOT_BE_EMPTY); - } - if (domain == null || domain.trim().length() == 0) { - errors.put(DOMAIN_PARAMETER, Messages.CANNOT_BE_EMPTY); - } - break; - default: - break; - } - - if (!errors.isEmpty()) { - LOG.info("unable to save: {}", errors); - return new BadRequest(errors); - } - - if (username != null) { - config.setProperty(USERNAME_PROPERTY_NAME, username); - } - if (password != null) { - config.setProperty(PASSWORD_PROPERTY_NAME, password); - } - if (domain != null) { - config.setProperty(DOMAIN_PROPERTY_NAME, domain); - } - config.setProperty(TYPE_PROPERTY_NAME, type.name()); - config.update(); - - ddnsClient.stop(); - ddnsClient.start(); - - return new Success(); - } - - @Override - public String getRequestMappingURL() { - return "/api/v1/admin/config/ddns"; - } -} \ No newline at end of file diff --git a/src/main/resources/config-common.properties b/src/main/resources/config-common.properties index 374ab20b..214611ba 100644 --- a/src/main/resources/config-common.properties +++ b/src/main/resources/config-common.properties @@ -2,8 +2,6 @@ # DO NOT EDIT # instead edit user-specific at: # ~/.r2cloud -ddns.type=NONE -ddns.interval.seconds=300 #1 - Monday, 7 - Sunday tle.urls=http://r4uab.ru/satonline.txt,https://celestrak.org/NORAD/elements/gp.php?GROUP=satnogs&FORMAT=tle,https://celestrak.org/NORAD/elements/gp.php?GROUP=active&FORMAT=tle diff --git a/src/test/java/ru/r2cloud/ddns/ExternalIpClientTest.java b/src/test/java/ru/r2cloud/ddns/ExternalIpClientTest.java deleted file mode 100644 index 8005e0d9..00000000 --- a/src/test/java/ru/r2cloud/ddns/ExternalIpClientTest.java +++ /dev/null @@ -1,47 +0,0 @@ -package ru.r2cloud.ddns; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import ru.r2cloud.MockServer; - -public class ExternalIpClientTest { - - private MockServer mockServer; - private ExternalIpClient client; - - @Test - public void testSuccess() throws Exception { - mockServer.mockResponse("/", "1.2.3.4"); - assertEquals("1.2.3.4", client.getExternalIp()); - } - - @Test - public void testNotFound() throws Exception { - assertNull(client.getExternalIp()); - } - - @Test - public void testNoconnection() throws Exception { - mockServer.stop(); - assertNull(client.getExternalIp()); - } - - @Before - public void start() throws Exception { - mockServer = new MockServer(); - mockServer.start(); - client = new ExternalIpClient(mockServer.getUrl()); - } - - @After - public void stop() { - if (mockServer != null) { - mockServer.stop(); - } - } -} diff --git a/src/test/java/ru/r2cloud/ddns/noip/NoIpClientTest.java b/src/test/java/ru/r2cloud/ddns/noip/NoIpClientTest.java deleted file mode 100644 index b7dc483a..00000000 --- a/src/test/java/ru/r2cloud/ddns/noip/NoIpClientTest.java +++ /dev/null @@ -1,72 +0,0 @@ -package ru.r2cloud.ddns.noip; - -import static org.junit.Assert.assertEquals; - -import java.util.UUID; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - -import ru.r2cloud.MockServer; - -public class NoIpClientTest { - - private MockServer mockServer; - private NoIpClient client; - - @Test - public void testSuccess() throws Exception { - mockServer.mockResponse("/nic/update", "good 1.2.3.4"); - assertEquals("1.2.3.4", client.update(UUID.randomUUID().toString())); - } - - @Test(expected = NoIpException.class) - public void testExpectedError() throws Exception { - mockServer.mockResponse("/nic/update", "nohost"); - client.update(UUID.randomUUID().toString()); - } - - @Test(expected = NoIpException.class) - public void testInvalidFormat() throws Exception { - mockServer.mockResponse("/nic/update", "good1.2.3.4"); - client.update(UUID.randomUUID().toString()); - } - - @Test(expected = NoIpException.class) - public void testUnexpectedError() throws Exception { - mockServer.mockResponse("/nic/update", UUID.randomUUID().toString()); - client.update(UUID.randomUUID().toString()); - } - - @Test(expected = NoIpException.class) - public void testNotFound() throws Exception { - client.update(UUID.randomUUID().toString()); - } - - @Test(expected = RetryException.class) - public void testRetryException() throws Exception { - mockServer.mockResponse("/nic/update", "911"); - client.update(UUID.randomUUID().toString()); - } - - @Test(expected = NoIpException.class) - public void testNoconnection() throws Exception { - mockServer.stop(); - client.update(UUID.randomUUID().toString()); - } - - @Before - public void start() throws Exception { - mockServer = new MockServer(); - mockServer.start(); - client = new NoIpClient(mockServer.getUrl(), UUID.randomUUID().toString(), UUID.randomUUID().toString()); - } - - @After - public void stop() { - if (mockServer != null) { - mockServer.stop(); - } - } -} diff --git a/src/test/java/ru/r2cloud/it/DdnsTest.java b/src/test/java/ru/r2cloud/it/DdnsTest.java deleted file mode 100644 index 4e259344..00000000 --- a/src/test/java/ru/r2cloud/it/DdnsTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package ru.r2cloud.it; - -import static org.junit.Assert.assertEquals; - -import java.net.http.HttpResponse; -import java.util.UUID; - -import org.junit.Test; - -import ru.r2cloud.TestUtil; -import ru.r2cloud.it.util.RegisteredTest; - -public class DdnsTest extends RegisteredTest { - - @Test - public void testGetSettings() { - TestUtil.assertJson("noDdns.json", client.getDdnsConfiguration()); - } - - @Test - public void testSaveNONE() { - client.saveDdnsConfiguration("NONE", null, null, null); - TestUtil.assertJson("noDdns.json", client.getDdnsConfiguration()); - } - - @Test - public void testEmptyUsername() { - HttpResponse response = client.saveDdnsConfigurationResponse("NOIP", null, UUID.randomUUID().toString(), UUID.randomUUID().toString()); - assertEquals(400, response.statusCode()); - assertErrorInField("username", response); - } - - @Test - public void testEmptyPassword() { - HttpResponse response = client.saveDdnsConfigurationResponse("NOIP", UUID.randomUUID().toString(), null, UUID.randomUUID().toString()); - assertEquals(400, response.statusCode()); - assertErrorInField("password", response); - } - - @Test - public void testEmptyDomain() { - HttpResponse response = client.saveDdnsConfigurationResponse("NOIP", UUID.randomUUID().toString(), UUID.randomUUID().toString(), null); - assertEquals(400, response.statusCode()); - assertErrorInField("domain", response); - } - - @Test - public void testSaveUnknownType() { - HttpResponse response = client.saveDdnsConfigurationResponse(UUID.randomUUID().toString(), null, null, null); - assertEquals(400, response.statusCode()); - assertErrorInField("type", response); - } - - @Test - public void testEmptyType() { - HttpResponse response = client.saveDdnsConfigurationResponse(null, null, null, null); - assertEquals(400, response.statusCode()); - assertErrorInField("type", response); - } -} diff --git a/src/test/java/ru/r2cloud/it/util/RestClient.java b/src/test/java/ru/r2cloud/it/util/RestClient.java index 5af36589..ebccd9dc 100644 --- a/src/test/java/ru/r2cloud/it/util/RestClient.java +++ b/src/test/java/ru/r2cloud/it/util/RestClient.java @@ -634,44 +634,8 @@ private static String createQuery(Map params) { return result.toString(); } - public JsonObject getDdnsConfiguration() { - return getData("/api/v1/admin/config/ddns"); - } - public JsonObject getPresentationModeData() { return getData("/api/v1/presentationMode"); } - public void saveDdnsConfiguration(String type, String username, String password, String domain) { - HttpResponse response = saveDdnsConfigurationResponse(type, username, password, domain); - if (response.statusCode() != 200) { - throw new RuntimeException("invalid status code: " + response.statusCode()); - } - } - - public HttpResponse saveDdnsConfigurationResponse(String type, String username, String password, String domain) { - JsonObject json = Json.object(); - if (type != null) { - json.add("type", type); - } - if (username != null) { - json.add("username", username); - } - if (password != null) { - json.add("password", password); - } - if (domain != null) { - json.add("domain", domain); - } - HttpRequest request = createJsonPost("/api/v1/admin/config/ddns", json).build(); - try { - return httpclient.send(request, BodyHandlers.ofString()); - } catch (IOException e) { - throw new RuntimeException(e); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - throw new RuntimeException("unable to send request"); - } - } - } diff --git a/src/test/resources/config-common-test.properties b/src/test/resources/config-common-test.properties index 14467128..62d08d26 100644 --- a/src/test/resources/config-common-test.properties +++ b/src/test/resources/config-common-test.properties @@ -1,6 +1,3 @@ -ddns.type=NONE -ddns.interval.seconds=300 - #1 - Monday, 7 - Sunday tle.timeout=60000 tle.cacheFileLocation=./data/tle.txt