Elffuss BraidHub

kikocis/java-driver public

JAVA-1272: Implement parse() and format() for MappedUDTCodec.
Changeset00fb99dbb7974596be2d9528c3b411f632fca4963a9c1a33d5b82a23af1d0285
AuthorAlexandre Dutra <alexandre.dutra@datastax.com> · 2016-09-06 18:54 +02:00
Committerolim7t <omichallat+github@gmail.com> · 2016-09-16 10:45 -07:00
Parentsc0702f73685f

Browse the files at this changeset

2 files changed

modified driver-mapping/src/main/java/com/datastax/driver/mapping/MappedUDTCodec.java +26 −4
@@ -87,13 +87,35 @@
8787
8888 @Override
8989 protected String formatField(T source, String fieldName) {
90- // This codec implementation is internal-use only, and its format method is never used
91- throw new UnsupportedOperationException();
90+ if (!fieldName.startsWith("\""))
91+ fieldName = Metadata.quote(fieldName);
92+
93+ ColumnMapper<T> columnMapper = columnMappers.get(fieldName);
94+
95+ if (columnMapper == null)
96+ return null;
97+
98+ Object value = columnMapper.getValue(source);
99+
100+ TypeCodec<Object> codec = columnMapper.getCustomCodec();
101+ if (codec == null)
102+ codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType());
103+
104+ return codec.format(value);
92105 }
93106
94107 @Override
95108 protected T parseAndSetField(String input, T target, String fieldName) {
96- // This codec implementation is internal-use only, and its parse method is never used
97- throw new UnsupportedOperationException();
109+ if (!fieldName.startsWith("\""))
110+ fieldName = Metadata.quote(fieldName);
111+
112+ ColumnMapper<T> columnMapper = columnMappers.get(fieldName);
113+ if (columnMapper != null) {
114+ TypeCodec<Object> codec = columnMapper.getCustomCodec();
115+ if (codec == null)
116+ codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType());
117+ columnMapper.setValue(target, codec.parse(input));
118+ }
119+ return target;
98120 }
99121 }
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java +45 −0
@@ -18,6 +18,7 @@
1818 import com.datastax.driver.core.*;
1919 import com.datastax.driver.core.exceptions.CodecNotFoundException;
2020 import com.datastax.driver.core.exceptions.InvalidQueryException;
21+import com.datastax.driver.core.querybuilder.BuiltStatement;
2122 import com.datastax.driver.core.utils.CassandraVersion;
2223 import com.datastax.driver.core.utils.UUIDs;
2324 import com.datastax.driver.mapping.annotations.*;
@@ -31,6 +32,7 @@
3132 import java.util.*;
3233
3334 import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD;
35+import static com.datastax.driver.core.querybuilder.QueryBuilder.*;
3436 import static org.assertj.core.api.Assertions.assertThat;
3537 import static org.assertj.core.api.Assertions.fail;
3638 import static org.testng.Assert.*;
@@ -728,4 +730,47 @@
728730 manager.mapper(UserWithAddressUnknownField.class);
729731 }
730732
733+ /**
734+ * Ensures that MappedUDTCodec is able to properly format UDTs when printing the query string of a BuiltStatement.
735+ *
736+ * @jira_ticket JAVA-1272
737+ * @test_category object_mapper
738+ */
739+ @Test(groups = "short")
740+ public void should_format_mapped_udt() throws Exception {
741+ MappingManager manager = new MappingManager(session());
742+ UUID uuid = UUIDs.random();
743+ BuiltStatement update =
744+ update("users")
745+ .with(set("mainaddress", new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245")))
746+ .where(eq("user_id", uuid));
747+ CodecRegistry codecRegistry = cluster().getConfiguration().getCodecRegistry();
748+ codecRegistry.register(manager.udtCodec(Address.class));
749+ String queryString = update.getQueryString(codecRegistry);
750+ assertThat(queryString)
751+ .isEqualTo("UPDATE users SET mainaddress=? WHERE user_id=?;");
752+ update.setForceNoValues(true);
753+ queryString = update.getQueryString(codecRegistry);
754+ assertThat(queryString).isEqualTo(
755+ "UPDATE users " +
756+ "SET mainaddress={street:'12 4th Street',city:'Springfield',\"ZIP_code\":12345,phones:{'435423245','12341343'}} " +
757+ "WHERE user_id=" + uuid + ";");
758+ // check that the query string is valid
759+ session().execute(queryString);
760+ }
761+
762+ /**
763+ * Ensures that MappedUDTCodec is able to properly parse UDTs.
764+ *
765+ * @jira_ticket JAVA-1272
766+ * @test_category object_mapper
767+ */
768+ @Test(groups = "short")
769+ public void should_parse_mapped_udt() throws Exception {
770+ MappingManager manager = new MappingManager(session());
771+ TypeCodec<Address> codec = manager.udtCodec(Address.class);
772+ Address actual = codec.parse("{street:'12 4th Street',city:'Springfield',\"ZIP_code\":12345,phones:{'435423245','12341343'}}");
773+ assertThat(actual).isEqualTo(new Address("12 4th Street", "Springfield", 12345, "12341343", "435423245"));
774+ }
775+
731776 }