|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.indexmanagement.indexstatemanagement |
| 7 | + |
| 8 | +import org.opensearch.indexmanagement.util.IndexManagementException |
| 9 | +import org.opensearch.test.OpenSearchTestCase |
| 10 | + |
| 11 | +class ISMTemplateServiceTests : OpenSearchTestCase() { |
| 12 | + fun `test validateFormat with pattern containing hash`() { |
| 13 | + val patterns = listOf("log#*") |
| 14 | + val exception = validateFormat(patterns) |
| 15 | + assertNotNull("Expected validation error for pattern with #", exception) |
| 16 | + assertTrue(exception is IndexManagementException) |
| 17 | + assertTrue(exception!!.message!!.contains("must not contain a '#'")) |
| 18 | + } |
| 19 | + |
| 20 | + fun `test validateFormat with exclusion pattern containing hash`() { |
| 21 | + val patterns = listOf("log-*", "-test#*") |
| 22 | + val exception = validateFormat(patterns) |
| 23 | + assertNotNull("Expected validation error for exclusion pattern with #", exception) |
| 24 | + assertTrue(exception is IndexManagementException) |
| 25 | + assertTrue(exception!!.message!!.contains("must not contain a '#'")) |
| 26 | + } |
| 27 | + |
| 28 | + fun `test validateFormat with pattern containing colon`() { |
| 29 | + val patterns = listOf("log:*") |
| 30 | + val exception = validateFormat(patterns) |
| 31 | + assertNotNull("Expected validation error for pattern with :", exception) |
| 32 | + assertTrue(exception is IndexManagementException) |
| 33 | + assertTrue(exception!!.message!!.contains("must not contain a ':'")) |
| 34 | + } |
| 35 | + |
| 36 | + fun `test validateFormat with exclusion pattern containing colon`() { |
| 37 | + val patterns = listOf("log-*", "-test:*") |
| 38 | + val exception = validateFormat(patterns) |
| 39 | + assertNotNull("Expected validation error for exclusion pattern with :", exception) |
| 40 | + assertTrue(exception is IndexManagementException) |
| 41 | + assertTrue(exception!!.message!!.contains("must not contain a ':'")) |
| 42 | + } |
| 43 | + |
| 44 | + fun `test validateFormat with pattern starting with underscore`() { |
| 45 | + val patterns = listOf("_log*") |
| 46 | + val exception = validateFormat(patterns) |
| 47 | + assertNotNull("Expected validation error for pattern starting with _", exception) |
| 48 | + assertTrue(exception is IndexManagementException) |
| 49 | + assertTrue(exception!!.message!!.contains("must not start with '_'")) |
| 50 | + } |
| 51 | + |
| 52 | + fun `test validateFormat with exclusion pattern starting with underscore`() { |
| 53 | + val patterns = listOf("log-*", "-_test*") |
| 54 | + val exception = validateFormat(patterns) |
| 55 | + assertNotNull("Expected validation error for exclusion pattern starting with _", exception) |
| 56 | + assertTrue(exception is IndexManagementException) |
| 57 | + assertTrue(exception!!.message!!.contains("must not start with '_'")) |
| 58 | + } |
| 59 | + |
| 60 | + fun `test validateFormat with empty exclusion pattern`() { |
| 61 | + val patterns = listOf("log-*", "-") |
| 62 | + val exception = validateFormat(patterns) |
| 63 | + assertNotNull("Expected validation error for empty exclusion pattern", exception) |
| 64 | + assertTrue(exception is IndexManagementException) |
| 65 | + assertTrue(exception!!.message!!.contains("must have content after '-' exclusion prefix")) |
| 66 | + } |
| 67 | + |
| 68 | + fun `test validateFormat with only exclusion patterns`() { |
| 69 | + val patterns = listOf("-log-test-*", "-log-debug-*") |
| 70 | + val exception = validateFormat(patterns) |
| 71 | + assertNotNull("Expected validation error for only exclusion patterns", exception) |
| 72 | + assertTrue(exception is IndexManagementException) |
| 73 | + assertTrue(exception!!.message!!.contains("must contain at least one inclusion pattern")) |
| 74 | + } |
| 75 | + |
| 76 | + fun `test validateFormat with valid inclusion and exclusion patterns`() { |
| 77 | + val patterns = listOf("log-*", "-log-test-*", "-log-*-debug-*") |
| 78 | + val exception = validateFormat(patterns) |
| 79 | + assertNull("Expected no validation error for valid patterns", exception) |
| 80 | + } |
| 81 | + |
| 82 | + fun `test validateFormat with valid inclusion patterns only`() { |
| 83 | + val patterns = listOf("log-*", "app-*") |
| 84 | + val exception = validateFormat(patterns) |
| 85 | + assertNull("Expected no validation error for valid inclusion patterns", exception) |
| 86 | + } |
| 87 | + |
| 88 | + fun `test validateFormat with empty string pattern`() { |
| 89 | + val patterns = listOf("") |
| 90 | + val exception = validateFormat(patterns) |
| 91 | + // Empty string is treated as an inclusion pattern, so it should not fail the "only exclusions" check |
| 92 | + // It may fail other validations depending on Strings.validFileNameExcludingAstrix |
| 93 | + // For now, we're just testing that it doesn't fail the exclusion-only check |
| 94 | + if (exception != null) { |
| 95 | + assertFalse(exception.message!!.contains("must contain at least one inclusion pattern")) |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments