Search in sources :

Example 1 with PubSubServiceImpl

use of com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubServiceImpl in project bq-pii-classifier by GoogleCloudPlatform.

the class DispatcherTest method testWithInput.

// @Test
// public void testDispatcher_withDatasets () throws IOException {
// 
// String jsonPayLoad = "{\"tablesInclude\":\"\""
// + ",\"tablesExclude\":\"p1.d1.t1\""
// + ",\"datasetsInclude\":\"p1.d1, p1.d2\""
// + ",\"datasetsExclude\":\"\""
// + ",\"projectsInclude\":\"p2\"" // should have no effect
// + "}";
// 
// List<String> expectedOutput = Lists.newArrayList("p1.d1.t2", "p1.d2.t1", "p1.d2.t2");
// List<String> actualOutput = testWithInput(jsonPayLoad);
// 
// assertEquals(expectedOutput, actualOutput);
// }
// 
// @Test
// public void testDispatcher_withProjects () throws IOException {
// 
// String jsonPayLoad = "{\"tablesInclude\":\"\""
// + ",\"tablesExclude\":\"p1.d2.t1\""
// + ",\"datasetsInclude\":\"\""
// + ",\"datasetsExclude\":\"p1.d1\""
// + ",\"projectsInclude\":\"p1, p2\"" // should have no effect
// + "}";
// 
// List<String> expectedOutput = Lists.newArrayList("p1.d2.t2", "p2.d1.t1", "p2.d1.t2");
// List<String> actualOutput = testWithInput(jsonPayLoad);
// 
// assertEquals(expectedOutput, actualOutput);
// }
private List<String> testWithInput(BigQueryScope bigQueryScope) throws IOException, NonRetryableApplicationException, InterruptedException {
    // Dispatcher function = new Dispatcher(envMock, bqServiceMock, cloudTasksServiceMock);
    PubSubPublishResults results = function.execute(bigQueryScope, "");
    PubSubServiceImpl pubSubServiceMock = mock(PubSubServiceImpl.class);
    lenient().when(pubSubServiceMock.publishTableOperationRequests(anyString(), anyString(), any())).thenReturn(new PubSubPublishResults(Arrays.asList(new SuccessPubSubMessage(new Operation("p1.d1.t1", "runId", "trackingId"), "publishedMessageId"), new SuccessPubSubMessage(new Operation("p1.d1.t2", "runId", "trackingId"), "publishedMessageId")), Arrays.asList(new FailedPubSubMessage(new Operation("", "", ""), new Exception("test fail message")))));
    return results.getSuccessMessages().stream().map(x -> ((Operation) x.getMsg()).getEntityKey()).collect(Collectors.toList());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) DispatcherType(com.google.cloud.pso.bq_pii_classifier.entities.DispatcherType) Mock(org.mockito.Mock) RunWith(org.junit.runner.RunWith) PubSubServiceImpl(com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubServiceImpl) SolutionMode(com.google.cloud.pso.bq_pii_classifier.entities.SolutionMode) PubSubPublishResults(com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubPublishResults) Mockito.lenient(org.mockito.Mockito.lenient) ArrayList(java.util.ArrayList) StandardDlpResultsScannerImpl(com.google.cloud.pso.bq_pii_classifier.services.scan.StandardDlpResultsScannerImpl) Lists(com.google.common.collect.Lists) Before(org.junit.Before) InjectMocks(org.mockito.InjectMocks) BigQueryServiceImpl(com.google.cloud.pso.bq_pii_classifier.services.bq.BigQueryServiceImpl) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) FailedPubSubMessage(com.google.cloud.pso.bq_pii_classifier.services.pubsub.FailedPubSubMessage) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Operation(com.google.cloud.pso.bq_pii_classifier.entities.Operation) List(java.util.List) SuccessPubSubMessage(com.google.cloud.pso.bq_pii_classifier.services.pubsub.SuccessPubSubMessage) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) PubSubPublishResults(com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubPublishResults) FailedPubSubMessage(com.google.cloud.pso.bq_pii_classifier.services.pubsub.FailedPubSubMessage) Operation(com.google.cloud.pso.bq_pii_classifier.entities.Operation) SuccessPubSubMessage(com.google.cloud.pso.bq_pii_classifier.services.pubsub.SuccessPubSubMessage) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) IOException(java.io.IOException) PubSubServiceImpl(com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubServiceImpl)

Example 2 with PubSubServiceImpl

use of com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubServiceImpl in project bq-pii-classifier by GoogleCloudPlatform.

the class TaggingDispatcherController method receiveMessage.

@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity receiveMessage(@RequestBody PubSubEvent requestBody) {
    String runId = TrackingHelper.generateTaggingRunId();
    String state = "";
    try {
        if (requestBody == null || requestBody.getMessage() == null) {
            String msg = "Bad Request: invalid message format";
            logger.logSevereWithTracker(runId, msg);
            throw new NonRetryableApplicationException("Request body or message is Null.");
        }
        String requestJsonString = requestBody.getMessage().dataToUtf8String();
        // remove any escape characters (e.g. from Terraform
        requestJsonString = requestJsonString.replace("\\", "");
        logger.logInfoWithTracker(runId, String.format("Received payload: %s", requestJsonString));
        BigQueryScope bqScope = gson.fromJson(requestJsonString, BigQueryScope.class);
        logger.logInfoWithTracker(runId, String.format("Parsed JSON input %s ", bqScope.toString()));
        Scanner dlpResultsScanner;
        if (environment.getIsAutoDlpMode()) {
            dlpResultsScanner = new AutoDlpResultsScannerImpl(environment.getProjectId(), environment.getSolutionDataset(), environment.getDlpTableAuto(), new BigQueryServiceImpl());
        } else {
            dlpResultsScanner = new StandardDlpResultsScannerImpl(environment.getProjectId(), environment.getSolutionDataset(), environment.getDlpTableStandard(), environment.getLoggingTable(), new BigQueryServiceImpl());
        }
        Dispatcher dispatcher = new Dispatcher(environment.toConfig(), new BigQueryServiceImpl(), new PubSubServiceImpl(), dlpResultsScanner, new GCSPersistentSetImpl(environment.getGcsFlagsBucket()), "tagging-dispatcher-flags", runId);
        PubSubPublishResults results = dispatcher.execute(bqScope, requestBody.getMessage().getMessageId());
        state = String.format("Publishing results: %s SUCCESS MESSAGES and %s FAILED MESSAGES", results.getSuccessMessages().size(), results.getFailedMessages().size());
        logger.logInfoWithTracker(runId, state);
    } catch (Exception e) {
        logger.logNonRetryableExceptions(runId, e);
        state = String.format("ERROR '%s'", e.getMessage());
    }
    return new ResponseEntity(String.format("Process completed with state = %s", state), HttpStatus.OK);
}
Also used : BigQueryServiceImpl(com.google.cloud.pso.bq_pii_classifier.services.bq.BigQueryServiceImpl) PubSubPublishResults(com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubPublishResults) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) Dispatcher(com.google.cloud.pso.bq_pii_classifier.functions.dispatcher.Dispatcher) GCSPersistentSetImpl(com.google.cloud.pso.bq_pii_classifier.services.set.GCSPersistentSetImpl) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) BigQueryScope(com.google.cloud.pso.bq_pii_classifier.functions.dispatcher.BigQueryScope) PubSubServiceImpl(com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubServiceImpl) ResponseEntity(org.springframework.http.ResponseEntity) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with PubSubServiceImpl

use of com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubServiceImpl in project bq-pii-classifier by GoogleCloudPlatform.

the class InspectionDispatcherController method receiveMessage.

