Skip to content

Commit 453c6a8

Browse files
committed
chore(schemapropertyfilter): upd deprecated fun + added tests
1 parent 21f5ee9 commit 453c6a8

File tree

2 files changed

+117
-3
lines changed

2 files changed

+117
-3
lines changed

src/main/java/io/kaoto/camelcatalog/generators/SchemaPropertyFilter.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
public class SchemaPropertyFilter {
1212

1313
private final Map<String, List<String>> processorPropertyBlockList;
14-
14+
/**
15+
* Deleted properties from the schema so that in the construction
16+
* of the forms they don't influence the creation of container
17+
* i.e. adding an extra component or extra container named steps
18+
* as steps include inside the components and EIPs
19+
*/
1520
public SchemaPropertyFilter() {
1621
this.processorPropertyBlockList = Map.ofEntries(
1722
Map.entry("choice", List.of("when", "otherwise")),
@@ -49,7 +54,7 @@ public SchemaPropertyFilter() {
4954
Map.entry("onCompletion", List.of("steps")),
5055
Map.entry("onException", List.of("steps"))
5156
);
52-
}
57+
}
5358

5459
void schemaPropertyFilter(String eipName, ObjectNode node) {
5560
if (!processorPropertyBlockList.containsKey(eipName)) return;
@@ -75,7 +80,7 @@ void filterProperties(String eipName, ObjectNode node) {
7580
if (node.has("properties")) {
7681
var properties = (ObjectNode) node.get("properties");
7782
Set<String> propToRemove = new HashSet<>();
78-
properties.fields().forEachRemaining(entry -> {
83+
properties.properties().forEach(entry -> {
7984
if (processorPropertyBlockList.get(eipName).contains(entry.getKey())) {
8085
propToRemove.add(entry.getKey());
8186
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (C) 2025 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.kaoto.camelcatalog.generators;
17+
18+
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import com.fasterxml.jackson.databind.node.ArrayNode;
20+
import com.fasterxml.jackson.databind.node.ObjectNode;
21+
import org.junit.jupiter.api.BeforeEach;
22+
import org.junit.jupiter.api.Test;
23+
24+
import static org.junit.jupiter.api.Assertions.*;
25+
26+
class SchemaPropertyFilterTest {
27+
private SchemaPropertyFilter schemaPropertyFilter;
28+
private ObjectMapper objectMapper;
29+
30+
@BeforeEach
31+
void setUp() {
32+
schemaPropertyFilter = new SchemaPropertyFilter();
33+
objectMapper = new ObjectMapper();
34+
}
35+
36+
@Test
37+
void shouldFilterMultipleBlocklistedProperties() {
38+
ObjectNode node = createNodeWithProperties("when", "otherwise", "id", "description");
39+
40+
schemaPropertyFilter.schemaPropertyFilter("choice", node);
41+
42+
ObjectNode properties = (ObjectNode) node.get("properties");
43+
assertFalse(properties.has("when"), "Blocklisted property 'when' should be filtered");
44+
assertFalse(properties.has("otherwise"), "Blocklisted property 'otherwise' should be filtered");
45+
assertTrue(properties.has("id"), "Non-blocklisted property should remain");
46+
assertTrue(properties.has("description"), "Non-blocklisted property should remain");
47+
}
48+
49+
@Test
50+
void shouldNotFilterWhenEipNotInBlocklist() {
51+
ObjectNode node = createNodeWithProperties("steps", "when", "otherwise", "id");
52+
53+
schemaPropertyFilter.schemaPropertyFilter("unknownEip", node);
54+
55+
ObjectNode properties = (ObjectNode) node.get("properties");
56+
assertTrue(properties.has("steps"), "All properties should remain when EIP not in blocklist");
57+
assertTrue(properties.has("when"), "All properties should remain when EIP not in blocklist");
58+
assertTrue(properties.has("otherwise"), "All properties should remain when EIP not in blocklist");
59+
assertTrue(properties.has("id"), "All properties should remain when EIP not in blocklist");
60+
}
61+
62+
@Test
63+
void shouldFilterPropertiesInOneOfArray() {
64+
ObjectNode node = objectMapper.createObjectNode();
65+
ArrayNode oneOfArray = node.putArray("oneOf");
66+
67+
ObjectNode option = objectMapper.createObjectNode();
68+
ObjectNode properties = option.putObject("properties");
69+
properties.putObject("steps");
70+
properties.putObject("id");
71+
oneOfArray.add(option);
72+
73+
schemaPropertyFilter.schemaPropertyFilter("filter", node);
74+
75+
ObjectNode resultProperties = (ObjectNode) oneOfArray.get(0).get("properties");
76+
assertFalse(resultProperties.has("steps"), "Blocklisted property should be filtered in oneOf");
77+
assertTrue(resultProperties.has("id"), "Non-blocklisted property should remain in oneOf");
78+
}
79+
80+
@Test
81+
void shouldFilterPropertiesInAnyOfArray() {
82+
ObjectNode node = objectMapper.createObjectNode();
83+
ArrayNode anyOfArray = node.putArray("anyOf");
84+
85+
ObjectNode option = objectMapper.createObjectNode();
86+
ObjectNode properties = option.putObject("properties");
87+
properties.putObject("steps");
88+
properties.putObject("id");
89+
anyOfArray.add(option);
90+
91+
schemaPropertyFilter.schemaPropertyFilter("filter", node);
92+
93+
ObjectNode resultProperties = (ObjectNode) anyOfArray.get(0).get("properties");
94+
assertFalse(resultProperties.has("steps"), "Blocklisted property should be filtered in anyOf");
95+
assertTrue(resultProperties.has("id"), "Non-blocklisted property should remain in anyOf");
96+
}
97+
98+
/**
99+
* Helper method to create a node with properties
100+
*/
101+
private ObjectNode createNodeWithProperties(String... propertyNames) {
102+
ObjectNode node = objectMapper.createObjectNode();
103+
ObjectNode properties = node.putObject("properties");
104+
for (String propertyName : propertyNames) {
105+
properties.putObject(propertyName);
106+
}
107+
return node;
108+
}
109+
}

0 commit comments

Comments
 (0)