|
| 1 | +package com.datastax.oss.driver.mapper; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertThrows; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | + |
| 6 | +import com.datastax.oss.driver.api.core.CqlIdentifier; |
| 7 | +import com.datastax.oss.driver.api.core.CqlSession; |
| 8 | +import com.datastax.oss.driver.api.core.PagingIterable; |
| 9 | +import com.datastax.oss.driver.api.core.cql.ResultSet; |
| 10 | +import com.datastax.oss.driver.api.core.cql.SimpleStatement; |
| 11 | +import com.datastax.oss.driver.api.core.servererrors.InvalidQueryException; |
| 12 | +import com.datastax.oss.driver.api.mapper.annotations.Dao; |
| 13 | +import com.datastax.oss.driver.api.mapper.annotations.DaoFactory; |
| 14 | +import com.datastax.oss.driver.api.mapper.annotations.DaoKeyspace; |
| 15 | +import com.datastax.oss.driver.api.mapper.annotations.Entity; |
| 16 | +import com.datastax.oss.driver.api.mapper.annotations.Insert; |
| 17 | +import com.datastax.oss.driver.api.mapper.annotations.Mapper; |
| 18 | +import com.datastax.oss.driver.api.mapper.annotations.PartitionKey; |
| 19 | +import com.datastax.oss.driver.api.mapper.annotations.Select; |
| 20 | +import com.datastax.oss.driver.api.mapper.annotations.Update; |
| 21 | +import com.datastax.oss.driver.api.testinfra.CassandraSkip; |
| 22 | +import com.datastax.oss.driver.api.testinfra.ScyllaRequirement; |
| 23 | +import com.datastax.oss.driver.api.testinfra.ccm.CcmRule; |
| 24 | +import com.datastax.oss.driver.api.testinfra.session.SessionRule; |
| 25 | +import com.datastax.oss.driver.categories.ParallelizableTests; |
| 26 | +import org.junit.BeforeClass; |
| 27 | +import org.junit.ClassRule; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.experimental.categories.Category; |
| 30 | +import org.junit.rules.RuleChain; |
| 31 | +import org.junit.rules.TestRule; |
| 32 | + |
| 33 | +@Category(ParallelizableTests.class) |
| 34 | +@CassandraSkip |
| 35 | +@ScyllaRequirement(minOSS = "3.1.0", minEnterprise = "2020.1.0") |
| 36 | +public class UsingTimeoutIT { |
| 37 | + private static final CcmRule CCM_RULE = CcmRule.getInstance(); |
| 38 | + private static final SessionRule<CqlSession> SESSION_RULE = SessionRule.builder(CCM_RULE).build(); |
| 39 | + |
| 40 | + @ClassRule |
| 41 | + public static final TestRule CHAIN = RuleChain.outerRule(CCM_RULE).around(SESSION_RULE); |
| 42 | + |
| 43 | + private static TimeoutTestEntityDao dao; |
| 44 | + private static TimeoutTestMapper mapper; |
| 45 | + |
| 46 | + @BeforeClass |
| 47 | + public static void setup() { |
| 48 | + CqlSession session = SESSION_RULE.session(); |
| 49 | + |
| 50 | + session.execute( |
| 51 | + SimpleStatement.newInstance( |
| 52 | + "CREATE TABLE timeout_test_entity(id int, col int, PRIMARY KEY (id))") |
| 53 | + .setExecutionProfile(SESSION_RULE.slowProfile())); |
| 54 | + |
| 55 | + mapper = new UsingTimeoutIT_TimeoutTestMapperBuilder(session).build(); |
| 56 | + dao = mapper.timeoutTestEntityDao(SESSION_RULE.keyspace()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void ensure_use_of_timeout() { |
| 61 | + Exception exInsert = |
| 62 | + assertThrows(InvalidQueryException.class, () -> dao.save(new TimeoutTestEntity(1, 1))); |
| 63 | + Exception exSelect = assertThrows(InvalidQueryException.class, () -> dao.findById(1)); |
| 64 | + Exception exUpdate = |
| 65 | + assertThrows(InvalidQueryException.class, () -> dao.update(new TimeoutTestEntity(1, 2))); |
| 66 | + assertTrue(exInsert.getMessage().contains("Timeout values must be non-negative")); |
| 67 | + assertTrue(exSelect.getMessage().contains("Timeout values must be non-negative")); |
| 68 | + assertTrue(exUpdate.getMessage().contains("Timeout values must be non-negative")); |
| 69 | + } |
| 70 | + |
| 71 | + @Entity |
| 72 | + public static class TimeoutTestEntity { |
| 73 | + @PartitionKey private Integer id; |
| 74 | + private Integer col; |
| 75 | + |
| 76 | + public TimeoutTestEntity() {} |
| 77 | + |
| 78 | + public TimeoutTestEntity(Integer id, Integer col) { |
| 79 | + this.id = id; |
| 80 | + this.col = col; |
| 81 | + } |
| 82 | + |
| 83 | + public Integer getId() { |
| 84 | + return id; |
| 85 | + } |
| 86 | + |
| 87 | + public void setId(Integer id) { |
| 88 | + this.id = id; |
| 89 | + } |
| 90 | + |
| 91 | + public Integer getCol() { |
| 92 | + return col; |
| 93 | + } |
| 94 | + |
| 95 | + public void setCol(Integer col) { |
| 96 | + this.col = col; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Dao |
| 101 | + public interface TimeoutTestEntityDao { |
| 102 | + @Select(usingTimeout = "-5s") |
| 103 | + PagingIterable<TimeoutTestEntity> findById(Integer id); |
| 104 | + |
| 105 | + @Insert(usingTimeout = "-5s") |
| 106 | + ResultSet save(TimeoutTestEntity timeoutTestEntity); |
| 107 | + |
| 108 | + @Update(usingTimeout = "-5s") |
| 109 | + ResultSet update(TimeoutTestEntity timeoutTestEntity); |
| 110 | + } |
| 111 | + |
| 112 | + @Mapper |
| 113 | + public interface TimeoutTestMapper { |
| 114 | + @DaoFactory |
| 115 | + TimeoutTestEntityDao timeoutTestEntityDao(@DaoKeyspace CqlIdentifier keyspace); |
| 116 | + } |
| 117 | +} |
0 commit comments