Skip to content

Commit

Permalink
JAVA-2115: Generate Insert DAO methods
Browse files Browse the repository at this point in the history
  • Loading branch information
olim7t committed Jun 21, 2019
1 parent ff63732 commit ab87536
Show file tree
Hide file tree
Showing 15 changed files with 1,012 additions and 150 deletions.
Expand Up @@ -22,6 +22,7 @@
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;
Expand Down Expand Up @@ -72,6 +73,40 @@ public static <T> void whenAllDone(
List<CompletionStage<T>> inputs, Runnable callback, Executor executor) {
allDone(inputs).thenRunAsync(callback, executor).exceptionally(UncaughtExceptions::log);
}
/**
* @return a completion stage that completes when all inputs are successful, or fails if any of
* them failed.
*/
public static <T> CompletionStage<Void> allSuccessful(List<CompletionStage<T>> inputs) {
CompletableFuture<Void> result = new CompletableFuture<>();
if (inputs.isEmpty()) {
result.complete(null);
} else {
final int todo = inputs.size();
final AtomicInteger done = new AtomicInteger();
final CopyOnWriteArrayList<Throwable> errors = new CopyOnWriteArrayList<>();
for (CompletionStage<?> input : inputs) {
input.whenComplete(
(v, error) -> {
if (error != null) {
errors.add(error);
}
if (done.incrementAndGet() == todo) {
if (errors.isEmpty()) {
result.complete(null);
} else {
Throwable finalError = errors.get(0);
for (int i = 1; i < errors.size(); i++) {
finalError.addSuppressed(errors.get(i));
}
result.completeExceptionally(finalError);
}
}
});
}
}
return result;
}

/** Get the result now, when we know for sure that the future is complete. */
public static <T> T getCompleted(CompletionStage<T> stage) {
Expand Down
@@ -0,0 +1,110 @@
/*
* Copyright DataStax, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.datastax.oss.driver.mapper;

import static org.assertj.core.api.Assertions.assertThat;

import com.datastax.oss.driver.api.core.CqlSession;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.cql.SimpleStatement;
import com.datastax.oss.driver.api.testinfra.ccm.CcmRule;
import com.datastax.oss.driver.api.testinfra.session.SessionRule;
import com.datastax.oss.driver.categories.ParallelizableTests;
import com.datastax.oss.driver.mapper.model.inventory.InventoryFixtures;
import com.datastax.oss.driver.mapper.model.inventory.InventoryMapper;
import com.datastax.oss.driver.mapper.model.inventory.InventoryMapperBuilder;
import com.datastax.oss.driver.mapper.model.inventory.Product;
import com.datastax.oss.driver.mapper.model.inventory.ProductDao;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;

@Category(ParallelizableTests.class)
public class InsertEntityIT {

private static CcmRule ccm = CcmRule.getInstance();

private static SessionRule<CqlSession> sessionRule = SessionRule.builder(ccm).build();

@ClassRule public static TestRule chain = RuleChain.outerRule(ccm).around(sessionRule);

private static ProductDao productDao;

@BeforeClass
public static void setup() {
CqlSession session = sessionRule.session();

for (String query : InventoryFixtures.createStatements()) {
session.execute(
SimpleStatement.builder(query).setExecutionProfile(sessionRule.slowProfile()).build());
}

InventoryMapper inventoryMapper = new InventoryMapperBuilder(session).build();
productDao = inventoryMapper.productDao(sessionRule.keyspace());
}

@Before
public void clearProductData() {
CqlSession session = sessionRule.session();
session.execute(
SimpleStatement.builder("TRUNCATE product")
.setExecutionProfile(sessionRule.slowProfile())
.build());
}

@Test
public void should_insert_entity() {
// Given
CqlSession session = sessionRule.session();

// When
Product product = InventoryFixtures.FLAMETHROWER.entity;
productDao.save(product);
Row row =
session
.execute(
SimpleStatement.newInstance("SELECT * FROM product WHERE id = ?", product.getId()))
.one();

// Then
InventoryFixtures.FLAMETHROWER.assertMatches(row);
}

@Test
public void should_insert_entity_with_custom_clause() {
// Given
CqlSession session = sessionRule.session();
long timestamp = 1234;

// When
Product product = InventoryFixtures.FLAMETHROWER.entity;
productDao.saveWithBoundTimestamp(product, timestamp);
Row row =
session
.execute(
SimpleStatement.newInstance(
"SELECT WRITETIME(description) FROM product WHERE id = ?", product.getId()))
.one();
long writeTime = row.getLong(0);

// Then
assertThat(writeTime).isEqualTo(timestamp);
}
}
Expand Up @@ -25,6 +25,7 @@
import com.datastax.oss.driver.api.core.data.UdtValue;
import com.datastax.oss.driver.api.mapper.annotations.Dao;
import com.datastax.oss.driver.api.mapper.annotations.GetEntity;
import com.datastax.oss.driver.api.mapper.annotations.Insert;
import com.datastax.oss.driver.api.mapper.annotations.SetEntity;

@Dao
Expand Down Expand Up @@ -53,4 +54,10 @@ public interface ProductDao {

@GetEntity
Product getOne(AsyncResultSet resultSet);

@Insert
void save(Product product);

@Insert(customClause = "USING TIMESTAMP :timestamp")
Product saveWithBoundTimestamp(Product product, long timestamp);
}

0 comments on commit ab87536

Please sign in to comment.