@Test
  public void duplicateKeyUpsertSameKeyDifferentVersions() throws Exception {
    ImmutableEntity entity = ImmutableEntity.builder().id("e1").version(0).value("v0").build();
    repository.upsert(entity).getUnchecked();

    // first upsert successful (document should be with new version)
    repository.find(repository.criteria().id(entity.id()).version(0))
        .andReplaceFirst(entity.withVersion(1))
        .upsert()
        .getUnchecked();

    try {
      // this should fail because here upsert == insert (document e1 with version 0 doesn't exist)
      repository.find(repository.criteria().id(entity.id()).version(0))
          .andReplaceFirst(entity.withVersion(1))
          .upsert()
          .getUnchecked();

      fail("Should fail with " + DuplicateKeyException.class.getName());
    } catch (Exception e) {
      MongoAsserts.assertDuplicateKeyException(e);
    }
  }
When upsert is requested on different versions but same ID there should be duplicate
key exception thrown by Mongo since there will be an attempt to insert new document (same id
different version)
Based on criteria it is a new document, based on primary key ({@code _id}) it exists already.
mongo/test/org/immutables/mongo/fixture/SimpleReplacerTest.java