Initial commit
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
package codes.thischwa.cf;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
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 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 final String email = System.getenv("API_EMAIL");
|
||||
private final String apiKey = System.getenv("API_KEY");
|
||||
private final String apiToken = System.getenv("API_TOKEN");
|
||||
|
||||
private final CfDnsClient client = new CfDnsClient(email, apiKey, apiToken);
|
||||
|
||||
@Test
|
||||
void testList() 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), "notexisting"));
|
||||
|
||||
client.setEmptyResultThrowsException(false);
|
||||
rList = client.sldListAll(zList.get(0), "notexisting");
|
||||
assertTrue(rList.isEmpty());
|
||||
client.setEmptyResultThrowsException(true);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testDns() throws Exception {
|
||||
ZoneEntity z = client.zoneInfo(zoneStr);
|
||||
assertEquals("0a83dd6e7f8c46039f2517bbded8115e", z.getId());
|
||||
assertEquals("mein-d-ns.de", z.getName());
|
||||
assertEquals("active", z.getStatus());
|
||||
assertEquals(2, z.getNameServers().size());
|
||||
assertTrue(z.getNameServers().contains("sergi.ns.cloudflare.com"));
|
||||
assertEquals(4, z.getOriginalNameServers().size());
|
||||
assertTrue(z.getOriginalNameServers().contains("a.ns14.net"));
|
||||
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());
|
||||
|
||||
RecordEntity createdRe1 =
|
||||
client.recordCreate(
|
||||
z, RecordEntity.build(sldStr + "." + zoneStr, RecordType.A, ttl, "130.0.0.3"));
|
||||
r = client.sldInfo(z, sldStr, 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);
|
||||
assertEquals("2a0a:4cc0:c0:2e4::1", r.getContent());
|
||||
|
||||
createdRe2.setContent("2a0a:4cc0:c0:2e4::2");
|
||||
client.recordUpdate(z, createdRe2);
|
||||
r = client.sldInfo(z, sldStr, RecordType.AAAA);
|
||||
assertEquals("2a0a:4cc0:c0:2e4::2", r.getContent());
|
||||
|
||||
r = client.sldInfo(z, sldStr, RecordType.A);
|
||||
assertEquals("130.0.0.3", r.getContent());
|
||||
assertTrue(client.recordDelete(z, createdRe2));
|
||||
assertThrows(
|
||||
CloudflareNotFoundException.class, () -> client.sldInfo(z, sldStr, RecordType.AAAA));
|
||||
|
||||
client.recordDeleteTypeIfExists(z, sldStr, RecordType.A);
|
||||
assertThrows(CloudflareNotFoundException.class, () -> client.sldInfo(z, sldStr, RecordType.A));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package codes.thischwa.cf;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import codes.thischwa.cf.model.RecordType;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class CfRequestTest {
|
||||
|
||||
@Test
|
||||
public void testBuildPathWithSingleVariable() {
|
||||
String result = CfRequest.RECORD_CREATE.buildPath("zone123");
|
||||
assertEquals("/zones/zone123/dns_records", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildPathWithMultipleVariables() {
|
||||
String result = CfRequest.RECORD_UPDATE.buildPath("zone123", "record456");
|
||||
assertEquals("/zones/zone123/dns_records/record456", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildPathWithoutVariables() {
|
||||
String result = CfRequest.ZONE_LIST.buildPath();
|
||||
assertEquals("/zones", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildRecordInfoName() {
|
||||
String result = CfRequest.RECORD_INFO_NAME.buildPath("zone123", "sub.domain.com");
|
||||
assertEquals("/zones/zone123/dns_records?name=sub.domain.com", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildRecordDelete() {
|
||||
String result = CfRequest.RECORD_DELETE.buildPath("zone123", "record789");
|
||||
assertEquals("/zones/zone123/dns_records/record789", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildZoneInfo() {
|
||||
String result = CfRequest.ZONE_INFO.buildPath("zone123");
|
||||
assertEquals("/zones?name=zone123", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildRecordInfo() {
|
||||
String result = CfRequest.RECORD_INFO_NAME_TYPE.buildPath("zone123", "sld.domain.com", RecordType.A);
|
||||
assertEquals("/zones/zone123/dns_records?name=sld.domain.com&type=A", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildPathInvalidArguments() {
|
||||
assertThrows(
|
||||
IllegalArgumentException.class,
|
||||
() -> CfRequest.RECORD_INFO_NAME_TYPE.buildPath("zone123", "sld.domain.com"));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package codes.thischwa.cf.model;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class PagingRequestTest {
|
||||
|
||||
@Test
|
||||
public void testBuildPath() {
|
||||
String result = PagingRequest.defaultPaging().addQueryString("/zones");
|
||||
assertEquals("/zones?page=1&perPage=5000000", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBuildPathAdditional() {
|
||||
String result = new PagingRequest( 10, 100).addQueryString("/zones?foo=bar");
|
||||
assertEquals("/zones?foo=bar&page=10&perPage=100", result);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration debug="false">
|
||||
|
||||
<appender name="current"
|
||||
class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%t] %-5level %logger{50} - %msg%n</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="debug">
|
||||
<appender-ref ref="current"/>
|
||||
</root>
|
||||
</configuration>
|
||||
Reference in New Issue
Block a user