Search in sources :

Example 1 with ScanTextRequest

use of ai.nightfall.scan.model.ScanTextRequest 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 ScanTextRequest

use of ai.nightfall.scan.model.ScanTextRequest 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 ScanTextRequest

use of ai.nightfall.scan.model.ScanTextRequest in project nightfall-java-sdk by nightfallai.

the class NightfallClientTest method testScanText_InternalException.

@Test
public void testScanText_InternalException() {
    NightfallErrorResponse expectedErr = new NightfallErrorResponse(500, "internal", "internal error", null);
    NightfallAPIException expectedException = new NightfallAPIException("unsuccessful response", expectedErr, 500);
    NightfallAPIException actualException = assertThrows(NightfallAPIException.class, () -> {
        try (MockWebServer server = new MockWebServer()) {
            String body = "{\"code\": 500, \"message\": \"internal\", \"description\": \"internal error\"}";
            server.enqueue(new MockResponse().setResponseCode(500).setBody(body));
            String serverURL = new HttpUrl.Builder().scheme("http").host(server.getHostName()).port(server.getPort()).build().url().toString();
            NightfallClient c = new NightfallClient(serverURL, "key", 1, getHttpClient());
            ScanTextRequest req = new ScanTextRequest(null, (ScanTextConfig) null);
            c.scanText(req);
            fail("did not expect scanText to succeed");
        } catch (IOException e) {
            fail("IOException during test: " + e.getMessage());
        }
    });
    assertEquals(expectedException.getError(), actualException.getError());
    assertEquals(expectedException.getMessage(), actualException.getMessage());
}
Also used : NightfallErrorResponse(ai.nightfall.scan.model.NightfallErrorResponse) MockResponse(okhttp3.mockwebserver.MockResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) IOException(java.io.IOException) NightfallAPIException(ai.nightfall.scan.model.NightfallAPIException) ScanTextRequest(ai.nightfall.scan.model.ScanTextRequest) Test(org.junit.jupiter.api.Test)

Example 4 with ScanTextRequest

use of ai.nightfall.scan.model.ScanTextRequest 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 5 with ScanTextRequest

use of ai.nightfall.scan.model.ScanTextRequest in project nightfall-java-sdk by nightfallai.

the class NightfallClientTest method testScanText_429RetryCountExceeded.

@Test
public void testScanText_429RetryCountExceeded() {
    NightfallErrorResponse expectedErr = new NightfallErrorResponse(429, "too many requests", "too many requests", null);
    NightfallAPIException expectedException = new NightfallAPIException("exceeded max retry count on request: /v3/scan", expectedErr, 429);
    NightfallAPIException actualException = assertThrows(NightfallAPIException.class, () -> {
        try (MockWebServer server = new MockWebServer()) {
            String body = "{\"code\": 429, \"message\": \"too many requests\", \"description\": \"too many requests\"}";
            server.setDispatcher(new Dispatcher() {

                @NotNull
                @Override
                public MockResponse dispatch(@NotNull RecordedRequest r) {
                    return new MockResponse().setResponseCode(429).setBody(body);
                }
            });
            String serverURL = new HttpUrl.Builder().scheme("http").host(server.getHostName()).port(server.getPort()).build().url().toString();
            NightfallClient c = new NightfallClient(serverURL, "key", 1, getHttpClient());
            ScanTextRequest req = new ScanTextRequest(null, (ScanTextConfig) null);
            c.scanText(req);
            fail("did not expect scanText to succeed");
        } catch (IOException e) {
            fail("IOException during test: " + e.getMessage());
        }
    });
    assertEquals(expectedException.getError(), actualException.getError());
    assertEquals(expectedException.getMessage(), actualException.getMessage());
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) MockResponse(okhttp3.mockwebserver.MockResponse) IOException(java.io.IOException) Dispatcher(okhttp3.mockwebserver.Dispatcher) NightfallAPIException(ai.nightfall.scan.model.NightfallAPIException) NotNull(org.jetbrains.annotations.NotNull) ScanTextRequest(ai.nightfall.scan.model.ScanTextRequest) NightfallErrorResponse(ai.nightfall.scan.model.NightfallErrorResponse) MockWebServer(okhttp3.mockwebserver.MockWebServer) Test(org.junit.jupiter.api.Test)

Aggregations

ScanTextRequest (ai.nightfall.scan.model.ScanTextRequest)7 IOException (java.io.IOException)6 MockResponse (okhttp3.mockwebserver.MockResponse)6 MockWebServer (okhttp3.mockwebserver.MockWebServer)6 Test (org.junit.jupiter.api.Test)6 ScanTextResponse (ai.nightfall.scan.model.ScanTextResponse)4 ScanTextConfig (ai.nightfall.scan.model.ScanTextConfig)3 List (java.util.List)3 Dispatcher (okhttp3.mockwebserver.Dispatcher)3 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)3 NotNull (org.jetbrains.annotations.NotNull)3 NightfallAPIException (ai.nightfall.scan.model.NightfallAPIException)2 NightfallErrorResponse (ai.nightfall.scan.model.NightfallErrorResponse)2 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