DATACASS-202 - Polishing.

Add author tags. Extend date range in headers. Simplify resolution by removing intermediate variables. Add test to verify ConsistencyLevel resolution. Guard consistency level against null.

Originall pull request: #54.
This commit is contained in:
Mark Paluch
2016-07-28 15:12:58 +02:00
parent da263df25c
commit 43ab34ac54
3 changed files with 112 additions and 31 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,10 +19,12 @@ package org.springframework.cassandra.core;
* Generic Consistency Levels associated with Cassandra.
*
* @author David Webb
* @author Antoine Toulme
*/
public enum ConsistencyLevel {
ANY, ONE, TWO, THREE,
/**
* @deprecated Use {@link #QUORUM}
*/

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -15,10 +15,13 @@
*/
package org.springframework.cassandra.core;
import org.springframework.util.Assert;
/**
* Determine driver consistency level based on ConsistencyLevel
*
* @author David Webb
* @author Antoine Toulme
*/
public final class ConsistencyLevelResolver {
@@ -30,59 +33,44 @@ public final class ConsistencyLevelResolver {
/**
* Decode the generic spring data cassandra enum to the type required by the DataStax Driver.
*
* @param level
* @param level the consistency level to resolve, must not be {@literal null}.
* @return The DataStax Driver Consistency Level.
*/
public static com.datastax.driver.core.ConsistencyLevel resolve(ConsistencyLevel level) {
com.datastax.driver.core.ConsistencyLevel resolvedLevel = com.datastax.driver.core.ConsistencyLevel.ONE;
Assert.notNull(level, "ConsistencyLevel must not be null");
/*
* Determine the driver level based on our enum
*/
switch (level) {
case ONE:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.ONE;
break;
return com.datastax.driver.core.ConsistencyLevel.ONE;
case LOCAL_ONE:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.LOCAL_ONE;
break;
return com.datastax.driver.core.ConsistencyLevel.LOCAL_ONE;
case ALL:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.ALL;
break;
return com.datastax.driver.core.ConsistencyLevel.ALL;
case ANY:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.ANY;
break;
return com.datastax.driver.core.ConsistencyLevel.ANY;
case EACH_QUORUM:
case EACH_QUOROM:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.EACH_QUORUM;
break;
return com.datastax.driver.core.ConsistencyLevel.EACH_QUORUM;
case LOCAL_QUORUM:
case LOCAL_QUOROM:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.LOCAL_QUORUM;
break;
return com.datastax.driver.core.ConsistencyLevel.LOCAL_QUORUM;
case QUORUM:
case QUOROM:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.QUORUM;
break;
return com.datastax.driver.core.ConsistencyLevel.QUORUM;
case THREE:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.THREE;
break;
return com.datastax.driver.core.ConsistencyLevel.THREE;
case TWO:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.TWO;
break;
return com.datastax.driver.core.ConsistencyLevel.TWO;
case SERIAL:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.SERIAL;
break;
return com.datastax.driver.core.ConsistencyLevel.SERIAL;
case LOCAL_SERIAL:
resolvedLevel = com.datastax.driver.core.ConsistencyLevel.LOCAL_SERIAL;
break;
return com.datastax.driver.core.ConsistencyLevel.LOCAL_SERIAL;
default:
break;
throw new IllegalArgumentException(String.format("ConsistencyLevel [%s] not supported", level));
}
return resolvedLevel;
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright 2016 the original author or authors.
*
* 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 org.springframework.cassandra.core;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
/**
* Unit tests for {@link ConsistencyLevelResolver}.
*
* @author Mark Paluch
* @see DATACASS-202
*/
@RunWith(Parameterized.class)
public class ConsistencyLevelResolverUnitTests {
private final ConsistencyLevel from;
private final com.datastax.driver.core.ConsistencyLevel expected;
public ConsistencyLevelResolverUnitTests(ConsistencyLevel from, com.datastax.driver.core.ConsistencyLevel expected) {
this.from = from;
this.expected = expected;
}
@Parameters(name = "{0}")
public static List<Object[]> parameters() {
Map<ConsistencyLevel, com.datastax.driver.core.ConsistencyLevel> expectations = new LinkedHashMap<ConsistencyLevel, com.datastax.driver.core.ConsistencyLevel>();
expectations.put(ConsistencyLevel.ALL, com.datastax.driver.core.ConsistencyLevel.ALL);
expectations.put(ConsistencyLevel.ANY, com.datastax.driver.core.ConsistencyLevel.ANY);
expectations.put(ConsistencyLevel.QUOROM, com.datastax.driver.core.ConsistencyLevel.QUORUM);
expectations.put(ConsistencyLevel.QUORUM, com.datastax.driver.core.ConsistencyLevel.QUORUM);
expectations.put(ConsistencyLevel.LOCAL_QUOROM, com.datastax.driver.core.ConsistencyLevel.LOCAL_QUORUM);
expectations.put(ConsistencyLevel.LOCAL_QUORUM, com.datastax.driver.core.ConsistencyLevel.LOCAL_QUORUM);
expectations.put(ConsistencyLevel.EACH_QUOROM, com.datastax.driver.core.ConsistencyLevel.EACH_QUORUM);
expectations.put(ConsistencyLevel.EACH_QUORUM, com.datastax.driver.core.ConsistencyLevel.EACH_QUORUM);
expectations.put(ConsistencyLevel.LOCAL_ONE, com.datastax.driver.core.ConsistencyLevel.LOCAL_ONE);
expectations.put(ConsistencyLevel.LOCAL_SERIAL, com.datastax.driver.core.ConsistencyLevel.LOCAL_SERIAL);
expectations.put(ConsistencyLevel.SERIAL, com.datastax.driver.core.ConsistencyLevel.SERIAL);
expectations.put(ConsistencyLevel.ONE, com.datastax.driver.core.ConsistencyLevel.ONE);
expectations.put(ConsistencyLevel.TWO, com.datastax.driver.core.ConsistencyLevel.TWO);
expectations.put(ConsistencyLevel.THREE, com.datastax.driver.core.ConsistencyLevel.THREE);
List<Object[]> parameters = new ArrayList<Object[]>();
for (Entry<ConsistencyLevel, com.datastax.driver.core.ConsistencyLevel> entry : expectations.entrySet()) {
parameters.add(new Object[] { entry.getKey(), entry.getValue() });
}
return parameters;
}
/**
* @see DATACASS-202
*/
@Test
public void shouldResolveCorrectly() {
assertThat(ConsistencyLevelResolver.resolve(from), is(equalTo(expected)));
}
}