1515 * See the License for the specific language governing permissions and
1616 * limitations under the License.
1717 */
18- package org .apache .beam .sdk .io .kafka ;
19-
20- import static org .junit .Assert .assertEquals ;
21- import static org .junit .Assert .assertThrows ;
22- import static org .junit .Assert .assertTrue ;
23- import static org .mockito .ArgumentMatchers .any ;
24- import static org .mockito .ArgumentMatchers .eq ;
25- import static org .mockito .Mockito .doReturn ;
26- import static org .mockito .Mockito .mock ;
27- import static org .mockito .Mockito .mockStatic ;
28- import static org .mockito .Mockito .spy ;
29- import static org .mockito .Mockito .when ;
18+ package org .apache .beam .sdk .io .kafka .file .aware .factories ;
3019
3120import java .io .ByteArrayInputStream ;
3221import java .io .File ;
4231import org .apache .beam .sdk .io .FileSystems ;
4332import org .apache .beam .sdk .io .fs .MatchResult ;
4433import org .apache .beam .sdk .io .fs .ResourceId ;
34+ import org .junit .Assert ;
4535import org .junit .Before ;
4636import org .junit .Rule ;
4737import org .junit .Test ;
4838import org .junit .rules .TemporaryFolder ;
4939import org .junit .runner .RunWith ;
5040import org .junit .runners .JUnit4 ;
41+ import org .mockito .ArgumentMatchers ;
5142import org .mockito .MockedStatic ;
43+ import org .mockito .Mockito ;
5244
5345@ RunWith (JUnit4 .class )
5446public class FileAwareFactoryFnTest {
@@ -75,8 +67,8 @@ protected Object createObject(Map<String, Object> config) {
7567 @ Before
7668 public void setup () throws IOException {
7769 baseDir = "/tmp/" + TEST_FACTORY_TYPE ;
78- factory = spy (new TestFactoryFn ());
79- doReturn (baseDir ).when (factory ).getBaseDirectory ();
70+ factory = Mockito . spy (new TestFactoryFn ());
71+ Mockito . doReturn (baseDir ).when (factory ).getBaseDirectory ();
8072 }
8173
8274 @ Test
@@ -93,11 +85,11 @@ public void testHappyPathReplacesGcsPath() {
9385
9486 // Act & Assert
9587 // Use try-with-resources to manage the scope of the static mock on FileSystems
96- try (MockedStatic <FileSystems > mockedFileSystems = mockStatic (FileSystems .class )) {
88+ try (MockedStatic <FileSystems > mockedFileSystems = Mockito . mockStatic (FileSystems .class )) {
9789 // 1. Mock the underlying static FileSystems calls to avoid real network I/O
98- MatchResult .Metadata metadata = mock (MatchResult .Metadata .class );
99- ResourceId resourceId = mock (ResourceId .class );
100- when (metadata .resourceId ()).thenReturn (resourceId );
90+ MatchResult .Metadata metadata = Mockito . mock (MatchResult .Metadata .class );
91+ ResourceId resourceId = Mockito . mock (ResourceId .class );
92+ Mockito . when (metadata .resourceId ()).thenReturn (resourceId );
10193 mockedFileSystems .when (() -> FileSystems .matchSingleFileSpec (gcsPath )).thenReturn (metadata );
10294
10395 // 2. Mock 'open' to return a channel with no data, simulating a successful download
@@ -108,8 +100,9 @@ public void testHappyPathReplacesGcsPath() {
108100 Map <String , Object > processedConfig = (Map <String , Object >) factory .apply (config );
109101
110102 // Assert
111- assertEquals (expectedLocalPath , processedConfig .get ("config.file.path" ));
112- assertTrue ("Local file should have been created" , new File (expectedLocalPath ).exists ());
103+ Assert .assertEquals (expectedLocalPath , processedConfig .get ("config.file.path" ));
104+ Assert .assertTrue (
105+ "Local file should have been created" , new File (expectedLocalPath ).exists ());
113106 }
114107 }
115108
@@ -121,17 +114,17 @@ public void testApplyFailurePathThrowsRuntimeExceptionOnDownloadFailure() {
121114 config .put ("critical.file" , gcsPath );
122115
123116 // Mock the static FileSystems.matchSingleFileSpec to throw an exception
124- try (MockedStatic <FileSystems > mockedFileSystems = mockStatic (FileSystems .class )) {
117+ try (MockedStatic <FileSystems > mockedFileSystems = Mockito . mockStatic (FileSystems .class )) {
125118 mockedFileSystems
126119 .when (() -> FileSystems .matchSingleFileSpec (gcsPath ))
127120 .thenThrow (new IOException ("GCS file not found" ));
128121
129122 // Act & Assert
130123 RuntimeException exception =
131- assertThrows (RuntimeException .class , () -> factory .apply (config ));
132- assertTrue (exception .getMessage ().contains ("Failed trying to process value" ));
133- assertTrue (exception .getCause () instanceof IOException );
134- assertTrue (exception .getCause ().getMessage ().contains ("Failed to download file" ));
124+ Assert . assertThrows (RuntimeException .class , () -> factory .apply (config ));
125+ Assert . assertTrue (exception .getMessage ().contains ("Failed trying to process value" ));
126+ Assert . assertTrue (exception .getCause () instanceof IOException );
127+ Assert . assertTrue (exception .getCause ().getMessage ().contains ("Failed to download file" ));
135128 }
136129 }
137130
@@ -147,7 +140,7 @@ public void testApplyHappyPathIgnoresNonGcsValues() {
147140 Map <String , Object > processedConfig = (Map <String , Object >) factory .apply (config );
148141
149142 // Assert
150- assertEquals (config , processedConfig );
143+ Assert . assertEquals (config , processedConfig );
151144 }
152145
153146 @ Test
@@ -172,7 +165,7 @@ public void testApplyEdgeCaseMultipleGcsPathsInSingleValue() {
172165 Map <String , Object > config = new HashMap <>();
173166 config .put ("jaas.config" , originalValue );
174167
175- try (MockedStatic <FileSystems > mockedFileSystems = mockStatic (FileSystems .class )) {
168+ try (MockedStatic <FileSystems > mockedFileSystems = Mockito . mockStatic (FileSystems .class )) {
176169 // Mock GCS calls for both paths
177170 mockSuccessfulDownload (mockedFileSystems , gcsPath1 );
178171 mockSuccessfulDownload (mockedFileSystems , gcsPath2 );
@@ -181,7 +174,7 @@ public void testApplyEdgeCaseMultipleGcsPathsInSingleValue() {
181174 Map <String , Object > processedConfig = (Map <String , Object >) factory .apply (config );
182175
183176 // Assert
184- assertEquals (expectedProcessedValue , processedConfig .get ("jaas.config" ));
177+ Assert . assertEquals (expectedProcessedValue , processedConfig .get ("jaas.config" ));
185178 }
186179 }
187180
@@ -193,22 +186,25 @@ public void testApplyEdgeCaseLocalFileWriteFails() throws IOException {
193186 config .put ("a.file" , gcsPath );
194187
195188 // Mock GCS part to succeed
196- try (MockedStatic <FileSystems > mockedFileSystems = mockStatic (FileSystems .class );
197- MockedStatic <FileChannel > mockedFileChannel = mockStatic (FileChannel .class )) {
189+ try (MockedStatic <FileSystems > mockedFileSystems = Mockito . mockStatic (FileSystems .class );
190+ MockedStatic <FileChannel > mockedFileChannel = Mockito . mockStatic (FileChannel .class )) {
198191 mockSuccessfulDownload (mockedFileSystems , gcsPath );
199192
200193 // Mock the local file writing part to fail
201194 mockedFileChannel
202- .when (() -> FileChannel .open (any (Path .class ), any (Set .class )))
195+ .when (
196+ () ->
197+ FileChannel .open (
198+ ArgumentMatchers .any (Path .class ), ArgumentMatchers .any (Set .class )))
203199 .thenThrow (new IOException ("Permission denied" ));
204200
205201 // Act & Assert
206202 RuntimeException exception =
207- assertThrows (RuntimeException .class , () -> factory .apply (config ));
208- assertTrue (exception .getMessage ().contains ("Failed trying to process value" ));
209- assertTrue (exception .getCause () instanceof IOException );
203+ Assert . assertThrows (RuntimeException .class , () -> factory .apply (config ));
204+ Assert . assertTrue (exception .getMessage ().contains ("Failed trying to process value" ));
205+ Assert . assertTrue (exception .getCause () instanceof IOException );
210206 // Check that the root cause is our "Permission denied" mock
211- assertTrue (exception .getCause ().getCause ().getMessage ().contains ("Permission denied" ));
207+ Assert . assertTrue (exception .getCause ().getCause ().getMessage ().contains ("Permission denied" ));
212208 }
213209 }
214210
@@ -233,7 +229,7 @@ public void testApplyHappyPathResolvesSecretValue() {
233229 @ Override
234230 public byte [] getSecret (String secretIdentifier ) {
235231 // Assert that the correct identifier is passed
236- assertEquals (secretVersionParsed , secretIdentifier );
232+ Assert . assertEquals (secretVersionParsed , secretIdentifier );
237233 // Return a predictable, hardcoded value for the test
238234 return secretValue .getBytes (StandardCharsets .UTF_8 );
239235 }
@@ -245,7 +241,7 @@ public byte[] getSecret(String secretIdentifier) {
245241 (Map <String , Object >) factoryWithMockedSecret .apply (config );
246242
247243 // Assert
248- assertEquals (expectedProcessedValue , processedConfig .get ("db.password" ));
244+ Assert . assertEquals (expectedProcessedValue , processedConfig .get ("db.password" ));
249245 }
250246
251247 @ Test
@@ -258,18 +254,22 @@ public void testApplyFailurePathThrowsExceptionForInvalidSecretFormat() {
258254 // Act & Assert
259255 // The spy will call the real method here, which will throw an exception
260256 // because the secret path is not parsable.
261- RuntimeException ex = assertThrows (RuntimeException .class , () -> factory .apply (config ));
262- assertEquals (IllegalArgumentException .class , ex .getCause ().getClass ());
257+ RuntimeException ex = Assert . assertThrows (RuntimeException .class , () -> factory .apply (config ));
258+ Assert . assertEquals (IllegalArgumentException .class , ex .getCause ().getClass ());
263259 }
264260
265261 // Helper method to reduce boilerplate in mocking successful GCS downloads
266262 private void mockSuccessfulDownload (MockedStatic <FileSystems > mockedFileSystems , String gcsPath ) {
267- MatchResult .Metadata metadata = mock (MatchResult .Metadata .class );
268- ResourceId resourceId = mock (ResourceId .class );
269- when (metadata .resourceId ()).thenReturn (resourceId );
270- mockedFileSystems .when (() -> FileSystems .matchSingleFileSpec (eq (gcsPath ))).thenReturn (metadata );
263+ MatchResult .Metadata metadata = Mockito .mock (MatchResult .Metadata .class );
264+ ResourceId resourceId = Mockito .mock (ResourceId .class );
265+ Mockito .when (metadata .resourceId ()).thenReturn (resourceId );
266+ mockedFileSystems
267+ .when (() -> FileSystems .matchSingleFileSpec (ArgumentMatchers .eq (gcsPath )))
268+ .thenReturn (metadata );
271269
272270 ReadableByteChannel channel = Channels .newChannel (new ByteArrayInputStream (new byte [0 ]));
273- mockedFileSystems .when (() -> FileSystems .open (eq (resourceId ))).thenReturn (channel );
271+ mockedFileSystems
272+ .when (() -> FileSystems .open (ArgumentMatchers .eq (resourceId )))
273+ .thenReturn (channel );
274274 }
275275}
0 commit comments