|
| 1 | +/* |
| 2 | + * Copyright (C) 2020-2025 Linagora |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General |
| 5 | + * Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) |
| 6 | + * any later version, provided you comply with the Additional Terms applicable for LinID Directory Manager software by |
| 7 | + * LINAGORA pursuant to Section 7 of the GNU Affero General Public License, subsections (b), (c), and (e), pursuant to |
| 8 | + * which these Appropriate Legal Notices must notably (i) retain the display of the "LinID™" trademark/logo at the top |
| 9 | + * of the interface window, the display of the “You are using the Open Source and free version of LinID™, powered by |
| 10 | + * Linagora © 2009–2013. Contribute to LinID R&D by subscribing to an Enterprise offer!” infobox and in the e-mails |
| 11 | + * sent with the Program, notice appended to any type of outbound messages (e.g. e-mail and meeting requests) as well |
| 12 | + * as in the LinID Directory Manager user interface, (ii) retain all hypertext links between LinID Directory Manager |
| 13 | + * and https://linid.org/, as well as between LINAGORA and LINAGORA.com, and (iii) refrain from infringing LINAGORA |
| 14 | + * intellectual property rights over its trademarks and commercial brands. Other Additional Terms apply, see |
| 15 | + * <http://www.linagora.com/licenses/> for more details. |
| 16 | + * |
| 17 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
| 18 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more |
| 19 | + * details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Affero General Public License and its applicable Additional Terms for |
| 22 | + * LinID Directory Manager along with this program. If not, see <http://www.gnu.org/licenses/> for the GNU Affero |
| 23 | + * General Public License version 3 and <http://www.linagora.com/licenses/> for the Additional Terms applicable to the |
| 24 | + * LinID Directory Manager software. |
| 25 | + */ |
| 26 | + |
| 27 | +package io.github.linagora.linid.im.rvp; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 34 | + |
| 35 | +import java.util.Map; |
| 36 | +import org.junit.jupiter.api.DisplayName; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | +import io.github.linagora.linid.im.corelib.exception.ApiException; |
| 39 | +import io.github.linagora.linid.im.corelib.plugin.config.dto.ValidationConfiguration; |
| 40 | + |
| 41 | +@DisplayName("Test class: RegexValidationPlugin") |
| 42 | +public class RegexValidationPluginTest { |
| 43 | + |
| 44 | + @Test |
| 45 | + @DisplayName("test supports: should return true on valid type") |
| 46 | + void testSupports() { |
| 47 | + var plugin = new RegexValidationPlugin(); |
| 48 | + |
| 49 | + assertTrue(plugin.supports("regex")); |
| 50 | + assertFalse(plugin.supports("other")); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @DisplayName("test validate: should thrown an exception on missing pattern option") |
| 55 | + void testValidateMissingPatternOption() { |
| 56 | + var plugin = new RegexValidationPlugin(); |
| 57 | + var configuration = new ValidationConfiguration(); |
| 58 | + |
| 59 | + ApiException exception = assertThrows(ApiException.class, () -> plugin.validate(configuration, "")); |
| 60 | + |
| 61 | + assertEquals(500, exception.getStatusCode()); |
| 62 | + assertEquals("error.plugin.default.missing.option", exception.getError().key()); |
| 63 | + assertEquals(Map.of("option", "pattern"), exception.getError().context()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + @DisplayName("test validate: should thrown an exception on invalid pattern option") |
| 68 | + void testValidateInvalidPatternOption() { |
| 69 | + var plugin = new RegexValidationPlugin(); |
| 70 | + var configuration = new ValidationConfiguration(); |
| 71 | + configuration.addOption("pattern", "[a-z"); |
| 72 | + |
| 73 | + ApiException exception = assertThrows(ApiException.class, () -> plugin.validate(configuration, "")); |
| 74 | + |
| 75 | + assertEquals(500, exception.getStatusCode()); |
| 76 | + assertEquals("error.plugin.default.invalid.option", exception.getError().key()); |
| 77 | + assertEquals(Map.of("option", "pattern", "value", "[a-z"), exception.getError().context()); |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + @DisplayName("test validate: should thrown an exception on invalid insensitive pattern option") |
| 82 | + void testValidateInvalidInsensitivePatternOption() { |
| 83 | + var plugin = new RegexValidationPlugin(); |
| 84 | + var configuration = new ValidationConfiguration(); |
| 85 | + configuration.addOption("pattern", "[a-z"); |
| 86 | + configuration.addOption("insensitive", true); |
| 87 | + |
| 88 | + ApiException exception = assertThrows(ApiException.class, () -> plugin.validate(configuration, "")); |
| 89 | + |
| 90 | + assertEquals(500, exception.getStatusCode()); |
| 91 | + assertEquals("error.plugin.default.invalid.option", exception.getError().key()); |
| 92 | + assertEquals(Map.of("option", "pattern", "value", "[a-z"), exception.getError().context()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + @DisplayName("test validate: should return message on invalid value") |
| 97 | + void testValidateInvalidValue() { |
| 98 | + var plugin = new RegexValidationPlugin(); |
| 99 | + var configuration = new ValidationConfiguration(); |
| 100 | + configuration.addOption("pattern", "[a-z]{1}"); |
| 101 | + |
| 102 | + var error = plugin.validate(configuration, "A"); |
| 103 | + |
| 104 | + assertNotNull(error); |
| 105 | + assertTrue(error.isPresent()); |
| 106 | + assertEquals("error.plugin.regexValidation.invalid.value", error.get().key()); |
| 107 | + assertEquals(Map.of("pattern", "[a-z]{1}", "value", "A"), error.get().context()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + @DisplayName("test validate: should not return message on valid value") |
| 112 | + void testValidateValidValue() { |
| 113 | + var plugin = new RegexValidationPlugin(); |
| 114 | + var configuration = new ValidationConfiguration(); |
| 115 | + configuration.addOption("pattern", "[a-z]{1}"); |
| 116 | + |
| 117 | + var error = plugin.validate(configuration, "a"); |
| 118 | + |
| 119 | + assertNotNull(error); |
| 120 | + assertTrue(error.isEmpty()); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + @DisplayName("test validate: should not return message on insensitive valid value") |
| 125 | + void testValidateInsensitiveValidValue() { |
| 126 | + var plugin = new RegexValidationPlugin(); |
| 127 | + var configuration = new ValidationConfiguration(); |
| 128 | + configuration.addOption("pattern", "[a-z]{1}"); |
| 129 | + configuration.addOption("insensitive", true); |
| 130 | + |
| 131 | + var error = plugin.validate(configuration, "A"); |
| 132 | + |
| 133 | + assertNotNull(error); |
| 134 | + assertTrue(error.isEmpty()); |
| 135 | + } |
| 136 | +} |
0 commit comments