@RequestMapping(value = "/", method = RequestMethod.POST)
public ResponseEntity receiveMessage(@RequestBody PubSubEvent requestBody) {
    String runId = TrackingHelper.generateInspectionRunId();
    String state = "";
    try {
        if (requestBody == null || requestBody.getMessage() == null) {
            String msg = "Bad Request: invalid message format";
            logger.logSevereWithTracker(runId, msg);
            throw new NonRetryableApplicationException("Request body or message is Null.");
        }
        String requestJsonString = requestBody.getMessage().dataToUtf8String();
        // remove any escape characters (e.g. from Terraform
        requestJsonString = requestJsonString.replace("\\", "");
        logger.logInfoWithTracker(runId, String.format("Received payload: %s", requestJsonString));
        BigQueryScope bqScope = gson.fromJson(requestJsonString, BigQueryScope.class);
        logger.logInfoWithTracker(runId, String.format("Parsed JSON input %s ", bqScope.toString()));
        Dispatcher dispatcher = new Dispatcher(environment.toConfig(), new BigQueryServiceImpl(), new PubSubServiceImpl(), new BigQueryScannerImpl(), new GCSPersistentSetImpl(environment.getGcsFlagsBucket()), "inspection-dispatcher-flags", runId);
        PubSubPublishResults results = dispatcher.execute(bqScope, requestBody.getMessage().getMessageId());
        state = String.format("Publishing results: %s SUCCESS MESSAGES and %s FAILED MESSAGES", results.getSuccessMessages().size(), results.getFailedMessages().size());
        logger.logInfoWithTracker(runId, state);
    } catch (Exception e) {
        logger.logNonRetryableExceptions(runId, e);
        state = String.format("ERROR '%s'", e.getMessage());
    }
    return new ResponseEntity(String.format("Process completed with state = %s", state), HttpStatus.OK);
}
Also used : BigQueryScannerImpl(com.google.cloud.pso.bq_pii_classifier.services.scan.BigQueryScannerImpl) ResponseEntity(org.springframework.http.ResponseEntity) BigQueryServiceImpl(com.google.cloud.pso.bq_pii_classifier.services.bq.BigQueryServiceImpl) PubSubPublishResults(com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubPublishResults) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) Dispatcher(com.google.cloud.pso.bq_pii_classifier.functions.dispatcher.Dispatcher) GCSPersistentSetImpl(com.google.cloud.pso.bq_pii_classifier.services.set.GCSPersistentSetImpl) NonRetryableApplicationException(com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException) BigQueryScope(com.google.cloud.pso.bq_pii_classifier.functions.dispatcher.BigQueryScope) PubSubServiceImpl(com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubServiceImpl) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

NonRetryableApplicationException (com.google.cloud.pso.bq_pii_classifier.entities.NonRetryableApplicationException)3 BigQueryServiceImpl (com.google.cloud.pso.bq_pii_classifier.services.bq.BigQueryServiceImpl)3 PubSubPublishResults (com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubPublishResults)3 PubSubServiceImpl (com.google.cloud.pso.bq_pii_classifier.services.pubsub.PubSubServiceImpl)3 BigQueryScope (com.google.cloud.pso.bq_pii_classifier.functions.dispatcher.BigQueryScope)2 Dispatcher (com.google.cloud.pso.bq_pii_classifier.functions.dispatcher.Dispatcher)2 GCSPersistentSetImpl (com.google.cloud.pso.bq_pii_classifier.services.set.GCSPersistentSetImpl)2 ResponseEntity (org.springframework.http.ResponseEntity)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 DispatcherType (com.google.cloud.pso.bq_pii_classifier.entities.DispatcherType)1 Operation (com.google.cloud.pso.bq_pii_classifier.entities.Operation)1 SolutionMode (com.google.cloud.pso.bq_pii_classifier.entities.SolutionMode)1 FailedPubSubMessage (com.google.cloud.pso.bq_pii_classifier.services.pubsub.FailedPubSubMessage)1 SuccessPubSubMessage (com.google.cloud.pso.bq_pii_classifier.services.pubsub.SuccessPubSubMessage)1 BigQueryScannerImpl (com.google.cloud.pso.bq_pii_classifier.services.scan.BigQueryScannerImpl)1 StandardDlpResultsScannerImpl (com.google.cloud.pso.bq_pii_classifier.services.scan.StandardDlpResultsScannerImpl)1 Lists (com.google.common.collect.Lists)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1