|
1 | 1 | package com.scalar.db.dataloader.cli.util; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertArrayEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertEquals; |
5 | 6 | import static org.junit.jupiter.api.Assertions.assertThrows; |
6 | 7 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 8 | +import static org.mockito.Mockito.mock; |
7 | 9 |
|
8 | 10 | import com.scalar.db.common.error.CoreError; |
9 | 11 | import java.util.Map; |
10 | 12 | import org.junit.jupiter.api.Test; |
| 13 | +import picocli.CommandLine; |
11 | 14 |
|
12 | 15 | class CommandLineInputUtilsTest { |
13 | 16 |
|
@@ -97,4 +100,95 @@ void splitByDelimiter_nullDelimiter_shouldThrowException() { |
97 | 100 | .getMessage() |
98 | 101 | .contains(CoreError.DATA_LOADER_SPLIT_INPUT_DELIMITER_NULL.buildMessage())); |
99 | 102 | } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void validatePositiveValue_positiveValue_shouldNotThrowException() { |
| 106 | + // Arrange |
| 107 | + CommandLine commandLine = mock(CommandLine.class); |
| 108 | + int positiveValue = 5; |
| 109 | + |
| 110 | + // Act & Assert - No exception should be thrown |
| 111 | + assertDoesNotThrow( |
| 112 | + () -> |
| 113 | + CommandLineInputUtils.validatePositiveValue( |
| 114 | + commandLine, positiveValue, CoreError.DATA_LOADER_INVALID_DATA_CHUNK_SIZE)); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void validatePositiveValue_one_shouldNotThrowException() { |
| 119 | + // Arrange |
| 120 | + CommandLine commandLine = mock(CommandLine.class); |
| 121 | + int minimumPositiveValue = 1; |
| 122 | + |
| 123 | + // Act & Assert - No exception should be thrown |
| 124 | + assertDoesNotThrow( |
| 125 | + () -> |
| 126 | + CommandLineInputUtils.validatePositiveValue( |
| 127 | + commandLine, minimumPositiveValue, CoreError.DATA_LOADER_INVALID_DATA_CHUNK_SIZE)); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void validatePositiveValue_zero_shouldThrowException() { |
| 132 | + // Arrange |
| 133 | + CommandLine commandLine = mock(CommandLine.class); |
| 134 | + int zeroValue = 0; |
| 135 | + CoreError error = CoreError.DATA_LOADER_INVALID_DATA_CHUNK_SIZE; |
| 136 | + |
| 137 | + // Act & Assert |
| 138 | + CommandLine.ParameterException exception = |
| 139 | + assertThrows( |
| 140 | + CommandLine.ParameterException.class, |
| 141 | + () -> CommandLineInputUtils.validatePositiveValue(commandLine, zeroValue, error)); |
| 142 | + |
| 143 | + // Verify the exception message contains the error message |
| 144 | + assertTrue(exception.getMessage().contains(error.buildMessage())); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void validatePositiveValue_negativeValue_shouldThrowException() { |
| 149 | + // Arrange |
| 150 | + CommandLine commandLine = mock(CommandLine.class); |
| 151 | + int negativeValue = -5; |
| 152 | + CoreError error = CoreError.DATA_LOADER_INVALID_TRANSACTION_SIZE; |
| 153 | + |
| 154 | + // Act & Assert |
| 155 | + CommandLine.ParameterException exception = |
| 156 | + assertThrows( |
| 157 | + CommandLine.ParameterException.class, |
| 158 | + () -> CommandLineInputUtils.validatePositiveValue(commandLine, negativeValue, error)); |
| 159 | + |
| 160 | + // Verify the exception message contains the error message |
| 161 | + assertTrue(exception.getMessage().contains(error.buildMessage())); |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + public void validatePositiveValue_differentErrorTypes_shouldUseCorrectErrorMessage() { |
| 166 | + // Arrange |
| 167 | + CommandLine commandLine = mock(CommandLine.class); |
| 168 | + int negativeValue = -1; |
| 169 | + |
| 170 | + // Act & Assert for DATA_LOADER_INVALID_MAX_THREADS |
| 171 | + CommandLine.ParameterException exception1 = |
| 172 | + assertThrows( |
| 173 | + CommandLine.ParameterException.class, |
| 174 | + () -> |
| 175 | + CommandLineInputUtils.validatePositiveValue( |
| 176 | + commandLine, negativeValue, CoreError.DATA_LOADER_INVALID_MAX_THREADS)); |
| 177 | + assertTrue( |
| 178 | + exception1.getMessage().contains(CoreError.DATA_LOADER_INVALID_MAX_THREADS.buildMessage())); |
| 179 | + |
| 180 | + // Act & Assert for DATA_LOADER_INVALID_DATA_CHUNK_QUEUE_SIZE |
| 181 | + CommandLine.ParameterException exception2 = |
| 182 | + assertThrows( |
| 183 | + CommandLine.ParameterException.class, |
| 184 | + () -> |
| 185 | + CommandLineInputUtils.validatePositiveValue( |
| 186 | + commandLine, |
| 187 | + negativeValue, |
| 188 | + CoreError.DATA_LOADER_INVALID_DATA_CHUNK_QUEUE_SIZE)); |
| 189 | + assertTrue( |
| 190 | + exception2 |
| 191 | + .getMessage() |
| 192 | + .contains(CoreError.DATA_LOADER_INVALID_DATA_CHUNK_QUEUE_SIZE.buildMessage())); |
| 193 | + } |
100 | 194 | } |
0 commit comments