test: cover AI code review changes in CfClientTest (#2)
Build and Analyse / build-and-analyse (push) Successful in 30s
Build and Analyse / build-and-analyse (push) Successful in 30s
- live tests for API error details (status code, Cloudflare errors), 404 on deleting an unknown record, unknown zones with and without emptyResultThrowsException, closing the client, multi-level subdomains and the zone apex, server- and client-side type filter, record attributes (priority, comment, SRV data) and Instant timestamps - testPaging fetches all pages with a small page size, therefore CfDnsClient got a package-private constructor with a custom page size - testDns checks that recordUpdate doesn't modify the given record - SRV records: the priority is part of the data, fixed javadoc, README and changelog accordingly refs #2 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
20fabf9667
commit
c3a5c5f3e5
6 files changed
+229
-17
No files matched your search
@@ -200,10 +200,9 @@ RecordEntity mx = RecordEntity.build("example.com", RecordType.MX, 3600, "mail.e
|
|||||||
mx.setPriority(10);
|
mx.setPriority(10);
|
||||||
cfDnsClient.recordCreate(zone, mx);
|
cfDnsClient.recordCreate(zone, mx);
|
||||||
|
|
||||||
// SRV record with structured data (keys as in the Cloudflare API)
|
// SRV record with structured data (keys as in the Cloudflare API), the priority is part of the data
|
||||||
RecordEntity srv = RecordEntity.build("_sip._tcp.example.com", RecordType.SRV, 3600, null);
|
RecordEntity srv = RecordEntity.build("_sip._tcp.example.com", RecordType.SRV, 3600, null);
|
||||||
srv.setPriority(10);
|
srv.setData(Map.of("priority", 10, "weight", 5, "port", 5060, "target", "sip.example.com"));
|
||||||
srv.setData(Map.of("weight", 5, "port", 5060, "target", "sip.example.com"));
|
|
||||||
cfDnsClient.recordCreate(zone, srv);
|
cfDnsClient.recordCreate(zone, srv);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -12,7 +12,7 @@
|
|||||||
- batch DELETE sends only the record IDs, batch PATCH sends all writable attributes which aren't `null` (before: only the content)
|
- batch DELETE sends only the record IDs, batch PATCH sends all writable attributes which aren't `null` (before: only the content)
|
||||||
- `CfDnsClient` uses one shared HTTP client with connection pooling instead of a new one per request and implements `AutoCloseable`, call `close()` to release the connections
|
- `CfDnsClient` uses one shared HTTP client with connection pooling instead of a new one per request and implements `AutoCloseable`, call `close()` to release the connections
|
||||||
- `CloudflareApiException` provides the HTTP status code (`getStatusCode()`) and the errors reported by Cloudflare (`getErrors()`)
|
- `CloudflareApiException` provides the HTTP status code (`getStatusCode()`) and the errors reported by Cloudflare (`getErrors()`)
|
||||||
- `RecordEntity` supports the attributes `priority` (MX, SRV, URI), `data` (e.g. SRV, CAA), `comment` and `tags`; they are also considered by batch operations, as is `proxied`
|
- `RecordEntity` supports the attributes `priority` (MX, URI), `data` (e.g. SRV incl. its priority, CAA), `comment` and `tags`; they are also considered by batch operations, as is `proxied`
|
||||||
- `recordCreate` and `recordUpdate` don't modify the given `RecordEntity` anymore and send only writable attributes
|
- `recordCreate` and `recordUpdate` don't modify the given `RecordEntity` anymore and send only writable attributes
|
||||||
- fluent `update(content)` sends only the new content (partial update)
|
- fluent `update(content)` sends only the new content (partial update)
|
||||||
- filtering by exactly one record type is done by Cloudflare (`?type=`), unknown record types in responses don't break the client-side filter anymore
|
- filtering by exactly one record type is done by Cloudflare (`?type=`), unknown record types in responses don't break the client-side filter anymore
|
||||||
|
|||||||
@@ -104,6 +104,8 @@ public class CfDnsClient extends CfBasicHttpClient {
|
|||||||
|
|
||||||
private final boolean emptyResultThrowsException;
|
private final boolean emptyResultThrowsException;
|
||||||
|
|
||||||
|
private final int recordsPerPage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new instance of {@code CfDnsClient}.
|
* Constructs a new instance of {@code CfDnsClient}.
|
||||||
*
|
*
|
||||||
@@ -114,9 +116,19 @@ public class CfDnsClient extends CfBasicHttpClient {
|
|||||||
* @param auth The authentication mechanism to use (ApiTokenAuth or EmailKeyAuth)
|
* @param auth The authentication mechanism to use (ApiTokenAuth or EmailKeyAuth)
|
||||||
*/
|
*/
|
||||||
CfDnsClient(boolean emptyResultThrowsException, String baseUrl, CfDnsClientBuilder.CfAuth auth) {
|
CfDnsClient(boolean emptyResultThrowsException, String baseUrl, CfDnsClientBuilder.CfAuth auth) {
|
||||||
|
this(emptyResultThrowsException, baseUrl, auth, RECORDS_PER_PAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Constructs a new instance of {@code CfDnsClient} with a custom page size for fetching all DNS
|
||||||
|
* records. Intended for tests, which need to fetch multiple pages with only a few records.
|
||||||
|
*/
|
||||||
|
CfDnsClient(boolean emptyResultThrowsException, String baseUrl, CfDnsClientBuilder.CfAuth auth,
|
||||||
|
int recordsPerPage) {
|
||||||
super(baseUrl, auth);
|
super(baseUrl, auth);
|
||||||
this.responseValidator = new ResponseValidator(emptyResultThrowsException);
|
this.responseValidator = new ResponseValidator(emptyResultThrowsException);
|
||||||
this.emptyResultThrowsException = emptyResultThrowsException;
|
this.emptyResultThrowsException = emptyResultThrowsException;
|
||||||
|
this.recordsPerPage = recordsPerPage;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String buildFqdn(ZoneEntity zone, String subdomain) {
|
private static String buildFqdn(ZoneEntity zone, String subdomain) {
|
||||||
@@ -239,7 +251,7 @@ public class CfDnsClient extends CfBasicHttpClient {
|
|||||||
String path = CfRequest.RECORD_LIST.buildPath(zone.getId());
|
String path = CfRequest.RECORD_LIST.buildPath(zone.getId());
|
||||||
if (pagingRequest == null) {
|
if (pagingRequest == null) {
|
||||||
return filterAndSetZoneRecords(zone, null,
|
return filterAndSetZoneRecords(zone, null,
|
||||||
getAllPages(path, RecordMultipleResponse.class, RECORDS_PER_PAGE));
|
getAllPages(path, RecordMultipleResponse.class, recordsPerPage));
|
||||||
}
|
}
|
||||||
RecordMultipleResponse resp = getRequest(pagingRequest.addQueryString(path), RecordMultipleResponse.class);
|
RecordMultipleResponse resp = getRequest(pagingRequest.addQueryString(path), RecordMultipleResponse.class);
|
||||||
checkResponse(resp);
|
checkResponse(resp);
|
||||||
@@ -265,7 +277,7 @@ public class CfDnsClient extends CfBasicHttpClient {
|
|||||||
throws CloudflareApiException {
|
throws CloudflareApiException {
|
||||||
String fqdn = buildFqdn(zone, subdomain);
|
String fqdn = buildFqdn(zone, subdomain);
|
||||||
String endpoint = addTypeFilter(CfRequest.RECORD_LIST_NAME.buildPath(zone.getId(), fqdn), types);
|
String endpoint = addTypeFilter(CfRequest.RECORD_LIST_NAME.buildPath(zone.getId(), fqdn), types);
|
||||||
List<RecordEntity> recs = getAllPages(endpoint, RecordMultipleResponse.class, RECORDS_PER_PAGE);
|
List<RecordEntity> recs = getAllPages(endpoint, RecordMultipleResponse.class, recordsPerPage);
|
||||||
return filterAndSetZoneRecords(zone, types, recs);
|
return filterAndSetZoneRecords(zone, types, recs);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,7 +295,7 @@ public class CfDnsClient extends CfBasicHttpClient {
|
|||||||
public List<RecordEntity> recordListByType(ZoneEntity zone, @Nullable RecordType... types)
|
public List<RecordEntity> recordListByType(ZoneEntity zone, @Nullable RecordType... types)
|
||||||
throws CloudflareApiException {
|
throws CloudflareApiException {
|
||||||
String endpoint = addTypeFilter(CfRequest.RECORD_LIST.buildPath(zone.getId()), types);
|
String endpoint = addTypeFilter(CfRequest.RECORD_LIST.buildPath(zone.getId()), types);
|
||||||
List<RecordEntity> recs = getAllPages(endpoint, RecordMultipleResponse.class, RECORDS_PER_PAGE);
|
List<RecordEntity> recs = getAllPages(endpoint, RecordMultipleResponse.class, recordsPerPage);
|
||||||
return filterAndSetZoneRecords(zone, types, recs);
|
return filterAndSetZoneRecords(zone, types, recs);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ import org.jetbrains.annotations.Nullable;
|
|||||||
* <li>Content of the DNS record, such as an IP address.
|
* <li>Content of the DNS record, such as an IP address.
|
||||||
* <li>Flags indicating whether the record is proxiable or proxied.
|
* <li>Flags indicating whether the record is proxiable or proxied.
|
||||||
* <li>TTL (Time-To-Live) for the DNS record.
|
* <li>TTL (Time-To-Live) for the DNS record.
|
||||||
* <li>Priority, required for MX, SRV and URI records.
|
* <li>Priority, required for MX and URI records (for SRV records it's part of the data).
|
||||||
* <li>Structured data, required for e.g. SRV, CAA, LOC or TLSA records instead of content.
|
* <li>Structured data, required for e.g. SRV, CAA, LOC or TLSA records instead of content.
|
||||||
* <li>An optional comment and tags.
|
* <li>An optional comment and tags.
|
||||||
* <li>A locked status to indicate the immutability of the record.
|
* <li>A locked status to indicate the immutability of the record.
|
||||||
@@ -43,7 +43,8 @@ public class RecordEntity extends AbstractEntity {
|
|||||||
private Boolean proxied;
|
private Boolean proxied;
|
||||||
private Integer ttl;
|
private Integer ttl;
|
||||||
/**
|
/**
|
||||||
* Priority of the record, required for MX, SRV and URI records.
|
* Priority of the record, required for MX and URI records. For SRV records the priority is
|
||||||
|
* part of {@link #data}.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
private Integer priority;
|
private Integer priority;
|
||||||
@@ -58,7 +59,8 @@ public class RecordEntity extends AbstractEntity {
|
|||||||
@Nullable
|
@Nullable
|
||||||
private List<String> tags;
|
private List<String> tags;
|
||||||
/**
|
/**
|
||||||
* Structured content of the record, e.g. for SRV records the keys "weight", "port" and "target".
|
* Structured content of the record, e.g. for SRV records the keys "priority", "weight", "port"
|
||||||
|
* and "target".
|
||||||
* The keys are the same as in the Cloudflare API.
|
* The keys are the same as in the Cloudflare API.
|
||||||
*/
|
*/
|
||||||
@Nullable
|
@Nullable
|
||||||
|
|||||||
@@ -1,8 +1,11 @@
|
|||||||
package codes.thischwa.cf;
|
package codes.thischwa.cf;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.junit.jupiter.api.Assertions.fail;
|
import static org.junit.jupiter.api.Assertions.fail;
|
||||||
@@ -13,6 +16,7 @@ import codes.thischwa.cf.model.PagingRequest;
|
|||||||
import codes.thischwa.cf.model.RecordEntity;
|
import codes.thischwa.cf.model.RecordEntity;
|
||||||
import codes.thischwa.cf.model.RecordType;
|
import codes.thischwa.cf.model.RecordType;
|
||||||
import codes.thischwa.cf.model.ZoneEntity;
|
import codes.thischwa.cf.model.ZoneEntity;
|
||||||
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
@@ -36,6 +40,9 @@ public class CfClientTest {
|
|||||||
|
|
||||||
private final CfDnsClient client = new CfDnsClientBuilder().withEmptyResultThrowsException(true).withApiTokenAuth(API_TOKEN).build();
|
private final CfDnsClient client = new CfDnsClientBuilder().withEmptyResultThrowsException(true).withApiTokenAuth(API_TOKEN).build();
|
||||||
|
|
||||||
|
private static final String IP_PREFIX = "130.0.0.";
|
||||||
|
private static final String UPDATED_IP_PREFIX = "130.1.0.";
|
||||||
|
|
||||||
@BeforeAll
|
@BeforeAll
|
||||||
static void checkEnv() {
|
static void checkEnv() {
|
||||||
assumeTrue(API_TOKEN != null && !API_TOKEN.isBlank(), "API_TOKEN not set; skipping client tests");
|
assumeTrue(API_TOKEN != null && !API_TOKEN.isBlank(), "API_TOKEN not set; skipping client tests");
|
||||||
@@ -207,9 +214,14 @@ public class CfClientTest {
|
|||||||
assertFalse(fluentList.isEmpty());
|
assertFalse(fluentList.isEmpty());
|
||||||
assertTrue(fluentList.stream().anyMatch(re -> re.getId().equals(createdRe1.getId())));
|
assertTrue(fluentList.stream().anyMatch(re -> re.getId().equals(createdRe1.getId())));
|
||||||
|
|
||||||
// update AAAA record
|
// update AAAA record, the given record (incl. its timestamps) must not be modified
|
||||||
createdRe2.setContent("2a0a:4cc0:c0:2e4::2");
|
createdRe2.setContent("2a0a:4cc0:c0:2e4::2");
|
||||||
client.recordUpdate(z, createdRe2);
|
Instant createdOn = createdRe2.getCreatedOn();
|
||||||
|
RecordEntity updatedRe2 = client.recordUpdate(z, createdRe2);
|
||||||
|
assertEquals(createdOn, createdRe2.getCreatedOn());
|
||||||
|
assertNotNull(createdRe2.getModifiedOn());
|
||||||
|
assertEquals("2a0a:4cc0:c0:2e4::2", updatedRe2.getContent());
|
||||||
|
assertEquals(z.getId(), updatedRe2.getZoneId());
|
||||||
aaaaRecords = client.recordListBySubdomain(z, randomSld, RecordType.AAAA);
|
aaaaRecords = client.recordListBySubdomain(z, randomSld, RecordType.AAAA);
|
||||||
assertEquals(1, aaaaRecords.size());
|
assertEquals(1, aaaaRecords.size());
|
||||||
r = aaaaRecords.get(0);
|
r = aaaaRecords.get(0);
|
||||||
@@ -246,10 +258,6 @@ public class CfClientTest {
|
|||||||
assertTrue(exception.getMessage().contains("Must be one of:"));
|
assertTrue(exception.getMessage().contains("Must be one of:"));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String IP_PREFIX = "130.0.0.";
|
|
||||||
private static final String UPDATED_IP_PREFIX = "130.1.0.";
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testBatch() throws Exception {
|
void testBatch() throws Exception {
|
||||||
// starting point: already existing zone 'mein-d-ns.de'
|
// starting point: already existing zone 'mein-d-ns.de'
|
||||||
@@ -518,6 +526,15 @@ public class CfClientTest {
|
|||||||
Set<String> allRecordIds = allRecords.stream().map(RecordEntity::getId).collect(Collectors.toSet());
|
Set<String> allRecordIds = allRecords.stream().map(RecordEntity::getId).collect(Collectors.toSet());
|
||||||
assertEquals(createdRecords.size(), allRecordIds.size());
|
assertEquals(createdRecords.size(), allRecordIds.size());
|
||||||
assertTrue(allRecordIds.containsAll(generatedRecordIds));
|
assertTrue(allRecordIds.containsAll(generatedRecordIds));
|
||||||
|
|
||||||
|
// Without PagingRequest all pages must be fetched, also if the page size is small
|
||||||
|
try (CfDnsClient smallPageClient = new CfDnsClient(true, CfDnsClientBuilder.DEFAULT_BASEURL,
|
||||||
|
new CfDnsClientBuilder.ApiTokenAuth(API_TOKEN), 5)) {
|
||||||
|
assertEquals(allRecordIds, smallPageClient.recordList(zone).stream()
|
||||||
|
.map(RecordEntity::getId).collect(Collectors.toSet()));
|
||||||
|
assertEquals(createdRecords.size(), smallPageClient.recordListBySubdomain(zone, pagingSld).size());
|
||||||
|
assertEquals(createdRecords.size(), smallPageClient.recordListByType(zone, RecordType.A).size());
|
||||||
|
}
|
||||||
} finally {
|
} finally {
|
||||||
try {
|
try {
|
||||||
client.recordDeleteTypeIfExists(zone, pagingSld, RecordType.A);
|
client.recordDeleteTypeIfExists(zone, pagingSld, RecordType.A);
|
||||||
@@ -525,4 +542,186 @@ public class CfClientTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testApiErrorDetails() throws Exception {
|
||||||
|
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
||||||
|
String sub = "invalid-" + System.currentTimeMillis();
|
||||||
|
try {
|
||||||
|
// an A record with an invalid IP is rejected by Cloudflare
|
||||||
|
CloudflareApiException e = assertThrows(CloudflareApiException.class,
|
||||||
|
() -> client.recordCreateSubdomain(zone, sub, TTL, RecordType.A, "not-an-ip"));
|
||||||
|
assertEquals(CloudflareApiException.class, e.getClass());
|
||||||
|
assertEquals(400, e.getStatusCode());
|
||||||
|
assertFalse(e.getErrors().isEmpty());
|
||||||
|
assertTrue(e.getErrors().get(0).getCode() > 0);
|
||||||
|
// the Cloudflare error message is part of the exception message, not wrapped
|
||||||
|
assertTrue(e.getMessage().contains(e.getErrors().get(0).getMessage()), e.getMessage());
|
||||||
|
} finally {
|
||||||
|
try {
|
||||||
|
client.recordDeleteTypeIfExists(zone, sub, RecordType.A);
|
||||||
|
} catch (Exception e) { /* ignore */ }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRecordDeleteUnknownId() throws Exception {
|
||||||
|
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
||||||
|
CloudflareApiException e = assertThrows(CloudflareApiException.class,
|
||||||
|
() -> client.recordDelete(zone, "0123456789abcdef0123456789abcdef"));
|
||||||
|
assertInstanceOf(CloudflareNotFoundException.class, e);
|
||||||
|
assertEquals(404, e.getStatusCode());
|
||||||
|
assertFalse(e.getErrors().isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testZoneNotFound() throws Exception {
|
||||||
|
String unknownZone = "not-existing-" + System.currentTimeMillis() + ".de";
|
||||||
|
// emptyResultThrowsException = true
|
||||||
|
assertThrows(CloudflareNotFoundException.class, () -> client.zoneGet(unknownZone));
|
||||||
|
assertThrows(CloudflareNotFoundException.class, () -> client.zone(unknownZone));
|
||||||
|
|
||||||
|
// emptyResultThrowsException = false
|
||||||
|
try (CfDnsClient lenientClient = new CfDnsClientBuilder().withApiTokenAuth(API_TOKEN).build()) {
|
||||||
|
assertNull(lenientClient.zoneGet(unknownZone));
|
||||||
|
assertThrows(CloudflareNotFoundException.class, () -> lenientClient.zone(unknownZone));
|
||||||
|
assertNotNull(lenientClient.zoneGet(ZONE_STR));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testClientClose() throws Exception {
|
||||||
|
CfDnsClient closeableClient = new CfDnsClientBuilder().withApiTokenAuth(API_TOKEN).build();
|
||||||
|
// multiple requests with the same client
|
||||||
|
assertNotNull(closeableClient.zoneGet(ZONE_STR));
|
||||||
|
assertFalse(closeableClient.zoneList().isEmpty());
|
||||||
|
assertDoesNotThrow(closeableClient::close);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testMultiLevelSubdomainAndApex() throws Exception {
|
||||||
|
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
||||||
|
String multiLevel = "a.b-" + System.currentTimeMillis();
|
||||||
|
String apexContent = "cf-client-test-" + System.currentTimeMillis();
|
||||||
|
List<RecordEntity> created = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
// multi-level subdomain
|
||||||
|
RecordEntity rec = client.recordCreateSubdomain(zone, multiLevel, TTL, RecordType.A, "130.0.0.10");
|
||||||
|
created.add(rec);
|
||||||
|
assertEquals(multiLevel + "." + ZONE_STR, rec.getName());
|
||||||
|
assertEquals(multiLevel, rec.getSubdomain());
|
||||||
|
List<RecordEntity> fetched = client.recordListBySubdomain(zone, multiLevel, RecordType.A);
|
||||||
|
assertEquals(1, fetched.size());
|
||||||
|
assertEquals(multiLevel, fetched.get(0).getSubdomain());
|
||||||
|
assertEquals(ZONE_STR, fetched.get(0).getZoneName());
|
||||||
|
|
||||||
|
// batch with a multi-level name must not cut the name
|
||||||
|
String batchName = "c.d-" + System.currentTimeMillis() + "." + ZONE_STR;
|
||||||
|
BatchEntry batchResult = client.recordBatch(zone,
|
||||||
|
List.of(RecordEntity.build(batchName, RecordType.A, TTL, "130.0.0.11")), null, null, null);
|
||||||
|
RecordEntity batched = batchResult.getPosts().get(0);
|
||||||
|
created.add(batched);
|
||||||
|
assertEquals(batchName, batched.getName());
|
||||||
|
|
||||||
|
// zone apex (TXT record with unique content, existing apex records stay untouched)
|
||||||
|
RecordEntity apex = client.recordCreateSubdomain(zone, CfDnsClient.APEX, TTL, RecordType.TXT,
|
||||||
|
"\"" + apexContent + "\"");
|
||||||
|
created.add(apex);
|
||||||
|
assertEquals(ZONE_STR, apex.getName());
|
||||||
|
assertEquals(CfDnsClient.APEX, apex.getSubdomain());
|
||||||
|
assertTrue(client.recordListBySubdomain(zone, CfDnsClient.APEX, RecordType.TXT).stream()
|
||||||
|
.anyMatch(r -> r.getId().equals(apex.getId()) && CfDnsClient.APEX.equals(r.getSubdomain())));
|
||||||
|
} finally {
|
||||||
|
deleteQuietly(zone, created);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testTypeFilter() throws Exception {
|
||||||
|
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
||||||
|
String sub = "types-" + System.currentTimeMillis();
|
||||||
|
List<RecordEntity> created = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
created.add(client.recordCreateSubdomain(zone, sub, TTL, RecordType.A, "130.0.0.20"));
|
||||||
|
created.add(client.recordCreateSubdomain(zone, sub, TTL, RecordType.AAAA, "2001:db8::20"));
|
||||||
|
created.add(client.recordCreateSubdomain(zone, sub, TTL, RecordType.TXT, "\"type filter\""));
|
||||||
|
|
||||||
|
// one type: filtered by Cloudflare
|
||||||
|
List<RecordEntity> txt = client.recordListBySubdomain(zone, sub, RecordType.TXT);
|
||||||
|
assertEquals(1, txt.size());
|
||||||
|
assertEquals(RecordType.TXT.getType(), txt.get(0).getType());
|
||||||
|
assertTrue(client.recordListByType(zone, RecordType.TXT).stream()
|
||||||
|
.allMatch(r -> RecordType.TXT.getType().equals(r.getType())));
|
||||||
|
|
||||||
|
// multiple types: filtered by the client
|
||||||
|
List<RecordEntity> ip = client.recordListBySubdomain(zone, sub, RecordType.A, RecordType.AAAA);
|
||||||
|
assertEquals(Set.of("A", "AAAA"), ip.stream().map(RecordEntity::getType).collect(Collectors.toSet()));
|
||||||
|
|
||||||
|
// no type: all records
|
||||||
|
assertEquals(3, client.recordListBySubdomain(zone, sub).size());
|
||||||
|
} finally {
|
||||||
|
deleteQuietly(zone, created);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testRecordAttributes() throws Exception {
|
||||||
|
ZoneEntity zone = client.zoneGet(ZONE_STR);
|
||||||
|
String sub = "attr-" + System.currentTimeMillis();
|
||||||
|
List<RecordEntity> created = new ArrayList<>();
|
||||||
|
Instant start = Instant.now().minusSeconds(60);
|
||||||
|
try {
|
||||||
|
// MX record with priority and comment
|
||||||
|
RecordEntity mx = RecordEntity.build(sub + "." + ZONE_STR, RecordType.MX, TTL, "mail." + ZONE_STR);
|
||||||
|
mx.setPriority(10);
|
||||||
|
mx.setComment("cf client test");
|
||||||
|
RecordEntity createdMx = client.recordCreate(zone, mx);
|
||||||
|
created.add(createdMx);
|
||||||
|
assertEquals(10, createdMx.getPriority());
|
||||||
|
assertEquals("cf client test", createdMx.getComment());
|
||||||
|
assertNull(mx.getId(), "the given record must not be modified");
|
||||||
|
// timestamps are Instants (UTC)
|
||||||
|
assertTrue(createdMx.getCreatedOn().isAfter(start), createdMx.getCreatedOn().toString());
|
||||||
|
assertTrue(createdMx.getCreatedOn().isBefore(Instant.now().plusSeconds(60)));
|
||||||
|
|
||||||
|
RecordEntity fetchedMx = client.recordListBySubdomain(zone, sub, RecordType.MX).get(0);
|
||||||
|
assertEquals(10, fetchedMx.getPriority());
|
||||||
|
assertEquals("cf client test", fetchedMx.getComment());
|
||||||
|
|
||||||
|
// partial update keeps the attributes which aren't sent
|
||||||
|
RecordEntity updatedMx = client.recordUpdate(zone, RecordEntity.build(fetchedMx.getId(), "mx2." + ZONE_STR));
|
||||||
|
assertEquals("mx2." + ZONE_STR, updatedMx.getContent());
|
||||||
|
assertEquals(10, updatedMx.getPriority());
|
||||||
|
assertEquals("cf client test", updatedMx.getComment());
|
||||||
|
|
||||||
|
// batch PATCH changes the priority
|
||||||
|
fetchedMx = client.recordListBySubdomain(zone, sub, RecordType.MX).get(0);
|
||||||
|
fetchedMx.setPriority(20);
|
||||||
|
client.recordBatch(zone, null, null, List.of(fetchedMx), null);
|
||||||
|
assertEquals(20, client.recordListBySubdomain(zone, sub, RecordType.MX).get(0).getPriority());
|
||||||
|
|
||||||
|
// SRV record with structured data, for SRV records the priority is part of the data
|
||||||
|
String srvName = "_sip._tcp." + sub + "." + ZONE_STR;
|
||||||
|
RecordEntity srv = RecordEntity.build(srvName, RecordType.SRV, TTL, null);
|
||||||
|
srv.setData(Map.of("priority", 10, "weight", 5, "port", 5060, "target", "sip." + ZONE_STR));
|
||||||
|
RecordEntity createdSrv = client.recordCreate(zone, srv);
|
||||||
|
created.add(createdSrv);
|
||||||
|
assertEquals(srvName, createdSrv.getName());
|
||||||
|
assertEquals(10, ((Number) createdSrv.getData().get("priority")).intValue());
|
||||||
|
assertEquals(5060, ((Number) createdSrv.getData().get("port")).intValue());
|
||||||
|
assertEquals(5, ((Number) createdSrv.getData().get("weight")).intValue());
|
||||||
|
assertEquals("sip." + ZONE_STR, createdSrv.getData().get("target"));
|
||||||
|
} finally {
|
||||||
|
deleteQuietly(zone, created);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void deleteQuietly(ZoneEntity zone, List<RecordEntity> records) {
|
||||||
|
for (RecordEntity rec : records) {
|
||||||
|
try {
|
||||||
|
client.recordDelete(zone, rec.getId());
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.warn("Couldn't delete test record {}: {}", rec.getName(), e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -706,7 +706,7 @@ class CfDnsClientMockTest {
|
|||||||
RecordEntity srv = RecordEntity.build("_sip._tcp.example.com", RecordType.SRV, 300, null);
|
RecordEntity srv = RecordEntity.build("_sip._tcp.example.com", RecordType.SRV, 300, null);
|
||||||
srv.setId("srv1");
|
srv.setId("srv1");
|
||||||
srv.setPriority(10);
|
srv.setPriority(10);
|
||||||
srv.setData(Map.of("weight", 5, "port", 5060, "target", "sip.example.com"));
|
srv.setData(Map.of("priority", 10, "weight", 5, "port", 5060, "target", "sip.example.com"));
|
||||||
RecordEntity proxied = RecordEntity.build("www.example.com", RecordType.A, 1, "1.2.3.4");
|
RecordEntity proxied = RecordEntity.build("www.example.com", RecordType.A, 1, "1.2.3.4");
|
||||||
proxied.setId("www1");
|
proxied.setId("www1");
|
||||||
proxied.setProxied(true);
|
proxied.setProxied(true);
|
||||||
|
|||||||
Reference in new issue
Block a user