Skip to content

Commit

Permalink
merge: #9458
Browse files Browse the repository at this point in the history
9458: [Backport stable/8.0] Support deploying multiple DMN files at once r=saig0 a=backport-action

# Description
Backport of #9432 to `stable/8.0`.

relates to camunda/zeebe-process-test#357 #9337 #9115

Co-authored-by: Remco Westerhoud <[email protected]>
Co-authored-by: Philipp Ossler <[email protected]>
  • Loading branch information
3 people authored May 30, 2022
2 parents b360777 + f12e1d0 commit b1cf5c2
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package io.camunda.zeebe.engine.processing.deployment.transform;

import static io.camunda.zeebe.util.buffer.BufferUtil.wrapString;
import static java.util.function.Predicate.not;

import io.camunda.zeebe.dmn.DecisionEngine;
import io.camunda.zeebe.dmn.DecisionEngineFactory;
Expand Down Expand Up @@ -68,8 +69,8 @@ public Either<Failure, Void> transformResource(
return checkForDuplicateIds(resource, parsedDrg, deployment)
.map(
noDuplicates -> {
appendMetadataToDeploymentEvent(resource, parsedDrg, deployment);
writeRecords(deployment, resource);
final var drgKey = appendMetadataToDeploymentEvent(resource, parsedDrg, deployment);
writeRecords(deployment, resource, drgKey);
return null;
});

Expand Down Expand Up @@ -148,7 +149,7 @@ private String findResourceName(
.orElse("<?>");
}

private void appendMetadataToDeploymentEvent(
private long appendMetadataToDeploymentEvent(
final DeploymentResource resource,
final ParsedDecisionRequirementsGraph parsedDrg,
final DeploymentRecord deploymentEvent) {
Expand Down Expand Up @@ -223,6 +224,8 @@ private void appendMetadataToDeploymentEvent(
.setDecisionKey(newDecisionKey.getAsLong())
.setVersion(INITIAL_VERSION));
});

return drgRecord.getDecisionRequirementsKey();
}

