Skip to content

Commit 48bac34

Browse files
authored
JAVA-2263: Add optional schema validation (apache#1300)
1 parent 6cd2d8f commit 48bac34

16 files changed

Lines changed: 2144 additions & 2 deletions

File tree

changelog/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### 4.5.0 (in progress)
66

7+
- [new feature] JAVA-2263: Add optional schema validation to the mapper
8+
79
### 4.4.0
810

911
This version brings in all functionality that was formerly only in the DataStax Enterprise driver,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.internal.core.util;
17+
18+
import java.util.LinkedHashMap;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
public class CollectionsUtils {
23+
public static <K, V> Map<K, V> combineListsIntoOrderedMap(List<K> keys, List<V> values) {
24+
if (keys.size() != values.size()) {
25+
throw new IllegalArgumentException("Cannot combine lists with not matching sizes");
26+
}
27+
28+
Map<K, V> map = new LinkedHashMap<>();
29+
for (int i = 0; i < keys.size(); i++) {
30+
map.put(keys.get(i), values.get(i));
31+
}
32+
return map;
33+
}
34+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.internal.core.util;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
20+
21+
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableList;
22+
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
23+
import com.tngtech.java.junit.dataprovider.DataProvider;
24+
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
25+
import com.tngtech.java.junit.dataprovider.UseDataProvider;
26+
import java.util.List;
27+
import java.util.Map;
28+
import org.junit.Test;
29+
import org.junit.runner.RunWith;
30+
31+
@RunWith(DataProviderRunner.class)
32+
public class CollectionsUtilsTest {
33+
@Test
34+
@UseDataProvider("listsProvider")
35+
public void should_combine_two_lists_by_index(
36+
List<Integer> firstList, List<Integer> secondList, Map<Integer, Integer> expected) {
37+
38+
// when
39+
Map<Integer, Integer> result =
40+
CollectionsUtils.combineListsIntoOrderedMap(firstList, secondList);
41+
42+
// then
43+
assertThat(result).isEqualTo(expected);
44+
}
45+
46+
@Test
47+
public void should_throw_if_lists_have_not_matching_size() {
48+
// given
49+
List<Integer> list1 = ImmutableList.of(1);
50+
List<Integer> list2 = ImmutableList.of(1, 2);
51+
52+
// when
53+
assertThatThrownBy(() -> CollectionsUtils.combineListsIntoOrderedMap(list1, list2))
54+
.isInstanceOf(IllegalArgumentException.class)
55+
.hasMessageMatching("Cannot combine lists with not matching sizes");
56+
}
57+
58+
@DataProvider
59+
public static Object[][] listsProvider() {
60+
61+
return new Object[][] {
62+
{ImmutableList.of(1), ImmutableList.of(1), ImmutableMap.of(1, 1)},
63+
{ImmutableList.of(1, 10, 5), ImmutableList.of(1, 10, 5), ImmutableMap.of(1, 1, 10, 10, 5, 5)},
64+
{ImmutableList.of(1, 1), ImmutableList.of(1, 2), ImmutableMap.of(1, 2)}
65+
};
66+
}
67+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright DataStax, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.datastax.oss.driver.internal.core.util;
17+
18+
import static org.mockito.Mockito.mock;
19+
20+
import ch.qos.logback.classic.Level;
21+
import ch.qos.logback.classic.Logger;
22+
import ch.qos.logback.classic.spi.ILoggingEvent;
23+
import ch.qos.logback.core.Appender;
24+
import org.mockito.ArgumentCaptor;
25+
import org.slf4j.LoggerFactory;
26+
27+
public class LoggerTest {
28+
public static LoggerSetup setupTestLogger(Class<?> clazz, Level levelToCapture) {
29+
@SuppressWarnings("unchecked")
30+
Appender<ILoggingEvent> appender = (Appender<ILoggingEvent>) mock(Appender.class);
31+
32+
ArgumentCaptor<ILoggingEvent> loggingEventCaptor = ArgumentCaptor.forClass(ILoggingEvent.class);
33+
Logger logger = (Logger) LoggerFactory.getLogger(clazz);
34+
Level originalLoggerLevel = logger.getLevel();
35+
logger.setLevel(levelToCapture);
36+
logger.addAppender(appender);
37+
return new LoggerSetup(appender, originalLoggerLevel, logger, loggingEventCaptor);
38+
}
39+
40+
public static class LoggerSetup {
41+
42+
private final Level originalLoggerLevel;
43+
public final Appender<ILoggingEvent> appender;
44+
public final Logger logger;
45+
public ArgumentCaptor<ILoggingEvent> loggingEventCaptor;
46+
47+
private LoggerSetup(
48+
Appender<ILoggingEvent> appender,
49+
Level originalLoggerLevel,
50+
Logger logger,
51+
ArgumentCaptor<ILoggingEvent> loggingEventCaptor) {
52+
this.appender = appender;
53+
this.originalLoggerLevel = originalLoggerLevel;
54+
this.logger = logger;
55+
this.loggingEventCaptor = loggingEventCaptor;
56+
}
57+
58+
public void close() {
59+
logger.detachAppender(appender);
60+
logger.setLevel(originalLoggerLevel);
61+
}
62+
}
63+
}

integration-tests/src/test/java/com/datastax/oss/driver/mapper/DefaultKeyspaceIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package com.datastax.oss.driver.mapper;
1717

18+
import static com.datastax.oss.driver.api.mapper.MapperBuilder.*;
1819
import static org.assertj.core.api.Assertions.assertThat;
1920
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2021

@@ -121,6 +122,7 @@ public void should_fail_to_insert_if_default_ks_and_dao_ks_not_provided() {
121122
() -> {
122123
InventoryMapperKsNotSet mapper =
123124
new DefaultKeyspaceIT_InventoryMapperKsNotSetBuilder(SESSION_RULE.session())
125+
.withCustomState(SCHEMA_VALIDATION_ENABLED_SETTING, false)
124126
.build();
125127
mapper.productDaoDefaultKsNotSet();
126128
})

0 commit comments

Comments
 (0)