use of ai.nightfall.scan.model.ScanTextResponse in project nightfall-java-sdk by nightfallai.
the class NightfallClientTest method testScanText_RateLimitRetried.
@Test
public void testScanText_RateLimitRetried() {
// use reference type to work around Java inner class limitations
final int[] reqCount = { 0 };
try (MockWebServer server = new MockWebServer()) {
server.setDispatcher(new Dispatcher() {
@NotNull
@Override
public MockResponse dispatch(@NotNull RecordedRequest r) {
if (++reqCount[0] >= 3) {
return new MockResponse().setBody("{\"findings\": [[]]}");
}
// rate limit the first few requests
return new MockResponse().setResponseCode(429).setBody(getRateLimitErrorResponse());
}
});
List<List<Finding>> expectedFindings = Arrays.asList(Arrays.asList());
NightfallClient c = new NightfallClient(getRequestURL(server), "key", 1, getHttpClient());
ScanTextRequest req = new ScanTextRequest(null, (ScanTextConfig) null);
ScanTextResponse resp = c.scanText(req);
assertEquals(expectedFindings, resp.getFindings());
// validate that rate limit responses were actually returned
assertEquals(reqCount[0], 3);
} catch (IOException e) {
fail("IOException during test: " + e.getMessage());
}
}
use of ai.nightfall.scan.model.ScanTextResponse in project nightfall-java-sdk by nightfallai.
the class TextScannerExample method main.
/**
* Submit the provided args for scanning and print the result.
*/
public static void main(String[] args) {
if (args.length == 0) {
throw new RuntimeException(usage);
}
try (NightfallClient client = NightfallClient.Builder.defaultClient()) {
Detector creditCard = new Detector("CREDIT_CARD_NUMBER");
creditCard.setMinConfidence(Confidence.LIKELY);
creditCard.setMinNumFindings(1);
Detector ssn = new Detector("US_SOCIAL_SECURITY_NUMBER");
ssn.setMinConfidence(Confidence.POSSIBLE);
ssn.setMinNumFindings(1);
// A rule contains a set of detectors to scan with
DetectionRule rule = new DetectionRule(Arrays.asList(creditCard, ssn), LogicalOp.ANY);
List<String> payload = Arrays.asList(args).subList(1, args.length);
// Define some detectors to use to scan your data
ScanTextConfig config = ScanTextConfig.fromDetectionRules(Arrays.asList(rule), 20);
ScanTextRequest req = new ScanTextRequest(payload, config);
ScanTextResponse response = client.scanText(req);
System.out.println("findings: " + response.getFindings());
}
}
use of ai.nightfall.scan.model.ScanTextResponse in project nightfall-java-sdk by nightfallai.
the class NightfallClientTest method testScanText_HappyPath.
@Test
public void testScanText_HappyPath() {
try (MockWebServer server = new MockWebServer()) {
server.enqueue(new MockResponse().setBody("{\"findings\": [[]]}"));
List<List<Finding>> expectedFindings = Arrays.asList(Collections.emptyList());
NightfallClient c = new NightfallClient(getRequestURL(server), "key", 1, getHttpClient());
RedactionConfig defRedCfg = new RedactionConfig(new SubstitutionConfig("REDACTED"));
ScanTextConfig cfg = new ScanTextConfig(Arrays.asList(UUID.fromString("c08c6c43-85ca-40e5-8f46-7d1cf1e176a3")), Collections.emptyList(), 20, defRedCfg);
ScanTextRequest req = new ScanTextRequest(null, cfg);
ScanTextResponse resp = c.scanText(req);
assertEquals(expectedFindings, resp.getFindings());
} catch (IOException e) {
fail("IOException during test: " + e.getMessage());
}
}
use of ai.nightfall.scan.model.ScanTextResponse in project nightfall-java-sdk by nightfallai.
the class NightfallClientTest method testScanText_IgnoresUnrecognizedProperties.
@Test
public void testScanText_IgnoresUnrecognizedProperties() {
try (MockWebServer server = new MockWebServer()) {
// deserializer should not throw an exception just because of the unrecognized key `foo`
server.enqueue(new MockResponse().setBody("{\"findings\": [[]], \"foo\": \"bar\"}"));
List<List<Finding>> expectedFindings = Arrays.asList(Collections.emptyList());
NightfallClient c = new NightfallClient(getRequestURL(server), "key", 1, getHttpClient());
RedactionConfig defRedCfg = new RedactionConfig(new SubstitutionConfig("REDACTED"));
ScanTextConfig cfg = new ScanTextConfig(Arrays.asList(UUID.fromString("c08c6c43-85ca-40e5-8f46-7d1cf1e176a3")), Collections.emptyList(), 20, defRedCfg);
ScanTextRequest req = new ScanTextRequest(null, cfg);
ScanTextResponse resp = c.scanText(req);
assertEquals(expectedFindings, resp.getFindings());
} catch (IOException e) {
fail("IOException during test: " + e.getMessage());
}
}
Aggregations