Search in sources :

Example 6 with Submission

use of com.google.webrisk.v1.Submission in project java-webrisk by googleapis.

the class SubmitUriExampleTest method testSumbitUriExample.

@Test
public void testSumbitUriExample() throws IOException {
    String testUri = "http://testsafebrowsing.appspot.com/s/malware.html";
    Submission actualSubmission = SubmitUriExample.submitUriExample(testUri);
    Truth.assertThat(actualSubmission.getUri()).isEqualTo(testUri);
}
Also used : Submission(com.google.webrisk.v1.Submission) Test(org.junit.Test)

Example 7 with Submission

use of com.google.webrisk.v1.Submission in project oracle-db-examples by oracle-samples.

the class SessionOperationGroupTest method testCollect.

/**
 * Verify basic functionality of
 * {@link Session#collect(java.util.stream.Collector)}
 * @throws java.lang.Exception
 */
@Test
public void testCollect() throws Exception {
    Submission<Object> seSubmission = null;
    try (DataSource ds = TestConfig.getDataSource();
        Session se = ds.builder().build()) {
        Collector<Object, ?, Object> co = new ObjectCollector();
        se.attachOperation().timeout(getTimeout()).submit();
        se.collect(co).localOperation().onExecution(() -> 99).submit();
        se.rowOperation("SELECT COUNT(*) FROM " + TABLE).collect(Collectors.summingInt(row -> row.at(1).get(Integer.class))).timeout(getTimeout()).submit();
        se.endTransactionOperation(se.transactionCompletion()).submit();
        seSubmission = se.submit();
    }
    // se.closeOperation().submit() // 5
    assertNotNull(seSubmission);
    @SuppressWarnings("unchecked") List<Integer> result = (List<Integer>) seSubmission.getCompletionStage().toCompletableFuture().get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    assertNotNull(result);
    // Note the inline comments which count each submit (1, 2, 3...). This is
    // the expected count of accumulated results.
    assertEquals(5, result.size());
    // Attach operation
    assertNull(result.get(0));
    // Local operation
    assertEquals(99, result.get(1).intValue());
    // Row operation
    assertEquals(0, result.get(2).intValue());
    assertEquals(TransactionOutcome.COMMIT, // Transaction operation
    result.get(3));
    // Close operation
    assertNull(result.get(4));
}
Also used : BeforeClass(org.junit.BeforeClass) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) BiConsumer(java.util.function.BiConsumer) Collector(java.util.stream.Collector) Session(jdk.incubator.sql2.Session) SqlSkippedException(jdk.incubator.sql2.SqlSkippedException) AfterClass(org.junit.AfterClass) Assert.assertNotNull(org.junit.Assert.assertNotNull) AdbaType(jdk.incubator.sql2.AdbaType) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) CompletionException(java.util.concurrent.CompletionException) Collectors(java.util.stream.Collectors) TransactionOutcome(jdk.incubator.sql2.TransactionOutcome) BinaryOperator(java.util.function.BinaryOperator) ArrayRowCountOperation(jdk.incubator.sql2.ArrayRowCountOperation) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) List(java.util.List) Result(jdk.incubator.sql2.Result) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) Collections(java.util.Collections) Submission(jdk.incubator.sql2.Submission) Assert.assertEquals(org.junit.Assert.assertEquals) ArrayList(java.util.ArrayList) List(java.util.List) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 8 with Submission

use of com.google.webrisk.v1.Submission in project oracle-db-examples by oracle-samples.

the class SessionTest method testOperationGroup.

