public GrantDTO ensure(GRN grantee, Capability capability, GRN target, String creatorUsername) {
        final List<GrantDTO> existingGrants = getForTargetAndGrantee(target, grantee);
        if (existingGrants.isEmpty()) {
            return create(grantee, capability, target, creatorUsername);
        }
        // This should never happen
        Preconditions.checkState(existingGrants.size() == 1);

        final GrantDTO grantDTO = existingGrants.get(0);
        // Only upgrade capabilities: VIEW < MANAGE < OWNER
        if (capability.priority() > grantDTO.capability().priority()) {
            final GrantDTO grantUpdate = grantDTO.toBuilder().capability(capability).build();
            return save(grantUpdate);
        }
        return grantDTO;
    }
@Test
    @MongoDBFixtures("grants.json")
    public void ensure() {
        final GRN jane = grnRegistry.parse("grn::::user:jane");
        final GRN stream1 = grnRegistry.parse("grn::::stream:54e3deadbeefdeadbeef0000");
        final GRN newStream = grnRegistry.parse("grn::::stream:54e3deadbeefdeadbeef0888");

        // Matches existing grant. Returns original
        final GrantDTO stream1Grant = dbService.getForTargetAndGrantee(stream1, jane).get(0);
        GrantDTO result = dbService.ensure(jane, Capability.VIEW, stream1, "admin");
        assertThat(result).isEqualTo(stream1Grant);

        // Updates to a higher capability
        result = dbService.ensure(jane, Capability.MANAGE, stream1, "admin");
        assertThat(result.capability()).isEqualTo(Capability.MANAGE);

        // Don't downgrade to a lower capability
        result = dbService.ensure(jane, Capability.VIEW, stream1, "admin");
        assertThat(result.capability()).isEqualTo(Capability.MANAGE);

        // Create a new grant
        assertThat(dbService.ensure(jane, Capability.MANAGE, newStream, "admin")).isNotNull();
        assertThat(dbService.getForTarget(newStream)).satisfies(grantDTOS -> {
            assertThat(grantDTOS.size()).isEqualTo(1);
            assertThat(grantDTOS.get(0).grantee()).isEqualTo(jane);
            assertThat(grantDTOS.get(0).capability()).isEqualTo(Capability.MANAGE);
            assertThat(grantDTOS.get(0).target()).isEqualTo(newStream);
        });
    }