Skip to content

Commit

Permalink
Close FileInputStream automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
mekya committed Aug 25, 2024
1 parent 973af1b commit 0b1cc3e
Showing 1 changed file with 36 additions and 36 deletions.
72 changes: 36 additions & 36 deletions src/test/java/io/antmedia/test/rest/VoDRestServiceV2UnitTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public void testUploadVodFile() {
}

@Test
public void testUploadVoDfileNameAndResource() throws FileNotFoundException {
public void testUploadVoDfileNameAndResource() throws IOException {
VoDRestService streamSourceRest2 = Mockito.spy(restServiceReal);

MapDBStore datastore = spy(new MapDBStore("datastore", vertx));
Expand All @@ -384,36 +384,36 @@ public void testUploadVoDfileNameAndResource() throws FileNotFoundException {
streamSourceRest2.setAppCtx(context);

String fileName = RandomStringUtils.randomAlphabetic(11) + ".anythingelse";
FileInputStream inputStream = new FileInputStream("src/test/resources/sample_MP4_480.mp4");
try(FileInputStream inputStream = new FileInputStream("src/test/resources/sample_MP4_480.mp4")) {

Result result = streamSourceRest2.uploadVoDFile(fileName, inputStream);
Result result = streamSourceRest2.uploadVoDFile(fileName, inputStream);

assertFalse(result.isSuccess());
assertEquals("notSupportedFileType", result.getMessage());

AppSettings appSettings = new AppSettings();
Mockito.doReturn(appSettings).when(streamSourceRest2).getAppSettings();
Mockito.doReturn(datastore).when(streamSourceRest2).getDataStore();

fileName = RandomStringUtils.randomAlphabetic(11) + ".wmv";
result = streamSourceRest2.uploadVoDFile(fileName, inputStream);
assertTrue(result.isSuccess());


appSettings.setVodUploadFinishScript("src/test/resources/echo.sh");
result = streamSourceRest2.uploadVoDFile(fileName, inputStream);
assertTrue(result.isSuccess());


when(statsCollector.enoughResource()).thenReturn(false);
result = streamSourceRest2.uploadVoDFile(fileName, inputStream);
assertFalse(result.isSuccess());
assertFalse(result.isSuccess());
assertEquals("notSupportedFileType", result.getMessage());

AppSettings appSettings = new AppSettings();
Mockito.doReturn(appSettings).when(streamSourceRest2).getAppSettings();
Mockito.doReturn(datastore).when(streamSourceRest2).getDataStore();

fileName = RandomStringUtils.randomAlphabetic(11) + ".wmv";
result = streamSourceRest2.uploadVoDFile(fileName, inputStream);
assertTrue(result.isSuccess());


appSettings.setVodUploadFinishScript("src/test/resources/echo.sh");
result = streamSourceRest2.uploadVoDFile(fileName, inputStream);
assertTrue(result.isSuccess());


when(statsCollector.enoughResource()).thenReturn(false);
result = streamSourceRest2.uploadVoDFile(fileName, inputStream);
assertFalse(result.isSuccess());
}

}

@Test
public void testVoDUploadFinishedScript() throws FileNotFoundException {
public void testVoDUploadFinishedScript() throws IOException {

VoDRestService streamSourceRest2 = Mockito.spy(restServiceReal);

Expand Down Expand Up @@ -446,27 +446,27 @@ public void testVoDUploadFinishedScript() throws FileNotFoundException {


String fileName = RandomStringUtils.randomAlphabetic(11) + ".mp4";
FileInputStream inputStream = new FileInputStream("src/test/resources/sample_MP4_480.mp4");

Result result = streamSourceRest2.uploadVoDFile(fileName, inputStream);
assertTrue(result.isSuccess());
try(FileInputStream inputStream = new FileInputStream("src/test/resources/sample_MP4_480.mp4")) {

ArgumentCaptor<VoD> vodCapture = ArgumentCaptor.forClass(VoD.class);
Mockito.verify(datastore, Mockito.timeout(5000).times(1)).addVod(vodCapture.capture());
Result result = streamSourceRest2.uploadVoDFile(fileName, inputStream);
assertTrue(result.isSuccess());

assertEquals(VoD.PROCESS_STATUS_INQUEUE, vodCapture.getValue().getProcessStatus());
ArgumentCaptor<VoD> vodCapture = ArgumentCaptor.forClass(VoD.class);
Mockito.verify(datastore, Mockito.timeout(5000).times(1)).addVod(vodCapture.capture());

Mockito.verify(streamSourceRest2,Mockito.times(1)).startVoDScriptProcess(Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyString());
assertEquals(VoD.PROCESS_STATUS_INQUEUE, vodCapture.getValue().getProcessStatus());

verify(datastore, Mockito.timeout(5000).times(1)).updateVoDProcessStatus(result.getDataId(), VoD.PROCESS_STATUS_PROCESSING);
Mockito.verify(streamSourceRest2,Mockito.times(1)).startVoDScriptProcess(Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.anyString());

verify(datastore, Mockito.timeout(5000).times(1)).updateVoDProcessStatus(result.getDataId(), VoD.PROCESS_STATUS_FINISHED);
verify(datastore, Mockito.timeout(5000).times(1)).updateVoDProcessStatus(result.getDataId(), VoD.PROCESS_STATUS_PROCESSING);

verify(datastore, Mockito.timeout(5000).times(1)).updateVoDProcessStatus(result.getDataId(), VoD.PROCESS_STATUS_FINISHED);

VoD voD = datastore.getVoD(result.getDataId());

assertEquals(VoD.PROCESS_STATUS_FINISHED, voD.getProcessStatus());
VoD voD = datastore.getVoD(result.getDataId());

assertEquals(VoD.PROCESS_STATUS_FINISHED, voD.getProcessStatus());
}


}
Expand Down

0 comments on commit 0b1cc3e

Please sign in to comment.