Refactor CfClientTest to improve test clarity, ensure cleanup, and enhance validation logic
This commit is contained in:
@@ -3,11 +3,10 @@ package codes.thischwa.cf;
|
||||
import codes.thischwa.cf.model.RecordEntity;
|
||||
import codes.thischwa.cf.model.RecordType;
|
||||
import codes.thischwa.cf.model.ZoneEntity;
|
||||
import java.time.LocalDate;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
@@ -27,13 +26,10 @@ public class CfClientTest {
|
||||
private final CfDnsClient client = new CfDnsClient(API_EMAIL, API_KEY);
|
||||
|
||||
@Test
|
||||
void testList() throws Exception {
|
||||
void testZoneListAnlFailedSldList() throws Exception {
|
||||
List<ZoneEntity> zList = client.zoneListAll();
|
||||
assertEquals(1, zList.size());
|
||||
|
||||
List<RecordEntity> rList = client.sldListAll(zList.get(0), "test");
|
||||
assertFalse(rList.isEmpty());
|
||||
|
||||
assertThrows(CloudflareNotFoundException.class,
|
||||
() -> client.sldListAll(zList.get(0), "not-existing"));
|
||||
}
|
||||
@@ -48,6 +44,7 @@ public class CfClientTest {
|
||||
|
||||
@Test
|
||||
void testDns() throws Exception {
|
||||
// starting point: already existing zone 'mein-d-ns.de'
|
||||
ZoneEntity z = client.zoneInfo(ZONE_STR);
|
||||
assertEquals("0a83dd6e7f8c46039f2517bbded8115e", z.getId());
|
||||
assertEquals("mein-d-ns.de", z.getName());
|
||||
@@ -59,52 +56,82 @@ public class CfClientTest {
|
||||
assertNotNull(z.getActivatedOn());
|
||||
assertNotNull(z.getModifiedOn());
|
||||
assertNotNull(z.getCreatedOn());
|
||||
assertEquals(LocalDate.of(2025, 1, 20), z.getCreatedOn().toLocalDate());
|
||||
|
||||
RecordEntity r = client.sldInfo(z, "test", RecordType.A);
|
||||
assertEquals("b345fec8769a2980811a8ff901b4e158", r.getId());
|
||||
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());
|
||||
// for all other functions use a newly created SLD and ensure cleanup at the end
|
||||
String randomSld = SLD_STR + "-" + System.currentTimeMillis();
|
||||
String domain = randomSld + "." + ZONE_STR;
|
||||
|
||||
String domain = SLD_STR + "." + ZONE_STR;
|
||||
client.recordDeleteTypeIfExists(z, SLD_STR, RecordType.A, RecordType.AAAA);
|
||||
RecordEntity createdRe1 =
|
||||
client.recordCreate(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());
|
||||
RecordEntity r;
|
||||
RecordEntity createdRe1 = null;
|
||||
RecordEntity createdRe2 = null;
|
||||
|
||||
r = client.sldInfo(z, SLD_STR, RecordType.A);
|
||||
assertEquals("130.0.0.3", r.getContent());
|
||||
RecordEntity createdRe2 =
|
||||
client.recordCreateSld(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());
|
||||
try {
|
||||
// ensure clean state
|
||||
client.recordDeleteTypeIfExists(z, randomSld, RecordType.A, RecordType.AAAA);
|
||||
|
||||
createdRe2.setContent("2a0a:4cc0:c0:2e4::2");
|
||||
client.recordUpdate(z, createdRe2);
|
||||
r = client.sldInfo(z, SLD_STR, RecordType.AAAA);
|
||||
assertEquals("2a0a:4cc0:c0:2e4::2", r.getContent());
|
||||
// create A record using recordCreate with full domain
|
||||
createdRe1 =
|
||||
client.recordCreate(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());
|
||||
assertTrue(client.recordDelete(z, createdRe2));
|
||||
assertThrows(CloudflareNotFoundException.class,
|
||||
() -> client.sldInfo(z, SLD_STR, RecordType.AAAA));
|
||||
// verify sldInfo for A
|
||||
r = client.sldInfo(z, randomSld, RecordType.A);
|
||||
assertEquals("130.0.0.3", r.getContent());
|
||||
|
||||
client.recordDeleteTypeIfExists(z, SLD_STR, RecordType.A);
|
||||
assertThrows(CloudflareNotFoundException.class, () -> client.sldInfo(z, SLD_STR, RecordType.A));
|
||||
// create AAAA record using recordCreateSld
|
||||
createdRe2 =
|
||||
client.recordCreateSld(z, randomSld, TTL, RecordType.AAAA, "2a0a:4cc0:c0:2e4::1");
|
||||
r = client.sldInfo(z, randomSld, RecordType.AAAA);
|
||||
assertEquals("2a0a:4cc0:c0:2e4::1", r.getContent());
|
||||
assertEquals(RecordType.AAAA.getType(), r.getType());
|
||||
|
||||
// test sldListAll
|
||||
List<RecordEntity> rList = client.sldListAll(z, randomSld);
|
||||
assertEquals(2, rList.size());
|
||||
for (RecordEntity re : rList) {
|
||||
if (Objects.equals(re.getType(), RecordType.A.getType())) {
|
||||
assertEquals("130.0.0.3", re.getContent());
|
||||
} else if (Objects.equals(re.getType(), RecordType.AAAA.getType())) {
|
||||
assertEquals("2a0a:4cc0:c0:2e4::1", re.getContent());
|
||||
} else {
|
||||
throw new IllegalStateException("Unexpected record type: " + re.getType());
|
||||
}
|
||||
}
|
||||
|
||||
// update AAAA record
|
||||
createdRe2.setContent("2a0a:4cc0:c0:2e4::2");
|
||||
client.recordUpdate(z, createdRe2);
|
||||
r = client.sldInfo(z, randomSld, RecordType.AAAA);
|
||||
assertEquals("2a0a:4cc0:c0:2e4::2", r.getContent());
|
||||
|
||||
// verify A record still intact
|
||||
r = client.sldInfo(z, randomSld, RecordType.A);
|
||||
assertEquals("130.0.0.3", r.getContent());
|
||||
|
||||
// delete AAAA record and verify it's gone
|
||||
assertTrue(client.recordDelete(z, createdRe2));
|
||||
assertThrows(CloudflareNotFoundException.class,
|
||||
() -> client.sldInfo(z, randomSld, RecordType.AAAA));
|
||||
|
||||
// delete A record using helper and verify it's gone
|
||||
client.recordDeleteTypeIfExists(z, randomSld, RecordType.A);
|
||||
assertThrows(CloudflareNotFoundException.class,
|
||||
() -> client.sldInfo(z, randomSld, RecordType.A));
|
||||
} finally {
|
||||
// cleanup in case of failures during test
|
||||
try {
|
||||
client.recordDeleteTypeIfExists(z, randomSld, RecordType.AAAA);
|
||||
} catch (Exception e) { /* ignore */ }
|
||||
try {
|
||||
client.recordDeleteTypeIfExists(z, randomSld, RecordType.A);
|
||||
} catch (Exception e) { /* ignore */ }
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user