DATACASS-219 - On startup CREATE TABLE from entities should only add 'if not exists'.

This commit is contained in:
John Blum
2016-05-09 10:12:38 -07:00
parent 4772c5b628
commit a1e75b3857
17 changed files with 1531 additions and 305 deletions

View File

@@ -0,0 +1,349 @@
/*
* 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.
* 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.config;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.InOrder;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.cassandra.core.CqlOperations;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
/**
* The CassandraCqlSessionFactoryBeanUnitTests class is a test suite of test cases testing the contract
* and functionality of the {@link CassandraCqlSessionFactoryBean} class.
*
* @author John Blum
* @see org.junit.Rule
* @see org.junit.Test
* @see org.junit.rules.ExpectedException
* @see org.junit.runner.RunWith
* @see org.mockito.Mock
* @see org.mockito.Mockito
* @see org.mockito.runners.MockitoJUnitRunner
* @see org.springframework.cassandra.config.CassandraCqlSessionFactoryBean
* @since 1.5.0
*/
@RunWith(MockitoJUnitRunner.class)
public class CassandraCqlSessionFactoryBeanUnitTests {
@Rule
public ExpectedException exception = ExpectedException.none();
@Mock
private Cluster mockCluster;
@Mock
private Session mockSession;
private CassandraCqlSessionFactoryBean factoryBean;
protected <T> List<T> asList(T... array) {
return new ArrayList<T>(Arrays.asList(array));
}
protected void assertNonNullEmptyCollection(Collection<?> collection) {
assertThat(collection, is(notNullValue()));
assertThat(collection.isEmpty(), is(true));
}
@Before
public void setup() {
factoryBean = spy(new CassandraCqlSessionFactoryBean());
}
@Test
public void cassandraCqlSessionFactoryBeanIsSingleton() {
assertThat(factoryBean.isSingleton(), is(true));
}
@Test
public void objectTypeWhenSessionHasNotBeenInitializedIsSessionClass() {
assertThat(factoryBean.getObject(), is(nullValue()));
assertEquals(Session.class, factoryBean.<Session>getObjectType());
}
@Test
@SuppressWarnings("unchecked")
public void afterPropertiesSetInitializesSessionWithKeyspaceAndExecutesStartupScripts() throws Exception {
List<String> expectedStartupScripts = asList("/path/to/schema.cql", "/path/to/data.cql");
CqlOperations mockCqlOperations = mock(CqlOperations.class);
doReturn(mockSession).when(factoryBean).connect(eq("TestKeyspace"));
doReturn(mockCqlOperations).when(factoryBean).newCqlOperations(eq(mockSession));
factoryBean.setKeyspaceName("TestKeyspace");
factoryBean.setStartupScripts(expectedStartupScripts);
assertThat(factoryBean.getKeyspaceName(), is(equalTo("TestKeyspace")));
assertThat(factoryBean.getStartupScripts(), is(equalTo(expectedStartupScripts)));
factoryBean.afterPropertiesSet();
assertEquals(mockSession.getClass(), factoryBean.getObjectType());
assertThat(factoryBean.getObject(), is(equalTo(mockSession)));
assertThat(factoryBean.getSession(), is(equalTo(mockSession)));
InOrder inOrder = inOrder(factoryBean);
inOrder.verify(factoryBean, times(1)).connect(eq("TestKeyspace"));
inOrder.verify(factoryBean, times(1)).executeScripts(eq(expectedStartupScripts));
inOrder.verify(factoryBean, times(1)).newCqlOperations(eq(mockSession));
verify(mockCqlOperations, times(1)).execute(eq(expectedStartupScripts.get(0)));
verify(mockCqlOperations, times(1)).execute(eq(expectedStartupScripts.get(1)));
}
@Test
public void connectToSystemKeyspace() {
when(mockCluster.connect()).thenReturn(mockSession);
factoryBean.setCluster(mockCluster);
assertThat(factoryBean.connect(null), is(equalTo(mockSession)));
verify(mockCluster, times(1)).connect();
verify(mockCluster, never()).connect(anyString());
}
@Test
public void connectToTargetKeyspace() {
when(mockCluster.connect(eq("TestKeyspace"))).thenReturn(mockSession);
factoryBean.setCluster(mockCluster);
assertThat(factoryBean.connect("TestKeyspace"), is(equalTo(mockSession)));
verify(mockCluster, never()).connect();
verify(mockCluster, times(1)).connect(eq("TestKeyspace"));
}
@Test
@SuppressWarnings("unchecked")
public void destroySessionAndExecutesShutdownScripts() throws Exception {
List<String> expectedShutdownScripts = asList("/path/to/shutdown.cql");
CqlOperations mockCqlOperations = mock(CqlOperations.class);
doReturn(mockSession).when(factoryBean).getSession();
doReturn(mockCqlOperations).when(factoryBean).newCqlOperations(eq(mockSession));
factoryBean.setShutdownScripts(expectedShutdownScripts);
factoryBean.destroy();
InOrder inOrder = inOrder(factoryBean, mockSession);
inOrder.verify(factoryBean, times(1)).executeScripts(eq(expectedShutdownScripts));
verify(mockCqlOperations, times(1)).execute(eq(expectedShutdownScripts.get(0)));
inOrder.verify(mockSession, times(1)).close();
}
@Test
public void isConnectedWithNullSessionIsFalse() {
assertThat(factoryBean.getObject(), is(nullValue()));
assertThat(factoryBean.isConnected(), is(false));
}
@Test
public void isConnectedWithClosedSessionIsFalse() {
doReturn(mockSession).when(factoryBean).getObject();
when(mockSession.isClosed()).thenReturn(true);
assertThat(factoryBean.isConnected(), is(false));
verify(mockSession, times(1)).isClosed();
}
@Test
public void isConnectedWithOpenSessionIsTrue() {
doReturn(mockSession).when(factoryBean).getObject();
when(mockSession.isClosed()).thenReturn(false);
assertThat(factoryBean.isConnected(), is(true));
verify(mockSession, times(1)).isClosed();
}
@Test
public void setAndGetCluster() {
factoryBean.setCluster(mockCluster);
assertThat(factoryBean.getCluster(), is(equalTo(mockCluster)));
}
@Test
public void setClusterToNullThrowsIllegalArgumentException() {
exception.expect(IllegalArgumentException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("Cluster must not be null");
factoryBean.setCluster(null);
}
@Test
public void getClusterWhenUninitializedThrowsIllegalStateException() {
exception.expect(IllegalStateException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage("Cluster was not properly initialized");
factoryBean.getCluster();
}
@Test
public void setAndGetKeyspaceName() {
assertThat(factoryBean.getKeyspaceName(), is(nullValue()));
factoryBean.setKeyspaceName("TEST");
assertThat(factoryBean.getKeyspaceName(), is(equalTo("TEST")));
factoryBean.setKeyspaceName(null);
assertThat(factoryBean.getKeyspaceName(), is(nullValue()));
}
@Test
public void getSessionWhenUninitializedThrowsIllegalStateException() {
exception.expect(IllegalStateException.class);
exception.expectCause(is(nullValue(Throwable.class)));
exception.expectMessage(is(equalTo("Session was not properly initialized")));
assertThat(factoryBean.getObject(), is(nullValue()));
factoryBean.getSession();
}
@Test
public void setAndGetStartupScripts() {
assertNonNullEmptyCollection(factoryBean.getStartupScripts());
List<String> expectedStartupScripts = asList("/path/to/schema.cql", "/path/to/data.cql");
factoryBean.setStartupScripts(expectedStartupScripts);
List<String> actualStartupScripts = factoryBean.getStartupScripts();
assertThat(actualStartupScripts, is(not(sameInstance(expectedStartupScripts))));
assertThat(actualStartupScripts, is(equalTo(expectedStartupScripts)));
factoryBean.setStartupScripts(null);
assertNonNullEmptyCollection(factoryBean.getStartupScripts());
}
@Test
public void startupScriptsAreImmutable() {
List<String> startupScripts = asList("/path/to/startup.cql");
factoryBean.setStartupScripts(startupScripts);
List<String> actualStartupScripts = factoryBean.getStartupScripts();
assertThat(actualStartupScripts, is(notNullValue()));
assertThat(actualStartupScripts, is(not(sameInstance(startupScripts))));
assertThat(actualStartupScripts, is(equalTo(startupScripts)));
startupScripts.add("/path/to/another.cql");
actualStartupScripts = factoryBean.getStartupScripts();
assertThat(actualStartupScripts, is(not(equalTo(startupScripts))));
assertThat(actualStartupScripts.size(), is(equalTo(1)));
assertThat(actualStartupScripts.get(0), is(equalTo(startupScripts.get(0))));
try {
exception.expect(UnsupportedOperationException.class);
actualStartupScripts.add("/path/to/yetAnother.cql");
}
finally {
assertThat(actualStartupScripts.size(), is(equalTo(1)));
}
}
@Test
public void setAndGetShutdownScripts() {
assertNonNullEmptyCollection(factoryBean.getShutdownScripts());
List<String> expectedShutdownScripts = asList("/path/to/backup.cql", "/path/to/dropTables.cql");
factoryBean.setShutdownScripts(expectedShutdownScripts);
List<String> actualShutdownScripts = factoryBean.getShutdownScripts();
assertThat(actualShutdownScripts, is(not(sameInstance(expectedShutdownScripts))));
assertThat(actualShutdownScripts, is(equalTo(expectedShutdownScripts)));
factoryBean.setShutdownScripts(null);
assertNonNullEmptyCollection(factoryBean.getShutdownScripts());
}
@Test
public void shutdownScriptsAreImmutable() {
List<String> shutdownScripts = asList("/path/to/shutdown.cql");
factoryBean.setShutdownScripts(shutdownScripts);
List<String> actualShutdownScripts = factoryBean.getShutdownScripts();
assertThat(actualShutdownScripts, is(notNullValue()));
assertThat(actualShutdownScripts, is(not(sameInstance(shutdownScripts))));
assertThat(actualShutdownScripts, is(equalTo(shutdownScripts)));
shutdownScripts.add("/path/to/corruptSession.cql");
actualShutdownScripts = factoryBean.getShutdownScripts();
assertThat(actualShutdownScripts, is(not(sameInstance(shutdownScripts))));
assertThat(actualShutdownScripts, is(not(equalTo(shutdownScripts))));
assertThat(actualShutdownScripts.size(), is(equalTo(1)));
try {
exception.expect(UnsupportedOperationException.class);
actualShutdownScripts.add("/path/to/blowUpCluster.cql");
}
finally {
assertThat(actualShutdownScripts.size(), is(equalTo(1)));
}
}
}

View File

@@ -0,0 +1,143 @@
/*
* 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.
* 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.util;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
/**
* The CollectionUtilsUnitTests class is a test suite of test cases testing the contract and functionality
* of the {@link CollectionUtils} class.
*
* @author John Blum
* @see org.junit.Test
* @see org.springframework.cassandra.core.util.CollectionUtils
* @since 1.5.0
*/
public class CollectionUtilsUnitTests {
<T> List<T> asList(T... array) {
return Arrays.asList(array);
}
void assertNonNullEmptyArray(Object[] array) {
assertThat(array, is(notNullValue()));
assertThat(array.length, is(equalTo(0)));
}
void assertNonNullEmptyCollection(Collection<?> collection) {
assertThat(collection, is(notNullValue()));
assertThat(collection.isEmpty(), is(true));
}
<T> Iterable<T> newIterable(final T... elements) {
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
int index = 0;
@Override
public boolean hasNext() {
return (index < elements.length);
}
@Override
public T next() {
return elements[index++];
}
};
}
};
}
@Test
public void toArrayWithIterable() {
Object[] array = CollectionUtils.toArray(newIterable(1, 2, 3));
assertThat(array, is(notNullValue()));
assertThat(array.length, is(equalTo(3)));
for (int index = 0; index < array.length; index++) {
Object valueAtIndex = (index + 1);
assertThat(array[index], is(equalTo(valueAtIndex)));
}
}
@Test
public void toArrayWithEmptyIterable() {
assertNonNullEmptyArray(CollectionUtils.toArray(newIterable()));
}
@Test
public void toArrayWithNull() {
assertNonNullEmptyArray(CollectionUtils.toArray(null));
}
@Test
public void toListWithArray() {
List<String> list = CollectionUtils.toList("test", "testing", "tested");
assertThat(list, is(notNullValue()));
assertThat(list.size(), is(equalTo(3)));
assertThat(list.containsAll(asList("test", "testing", "tested")), is(true));
}
@Test
public void toListWithEmptyArray() {
assertNonNullEmptyCollection(CollectionUtils.toList());
}
@Test
public void toListWithNullArray() {
assertNonNullEmptyCollection(CollectionUtils.toList((Object[]) null));
}
@Test
@SuppressWarnings("unchecked")
public void toListWithIterable() {
List<Integer> list = CollectionUtils.toList(newIterable(1, 2, 3));
assertThat(list, is(notNullValue()));
assertThat(list.size(), is(equalTo(3)));
assertThat(list.containsAll(asList(1, 2, 3)), is(true));
}
@Test
public void toListWithList() {
List<Integer> expected = asList(1, 2, 3);
List<Integer> actual = CollectionUtils.toList(expected);
assertThat(actual, is(sameInstance(expected)));
}
@Test
public void toListWithNullIterable() {
assertNonNullEmptyCollection(CollectionUtils.toList((Iterable<?>) null));
}
}