FFFF Workflows enhancements by sguilhen · Pull Request #43716 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.keycloak.representations.workflows;

import static java.util.Optional.ofNullable;
import static org.keycloak.representations.workflows.WorkflowConstants.CONFIG_CONCURRENCY;
import static org.keycloak.representations.workflows.WorkflowConstants.CONFIG_CONDITIONS;
import static org.keycloak.representations.workflows.WorkflowConstants.CONFIG_ENABLED;
import static org.keycloak.representations.workflows.WorkflowConstants.CONFIG_IF;
import static org.keycloak.representations.workflows.WorkflowConstants.CONFIG_NAME;
Expand All @@ -13,14 +13,12 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
Expand All @@ -37,9 +35,6 @@ public static Builder create() {

private List<WorkflowStepRepresentation> steps;

@JsonProperty(CONFIG_IF)
private List<WorkflowConditionRepresentation> conditions;

private WorkflowStateRepresentation state;

@JsonProperty(CONFIG_CONCURRENCY)
Expand All @@ -49,28 +44,17 @@ public WorkflowRepresentation() {
super(null, null, null);
}

public WorkflowRepresentation(String id, String workflow, MultivaluedHashMap<String, String> config, List<WorkflowConditionRepresentation> conditions, List<WorkflowStepRepresentation> steps) {
public WorkflowRepresentation(String id, String workflow, MultivaluedHashMap<String, String> config, List<WorkflowStepRepresentation> steps) {
super(id, workflow, config);
this.conditions = conditions;
this.steps = steps;
}

public <T> T getOn() {
return getConfigValuesOrSingle(CONFIG_ON_EVENT);
}

public void setOn(String... events) {
setConfigValue(CONFIG_ON_EVENT, Arrays.asList(events));
public String getOn() {
return getConfigValue(CONFIG_ON_EVENT, String.class);
}

@JsonFormat(with = JsonFormat.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY)
public void setOn(List<String> events) {
setConfigValue(CONFIG_ON_EVENT, events);
}

@JsonIgnore
public List<String> getOnValues() {
return ofNullable(getConfigValues(CONFIG_ON_EVENT)).orElse(Collections.emptyList());
public void setOn(String eventConditions) {
setConfigValue(CONFIG_ON_EVENT, eventConditions);
}

public String getName() {
Expand All @@ -89,12 +73,13 @@ public void setEnabled(Boolean enabled) {
setConfigValue(CONFIG_ENABLED, enabled);
}

public void setConditions(List<WorkflowConditionRepresentation> conditions) {
this.conditions = conditions;
@JsonProperty(CONFIG_IF)
public String getConditions() {
return getConfigValue(CONFIG_CONDITIONS, String.class);
}

public List<WorkflowConditionRepresentation> getConditions() {
return conditions;
public void setConditions(String conditions) {
setConfigValue(CONFIG_CONDITIONS, conditions);
}

public void setSteps(List<WorkflowStepRepresentation> steps) {
Expand Down Expand Up @@ -145,7 +130,7 @@ public boolean equals(Object obj) {
WorkflowRepresentation that = (WorkflowRepresentation) obj;
// TODO: include state in comparison?
return Objects.equals(getUses(), that.getUses()) && Objects.equals(getConfig(), that.getConfig())
&& Objects.equals(getConditions(), that.getConditions()) && Objects.equals(getSteps(), that.getSteps());
&& Objects.equals(getSteps(), that.getSteps());
}

public static class Builder {
Expand Down Expand Up @@ -175,8 +160,8 @@ public Builder onEvent(String operation) {
return this;
}

public Builder onConditions(WorkflowConditionRepresentation... condition) {
representation.setConditions(Arrays.asList(condition));
public Builder onCondition(String condition) {
representation.setConditions(condition);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.time.Duration;
Expand All @@ -24,29 +23,12 @@ public void testFullDefinition() throws IOException {
expected.setUses("my-provider");
expected.setName("my-name");
expected.setOn("event");
expected.setConditions("condition-1(v1) AND (condition-2(key1:v1) OR condition-3(key2:v2,v3))");
expected.setSteps(null);
expected.setConditions(null);
expected.setEnabled(true);

expected.setConcurrency(new WorkflowConcurrencyRepresentation(true));

expected.setConditions(Arrays.asList(
WorkflowConditionRepresentation.create()
.of("condition-1")
.withConfig("key1", "v1")
.withConfig("key2", "v1", "v2")
.build(),
WorkflowConditionRepresentation.create()
.of("condition-2")
.withConfig("key1", "v1")
.withConfig("key2", "v1", "v2")
.build(),
WorkflowConditionRepresentation.create()
.of("condition-1")
.withConfig("key1", "v1")
.withConfig("key2", "v1", "v2", "v3")
.build()));

expected.setSteps(Arrays.asList(
WorkflowStepRepresentation.create()
.of("step-1")
Expand All @@ -71,26 +53,12 @@ public void testFullDefinition() throws IOException {

assertEquals(expected.getId(), actual.getId());
assertEquals(expected.getUses(), actual.getUses());
assertTrue(actual.getOn() instanceof String);
assertEquals(expected.getOn(), (String) actual.getOn());
assertNotNull(actual.getOn());
assertEquals(expected.getOn(), actual.getOn());
assertEquals(expected.getConcurrency(), actual.getConcurrency());
assertEquals(expected.getName(), actual.getName());
assertEquals(expected.getEnabled(), actual.getEnabled());

List<WorkflowConditionRepresentation> actualConditions = actual.getConditions();
assertNotNull(actualConditions);
actualConditions = actualConditions.stream().sorted(Comparator.comparing(WorkflowConditionRepresentation::getUses)).collect(Collectors.toList());
List<WorkflowConditionRepresentation> expectedConditions = expected.getConditions().stream().sorted(Comparator.comparing(WorkflowConditionRepresentation::getUses)).collect(Collectors.toList());

assertEquals(expectedConditions.size(), actualConditions.size());
assertEquals(expectedConditions.get(0).getUses(), actualConditions.get(0).getUses());
assertEquals(expectedConditions.get(0).getConfig().get("key1"), actualConditions.get(0).getConfig().get("key1"));
assertEquals(expectedConditions.get(0).getConfig().get("key2"), actualConditions.get(0).getConfig().get("key2"));
assertEquals(expectedConditions.get(1).getConfig().get("key1"), actualConditions.get(1).getConfig().get("key1"));
assertEquals(expectedConditions.get(1).getConfig().get("key2"), actualConditions.get(1).getConfig().get("key2"));
assertEquals(expectedConditions.get(2).getConfig().get("key1"), actualConditions.get(2).getConfig().get("key1"));
assertEquals(expectedConditions.get(2).getConfig().get("key2"), actualConditions.get(2).getConfig().get("key2"));

assertEquals(expected.getConditions(), actual.getConditions());

List<WorkflowStepRepresentation> actualSteps = actual.getSteps();
assertNotNull(actualSteps);
Expand All @@ -106,30 +74,16 @@ public void testFullDefinition() throws IOException {
System.out.println(json);
}

@Test
public void testOnEventAsArray() throws IOException {
WorkflowRepresentation expected = new WorkflowRepresentation();

expected.setOn("event", "event2");

String json = JsonSerialization.writeValueAsPrettyString(expected);
WorkflowRepresentation actual = JsonSerialization.readValue(json, WorkflowRepresentation.class);
assertTrue(actual.getOn() instanceof List);
assertEquals(Arrays.asList("event", "event2"), actual.getOn());

System.out.println(json);
}

@Test
public void testOnEventAsString() throws IOException {
WorkflowRepresentation expected = new WorkflowRepresentation();

expected.setOn("event");
expected.setOn("event OR other-event");

String json = JsonSerialization.writeValueAsPrettyString(expected);
WorkflowRepresentation actual = JsonSerialization.readValue(json, WorkflowRepresentation.class);
assertTrue(actual.getOn() instanceof String);
assertEquals("event", actual.getOn());
assertNotNull(actual.getOn());
assertEquals("event OR other-event", actual.getOn());

System.out.println(json);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
lexer grammar BooleanConditionLexer;

// --- DEFAULT_MODE (mode 0) ---
OR : 'OR';
AND : 'AND';
NOT : '!';

Identifier : [\p{L}_][\p{L}0-9_/-]*;

LPAREN : '(' ; // For (A OR B) expressions
RPAREN : ')' ; // For (A OR B) expressions

WS : [ \t\r\n]+ -> skip;

// --- PARAM_MODE (mode 1) ---
mode PARAM_MODE;

// 1. Matches the closing ')' and pops the mode
RPAREN_PARAM : ')' -> type(RPAREN), popMode ;

// 2. This rule understands escape characters
ParameterText : ( '\\' . | ~[\\)] )+ ;
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
parser grammar BooleanConditionParser;

options { tokenVocab = BooleanConditionLexer; }

// Parser Rules
evaluator : expression EOF;

expression : expression OR andExpression | andExpression;

andExpression : andExpression AND notExpression | notExpression;

notExpression : '!' notExpression | atom;

atom : LPAREN expression RPAREN // For grouping: (A OR B)
| conditionCall
;

conditionCall
// The 'pushMode' command must reference the Lexer name
: Identifier
{ ((Lexer)_input.getTokenSource()).pushMode(BooleanConditionLexer.PARAM_MODE); }
LPAREN
parameter
RPAREN
| Identifier // The form without parentheses
;

parameter : ParameterText? ;
Loading
0