Skip to content

Commit 1c5b27e

Browse files
committed
feat: started adding test cases
1 parent 27c61fe commit 1c5b27e

File tree

7 files changed

+69
-232
lines changed

7 files changed

+69
-232
lines changed

lesson_09/types/types_app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ dependencies {
2121
testImplementation("org.springframework.boot:spring-boot-starter-test")
2222
testImplementation("org.assertj:assertj-core:3.26.3")
2323
testImplementation("at.favre.lib:bcrypt:0.10.2")
24+
testImplementation("com.google.code.gson:gson:2.10.1")
2425

2526
// This dependency is used by the application.
2627
implementation("com.codedifferently.instructional:instructional-lib")

lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/Lesson9.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,16 @@ public void run(String... args) throws Exception {
3636
}
3737

3838
// If the option is "--bulk", generate data files for all providers.
39+
String path = getDataPath();
40+
var fileGenerator = new SampleFileGenerator();
3941
if (option.equals("--bulk")) {
4042
for (DataProvider provider : providers) {
4143
String providerName = provider.getProviderName();
42-
String path = getDataPath();
43-
var fileGenerator = new SampleFileGenerator();
4444
fileGenerator.createProviderFile(path, providerName, provider);
4545
}
4646
return;
4747
}
4848

49-
String path = getDataPath();
50-
var fileGenerator = new SampleFileGenerator();
5149
fileGenerator.createTestFile(path, option);
5250
}
5351

lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/JohnDoeProvider.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

lesson_09/types/types_app/src/main/java/com/codedifferently/lesson9/dataprovider/MarySueProvider.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

lesson_09/types/types_app/src/main/resources/data/johndoe.json

Lines changed: 0 additions & 92 deletions
This file was deleted.

lesson_09/types/types_app/src/main/resources/data/marysue.json

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.codedifferently.lesson9;
2+
3+
import com.codedifferently.lesson9.dataprovider.DataProvider;
4+
import org.junit.jupiter.api.*;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
8+
import java.io.File;
9+
import java.nio.file.Files;
10+
import java.util.List;
11+
12+
import static org.assertj.core.api.Assertions.assertThat;
13+
14+
@SpringBootTest
15+
class Lesson9IntegrationTest {
16+
17+
@Autowired
18+
private Lesson9 lesson9; // Spring injects your application
19+
20+
@Autowired
21+
private List<DataProvider> providers;
22+
23+
private static final String DATA_PATH = Lesson9.getDataPath();
24+
25+
@BeforeEach
26+
void cleanDataDir() throws Exception {
27+
File dir = new File(DATA_PATH);
28+
if (dir.exists()) {
29+
Files.walk(dir.toPath())
30+
.map(java.nio.file.Path::toFile)
31+
.sorted((a, b) -> -a.compareTo(b)) // delete children first
32+
.forEach(File::delete);
33+
}
34+
dir.mkdirs();
35+
}
36+
37+
@Test
38+
void run_withSingleProvider_createsFileInCorrectPath() throws Exception {
39+
// Arrange
40+
String providerName = providers.get(0).getProviderName();
41+
File expectedFile = new File(DATA_PATH, providerName + ".json");
42+
43+
// Act
44+
lesson9.run(providerName);
45+
46+
// Assert
47+
assertThat(expectedFile)
48+
.exists()
49+
.isFile();
50+
}
51+
52+
@Test
53+
void run_withBulkOption_createsAllProviderFiles() throws Exception {
54+
// Act
55+
lesson9.run("--bulk");
56+
57+
// Assert: every provider should have a file in the data path
58+
for (DataProvider provider : providers) {
59+
File file = new File(DATA_PATH, provider.getProviderName() + ".json");
60+
assertThat(file)
61+
.as("File for provider " + provider.getProviderName() + " should exist")
62+
.exists()
63+
.isFile();
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)