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.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.LocalDate;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
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.assertNotNull;
|
||||||
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;
|
||||||
@@ -27,13 +26,10 @@ public class CfClientTest {
|
|||||||
private final CfDnsClient client = new CfDnsClient(API_EMAIL, API_KEY);
|
private final CfDnsClient client = new CfDnsClient(API_EMAIL, API_KEY);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testList() throws Exception {
|
void testZoneListAnlFailedSldList() throws Exception {
|
||||||
List<ZoneEntity> zList = client.zoneListAll();
|
List<ZoneEntity> zList = client.zoneListAll();
|
||||||
assertEquals(1, zList.size());
|
assertEquals(1, zList.size());
|
||||||
|
|
||||||
List<RecordEntity> rList = client.sldListAll(zList.get(0), "test");
|
|
||||||
assertFalse(rList.isEmpty());
|
|
||||||
|
|
||||||
assertThrows(CloudflareNotFoundException.class,
|
assertThrows(CloudflareNotFoundException.class,
|
||||||
() -> client.sldListAll(zList.get(0), "not-existing"));
|
() -> client.sldListAll(zList.get(0), "not-existing"));
|
||||||
}
|
}
|
||||||
@@ -48,6 +44,7 @@ public class CfClientTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testDns() throws Exception {
|
void testDns() throws Exception {
|
||||||
|
// starting point: already existing zone 'mein-d-ns.de'
|
||||||
ZoneEntity z = client.zoneInfo(ZONE_STR);
|
ZoneEntity z = client.zoneInfo(ZONE_STR);
|
||||||
assertEquals("0a83dd6e7f8c46039f2517bbded8115e", z.getId());
|
assertEquals("0a83dd6e7f8c46039f2517bbded8115e", z.getId());
|
||||||
assertEquals("mein-d-ns.de", z.getName());
|
assertEquals("mein-d-ns.de", z.getName());
|
||||||
@@ -59,52 +56,82 @@ public class CfClientTest {
|
|||||||
assertNotNull(z.getActivatedOn());
|
assertNotNull(z.getActivatedOn());
|
||||||
assertNotNull(z.getModifiedOn());
|
assertNotNull(z.getModifiedOn());
|
||||||
assertNotNull(z.getCreatedOn());
|
assertNotNull(z.getCreatedOn());
|
||||||
assertEquals(LocalDate.of(2025, 1, 20), z.getCreatedOn().toLocalDate());
|
|
||||||
|
|
||||||
RecordEntity r = client.sldInfo(z, "test", RecordType.A);
|
// for all other functions use a newly created SLD and ensure cleanup at the end
|
||||||
assertEquals("b345fec8769a2980811a8ff901b4e158", r.getId());
|
String randomSld = SLD_STR + "-" + System.currentTimeMillis();
|
||||||
assertEquals("test.mein-d-ns.de", r.getName());
|
String domain = randomSld + "." + ZONE_STR;
|
||||||
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;
|
RecordEntity r;
|
||||||
client.recordDeleteTypeIfExists(z, SLD_STR, RecordType.A, RecordType.AAAA);
|
RecordEntity createdRe1 = null;
|
||||||
RecordEntity createdRe1 =
|
RecordEntity createdRe2 = null;
|
||||||
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);
|
try {
|
||||||
assertEquals("130.0.0.3", r.getContent());
|
// ensure clean state
|
||||||
RecordEntity createdRe2 =
|
client.recordDeleteTypeIfExists(z, randomSld, RecordType.A, RecordType.AAAA);
|
||||||
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());
|
|
||||||
|
|
||||||
createdRe2.setContent("2a0a:4cc0:c0:2e4::2");
|
// create A record using recordCreate with full domain
|
||||||
client.recordUpdate(z, createdRe2);
|
createdRe1 =
|
||||||
r = client.sldInfo(z, SLD_STR, RecordType.AAAA);
|
client.recordCreate(z, RecordEntity.build(domain, RecordType.A, TTL, "130.0.0.3"));
|
||||||
assertEquals("2a0a:4cc0:c0:2e4::2", r.getContent());
|
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);
|
// verify sldInfo for A
|
||||||
assertEquals("130.0.0.3", r.getContent());
|
r = client.sldInfo(z, randomSld, RecordType.A);
|
||||||
assertTrue(client.recordDelete(z, createdRe2));
|
assertEquals("130.0.0.3", r.getContent());
|
||||||
assertThrows(CloudflareNotFoundException.class,
|
|
||||||
() -> client.sldInfo(z, SLD_STR, RecordType.AAAA));
|
|
||||||
|
|
||||||
client.recordDeleteTypeIfExists(z, SLD_STR, RecordType.A);
|
// create AAAA record using recordCreateSld
|
||||||
assertThrows(CloudflareNotFoundException.class, () -> client.sldInfo(z, SLD_STR, RecordType.A));
|
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
|
@Test
|
||||||
|
|||||||
Reference in New Issue
Block a user