/** * Indicates whether this Policy has the given lifecycle. * * @param lifecycle the lifecycle to be checked for. * @return {@code true} if this Policy has {@code lifecycle} as its lifecycle, {@code false} else. */ default boolean hasLifecycle(@Nullable final PolicyLifecycle lifecycle) { return getLifecycle() .filter(actualLifecycle -> Objects.equals(actualLifecycle, lifecycle)) .isPresent(); }
public PolicyAssert hasNoLifecycle() { isNotNull(); final Optional<PolicyLifecycle> actualLifecycleOptional = actual.getLifecycle(); assertThat(actualLifecycleOptional.isPresent()) // .overridingErrorMessage("Expected Policy not to have a lifecycle but it had <%s>", actualLifecycleOptional.orElse(null)) // .isFalse(); return this; }
public PolicyAssert hasLifecycle(final PolicyLifecycle expectedLifecycle) { isNotNull(); final Optional<PolicyLifecycle> lifecycleOptional = actual.getLifecycle(); assertThat(lifecycleOptional.isPresent() && Objects.equals(lifecycleOptional.get(), expectedLifecycle)) // .overridingErrorMessage("Expected Policy lifecycle to have lifecycle \n<%s> but it had \n<%s>", expectedLifecycle, lifecycleOptional.orElse(null)) // .isTrue(); return this; }
/** * 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; }
@Override protected void doApply(final CreatePolicy command) { // Policy not yet created - do so .. final Policy newPolicy = command.getPolicy(); final PolicyBuilder newPolicyBuilder = PoliciesModelFactory.newPolicyBuilder(newPolicy); final DittoHeaders dittoHeaders = command.getDittoHeaders(); if (!newPolicy.getLifecycle().isPresent()) { newPolicyBuilder.setLifecycle(PolicyLifecycle.ACTIVE); } final Policy newPolicyWithLifecycle = newPolicyBuilder.build(); final PoliciesValidator validator = PoliciesValidator.newInstance(newPolicyWithLifecycle); if (validator.isValid()) { final PolicyCreated policyCreated = PolicyCreated.of(newPolicyWithLifecycle, getNextRevision(), getEventTimestamp(), dittoHeaders); processEvent(policyCreated, event -> { final CreatePolicyResponse response = CreatePolicyResponse.of(policyId, PolicyPersistenceActor.this.policy, dittoHeaders); sendSuccessResponse(command, response); log.debug("Created new Policy with ID <{}>.", policyId); becomePolicyCreatedHandler(); }); } else { policyInvalid(validator.getReason().orElse(null), dittoHeaders); } }
@Override protected void doApply(final CreatePolicy command) { // Policy not yet created - do so .. final Policy newPolicy = command.getPolicy(); final PolicyBuilder newPolicyBuilder = PoliciesModelFactory.newPolicyBuilder(newPolicy); final DittoHeaders dittoHeaders = command.getDittoHeaders(); if (!newPolicy.getLifecycle().isPresent()) { newPolicyBuilder.setLifecycle(PolicyLifecycle.ACTIVE); } final Policy newPolicyWithLifecycle = newPolicyBuilder.build(); final PoliciesValidator validator = PoliciesValidator.newInstance(newPolicyWithLifecycle); if (validator.isValid()) { final PolicyCreated policyCreated = PolicyCreated.of(newPolicyWithLifecycle, getNextRevision(), getEventTimestamp(), dittoHeaders); processEvent(policyCreated, event -> { final CreatePolicyResponse response = CreatePolicyResponse.of(policyId, PolicyPersistenceActor.this.policy, dittoHeaders); sendSuccessResponse(command, response); log.debug("Created new Policy with ID <{}>.", policyId); becomePolicyCreatedHandler(); }); } else { policyInvalid(validator.getReason().orElse(null), dittoHeaders); } }
@Override public Receive createReceiveRecover() { // defines how state is updated during recovery return handlePolicyEvents.orElse(ReceiveBuilder.create() // # Snapshot handling .match(SnapshotOffer.class, ss -> { policy = snapshotAdapter.fromSnapshotStore(ss); lastSnapshotSequenceNr = ss.metadata().sequenceNr(); }) // # Recovery handling .match(RecoveryCompleted.class, rc -> { if (policy != null) { log.debug("Policy <{}> was recovered.", policyId); if (isPolicyActive()) { becomePolicyCreatedHandler(); } else if (isPolicyDeleted()) { becomePolicyDeletedHandler(); } else { log.error("Unknown lifecycle state <{}> for Policy <{}>.", policy.getLifecycle(), policyId); } } }) // # Handle unknown .matchAny(m -> log.warning("Unknown recover message: {}", m)) .build()); }
@Override public Receive createReceiveRecover() { // defines how state is updated during recovery return handlePolicyEvents.orElse(ReceiveBuilder.create() // # Snapshot handling .match(SnapshotOffer.class, ss -> { policy = snapshotAdapter.fromSnapshotStore(ss); lastSnapshotSequenceNr = ss.metadata().sequenceNr(); }) // # Recovery handling .match(RecoveryCompleted.class, rc -> { if (policy != null) { log.debug("Policy <{}> was recovered.", policyId); if (isPolicyActive()) { becomePolicyCreatedHandler(); } else if (isPolicyDeleted()) { becomePolicyDeletedHandler(); } else { log.error("Unknown lifecycle state <{}> for Policy <{}>.", policy.getLifecycle(), policyId); } } }) // # Handle unknown .matchAny(m -> log.warning("Unknown recover message: {}", m)) .build()); }