EntityCodec implements CollectibleCodec<T> { @Override public T generateIdIfAbsentFromDocument(T document) { if (!documentHasId(document)) { if (!idHandler.generateIdIfAbsent()) throw new Error("id must be assigned, documentClass=" + document.getClass().getCanonicalName()); idHandler.set(document, idGenerator.generate()); } return document; } EntityCodec(Class<T> entityClass, EntityIdHandler<T> idHandler, EntityEncoder<T> encoder, EntityDecoder<T> decoder); @Override T generateIdIfAbsentFromDocument(T document); @Override boolean documentHasId(T document); @Override BsonValue getDocumentId(T document); @Override void encode(BsonWriter writer, T value, EncoderContext encoderContext); @Override T decode(BsonReader reader, DecoderContext decoderContext); @Override Class<T> getEncoderClass();  }
@Test void generateIdIfAbsentFromDocument() { TestEntity entity = new TestEntity(); entityCodec.generateIdIfAbsentFromDocument(entity); assertNotNull(entity.id); }