|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package com.datastax.oss.driver.internal.core.cql; |
| 19 | + |
| 20 | +import static org.assertj.core.api.Assertions.assertThat; |
| 21 | +import static org.mockito.Mockito.times; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | + |
| 24 | +import ch.qos.logback.classic.Level; |
| 25 | +import ch.qos.logback.classic.spi.ILoggingEvent; |
| 26 | +import com.datastax.oss.driver.api.core.DefaultConsistencyLevel; |
| 27 | +import com.datastax.oss.driver.api.core.cql.BatchStatementBuilder; |
| 28 | +import com.datastax.oss.driver.api.core.cql.BatchType; |
| 29 | +import com.datastax.oss.driver.api.core.cql.SimpleStatement; |
| 30 | +import com.datastax.oss.driver.internal.core.util.LoggerTest; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +public class DefaultBatchStatementTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void should_issue_log_warn_if_statement_have_consistency_level_set() { |
| 37 | + SimpleStatement simpleStatement = |
| 38 | + SimpleStatement.builder("SELECT * FROM some_table WHERE a = ?") |
| 39 | + .setConsistencyLevel(DefaultConsistencyLevel.QUORUM) |
| 40 | + .build(); |
| 41 | + |
| 42 | + BatchStatementBuilder batchStatementBuilder = new BatchStatementBuilder(BatchType.LOGGED); |
| 43 | + batchStatementBuilder.addStatement(simpleStatement); |
| 44 | + |
| 45 | + LoggerTest.LoggerSetup logger = |
| 46 | + LoggerTest.setupTestLogger(DefaultBatchStatement.class, Level.WARN); |
| 47 | + |
| 48 | + batchStatementBuilder.build(); |
| 49 | + |
| 50 | + verify(logger.appender).doAppend(logger.loggingEventCaptor.capture()); |
| 51 | + assertThat( |
| 52 | + logger.loggingEventCaptor.getAllValues().stream() |
| 53 | + .map(ILoggingEvent::getFormattedMessage)) |
| 54 | + .contains( |
| 55 | + "You have submitted statement with non-default [serial] consistency level to the DefaultBatchStatement. " |
| 56 | + + "Be aware that [serial] consistency level of child statements is not preserved by the DefaultBatchStatement. " |
| 57 | + + "Use DefaultBatchStatement.setConsistencyLevel()/DefaultBatchStatement.setSerialConsistencyLevel() instead."); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void should_issue_log_warn_if_statement_have_serial_consistency_level_set() { |
| 62 | + SimpleStatement simpleStatement = |
| 63 | + SimpleStatement.builder("SELECT * FROM some_table WHERE a = ?") |
| 64 | + .setSerialConsistencyLevel(DefaultConsistencyLevel.LOCAL_SERIAL) |
| 65 | + .build(); |
| 66 | + |
| 67 | + BatchStatementBuilder batchStatementBuilder = new BatchStatementBuilder(BatchType.LOGGED); |
| 68 | + batchStatementBuilder.addStatement(simpleStatement); |
| 69 | + |
| 70 | + LoggerTest.LoggerSetup logger = |
| 71 | + LoggerTest.setupTestLogger(DefaultBatchStatement.class, Level.WARN); |
| 72 | + |
| 73 | + batchStatementBuilder.build(); |
| 74 | + |
| 75 | + verify(logger.appender).doAppend(logger.loggingEventCaptor.capture()); |
| 76 | + assertThat( |
| 77 | + logger.loggingEventCaptor.getAllValues().stream() |
| 78 | + .map(ILoggingEvent::getFormattedMessage)) |
| 79 | + .contains( |
| 80 | + "You have submitted statement with non-default [serial] consistency level to the DefaultBatchStatement. " |
| 81 | + + "Be aware that [serial] consistency level of child statements is not preserved by the DefaultBatchStatement. " |
| 82 | + + "Use DefaultBatchStatement.setConsistencyLevel()/DefaultBatchStatement.setSerialConsistencyLevel() instead."); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void should_not_issue_log_warn_if_statement_have_no_consistency_level_set() { |
| 87 | + SimpleStatement simpleStatement = |
| 88 | + SimpleStatement.builder("SELECT * FROM some_table WHERE a = ?").build(); |
| 89 | + |
| 90 | + BatchStatementBuilder batchStatementBuilder = new BatchStatementBuilder(BatchType.LOGGED); |
| 91 | + batchStatementBuilder.addStatement(simpleStatement); |
| 92 | + |
| 93 | + LoggerTest.LoggerSetup logger = |
| 94 | + LoggerTest.setupTestLogger(DefaultBatchStatement.class, Level.WARN); |
| 95 | + |
| 96 | + batchStatementBuilder.build(); |
| 97 | + |
| 98 | + verify(logger.appender, times(0)).doAppend(logger.loggingEventCaptor.capture()); |
| 99 | + } |
| 100 | +} |
0 commit comments