Skip to content

Commit 311eb86

Browse files
authored
fix: autogenerate id if null (#5884)
1 parent 470bb4d commit 311eb86

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

spi/control-plane-spi/src/main/java/org/eclipse/edc/policy/cel/model/CelExpression.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.HashSet;
2020
import java.util.Objects;
2121
import java.util.Set;
22+
import java.util.UUID;
2223

2324
import static org.eclipse.edc.spi.constants.CoreConstants.EDC_NAMESPACE;
2425

@@ -126,6 +127,9 @@ public Builder updatedAt(Long updatedAt) {
126127
@Override
127128
public CelExpression build() {
128129
super.build();
130+
if (entity.getId() == null) {
131+
id(UUID.randomUUID().toString());
132+
}
129133
Objects.requireNonNull(entity.leftOperand, "CelExpression leftOperand cannot be null");
130134
Objects.requireNonNull(entity.expression, "CelExpression expression cannot be null");
131135
Objects.requireNonNull(entity.description, "CelExpression description cannot be null");
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (c) 2026 Metaform Systems, Inc.
3+
*
4+
* This program and the accompanying materials are made available under the
5+
* terms of the Apache License, Version 2.0 which is available at
6+
* https://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* SPDX-License-Identifier: Apache-2.0
9+
*
10+
* Contributors:
11+
* Metaform Systems, Inc. - initial API and implementation
12+
*
13+
*/
14+
15+
package org.eclipse.edc.policy.cel.model;
16+
17+
import org.junit.jupiter.api.Test;
18+
19+
import java.util.UUID;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.assertThatCode;
23+
24+
class CelExpressionTest {
25+
26+
@Test
27+
void build_whenIdNotProvided_shouldGenerateUuid() {
28+
var expression = CelExpression.Builder.newInstance()
29+
.leftOperand("leftOperand")
30+
.expression("true")
31+
.description("description")
32+
.build();
33+
34+
assertThat(expression.getId()).isNotNull();
35+
assertThatCode(() -> UUID.fromString(expression.getId())).doesNotThrowAnyException();
36+
}
37+
38+
@Test
39+
void build_whenIdProvided_shouldKeepIt() {
40+
var expression = CelExpression.Builder.newInstance()
41+
.id("my-id")
42+
.leftOperand("leftOperand")
43+
.expression("true")
44+
.description("description")
45+
.build();
46+
47+
assertThat(expression.getId()).isEqualTo("my-id");
48+
}
49+
50+
@Test
51+
void build_whenBuiltTwice_shouldGenerateDifferentIds() {
52+
var first = CelExpression.Builder.newInstance()
53+
.leftOperand("leftOperand")
54+
.expression("true")
55+
.description("description")
56+
.build();
57+
58+
var second = CelExpression.Builder.newInstance()
59+
.leftOperand("leftOperand")
60+
.expression("true")
61+
.description("description")
62+
.build();
63+
64+
assertThat(first.getId()).isNotEqualTo(second.getId());
65+
}
66+
}

0 commit comments

Comments
 (0)