-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for the transforming pre enforcers
Signed-off-by: Yannic Klem <Yannic.Klem@bosch.io>
- Loading branch information
Showing
4 changed files
with
190 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...a/org/eclipse/ditto/policies/service/enforcement/ModifyToCreatePolicyTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (c) 2022 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.policies.service.enforcement; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.eclipse.ditto.base.model.headers.DittoHeaders; | ||
import org.eclipse.ditto.base.model.signals.Signal; | ||
import org.eclipse.ditto.policies.enforcement.pre.ExistenceChecker; | ||
import org.eclipse.ditto.policies.model.Policy; | ||
import org.eclipse.ditto.policies.model.PolicyId; | ||
import org.eclipse.ditto.policies.model.signals.commands.modify.CreatePolicy; | ||
import org.eclipse.ditto.policies.model.signals.commands.modify.ModifyPolicy; | ||
import org.eclipse.ditto.policies.model.signals.commands.query.RetrievePolicy; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public final class ModifyToCreatePolicyTransformerTest { | ||
|
||
@Mock | ||
public ExistenceChecker existenceChecker; | ||
private ModifyToCreatePolicyTransformer underTest; | ||
|
||
@Before | ||
public void setup() { | ||
underTest = new ModifyToCreatePolicyTransformer(existenceChecker); | ||
} | ||
|
||
@Test | ||
public void modifyPolicyStaysModifyPolicyWhenAlreadyExisting() { | ||
final var policyId = PolicyId.generateRandom(); | ||
final var modifyPolicy = | ||
ModifyPolicy.of(policyId, Policy.newBuilder(policyId).build(), DittoHeaders.of(Map.of("foo", "bar"))); | ||
when(existenceChecker.checkExistence(modifyPolicy)).thenReturn(CompletableFuture.completedStage(true)); | ||
|
||
final Signal<?> result = underTest.apply(modifyPolicy).toCompletableFuture().join(); | ||
|
||
assertThat(result).isSameAs(modifyPolicy); | ||
verify(existenceChecker).checkExistence(modifyPolicy); | ||
} | ||
|
||
@Test | ||
public void modifyPolicyBecomesCreatePolicyWhenNotYetExisting() { | ||
final var policyId = PolicyId.generateRandom(); | ||
final var modifyPolicy = | ||
ModifyPolicy.of(policyId, Policy.newBuilder(policyId).build(), DittoHeaders.of(Map.of("foo", "bar"))); | ||
when(existenceChecker.checkExistence(modifyPolicy)).thenReturn(CompletableFuture.completedStage(false)); | ||
|
||
final Signal<?> result = underTest.apply(modifyPolicy).toCompletableFuture().join(); | ||
|
||
assertThat(result).isInstanceOf(CreatePolicy.class); | ||
final CreatePolicy createPolicy = (CreatePolicy) result; | ||
assertThat(createPolicy.getEntityId().toString()).isEqualTo(policyId.toString()); | ||
assertThat(createPolicy.getPolicy()).isSameAs(modifyPolicy.getPolicy()); | ||
assertThat(createPolicy.getDittoHeaders()).isSameAs(modifyPolicy.getDittoHeaders()); | ||
verify(existenceChecker).checkExistence(modifyPolicy); | ||
} | ||
|
||
@Test | ||
public void otherCommandsThanModifyPolicyAreJustPassedThrough() { | ||
final var policyId = PolicyId.generateRandom(); | ||
final var retrievePolicy = RetrievePolicy.of(policyId, DittoHeaders.of(Map.of("foo", "bar"))); | ||
|
||
final Signal<?> result = underTest.apply(retrievePolicy).toCompletableFuture().join(); | ||
|
||
assertThat(result).isSameAs(retrievePolicy); | ||
verifyNoInteractions(existenceChecker); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...java/org/eclipse/ditto/things/service/enforcement/ModifyToCreateThingTransformerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright (c) 2022 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.ditto.things.service.enforcement; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.Map; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import org.eclipse.ditto.base.model.headers.DittoHeaders; | ||
import org.eclipse.ditto.base.model.signals.Signal; | ||
import org.eclipse.ditto.policies.enforcement.pre.ExistenceChecker; | ||
import org.eclipse.ditto.things.model.Thing; | ||
import org.eclipse.ditto.things.model.ThingId; | ||
import org.eclipse.ditto.things.model.signals.commands.modify.CreateThing; | ||
import org.eclipse.ditto.things.model.signals.commands.modify.ModifyThing; | ||
import org.eclipse.ditto.things.model.signals.commands.query.RetrieveThing; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
@RunWith(MockitoJUnitRunner.class) | ||
public final class ModifyToCreateThingTransformerTest { | ||
|
||
@Mock | ||
public ExistenceChecker existenceChecker; | ||
private ModifyToCreateThingTransformer underTest; | ||
|
||
@Before | ||
public void setup() { | ||
underTest = new ModifyToCreateThingTransformer(existenceChecker); | ||
} | ||
|
||
@Test | ||
public void modifyThingStaysModifyPolicyWhenAlreadyExisting() { | ||
final var thingId = ThingId.generateRandom(); | ||
final var modifyThing = ModifyThing.of(thingId, Thing.newBuilder().setId(thingId).build(), null, null, | ||
DittoHeaders.of(Map.of("foo", "bar"))); | ||
when(existenceChecker.checkExistence(modifyThing)).thenReturn(CompletableFuture.completedStage(true)); | ||
|
||
final Signal<?> result = underTest.apply(modifyThing).toCompletableFuture().join(); | ||
|
||
assertThat(result).isSameAs(modifyThing); | ||
verify(existenceChecker).checkExistence(modifyThing); | ||
} | ||
|
||
@Test | ||
public void modifyThingBecomesCreatePolicyWhenNotYetExisting() { | ||
final var thingId = ThingId.generateRandom(); | ||
final var modifyThing = ModifyThing.of(thingId, Thing.newBuilder().setId(thingId).build(), null, null, | ||
DittoHeaders.of(Map.of("foo", "bar"))); | ||
when(existenceChecker.checkExistence(modifyThing)).thenReturn(CompletableFuture.completedStage(false)); | ||
|
||
final Signal<?> result = underTest.apply(modifyThing).toCompletableFuture().join(); | ||
|
||
assertThat(result).isInstanceOf(CreateThing.class); | ||
final CreateThing createThing = (CreateThing) result; | ||
assertThat(createThing.getEntityId().toString()).isEqualTo(thingId.toString()); | ||
assertThat(createThing.getThing()).isEqualTo(modifyThing.getThing()); | ||
assertThat(createThing.getDittoHeaders()).isSameAs(modifyThing.getDittoHeaders()); | ||
verify(existenceChecker).checkExistence(modifyThing); | ||
} | ||
|
||
@Test | ||
public void otherCommandsThanModifyThingAreJustPassedThrough() { | ||
final var thingId = ThingId.generateRandom(); | ||
final var retrieveThing = RetrieveThing.of(thingId, DittoHeaders.of(Map.of("foo", "bar"))); | ||
|
||
final Signal<?> result = underTest.apply(retrieveThing).toCompletableFuture().join(); | ||
|
||
assertThat(result).isSameAs(retrieveThing); | ||
verifyNoInteractions(existenceChecker); | ||
} | ||
|
||
} |