DATACASS-172 - Polishing.

Add tests. Add validations to Cql generators. Enhance JavaDoc. Create FieldSpecification for UDT fields.
This commit is contained in:
Mark Paluch
2016-09-06 08:19:35 +02:00
committed by John Blum
parent 1865c31708
commit a36e7beecb
20 changed files with 954 additions and 215 deletions

View File

@@ -0,0 +1,106 @@
/*
* 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.cql.generator;
import static org.springframework.cassandra.core.cql.generator.AlterUserTypeCqlGenerator.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cassandra.core.keyspace.AlterUserTypeSpecification;
import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest;
import com.datastax.driver.core.DataType;
/**
* Integration tests for {@link AlterUserTypeCqlGenerator}.
*
* @author Mark Paluch
*/
public class AlterUserTypeCqlGeneratorIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest {
@Before
public void setUp() throws Exception {
session.execute("DROP TYPE IF EXISTS address;");
session.execute("CREATE TYPE address (zip text, state text);");
}
/**
* @see DATACASS-172
*/
@Test
public void alterTypeShouldAddField() {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address")//
.add("street", DataType.varchar());
session.execute(toCql(spec));
}
/**
* @see DATACASS-172
*/
@Test
public void alterTypeShouldAlterField() {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address")//
.alter("zip", DataType.varchar());
session.execute(toCql(spec));
}
/**
* @see DATACASS-172
*/
@Test
public void alterTypeShouldRenameField() {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address")//
.rename("zip", "zap");
session.execute(toCql(spec));
}
/**
* @see DATACASS-172
*/
@Test
public void alterTypeShouldRenameFields() {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address")//
.rename("zip", "zap") //
.rename("state", "county");
session.execute(toCql(spec));
}
/**
* @see DATACASS-172
*/
@Test(expected = IllegalArgumentException.class)
public void generationFailsIfNameIsNotSet() {
toCql(AlterUserTypeSpecification.alterType());
}
/**
* @see DATACASS-172
*/
@Test(expected = IllegalArgumentException.class)
public void generationFailsWithoutFields() {
toCql(AlterUserTypeSpecification.alterType().name("hello"));
}
}

View File

@@ -0,0 +1,98 @@
/*
* 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.cql.generator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.cassandra.core.cql.generator.AlterUserTypeCqlGenerator.*;
import org.junit.Test;
import org.springframework.cassandra.core.keyspace.AlterUserTypeSpecification;
import com.datastax.driver.core.DataType;
/**
* Unit tests for {@link AlterUserTypeCqlGenerator}.
*
* @author Mark Paluch
*/
public class AlterUserTypeCqlGeneratorUnitTests {
/**
* @see DATACASS-172
*/
@Test
public void alterTypeShouldAddField() {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") //
.add("zip", DataType.varchar());
assertThat(toCql(spec), is(equalTo("ALTER TYPE address ADD zip varchar;")));
}
/**
* @see DATACASS-172
*/
@Test
public void alterTypeShouldAlterField() {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") //
.alter("zip", DataType.varchar());
assertThat(toCql(spec), is(equalTo("ALTER TYPE address ALTER zip TYPE varchar;")));
}
/**
* @see DATACASS-172
*/
@Test
public void alterTypeShouldRenameField() {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") //
.rename("zip", "zap");
assertThat(toCql(spec), is(equalTo("ALTER TYPE address RENAME zip TO zap;")));
}
/**
* @see DATACASS-172
*/
@Test
public void alterTypeShouldRenameFields() {
AlterUserTypeSpecification spec = AlterUserTypeSpecification.alterType("address") //
.rename("zip", "zap") //
.rename("city", "county");
assertThat(toCql(spec), is(equalTo("ALTER TYPE address RENAME zip TO zap AND city TO county;")));
}
/**
* @see DATACASS-172
*/
@Test(expected = IllegalArgumentException.class)
public void generationFailsIfNameIsNotSet() {
toCql(AlterUserTypeSpecification.alterType());
}
/**
* @see DATACASS-172
*/
@Test(expected = IllegalArgumentException.class)
public void generationFailsWithoutFields() {
toCql(AlterUserTypeSpecification.alterType().name("hello"));
}
}

View File