@Test
public void testOperationGroup() throws Exception {
    try (DataSource ds = getDataSource();
        Session se = ds.getSession()) {
        AtomicInteger opCounter = new AtomicInteger();
        se.rowCountOperation("INSERT INTO " + OP_GROUP_TABLE + " VALUES (0)").apply(rc -> opCounter.incrementAndGet()).submit();
        Submission<List<Boolean>> opgSubmission = null;
        try (OperationGroup<Boolean, List<Boolean>> opg = se.operationGroup()) {
            opgSubmission = opg.collect(Collectors.toList()).timeout(getTimeout()).submit();
            opg.rowOperation("SELECT c FROM " + OP_GROUP_TABLE).collect(Collectors.reducing(Boolean.TRUE, row -> opCounter.compareAndSet(1, 2), (b1, b2) -> b1 && b2)).submit();
            opg.localOperation().onExecution(() -> opCounter.compareAndSet(2, 3)).submit();
            opg.rowCountOperation("UPDATE " + OP_GROUP_TABLE + " SET c = 1 WHERE c = 0").apply(rc -> opCounter.compareAndSet(3, 4)).submit();
        }
        assertNotNull(opgSubmission);
        List<Boolean> opgResult = opgSubmission.getCompletionStage().toCompletableFuture().get();
        assertNotNull(opgResult);
        assertEquals(3, opgResult.size());
        for (Boolean memberResult : opgResult) assertTrue(memberResult);
        se.rollback().toCompletableFuture().get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    }
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) OperationGroup(jdk.incubator.sql2.OperationGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AdbaSessionProperty(jdk.incubator.sql2.AdbaSessionProperty) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TransactionIsolation(jdk.incubator.sql2.AdbaSessionProperty.TransactionIsolation) Assert.fail(org.junit.Assert.fail) Session(jdk.incubator.sql2.Session) AfterClass(org.junit.AfterClass) Validation(jdk.incubator.sql2.Session.Validation) Assert.assertNotNull(org.junit.Assert.assertNotNull) Lifecycle(jdk.incubator.sql2.Session.Lifecycle) Assert.assertTrue(org.junit.Assert.assertTrue) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) TransactionOutcome(jdk.incubator.sql2.TransactionOutcome) Operation(jdk.incubator.sql2.Operation) SessionLifecycleListener(jdk.incubator.sql2.Session.SessionLifecycleListener) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Builder(jdk.incubator.sql2.Session.Builder) Assert.assertFalse(org.junit.Assert.assertFalse) Submission(jdk.incubator.sql2.Submission) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 9 with Submission

use of com.google.webrisk.v1.Submission in project oracle-db-examples by oracle-samples.

the class SessionTest method testAbort.

/**
 * Verifies the following behavior:
 * <br>
 * (1) If lifecycle is Session.Lifecycle.NEW, Session.Lifecycle.OPEN,
 * Session.Lifecycle.INACTIVE or Session.Lifecycle.CLOSING ->
 * Session.Lifecycle.ABORTING.[Spec: {@link Session#abort()}]
 * <br>
 * (2) If lifecycle is Session.Lifecycle.ABORTING or Session.Lifecycle.
 * CLOSED this is a no-op. [Spec: {@link Session#abort()}]
 *
 * @throws java.lang.Exception
 */
