Search in sources :

Example 1 with ScanTextConfig

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

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

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

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