Skip to content

Commit

Permalink
[eclipse-ditto#890] review: remove unnecessary conditional; test chai…
Browse files Browse the repository at this point in the history
…ned expiry; add TODO.

Signed-off-by: Yufei Cai <yufei.cai@bosch.io>
  • Loading branch information
yufei-cai committed Dec 13, 2020
1 parent cd0ffbd commit 910f7bc
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,7 @@ protected void recoveryCompleted(final RecoveryCompleted event) {
@Override
protected void onEntityModified() {
findEarliestSubjectExpiryTimestamp(entity).ifPresent(earliestSubjectExpiryTimestamp -> {
if (timers().isTimerActive(NEXT_SUBJECT_EXPIRY_TIMER)) {
timers().cancel(NEXT_SUBJECT_EXPIRY_TIMER);
}
timers().cancel(NEXT_SUBJECT_EXPIRY_TIMER);
final Instant earliestExpiry = earliestSubjectExpiryTimestamp.getTimestamp();
final Duration durationBetweenNowAndEarliestExpiry = Duration.between(Instant.now(), earliestExpiry);
if (durationBetweenNowAndEarliestExpiry.isNegative()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.NoSuchElementException;
import java.util.concurrent.TimeUnit;

import org.assertj.core.api.Assertions;
import org.awaitility.Awaitility;
import org.eclipse.ditto.model.base.entity.Revision;
import org.eclipse.ditto.model.base.entity.id.DefaultEntityId;
Expand Down Expand Up @@ -727,7 +728,7 @@ public void createSubjectWithExpiry() {
DittoPolicyAssertions.assertThat(createPolicy1Response.getPolicyCreated().get())
.isEqualEqualToButModified(policy);

// THEN: a SubjectCreated event should be emitted
// THEN: a PolicyCreated event should be emitted
final DistributedPubSubMediator.Publish policyCreatedPublish =
pubSubMediatorTestProbe.expectMsgClass(DistributedPubSubMediator.Publish.class);
assertThat(policyCreatedPublish.msg()).isInstanceOf(PolicyCreated.class);
Expand All @@ -743,26 +744,30 @@ public void createSubjectWithExpiry() {
assertThat(subjectCreatedPublish.msg()).isInstanceOf(SubjectCreated.class);

final long secondsToAdd = 10 - (expiryInstant.getEpochSecond() % 10);
final Instant expectedRoundedExpiryInstant = expiryInstant.plusSeconds(secondsToAdd); // to next 10s rounded up
final Instant expectedRoundedExpiryInstant =
expiryInstant.plusSeconds(secondsToAdd); // to next 10s rounded up
final SubjectExpiry expectedSubjectExpiry = SubjectExpiry.newInstance(expectedRoundedExpiryInstant);
final Subject expectedAdjustedSubjectToAdd = Subject.newInstance(subjectToAdd.getId(),
subjectToAdd.getType(), expectedSubjectExpiry);

// THEN: the subject expiry should be rounded up to the configured "subject-expiry-granularity"
// (10s for this test)
expectMsgEquals(
modifySubjectResponse(policyId, POLICY_LABEL, expectedAdjustedSubjectToAdd, headersMockWithOtherAuth, true));
modifySubjectResponse(policyId, POLICY_LABEL, expectedAdjustedSubjectToAdd,
headersMockWithOtherAuth, true));

final RetrieveSubject retrieveSubject =
RetrieveSubject.of(policyId, POLICY_LABEL, subjectToAdd.getId(), headersMockWithOtherAuth);
final RetrieveSubjectResponse expectedResponse =
retrieveSubjectResponse(policyId, POLICY_LABEL, expectedAdjustedSubjectToAdd, headersMockWithOtherAuth);
retrieveSubjectResponse(policyId, POLICY_LABEL, expectedAdjustedSubjectToAdd,
headersMockWithOtherAuth);
underTest.tell(retrieveSubject, getRef());
expectMsgEquals(expectedResponse);

// THEN: waiting until the expiry interval should emit a SubjectDeleted event
final Duration between = Duration.between(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant(),
expectedRoundedExpiryInstant);
final Duration between =
Duration.between(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant(),
expectedRoundedExpiryInstant);
final long secondsToWaitForSubjectDeletedEvent = between.getSeconds() + 2;
final DistributedPubSubMediator.Publish policySubjectDeleted =
pubSubMediatorTestProbe.expectMsgClass(
Expand All @@ -788,6 +793,67 @@ public void createSubjectWithExpiry() {
};
}

@Test
public void createPolicyWith2SubjectsWithExpiry() {
new TestKit(actorSystem) {{
final Instant expiryInstant = LocalDateTime.now().truncatedTo(ChronoUnit.SECONDS)
.plus(2, ChronoUnit.SECONDS)
.atZone(ZoneId.systemDefault()).toInstant();
final SubjectExpiry subjectExpiry = SubjectExpiry.newInstance(expiryInstant);
final Subject subject1 =
Subject.newInstance(SubjectId.newInstance(SubjectIssuer.GOOGLE, "subject1"),
SubjectType.GENERATED, subjectExpiry);
final Subject subject2 =
Subject.newInstance(SubjectId.newInstance(SubjectIssuer.GOOGLE, "subject2"),
SubjectType.GENERATED, subjectExpiry);
final Policy policy = PoliciesModelFactory.newPolicyBuilder(PolicyId.of("policy:id"))
.forLabel(POLICY_LABEL)
.setSubjects(Subjects.newInstance(subject1, subject2))
.setResources(POLICY_RESOURCES_ALL)
.build();

final ActorRef underTest = createPersistenceActorFor(this, policy);

// GIVEN: a Policy is created with 2 subjects having an "expiry" date
final CreatePolicy createPolicyCommand = CreatePolicy.of(policy, dittoHeadersV2);
underTest.tell(createPolicyCommand, getRef());
final CreatePolicyResponse createPolicy1Response = expectMsgClass(CreatePolicyResponse.class);

// THEN: a PolicyCreated event should be emitted
final DistributedPubSubMediator.Publish policyCreatedPublish =
pubSubMediatorTestProbe.expectMsgClass(DistributedPubSubMediator.Publish.class);
assertThat(policyCreatedPublish.msg()).isInstanceOf(PolicyCreated.class);


// THEN: subject1 is deleted after expiry
final long secondsToAdd = 10 - (expiryInstant.getEpochSecond() % 10);
final Instant expectedRoundedExpiryInstant = expiryInstant.plusSeconds(secondsToAdd);
final Duration between =
Duration.between(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant(),
expectedRoundedExpiryInstant);
final long secondsToWaitForSubjectDeletedEvent = between.getSeconds() + 2;
final DistributedPubSubMediator.Publish subject1Deleted =
pubSubMediatorTestProbe.expectMsgClass(
FiniteDuration.apply(secondsToWaitForSubjectDeletedEvent, TimeUnit.SECONDS),
DistributedPubSubMediator.Publish.class);
assertThat(subject1Deleted.msg()).isInstanceOf(SubjectDeleted.class);
assertThat(((SubjectDeleted) subject1Deleted.msg()).getSubjectId()).isEqualTo(subject1.getId());
assertThat(pubSubMediatorTestProbe.expectMsgClass(DistributedPubSubMediator.Publish.class).msg())
.isInstanceOf(PolicyTag.class);

// THEN: subject2 is deleted immediately
final DistributedPubSubMediator.Publish subject2Deleted =
pubSubMediatorTestProbe.expectMsgClass(DistributedPubSubMediator.Publish.class);
assertThat(subject2Deleted.msg()).isInstanceOf(SubjectDeleted.class);
assertThat(((SubjectDeleted) subject2Deleted.msg()).getSubjectId()).isEqualTo(subject2.getId());

// THEN: the policy has no subjects. (TODO: rationalize or change this behavior.)
underTest.tell(RetrievePolicy.of(policy.getEntityId().orElseThrow(), DittoHeaders.empty()), getRef());
final RetrievePolicyResponse response = expectMsgClass(RetrievePolicyResponse.class);
Assertions.assertThat(response.getPolicy().getEntryFor(POLICY_LABEL).orElseThrow().getSubjects()).isEmpty();
}};
}

@Test
public void recoverPolicyCreated() {
new TestKit(actorSystem) {
Expand Down Expand Up @@ -968,7 +1034,8 @@ public void ensureSubjectExpiryIsCleanedUpAfterRecovery() {
.build();

final long secondsToAdd = 10 - (expiryInstant.getEpochSecond() % 10);
final Instant expectedRoundedExpiryInstant = expiryInstant.plusSeconds(secondsToAdd); // to next 10s rounded up
final Instant expectedRoundedExpiryInstant =
expiryInstant.plusSeconds(secondsToAdd); // to next 10s rounded up
final SubjectExpiry expectedSubjectExpiry = SubjectExpiry.newInstance(expectedRoundedExpiryInstant);
final Subject expectedAdjustedSubjectToAdd = Subject.newInstance(expiringSubject.getId(),
expiringSubject.getType(), expectedSubjectExpiry);
Expand Down Expand Up @@ -1007,8 +1074,9 @@ public void ensureSubjectExpiryIsCleanedUpAfterRecovery() {
assertThat(getLastSender()).isEqualTo(underTestRecovered);

// THEN: waiting until the expiry interval should emit a SubjectDeleted event
final Duration between = Duration.between(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant(),
expectedRoundedExpiryInstant);
final Duration between =
Duration.between(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant(),
expectedRoundedExpiryInstant);
final long secondsToWaitForSubjectDeletedEvent = between.getSeconds() + 2;
final DistributedPubSubMediator.Publish policySubjectDeleted =
pubSubMediatorTestProbe.expectMsgClass(
Expand Down

0 comments on commit 910f7bc

Please sign in to comment.