@@ -0,0 +1,104 @@
/*
* 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.cql.generator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.cassandra.core.cql.generator.CreateUserTypeCqlGenerator.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cassandra.core.keyspace.CreateUserTypeSpecification;
import org.springframework.cassandra.test.integration.AbstractKeyspaceCreatingIntegrationTest;
import com.datastax.driver.core.DataType;
import com.datastax.driver.core.KeyspaceMetadata;
import com.datastax.driver.core.UserType;
/**
* Integration tests for {@link CreateUserTypeCqlGenerator}.
*
* @author Mark Paluch
*/
public class CreateUserTypeCqlGeneratorIntegrationTests extends AbstractKeyspaceCreatingIntegrationTest {
@Before
public void setUp() throws Exception {
session.execute("DROP TYPE IF EXISTS person;");
session.execute("DROP TYPE IF EXISTS address;");
}
/**
* @see DATACASS-172
*/
@Test
public void createUserType() {
CreateUserTypeSpecification spec = CreateUserTypeSpecification //
.createType("address") //
.field("zip", DataType.ascii()) //
.field("city", DataType.varchar());
session.execute(toCql(spec));
KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace());
UserType address = keyspace.getUserType("address");
assertThat(address.getFieldNames(), contains("zip", "city"));
}
/**
* @see DATACASS-172
*/
@Test
public void createUserTypeIfNotExists() {
CreateUserTypeSpecification spec = CreateUserTypeSpecification //
.createType() //
.name("address").ifNotExists().field("zip", DataType.ascii()) //
.field("city", DataType.varchar());
session.execute(toCql(spec));
KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace());
UserType address = keyspace.getUserType("address");
assertThat(address.getFieldNames(), contains("zip", "city"));
}
/**
* @see DATACASS-172
*/
@Test
public void createNestedUserType() {
CreateUserTypeSpecification addressSpec = CreateUserTypeSpecification //
.createType() //
.name("address").ifNotExists().field("zip", DataType.ascii()) //
.field("city", DataType.varchar());
session.execute(toCql(addressSpec));
KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace());
UserType address = keyspace.getUserType("address");
CreateUserTypeSpecification personSpec = CreateUserTypeSpecification //
.createType() //
.name("person").ifNotExists().field("address", address) //
.field("city", DataType.varchar());
session.execute(toCql(personSpec));
}
}

View File

@@ -0,0 +1,90 @@
/*
* 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.cql.generator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.cassandra.core.cql.generator.CreateUserTypeCqlGenerator.*;
import org.junit.Test;
import org.springframework.cassandra.core.keyspace.CreateUserTypeSpecification;
import com.datastax.driver.core.DataType;
/**
* Unit tests for {@link CreateUserTypeCqlGenerator}.
*
* @author Mark Paluch
*/
public class CreateUserTypeCqlGeneratorUnitTests {
/**
* @see DATACASS-172
*/
@Test
public void createUserType() {
CreateUserTypeSpecification spec = CreateUserTypeSpecification //
.createType("address") //
.field("city", DataType.varchar());
assertThat(toCql(spec), is(equalTo("CREATE TYPE address (city varchar);")));
}
/**
* @see DATACASS-172
*/
@Test
public void createMultiFieldUserType() {
CreateUserTypeSpecification spec = CreateUserTypeSpecification //
.createType("address") //
.field("zip", DataType.ascii()) //
.field("city", DataType.varchar());
assertThat(toCql(spec), is(equalTo("CREATE TYPE address (zip ascii, city varchar);")));
}
/**
* @see DATACASS-172
*/
@Test
public void createUserTypeIfNotExists() {
CreateUserTypeSpecification spec = CreateUserTypeSpecification //
.createType() //
.name("address").ifNotExists().field("zip", DataType.ascii()) //
.field("city", DataType.varchar());
assertThat(toCql(spec), is(equalTo("CREATE TYPE IF NOT EXISTS address (zip ascii, city varchar);")));
}
/**
* @see DATACASS-172
*/
@Test(expected = IllegalArgumentException.class)
public void generationFailsIfNameIsNotSet() {
toCql(CreateUserTypeSpecification.createType());
}
/**
* @see DATACASS-172
*/
@Test(expected = IllegalArgumentException.class)
public void generationFailsWithoutFields() {
toCql(CreateUserTypeSpecification.createType().name("hello"));
}
}

View File

@@ -0,0 +1,47 @@
/*
* 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.cql.generator;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.springframework.cassandra.core.cql.generator.DropUserTypeCqlGenerator.*;
import org.junit.Test;
import org.springframework.cassandra.core.keyspace.DropUserTypeSpecification;
/**
* Unit tests for {@link DropUserTypeCqlGenerator}.
*
* @author Mark Paluch
*/
public class DropUserTypeCqlGeneratorUnitTests {
@Test
public void shouldDropUserType() throws Exception {
DropUserTypeSpecification spec = DropUserTypeSpecification.dropType("address");
assertThat(toCql(spec), is(equalTo("DROP TYPE address;")));
}
@Test
public void shouldDropUserTypeIfExists() throws Exception {
DropUserTypeSpecification spec = DropUserTypeSpecification.dropType("address").ifExists();
assertThat(toCql(spec), is(equalTo("DROP TYPE IF EXISTS address;")));
}
}