Refactor DNS record handling and add record creation method

Introduced `buildFqdn` utility to avoid redundancy in FQDN construction. Added `recordCreate` method for simpler record creation with specified parameters. Enhanced `recordDeleteTypeIfExists` to handle multiple record types and improved test cases for better clarity and consistency.
This commit is contained in:
2025-04-18 11:49:24 +02:00
parent bebe20a12d
commit 0e5b878e9f
2 changed files with 70 additions and 32 deletions
@@ -11,18 +11,17 @@ import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
// TODO: #testDns should be clean-up it's test data
@Slf4j
public class CfClientTest {
private static final String zoneStr = "mein-d-ns.de";
private static final String sldStr = "devsld";
private static int ttl = 60;
private static final String ZONE_STR = "mein-d-ns.de";
private static final String SLD_STR = "devsld";
private static final int TTL = 60;
private final String email = System.getenv("API_EMAIL");
private final String apiKey = System.getenv("API_KEY");
private final String API_EMAIL = System.getenv("API_EMAIL");
private final String API_KEY = System.getenv("API_KEY");
private final CfDnsClient client = new CfDnsClient(email, apiKey);
private final CfDnsClient client = new CfDnsClient(API_EMAIL, API_KEY);
@Test
void testList() throws Exception {
@@ -33,17 +32,17 @@ public class CfClientTest {
assertFalse(rList.isEmpty());
assertThrows(
CloudflareNotFoundException.class, () -> client.sldListAll(zList.get(0), "notexisting"));
CloudflareNotFoundException.class, () -> client.sldListAll(zList.get(0), "not-existing"));
client.setEmptyResultThrowsException(false);
rList = client.sldListAll(zList.get(0), "notexisting");
rList = client.sldListAll(zList.get(0), "not-existing");
assertTrue(rList.isEmpty());
client.setEmptyResultThrowsException(true);
}
@Test
void testDns() throws Exception {
ZoneEntity z = client.zoneInfo(zoneStr);
ZoneEntity z = client.zoneInfo(ZONE_STR);
assertEquals("0a83dd6e7f8c46039f2517bbded8115e", z.getId());
assertEquals("mein-d-ns.de", z.getName());
assertEquals("active", z.getStatus());
@@ -61,33 +60,47 @@ public class CfClientTest {
assertEquals("test.mein-d-ns.de", r.getName());
assertEquals("A", r.getType());
assertEquals("129.0.0.3", r.getContent());
r = client.sldInfo(z, "test", RecordType.AAAA);
assertEquals("f76c420362220e0c8bab80cd08028214", r.getId());
assertEquals("test.mein-d-ns.de", r.getName());
assertEquals(RecordType.AAAA.getType(), r.getType());
assertEquals("2a0a:4cc0:c0:2e4::1", r.getContent());
String domain = SLD_STR + "." + ZONE_STR;
client.recordDeleteTypeIfExists(z, SLD_STR, RecordType.A, RecordType.AAAA);
RecordEntity createdRe1 =
client.recordCreate(
z, RecordEntity.build(sldStr + "." + zoneStr, RecordType.A, ttl, "130.0.0.3"));
r = client.sldInfo(z, sldStr, RecordType.A);
z, RecordEntity.build(domain, RecordType.A, TTL, "130.0.0.3"));
assertNotNull(createdRe1.getId());
assertEquals(domain, createdRe1.getName());
assertEquals(RecordType.A.getType(), createdRe1.getType());
assertEquals(TTL, createdRe1.getTtl());
assertEquals("130.0.0.3", createdRe1.getContent());
assertNotNull(createdRe1.getCreatedOn());
assertNotNull(createdRe1.getModifiedOn());
r = client.sldInfo(z, SLD_STR, RecordType.A);
assertEquals("130.0.0.3", r.getContent());
RecordEntity createdRe2 =
client.recordCreate(
z,
RecordEntity.build(
sldStr + "." + zoneStr, RecordType.AAAA, ttl, "2a0a:4cc0:c0:2e4::1"));
r = client.sldInfo(z, sldStr, RecordType.AAAA);
z, SLD_STR, TTL, RecordType.AAAA, "2a0a:4cc0:c0:2e4::1");
r = client.sldInfo(z, SLD_STR, RecordType.AAAA);
assertEquals("2a0a:4cc0:c0:2e4::1", r.getContent());
assertEquals(RecordType.AAAA.getType(), r.getType());
createdRe2.setContent("2a0a:4cc0:c0:2e4::2");
client.recordUpdate(z, createdRe2);
r = client.sldInfo(z, sldStr, RecordType.AAAA);
r = client.sldInfo(z, SLD_STR, RecordType.AAAA);
assertEquals("2a0a:4cc0:c0:2e4::2", r.getContent());
r = client.sldInfo(z, sldStr, RecordType.A);
r = client.sldInfo(z, SLD_STR, RecordType.A);
assertEquals("130.0.0.3", r.getContent());
assertTrue(client.recordDelete(z, createdRe2));
assertThrows(
CloudflareNotFoundException.class, () -> client.sldInfo(z, sldStr, RecordType.AAAA));
CloudflareNotFoundException.class, () -> client.sldInfo(z, SLD_STR, RecordType.AAAA));
client.recordDeleteTypeIfExists(z, sldStr, RecordType.A);
assertThrows(CloudflareNotFoundException.class, () -> client.sldInfo(z, sldStr, RecordType.A));
client.recordDeleteTypeIfExists(z, SLD_STR, RecordType.A);
assertThrows(CloudflareNotFoundException.class, () -> client.sldInfo(z, SLD_STR, RecordType.A));
}
@Test