Skip to content

Commit 4120644

Browse files
authored
test(service) testing SaveToFileService and WatchService (#1244)
* test(service) testing saveToFileService and WatchService Signed-off-by: doguhannilt <doguhannilt@gmail.com> * style: A comment line has been enhanced in pom Signed-off-by: doguhannilt <doguhannilt@gmail.com> --------- Signed-off-by: doguhannilt <doguhannilt@gmail.com>
1 parent 9bf5dc2 commit 4120644

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed

sdk-java/pom.xml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,47 @@
2323
<version>0.8.6</version>
2424
<scope>runtime</scope>
2525
</dependency>
26+
27+
28+
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-api</artifactId>
32+
<version>5.9.2</version>
33+
<scope>test</scope>
34+
</dependency>
35+
36+
<dependency>
37+
<groupId>org.junit.jupiter</groupId>
38+
<artifactId>junit-jupiter-engine</artifactId>
39+
<version>5.9.2</version>
40+
<scope>test</scope>
41+
</dependency>
42+
43+
44+
<!-- Mockito -->
45+
<dependency>
46+
<groupId>org.mockito</groupId>
47+
<artifactId>mockito-core</artifactId>
48+
<version>5.14.2</version>
49+
<scope>test</scope>
50+
</dependency>
51+
52+
<!-- Awaitility for asc tests -->
53+
<dependency>
54+
<groupId>org.awaitility</groupId>
55+
<artifactId>awaitility</artifactId>
56+
<version>4.2.0</version>
57+
<scope>test</scope>
58+
</dependency>
59+
60+
61+
<dependency>
62+
<groupId>org.junit.jupiter</groupId>
63+
<artifactId>junit-jupiter-params</artifactId>
64+
<version>5.9.2</version>
65+
<scope>test</scope>
66+
</dependency>
2667
</dependencies>
2768
<build>
2869
<plugins>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package service;
2+
3+
import org.junit.jupiter.api.AfterEach;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import org.vsecm.service.SaveToFileService;
7+
8+
import java.io.IOException;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
14+
class SaveToFileServiceTest {
15+
private Path tempFile;
16+
private Path tempDir;
17+
18+
@BeforeEach
19+
void setUp() throws IOException {
20+
21+
tempDir = Files.createTempDirectory("testDir");
22+
tempFile = tempDir.resolve("testFile.txt");
23+
}
24+
25+
@AfterEach
26+
void tearDown() throws IOException {
27+
if (Files.exists(tempDir)) {
28+
Files.walk(tempDir)
29+
.sorted((a, b) -> b.compareTo(a))
30+
.forEach(path -> {
31+
try {
32+
Files.deleteIfExists(path);
33+
} catch (IOException e) {
34+
System.err.println("Failed to delete: " + path + " - " + e.getMessage());
35+
}
36+
});
37+
}
38+
}
39+
40+
41+
@Test
42+
void testSaveData_FileCreatedAndDataWritten() throws IOException {
43+
String testData = "Hello, World!";
44+
45+
46+
SaveToFileService.saveData(testData, tempFile.toString());
47+
48+
49+
assertTrue(Files.exists(tempFile), "Folder has been created");
50+
51+
52+
String content = Files.readString(tempFile);
53+
assertEquals(testData, content, "Folder's content is valid");
54+
}
55+
56+
@Test
57+
void testSaveData_CreatesMissingDirectories() {
58+
Path nestedFile = tempDir.resolve("nested/directory/testFile.txt");
59+
60+
61+
SaveToFileService.saveData("Test", nestedFile.toString());
62+
63+
64+
assertTrue(Files.exists(nestedFile), "Folder has been created");
65+
assertTrue(Files.exists(nestedFile.getParent()), "Folder has been created");
66+
}
67+
68+
@Test
69+
void testSaveData_InvalidPath_ShouldNotThrowException() {
70+
71+
String invalidPath = "/root/protected/test.txt";
72+
73+
74+
assertDoesNotThrow(() -> SaveToFileService.saveData("Test", invalidPath));
75+
}
76+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package service;
2+
3+
import org.junit.jupiter.api.BeforeEach;
4+
import org.junit.jupiter.api.Test;
5+
import org.vsecm.service.WatchService;
6+
import org.vsecm.task.Task;
7+
import org.vsecm.task.TaskFactory;
8+
9+
10+
import java.util.concurrent.*;
11+
12+
import static org.junit.jupiter.api.Assertions.*;
13+
import static org.mockito.Mockito.*;
14+
import static org.awaitility.Awaitility.await;
15+
import java.time.Duration;
16+
17+
class WatchServiceTest {
18+
private TaskFactory mockTaskFactory;
19+
private Task mockTask;
20+
private ScheduledExecutorService scheduler;
21+
22+
@BeforeEach
23+
void setUp() {
24+
mockTaskFactory = mock(TaskFactory.class);
25+
mockTask = mock(Task.class);
26+
scheduler = Executors.newSingleThreadScheduledExecutor();
27+
}
28+
29+
@Test
30+
void testWatch_ShouldReturnFutureWithExpectedResult() throws Exception {
31+
// GIVEN
32+
String expectedResponse = "Mock Response";
33+
String testUri = "https://example.com/secret";
34+
35+
36+
CompletableFuture<String> fakeFuture = CompletableFuture.completedFuture(expectedResponse);
37+
38+
39+
when(mockTaskFactory.createTask(any())).thenReturn(mockTask);
40+
when(mockTask.execute(testUri)).thenReturn(fakeFuture);
41+
42+
// WHEN
43+
Future<String> resultFuture = WatchService.watch(mockTaskFactory, testUri);
44+
45+
// THEN
46+
await().atMost(Duration.ofSeconds(2)).until(resultFuture::isDone);
47+
48+
49+
String actualResult = resultFuture.get();
50+
assertEquals(expectedResponse, actualResult, "Unexpected condition!");
51+
52+
53+
verify(mockTaskFactory).createTask(any());
54+
verify(mockTask).execute(testUri);
55+
}
56+
}

0 commit comments

Comments
 (0)