|
| 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