@Test
public void testAbort() throws Exception {
    DataSource ds = DataSourceFactory.newFactory(getDataSourceFactoryName()).builder().url(getUrl()).username(getUser()).password(getPassword()).sessionProperty(AdbaSessionProperty.TRANSACTION_ISOLATION, TransactionIsolation.SERIALIZABLE).build();
    Session se1 = ds.builder().build();
    TestLifecycleListener se1LifecycleListener = new TestLifecycleListener();
    se1.registerLifecycleListener(se1LifecycleListener);
    Submission<?> se1Submission = se1.submit();
    se1.attachOperation().submit();
    // Create a record which two sessions will try to update.
    se1.rowCountOperation("INSERT INTO " + ABORT_TABLE_1 + " VALUES ('a')").submit();
    se1.commitMaybeRollback(se1.transactionCompletion());
    // Session 1 acquires a lock on the record.
    se1.rowCountOperation("UPDATE " + ABORT_TABLE_1 + " SET c='b' WHERE c='a'").submit();
    se1.<List<String>>rowOperation("SELECT * FROM " + ABORT_TABLE_1).collect(Collectors.mapping(row -> row.at(1).get(String.class), Collectors.toList())).timeout(getTimeout()).submit().getCompletionStage().toCompletableFuture().get();
    // Session 2 gets blocked acquiring a lock on the same record.
    Session se2 = ds.builder().build();
    Submission<?> se2Submission = se2.submit();
    se2.attachOperation().submit();
    Submission<Long> blockedSubmission = se2.<Long>rowCountOperation("UPDATE " + ABORT_TABLE_1 + " SET c='z' WHERE c='a'").apply(count -> count.getCount()).timeout(getTimeout()).submit();
    // Session 1 is aborted so that session 2 can proceed.
    se1.abort();
    // (2)
    se1.abort();
    // (1)
    LifecycleEvent[] expectedEvents = { new LifecycleEvent(se1, Lifecycle.NEW, Lifecycle.ATTACHED), new LifecycleEvent(se1, Lifecycle.ATTACHED, Lifecycle.ABORTING), new LifecycleEvent(se1, Lifecycle.ABORTING, Lifecycle.CLOSED) };
    for (LifecycleEvent expected : expectedEvents) {
        LifecycleEvent actual = se1LifecycleListener.record.poll(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
        if (actual == null)
            fail("Timeout waiting for lifecycle event: " + expected);
        assertEquals(expected, actual);
    }
    long unblockedResult = blockedSubmission.getCompletionStage().toCompletableFuture().get();
    assertEquals(1L, unblockedResult);
    se2.rollback();
    se2.close();
    se1Submission.getCompletionStage().toCompletableFuture().handle((r, e) -> {
        assertNotNull(e);
        return null;
    }).get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
    se2Submission.getCompletionStage().toCompletableFuture().handle((r, e) -> {
        assertNull(e);
        return null;
    }).get(getTimeout().toMillis(), TimeUnit.MILLISECONDS);
}
Also used : DataSourceFactory(jdk.incubator.sql2.DataSourceFactory) BeforeClass(org.junit.BeforeClass) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Function(java.util.function.Function) TestConfig(com.oracle.adbaoverjdbc.test.TestConfig) OperationGroup(jdk.incubator.sql2.OperationGroup) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AdbaSessionProperty(jdk.incubator.sql2.AdbaSessionProperty) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) TransactionIsolation(jdk.incubator.sql2.AdbaSessionProperty.TransactionIsolation) Assert.fail(org.junit.Assert.fail) Session(jdk.incubator.sql2.Session) AfterClass(org.junit.AfterClass) Validation(jdk.incubator.sql2.Session.Validation) Assert.assertNotNull(org.junit.Assert.assertNotNull) Lifecycle(jdk.incubator.sql2.Session.Lifecycle) Assert.assertTrue(org.junit.Assert.assertTrue) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) TransactionCompletion(jdk.incubator.sql2.TransactionCompletion) TransactionOutcome(jdk.incubator.sql2.TransactionOutcome) Operation(jdk.incubator.sql2.Operation) SessionLifecycleListener(jdk.incubator.sql2.Session.SessionLifecycleListener) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) DataSource(jdk.incubator.sql2.DataSource) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Builder(jdk.incubator.sql2.Session.Builder) Assert.assertFalse(org.junit.Assert.assertFalse) Submission(jdk.incubator.sql2.Submission) Assert.assertEquals(org.junit.Assert.assertEquals) LifecycleEvent(com.oracle.adbaoverjdbc.test.SessionTest.TestLifecycleListener.LifecycleEvent) List(java.util.List) DataSource(jdk.incubator.sql2.DataSource) Session(jdk.incubator.sql2.Session) Test(org.junit.Test)

Example 10 with Submission

use of com.google.webrisk.v1.Submission in project java-webrisk by googleapis.

the class WebRiskServiceClientTest method createSubmissionTest2.

@Test
public void createSubmissionTest2() throws Exception {
    Submission expectedResponse = Submission.newBuilder().setUri("uri116076").build();
    mockWebRiskService.addResponse(expectedResponse);
    String parent = "parent-995424086";
    Submission submission = Submission.newBuilder().build();
    Submission actualResponse = client.createSubmission(parent, submission);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockWebRiskService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateSubmissionRequest actualRequest = ((CreateSubmissionRequest) actualRequests.get(0));
    Assert.assertEquals(parent, actualRequest.getParent());
    Assert.assertEquals(submission, actualRequest.getSubmission());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) Submission(com.google.webrisk.v1.Submission) ByteString(com.google.protobuf.ByteString) CreateSubmissionRequest(com.google.webrisk.v1.CreateSubmissionRequest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)11 Submission (com.google.webrisk.v1.Submission)7 TestConfig (com.oracle.adbaoverjdbc.test.TestConfig)6 List (java.util.List)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 TimeUnit (java.util.concurrent.TimeUnit)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Function (java.util.function.Function)6 Collectors (java.util.stream.Collectors)6 DataSource (jdk.incubator.sql2.DataSource)6 Session (jdk.incubator.sql2.Session)6 Submission (jdk.incubator.sql2.Submission)6 TransactionOutcome (jdk.incubator.sql2.TransactionOutcome)6 AfterClass (org.junit.AfterClass)6 Assert.assertEquals (org.junit.Assert.assertEquals)6 Assert.assertFalse (org.junit.Assert.assertFalse)6 Assert.assertNotNull (org.junit.Assert.assertNotNull)6 Assert.assertNull (org.junit.Assert.assertNull)6 Assert.assertTrue (org.junit.Assert.assertTrue)6 BeforeClass (org.junit.BeforeClass)6