Search in sources :

Example 1 with ScanTextResponse

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());
    }
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) ScanTextResponse(ai.nightfall.scan.model.ScanTextResponse) IOException(java.io.IOException) Dispatcher(okhttp3.mockwebserver.Dispatcher) NotNull(org.jetbrains.annotations.NotNull) ScanTextRequest(ai.nightfall.scan.model.ScanTextRequest) MockWebServer(okhttp3.mockwebserver.MockWebServer) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 2 with ScanTextResponse

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());
    }
}
Also used : NightfallClient(ai.nightfall.scan.NightfallClient) Detector(ai.nightfall.scan.model.Detector) ScanTextConfig(ai.nightfall.scan.model.ScanTextConfig) DetectionRule(ai.nightfall.scan.model.DetectionRule) ScanTextResponse(ai.nightfall.scan.model.ScanTextResponse) ScanTextRequest(ai.nightfall.scan.model.ScanTextRequest)

Example 3 with ScanTextResponse

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());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) ScanTextConfig(ai.nightfall.scan.model.ScanTextConfig) List(java.util.List) SubstitutionConfig(ai.nightfall.scan.model.redaction.SubstitutionConfig) ScanTextResponse(ai.nightfall.scan.model.ScanTextResponse) IOException(java.io.IOException) RedactionConfig(ai.nightfall.scan.model.redaction.RedactionConfig) ScanTextRequest(ai.nightfall.scan.model.ScanTextRequest) Test(org.junit.jupiter.api.Test)

Example 4 with ScanTextResponse

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());
    }
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) ScanTextConfig(ai.nightfall.scan.model.ScanTextConfig) List(java.util.List) SubstitutionConfig(ai.nightfall.scan.model.redaction.SubstitutionConfig) ScanTextResponse(ai.nightfall.scan.model.ScanTextResponse) IOException(java.io.IOException) RedactionConfig(ai.nightfall.scan.model.redaction.RedactionConfig) ScanTextRequest(ai.nightfall.scan.model.ScanTextRequest) Test(org.junit.jupiter.api.Test)

Aggregations

ScanTextRequest (ai.nightfall.scan.model.ScanTextRequest)4 ScanTextResponse (ai.nightfall.scan.model.ScanTextResponse)4 ScanTextConfig (ai.nightfall.scan.model.ScanTextConfig)3 IOException (java.io.IOException)3 List (java.util.List)3 MockResponse (okhttp3.mockwebserver.MockResponse)3 MockWebServer (okhttp3.mockwebserver.MockWebServer)3 Test (org.junit.jupiter.api.Test)3 RedactionConfig (ai.nightfall.scan.model.redaction.RedactionConfig)2 SubstitutionConfig (ai.nightfall.scan.model.redaction.SubstitutionConfig)2 NightfallClient (ai.nightfall.scan.NightfallClient)1 DetectionRule (ai.nightfall.scan.model.DetectionRule)1 Detector (ai.nightfall.scan.model.Detector)1 Dispatcher (okhttp3.mockwebserver.Dispatcher)1 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)1 NotNull (org.jetbrains.annotations.NotNull)1