@Override public String getId() { return policy.getId().orElseThrow(() -> new NullPointerException("The Policy has no ID!")); }
@Override public String getId() { return policy.getId().orElseThrow(() -> new NullPointerException("The Policy has no ID!")); }
private PolicyCreated(final Policy policy, final long revision, @Nullable final Instant timestamp, final DittoHeaders dittoHeaders) { super(TYPE, checkNotNull(policy, PROPERTY_NAME_POLICY).getId().orElse(null), revision, timestamp, dittoHeaders); this.policy = policy; }
private PolicyModified(final Policy policy, final long revision, @Nullable final Instant timestamp, final DittoHeaders dittoHeaders) { super(TYPE, checkNotNull(policy, "Policy").getId().orElse(null), revision, timestamp, dittoHeaders); this.policy = policy; }
private PolicyCreated(final Policy policy, final long revision, @Nullable final Instant timestamp, final DittoHeaders dittoHeaders) { super(TYPE, checkNotNull(policy, PROPERTY_NAME_POLICY).getId().orElse(null), revision, timestamp, dittoHeaders); this.policy = policy; }
private PolicyModified(final Policy policy, final long revision, @Nullable final Instant timestamp, final DittoHeaders dittoHeaders) { super(TYPE, checkNotNull(policy, "Policy").getId().orElse(null), revision, timestamp, dittoHeaders); this.policy = policy; }
private void handleSyncPolicyResponse(final Thing syncedThing, final SudoRetrievePolicyResponse response) { log.debug("Retrieved policy response='{}' for thing ID='{}' and policyId='{}' (attempt={}).", response, thingId, policyId, syncAttempts); log.debug("Policy from retrieved policy response is: {}", response.getPolicy()); final Policy policy = response.getPolicy(); final boolean isExpectedPolicyId = policy.getId() .filter(policyId::equals) .isPresent(); if (isExpectedPolicyId) { policyRevision = policy.getRevision().map(PolicyRevision::toLong).orElse(UNKNOWN_REVISION); final Enforcer thePolicyEnforcer = PolicyEnforcers.defaultEvaluator(policy); this.policyEnforcer = thePolicyEnforcer; updateSearchIndexWithPolicy(syncedThing, thePolicyEnforcer); } else { log.warning("Received policy ID <{0}> is not expected ID <{1}>!", policy.getId(), policyId); } }
private void handleSyncPolicyResponse(final Thing syncedThing, final SudoRetrievePolicyResponse response) { log.debug("Retrieved policy response='{}' for thing ID='{}' and policyId='{}' (attempt={}).", response, thingId, policyId, syncAttempts); log.debug("Policy from retrieved policy response is: {}", response.getPolicy()); final Policy policy = response.getPolicy(); final boolean isExpectedPolicyId = policy.getId() .filter(policyId::equals) .isPresent(); if (isExpectedPolicyId) { policyRevision = policy.getRevision().map(PolicyRevision::toLong).orElse(UNKNOWN_REVISION); final Enforcer thePolicyEnforcer = PolicyEnforcers.defaultEvaluator(policy); this.policyEnforcer = thePolicyEnforcer; updateSearchIndexWithPolicy(syncedThing, thePolicyEnforcer); } else { log.warning("Received policy ID <{0}> is not expected ID <{1}>!", policy.getId(), policyId); } }
public PolicyAssert hasId(final String expectedIdentifier) { isNotNull(); final String actualId = actual.getId().orElse(null); assertThat(actualId).isEqualTo(expectedIdentifier) // .overridingErrorMessage("Expected Policy identifier to be \n<%s> but was \n<%s>", expectedIdentifier, actualId); return this; }
/** * Returns a Command for creating a new Policy which is passed as argument. * * @param policy the Policy to create. * @param dittoHeaders the headers of the command. * @return the command. * @throws NullPointerException if any argument is {@code null}. * @throws PolicyIdInvalidException if the {@link Policy}'s ID is not valid. */ public static CreatePolicy of(final Policy policy, final DittoHeaders dittoHeaders) { Objects.requireNonNull(policy, "The Policy must not be null!"); if (!policy.getId().isPresent()) { throw PolicyIdInvalidException.newBuilder("") .message("Policy ID must be present in 'CreatePolicy' payload") .dittoHeaders(dittoHeaders) .build(); } return new CreatePolicy(policy, dittoHeaders); }
/** * Returns a Command for creating a new Policy which is passed as argument. * * @param policy the Policy to create. * @param dittoHeaders the headers of the command. * @return the command. * @throws NullPointerException if any argument is {@code null}. * @throws PolicyIdInvalidException if the {@link Policy}'s ID is not valid. */ public static CreatePolicy of(final Policy policy, final DittoHeaders dittoHeaders) { Objects.requireNonNull(policy, "The Policy must not be null!"); if (!policy.getId().isPresent()) { throw PolicyIdInvalidException.newBuilder("") .message("Policy ID must be present in 'CreatePolicy' payload") .dittoHeaders(dittoHeaders) .build(); } return new CreatePolicy(policy, dittoHeaders); }
/** * Returns a new builder for a {@code Policy} based on the given {@code existingPolicy}. * * @param existingPolicy the existing Policy to instantiate the builder with. * @return the new builder. * @throws NullPointerException if {@code existingPolicy} is {@code null}. * @throws PolicyIdInvalidException if {@code policyId} did not comply to {@link Policy#ID_REGEX}. */ public static PolicyBuilder of(final Policy existingPolicy) { checkNotNull(existingPolicy, "existing Policy"); @SuppressWarnings("ConstantConditions") final ImmutablePolicyBuilder result = new ImmutablePolicyBuilder() .setLifecycle(existingPolicy.getLifecycle().orElse(null)) .setRevision(existingPolicy.getRevision().orElse(null)) .setModified(existingPolicy.getModified().orElse(null)); existingPolicy.getId().ifPresent(result::setId); existingPolicy.forEach(result::set); return result; }
/** * Creates a command for modifying a {@code Policy} from a JSON object. * * @param jsonObject the JSON object of which the command is to be created. * @param dittoHeaders the headers of the command. * @return the command. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. * @throws org.eclipse.ditto.json.JsonMissingFieldException if {@code jsonObject} did not contain a field for {@link * PolicyModifyCommand.JsonFields#JSON_POLICY_ID} or {@link #JSON_POLICY}. */ public static ModifyPolicy fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<ModifyPolicy>(TYPE, jsonObject).deserialize(() -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy policy = PoliciesModelFactory.newPolicy(policyJsonObject); final Optional<String> optionalPolicyId = jsonObject.getValue(PolicyModifyCommand.JsonFields .JSON_POLICY_ID); final String policyId = optionalPolicyId.orElseGet(() -> policy.getId().orElseThrow(() -> new JsonMissingFieldException(PolicyModifyCommand.JsonFields.JSON_POLICY_ID) )); return of(policyId, policy, dittoHeaders); }); }
/** * Creates a command for modifying a {@code Policy} from a JSON object. * * @param jsonObject the JSON object of which the command is to be created. * @param dittoHeaders the headers of the command. * @return the command. * @throws NullPointerException if {@code jsonObject} is {@code null}. * @throws org.eclipse.ditto.json.JsonParseException if the passed in {@code jsonObject} was not in the expected * format. * @throws org.eclipse.ditto.json.JsonMissingFieldException if {@code jsonObject} did not contain a field for {@link * PolicyModifyCommand.JsonFields#JSON_POLICY_ID} or {@link #JSON_POLICY}. */ public static ModifyPolicy fromJson(final JsonObject jsonObject, final DittoHeaders dittoHeaders) { return new CommandJsonDeserializer<ModifyPolicy>(TYPE, jsonObject).deserialize(() -> { final JsonObject policyJsonObject = jsonObject.getValueOrThrow(JSON_POLICY); final Policy policy = PoliciesModelFactory.newPolicy(policyJsonObject); final Optional<String> optionalPolicyId = jsonObject.getValue(PolicyModifyCommand.JsonFields .JSON_POLICY_ID); final String policyId = optionalPolicyId.orElseGet(() -> policy.getId().orElseThrow(() -> new JsonMissingFieldException(PolicyModifyCommand.JsonFields.JSON_POLICY_ID) )); return of(policyId, policy, dittoHeaders); }); }
public PolicyAssert isEqualEqualToButModified(final Policy expected) { assertThat(expected).isNotNull(); assertThat(actual).isNotNull(); assertThat(actual.getModified()).isPresent(); assertThat(actual.getId()).isEqualTo(expected.getId()); assertThat(actual.getEntriesSet()).isEqualTo(expected.getEntriesSet()); return this; }