issue #6 - ResponseResultInfo#Errors: wrong object structure

issue #5 - Add batch DNS record operations and refactor Cloudflare API client.
This commit is contained in:
thischwa committed 2025-12-25 14:27:18 +01:00
1 parent eb821a2218
commit b4ce867efc
10 files changed
+274 -49

No files matched your search

@@ -1,17 +1,17 @@
package codes.thischwa.cf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import codes.thischwa.cf.model.RecordEntity;
import codes.thischwa.cf.model.RecordType;
import codes.thischwa.cf.model.ZoneEntity;
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.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
@@ -149,4 +149,48 @@ public class CfClientTest {
assertThrows(IllegalArgumentException.class, () -> new CfDnsClient("email", ""));
assertThrows(IllegalArgumentException.class, () -> new CfDnsClient("", "key"));
}
@Test
void testBatch() throws Exception {
// starting point: already existing zone 'mein-d-ns.de'
ZoneEntity z = client.zoneInfo(ZONE_STR);
String sld1 = SLD_STR + "-1";
String sld2 = SLD_STR + "-2";
String sld3 = SLD_STR + "-3";
RecordEntity r1 = RecordEntity.build(sld1, RecordType.A, TTL, "130.0.0.1");
RecordEntity r2 = RecordEntity.build(sld2, RecordType.A, TTL, "130.0.0.2");
RecordEntity r3 = RecordEntity.build(sld3, RecordType.A, TTL, "130.0.0.3");
// ensure clean state
client.recordDeleteTypeIfExists(z, sld1, RecordType.A);
client.recordDeleteTypeIfExists(z, sld2, RecordType.A);
client.recordDeleteTypeIfExists(z, sld3, RecordType.A);
try {
// test put
client.recordBatch(z, 30, List.of(r1, r2, r3), null, null);
RecordEntity testRec = client.sldInfo(z, sld1, RecordType.A);
assertEquals("130.0.0.1", testRec.getContent());
testRec = client.sldInfo(z, sld2, RecordType.A);
assertEquals("130.0.0.2", testRec.getContent());
testRec = client.sldInfo(z, sld3, RecordType.A);
assertEquals("130.0.0.3", testRec.getContent());
// test patch
r1 = client.sldInfo(z, sld1, RecordType.A);
r1.setContent("130.1.0.1");
client.recordBatch(z, 30, null, List.of(r1), null);
testRec = client.sldInfo(z, sld1, RecordType.A);
assertEquals("130.1.0.1", testRec.getContent());
// test delete
client.recordBatch(z, 30, null, null, List.of(r1));
assertThrows(CloudflareNotFoundException.class,
() -> client.sldInfo(z, sld1, RecordType.A));
} finally {
client.recordDeleteTypeIfExists(z, sld1, RecordType.A);
client.recordDeleteTypeIfExists(z, sld2, RecordType.A);
client.recordDeleteTypeIfExists(z, sld3, RecordType.A);
}
}
}
@@ -1,20 +1,53 @@
package codes.thischwa.cf;
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.fail;
import codes.thischwa.cf.model.AbstractResponse;
import codes.thischwa.cf.model.BatchResponse;
import codes.thischwa.cf.model.RecordMultipleResponse;
import codes.thischwa.cf.model.RecordSingleResponse;
import codes.thischwa.cf.model.ResponseResultInfo;
import codes.thischwa.cf.model.ZoneMultipleResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import java.io.InputStream;
import java.util.List;
import org.junit.jupiter.api.Test;
public class ObjectMapperTest {
private final ObjectMapper mapper = JsonConf.initObjectMapper();
@Test
void testObjectMapper() throws IOException {
ObjectMapper mapper = JsonConf.initObjectMapper();
ZoneMultipleResponse resp =
mapper.readValue(this.getClass().getResourceAsStream("/zone-list-response.json"),
ZoneMultipleResponse.class);
mapper.readValue(this.getClass().getResourceAsStream("/zone-list-response.json"),
ZoneMultipleResponse.class);
assertNotNull(resp.getResponseResultInfo());
}
@Test
void testErrorResponse() throws IOException {
List<Class<? extends AbstractResponse>> respClasses =
List.of(RecordSingleResponse.class, RecordMultipleResponse.class, ZoneMultipleResponse.class, BatchResponse.class);
respClasses.forEach(this::assertErrorResponse);
}
private void assertErrorResponse(Class<? extends AbstractResponse> clazz) {
InputStream in = this.getClass().getResourceAsStream("/error-response.json");
try {
AbstractResponse resp = mapper.readValue(in, clazz);
assertNotNull(resp);
assertNotNull(resp.getResponseResultInfo());
ResponseResultInfo resultInfo = resp.getResponseResultInfo();
assertFalse(resultInfo.isSuccess());
assertEquals(1, resultInfo.getErrors().size());
assertEquals(81053, resultInfo.getErrors().get(0).getCode());
} catch (IOException e) {
fail("fail for " + clazz + ": " + e.getMessage());
}
}
}
@@ -1,16 +1,16 @@
package codes.thischwa.cf;
import codes.thischwa.cf.model.AbstractResponse;
import codes.thischwa.cf.model.RecordMultipleResponse;
import codes.thischwa.cf.model.ResponseResultInfo;
import codes.thischwa.cf.model.ResultInfo;
import java.util.Arrays;
import lombok.Getter;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
import codes.thischwa.cf.model.AbstractResponse;
import codes.thischwa.cf.model.RecordMultipleResponse;
import codes.thischwa.cf.model.ResponseResultInfo;
import codes.thischwa.cf.model.ResultInfo;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
@@ -48,9 +48,15 @@ class ResponseValidatorTest {
@Test
void validateFailedResponse() {
List<ResponseResultInfo.Error> errors = new ArrayList<>();
ResponseResultInfo.Error error = new ResponseResultInfo.Error(1, "Fehler 1");
errors.add(error);
error = new ResponseResultInfo.Error(2, "Fehler 2");
errors.add(error);
when(mockResponse.getResponseResultInfo()).thenReturn(mockResultInfo);
when(mockResultInfo.isSuccess()).thenReturn(false);
when(mockResultInfo.getErrors()).thenReturn(Arrays.asList("Fehler 1", "Fehler 2"));
when(mockResultInfo.getErrors()).thenReturn(errors);
CloudflareApiException exception = assertThrows(CloudflareApiException.class,
() -> validatorWithException.validate(mockResponse, false));