private boolean isDuplicate(
Expand All @@ -234,38 +237,44 @@ private boolean isDuplicate(
&& drg.getChecksum().equals(checksum);
}

private void writeRecords(final DeploymentRecord deployment, final DeploymentResource resource) {

for (final DecisionRequirementsMetadataRecord drg : deployment.decisionRequirementsMetadata()) {
if (!drg.isDuplicate()) {
stateWriter.appendFollowUpEvent(
drg.getDecisionRequirementsKey(),
DecisionRequirementsIntent.CREATED,
new DecisionRequirementsRecord()
.setDecisionRequirementsKey(drg.getDecisionRequirementsKey())
.setDecisionRequirementsId(drg.getDecisionRequirementsId())
.setDecisionRequirementsName(drg.getDecisionRequirementsName())
.setDecisionRequirementsVersion(drg.getDecisionRequirementsVersion())
.setNamespace(drg.getNamespace())
.setResourceName(drg.getResourceName())
.setChecksum(drg.getChecksumBuffer())
.setResource(resource.getResourceBuffer()));
}
}
private void writeRecords(
final DeploymentRecord deployment,
final DeploymentResource resource,
final long decisionRequirementsKey) {

for (final DecisionRecord decision : deployment.decisionsMetadata()) {
if (!decision.isDuplicate()) {
stateWriter.appendFollowUpEvent(
decision.getDecisionKey(),
DecisionIntent.CREATED,
new DecisionRecord()
.setDecisionKey(decision.getDecisionKey())
.setDecisionId(decision.getDecisionId())
.setDecisionName(decision.getDecisionName())
.setVersion(decision.getVersion())
.setDecisionRequirementsId(decision.getDecisionRequirementsId())
.setDecisionRequirementsKey(decision.getDecisionRequirementsKey()));
}
}
deployment.decisionRequirementsMetadata().stream()
.filter(drg -> drg.getDecisionRequirementsKey() == decisionRequirementsKey)
.filter(not(DecisionRequirementsMetadataRecord::isDuplicate))
.findFirst()
.ifPresent(
drg ->
stateWriter.appendFollowUpEvent(
drg.getDecisionRequirementsKey(),
DecisionRequirementsIntent.CREATED,
new DecisionRequirementsRecord()
.setDecisionRequirementsKey(drg.getDecisionRequirementsKey())
.setDecisionRequirementsId(drg.getDecisionRequirementsId())
.setDecisionRequirementsName(drg.getDecisionRequirementsName())
.setDecisionRequirementsVersion(drg.getDecisionRequirementsVersion())
.setNamespace(drg.getNamespace())
.setResourceName(drg.getResourceName())
.setChecksum(drg.getChecksumBuffer())
.setResource(resource.getResourceBuffer())));

deployment.decisionsMetadata().stream()
.filter(decision -> decision.getDecisionRequirementsKey() == decisionRequirementsKey)
.filter(not(DecisionRecord::isDuplicate))
.forEach(
decision ->
stateWriter.appendFollowUpEvent(
decision.getDecisionKey(),
DecisionIntent.CREATED,
new DecisionRecord()
.setDecisionKey(decision.getDecisionKey())
.setDecisionId(decision.getDecisionId())
.setDecisionName(decision.getDecisionName())
.setVersion(decision.getVersion())
.setDecisionRequirementsId(decision.getDecisionRequirementsId())
.setDecisionRequirementsKey(decision.getDecisionRequirementsKey())));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public final class DeploymentDmnTest {
private static final String DMN_DECISION_TABLE_V2 = "/dmn/decision-table_v2.dmn";
private static final String DMN_DECISION_TABLE_RENAMED_DRG =
"/dmn/decision-table-with-renamed-drg.dmn";
private static final String DMN_DECISION_TABLE_RENAMED_DRG_AND_DECISION =
"/dmn/decision-table-with-renamed-drg-and-decision.dmn";

private static final String DMN_INVALID_EXPRESSION =
"/dmn/decision-table-with-invalid-expression.dmn";
Expand Down Expand Up @@ -373,6 +375,55 @@ public void shouldDeployDecisionWithTwoDrg() {
tuple("jedi_or_sith", 3, "force_users"));
}

@Test
public void shouldDeployIfMultipleDrgHaveDifferentId() {
// when
final var deploymentEvent =
engine
.deployment()
.withXmlClasspathResource(DMN_DECISION_TABLE)
.withXmlClasspathResource(DMN_DECISION_TABLE_RENAMED_DRG_AND_DECISION)
.deploy();

// then
Assertions.assertThat(deploymentEvent)
.hasIntent(DeploymentIntent.CREATED)
.hasValueType(ValueType.DEPLOYMENT)
.hasRecordType(RecordType.EVENT);

assertThat(deploymentEvent.getValue().getDecisionRequirementsMetadata()).hasSize(2);

final var drgMetadata = deploymentEvent.getValue().getDecisionRequirementsMetadata().get(0);
Assertions.assertThat(drgMetadata).hasDecisionRequirementsId("force_users");
assertThat(drgMetadata.getDecisionRequirementsKey()).isPositive();
assertThat(drgMetadata.getChecksum())
.describedAs("Expect the MD5 checksum of the DMN resource")
.isEqualTo(getChecksum(DMN_DECISION_TABLE));

assertThat(deploymentEvent.getValue().getDecisionsMetadata()).hasSize(2);

final var decisionMetadata = deploymentEvent.getValue().getDecisionsMetadata().get(0);
Assertions.assertThat(decisionMetadata)
.hasDecisionId("jedi_or_sith")
.hasDecisionRequirementsKey(drgMetadata.getDecisionRequirementsKey());
assertThat(decisionMetadata.getDecisionKey()).isPositive();

final var drgMetadata2 = deploymentEvent.getValue().getDecisionRequirementsMetadata().get(1);
Assertions.assertThat(drgMetadata2).hasDecisionRequirementsId("star-wars");
assertThat(drgMetadata2.getDecisionRequirementsKey()).isPositive();
assertThat(drgMetadata2.getChecksum())
.describedAs("Expect the MD5 checksum of the DMN resource")
.isEqualTo(getChecksum(DMN_DECISION_TABLE_RENAMED_DRG_AND_DECISION));

assertThat(deploymentEvent.getValue().getDecisionsMetadata()).hasSize(2);

final var decisionMetadata2 = deploymentEvent.getValue().getDecisionsMetadata().get(1);
Assertions.assertThat(decisionMetadata2)
.hasDecisionId("sith_or_jedi")
.hasDecisionRequirementsKey(drgMetadata2.getDecisionRequirementsKey());
assertThat(decisionMetadata2.getDecisionKey()).isPositive();
}

@Test
public void shouldRejectIfMultipleDrgHaveTheSameId() {
// when
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="https://www.omg.org/spec/DMN/20191111/MODEL/" xmlns:dmndi="https://www.omg.org/spec/DMN/20191111/DMNDI/" xmlns:dc="http://www.omg.org/spec/DMN/20180521/DC/" xmlns:biodi="http://bpmn.io/schema/dmn/biodi/2.0" id="star-wars" name="Star Wars" namespace="http://camunda.org/schema/1.0/dmn" exporter="Camunda Modeler" exporterVersion="5.0.0">
<decision id="sith_or_jedi" name="Jedi or Sith">
<decisionTable id="DecisionTable_14n3bxx">
<input id="Input_1" label="Lightsaber color" biodi:width="192">
<inputExpression id="InputExpression_1" typeRef="string">
<text>lightsaberColor</text>
</inputExpression>
</input>
<output id="Output_1" label="Jedi or Sith" name="jedi_or_sith" typeRef="string" biodi:width="192">
<outputValues id="UnaryTests_0hj346a">
<text>"Jedi","Sith"</text>
</outputValues>
</output>
<rule id="DecisionRule_0zumznl">
<inputEntry id="UnaryTests_0leuxqi">
<text>"blue"</text>
</inputEntry>
<outputEntry id="LiteralExpression_0c9vpz8">
<text>"Jedi"</text>
</outputEntry>
</rule>
<rule id="DecisionRule_1utwb1e">
<inputEntry id="UnaryTests_1v3sd4m">
<text>"green"</text>
</inputEntry>
<outputEntry id="LiteralExpression_0tgh8k1">
<text>"Jedi"</text>
</outputEntry>
</rule>
<rule id="DecisionRule_1bwgcym">
<inputEntry id="UnaryTests_0n1ewm3">
<text>"red"</text>
</inputEntry>
<outputEntry id="LiteralExpression_19xnlkw">
<text>"Sith"</text>
</outputEntry>
</rule>
</decisionTable>
</decision>
<dmndi:DMNDI>
<dmndi:DMNDiagram>
<dmndi:DMNShape dmnElementRef="sith_or_jedi">
<dc:Bounds height="80" width="180" x="160" y="100" />
</dmndi:DMNShape>
</dmndi:DMNDiagram>
</dmndi:DMNDI>
</definitions>

0 comments on commit b1cf5c2

Please sign in to comment.