|
| 1 | +/* |
| 2 | + * Copyright 2017 the original author or authors. |
| 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 example.springdata.cassandra.udt; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.*; |
| 19 | + |
| 20 | +import example.springdata.cassandra.util.RequiresCassandraKeyspace; |
| 21 | + |
| 22 | +import java.util.Collections; |
| 23 | + |
| 24 | +import org.junit.Before; |
| 25 | +import org.junit.ClassRule; |
| 26 | +import org.junit.Test; |
| 27 | +import org.junit.runner.RunWith; |
| 28 | +import org.springframework.beans.factory.annotation.Autowired; |
| 29 | +import org.springframework.boot.test.context.SpringBootTest; |
| 30 | +import org.springframework.context.annotation.Configuration; |
| 31 | +import org.springframework.data.cassandra.config.SchemaAction; |
| 32 | +import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration; |
| 33 | +import org.springframework.data.cassandra.core.CassandraAdminOperations; |
| 34 | +import org.springframework.data.cassandra.core.CassandraOperations; |
| 35 | +import org.springframework.test.context.junit4.SpringRunner; |
| 36 | + |
| 37 | +import com.datastax.driver.core.KeyspaceMetadata; |
| 38 | +import com.datastax.driver.core.UDTValue; |
| 39 | +import com.datastax.driver.core.UserType; |
| 40 | + |
| 41 | +/** |
| 42 | + * Integration test to show User-Defined type support. |
| 43 | + * |
| 44 | + * @author Mark Paluch |
| 45 | + */ |
| 46 | +@RunWith(SpringRunner.class) |
| 47 | +@SpringBootTest |
| 48 | +public class UserDefinedTypeIntegrationTest { |
| 49 | + |
| 50 | + @ClassRule public final static RequiresCassandraKeyspace CASSANDRA_KEYSPACE = RequiresCassandraKeyspace.onLocalhost(); |
| 51 | + |
| 52 | + @Configuration |
| 53 | + static class Config extends AbstractCassandraConfiguration { |
| 54 | + |
| 55 | + @Override |
| 56 | + public String getKeyspaceName() { |
| 57 | + return "example"; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String[] getEntityBasePackages() { |
| 62 | + return new String[] { Person.class.getPackage().getName() }; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public SchemaAction getSchemaAction() { |
| 67 | + return SchemaAction.RECREATE; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + @Autowired CassandraOperations operations; |
| 72 | + @Autowired CassandraAdminOperations adminOperations; |
| 73 | + |
| 74 | + @Before |
| 75 | + public void before() throws Exception { |
| 76 | + operations.truncate("person"); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Insert a row with a mapped User-defined type. |
| 81 | + */ |
| 82 | + @Test |
| 83 | + public void insertMappedUdt() { |
| 84 | + |
| 85 | + Person person = new Person(); |
| 86 | + person.setId(42); |
| 87 | + person.setFirstname("Walter"); |
| 88 | + person.setLastname("White"); |
| 89 | + |
| 90 | + person.setCurrent(new Address("308 Negra Arroyo Lane", "87104", "Albuquerque")); |
| 91 | + person.setPrevious(Collections.singletonList(new Address("12000 – 12100 Coors Rd SW", "87045", "Albuquerque"))); |
| 92 | + |
| 93 | + operations.insert(person); |
| 94 | + |
| 95 | + Person loaded = operations.selectOne("SELECT * FROM person WHERE id = 42", Person.class); |
| 96 | + |
| 97 | + assertThat(loaded.getCurrent()).isEqualTo(person.getCurrent()); |
| 98 | + assertThat(loaded.getPrevious()).containsAll(person.getPrevious()); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Insert a row with a raw User-defined type. |
| 103 | + */ |
| 104 | + @Test |
| 105 | + public void insertRawUdt() { |
| 106 | + |
| 107 | + KeyspaceMetadata keyspaceMetadata = adminOperations.getKeyspaceMetadata(); |
| 108 | + UserType address = keyspaceMetadata.getUserType("address"); |
| 109 | + |
| 110 | + UDTValue udtValue = address.newValue(); |
| 111 | + udtValue.setString("street", "308 Negra Arroyo Lane"); |
| 112 | + udtValue.setString("zip", "87104"); |
| 113 | + udtValue.setString("city", "Albuquerque"); |
| 114 | + |
| 115 | + Person person = new Person(); |
| 116 | + person.setId(42); |
| 117 | + person.setFirstname("Walter"); |
| 118 | + person.setLastname("White"); |
| 119 | + |
| 120 | + person.setAlternative(udtValue); |
| 121 | + |
| 122 | + operations.insert(person); |
| 123 | + |
| 124 | + Person loaded = operations.selectOne("SELECT * FROM person WHERE id = 42", Person.class); |
| 125 | + |
| 126 | + assertThat(loaded.getAlternative().getString("zip")).isEqualTo("87104"); |
| 127 | + } |
| 128 | +} |
0 commit comments