@Test public void basicLifecycle() throws Exception { final String id = "basicLifecycle"; EntityManager<SampleEntity, String> entityPersister = new DefaultEntityManager.Builder<SampleEntity, String>() .withEntityType(SampleEntity.class) .withKeyspace(keyspace) .withColumnFamily(CF_SAMPLE_ENTITY) .build(); SampleEntity origEntity = createSampleEntity(id); entityPersister.put(origEntity); // use low-level astyanax API to confirm the write { ColumnList<String> cl = keyspace.prepareQuery(CF_SAMPLE_ENTITY).getKey(id).execute().getResult(); // 19 simple columns // 2 one-level-deep nested columns from Bar // 2 two-level-deep nested columns from BarBar Assert.assertEquals(23, cl.size()); // simple columns Assert.assertEquals(origEntity.getString(), cl.getColumnByName("STRING").getStringValue()); Assert.assertArrayEquals( origEntity.getByteArray(), cl.getColumnByName("BYTE_ARRAY").getByteArrayValue()); // nested fields Assert.assertEquals(origEntity.getBar().i, cl.getColumnByName("BAR.i").getIntegerValue()); Assert.assertEquals(origEntity.getBar().s, cl.getColumnByName("BAR.s").getStringValue()); Assert.assertEquals( origEntity.getBar().barbar.i, cl.getColumnByName("BAR.barbar.i").getIntegerValue()); Assert.assertEquals( origEntity.getBar().barbar.s, cl.getColumnByName("BAR.barbar.s").getStringValue()); } SampleEntity getEntity = entityPersister.get(id); Assert.assertEquals(origEntity, getEntity); entityPersister.delete(id); // use low-level astyanax API to confirm the delete { ColumnList<String> cl = keyspace.prepareQuery(CF_SAMPLE_ENTITY).getKey(id).execute().getResult(); Assert.assertEquals(0, cl.size()); } }
private SampleEntity createSampleEntity(String id) { Random prng = new Random(); SampleEntity entity = new SampleEntity(); entity.setId(id); entity.setBooleanPrimitive(prng.nextBoolean()); entity.setBooleanObject(prng.nextBoolean()); entity.setBytePrimitive((byte) prng.nextInt(Byte.MAX_VALUE)); entity.setByteObject((byte) prng.nextInt(Byte.MAX_VALUE)); entity.setShortPrimitive((short) prng.nextInt(Short.MAX_VALUE)); entity.setShortObject((short) prng.nextInt(Short.MAX_VALUE)); entity.setIntPrimitive(prng.nextInt()); entity.setIntObject(prng.nextInt()); entity.setLongPrimitive(prng.nextLong()); entity.setLongObject(prng.nextLong()); entity.setFloatPrimitive(prng.nextFloat()); entity.setFloatObject(prng.nextFloat()); entity.setDoublePrimitive(prng.nextDouble()); entity.setDoubleObject(prng.nextDouble()); entity.setString(RandomStringUtils.randomAlphanumeric(16)); entity.setByteArray(RandomStringUtils.randomAlphanumeric(16).getBytes(Charsets.UTF_8)); entity.setDate(new Date()); entity.setUuid(TimeUUIDUtils.getUniqueTimeUUIDinMicros()); Foo foo = new Foo(prng.nextInt(), RandomStringUtils.randomAlphanumeric(4)); entity.setFoo(foo); BarBar barbar = new BarBar(); barbar.i = prng.nextInt(); barbar.s = RandomStringUtils.randomAlphanumeric(4); Bar bar = new Bar(); bar.i = prng.nextInt(); bar.s = RandomStringUtils.randomAlphanumeric(4); bar.barbar = barbar; entity.setBar(bar); return entity; }