I have a series of test cases using Spring Jdbc, Database Rider, and H2 — with Java 17.x. Everything works fine, but I can't find a way to represent a byte array in the YAML configuration file :/
This is what I have:
@Getter
@Builder
public final class Category {
...
private byte[] picture;
}
public class CategoryJdbc implements ICategoryJdbc {
private static final RowMapper<Category> ROW_MAPPER = (rs, rowNum) -> Category.builder()
...
.picture(rs.getBytes("picture"))
.build();
private final NamedParameterJdbcTemplate template;
@Override
@Transactional(readOnly = true)
public Optional<Category> findById(final Long primaryKey) {
final var result = template.queryForObject(queries.getProperty("find-category-by-identifier"),
new MapSqlParameterSource("id", primaryKey), ROW_MAPPER);
return Optional.ofNullable(result);
}
}
@DBRider
@ExtendWith(SpringExtension.class)
class CategoryJdbcTest {
@Autowired
private ICategoryJdbc jdbc;
@Test
@DataSet("categories.yml")
void findById_WhenEntityExists() {
Assertions.assertThat(jdbc.findById(1L)).hasValueSatisfying(it -> {
Assertions.assertThat(it.getId()).isEqualTo(1);
Assertions.assertThat(it.getName()).isEqualTo("Beverages");
Assertions.assertThat(it.getDescription()).isEqualTo("Soft drinks, coffees, teas, beers, and ales");
Assertions.assertThat(it.getPicture())
.isEqualTo(new byte[] { 0x4c, 0x6f, 72, 65, 0x6d, 20, 69, 70, 73, 75, 0x6d });
});
}
}
categories:
- id: 1
name: "Beverages"
description: "Soft drinks, coffees, teas, beers, and ales"
picture: \x4c6f72656d20697073756d # Lorem ipsum
For testing, I converted
Lorem ipsumto a bytes array:4c 6f 72 65 6d 20 69 70 73 75 6d.
Does anyone knows how to represent the same in the YAML file, in such way that rs.getBytes("picture") interprets it correctly?
I've tested this with the built-in H2 binary data types as well — because bytea is built-in a PostgreSQL data type, but looks like it works as well in H2, even though I don't see it listed in H2's website.
After searching all over the places I found this issue. Looks like
Database Riderneeds the binary data "converted" with (the popular)Base 64binary-to-text algorithm. I think that makes sense, but I was under the assumption that all configuration files would store the data, in exactly the same format as the database — but for binary data, they have a small "indirection" layer.Anyway, for my example, I took the
Lorem ipsumstring as my data source. I then converted this string to a bytes array:new byte[] { 76, 111, 114, 101, 109, 32, 105, 112, 115, 117, 109 }, and finally encoded the resultant bytes array intoBase64:TG9yZW0gaXBzdW0=...and that's how it should be stored in the configuration files forDatabase Rider.