Skip to content

Commit ebfced4

Browse files
committed
Fix types and tests
1 parent f71c515 commit ebfced4

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/test/java/net/hackyourfuture/coursehub/CourseControllerEndToEndTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package net.hackyourfuture.coursehub;
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.node.IntNode;
5+
import com.fasterxml.jackson.databind.node.TextNode;
46
import org.junit.jupiter.api.Test;
57
import org.springframework.boot.test.context.SpringBootTest;
68
import org.springframework.boot.test.web.server.LocalServerPort;
@@ -10,6 +12,7 @@
1012
import java.net.http.HttpRequest;
1113
import java.net.http.HttpResponse;
1214

15+
import static io.micrometer.common.util.StringUtils.isNotEmpty;
1316
import static org.assertj.core.api.Assertions.assertThat;
1417

1518
/**
@@ -45,18 +48,23 @@ void shouldReturnJsonResponseWithDemoData() throws Exception {
4548

4649
assertThat(response.body()).isNotNull();
4750
var jsonNode = objectMapper.readTree(response.body());
48-
assertThat(jsonNode)
49-
.extracting(node -> node.get("courses"))
51+
assertThat(jsonNode.get("courses"))
5052
.isNotEmpty()
5153
.anySatisfy(course -> {
5254
assertThat(course.get("name").asText()).isEqualTo("Introduction to Calculus");
5355
assertThat(course.get("description").asText()).isEqualTo("Fundamental concepts of calculus including limits, derivatives, and integrals.");
5456
assertThat(course.get("instructor").asText()).isEqualTo("Alan Murray");
57+
assertThat(course.get("startDate").asText()).isEqualTo("2024-09-01");
58+
assertThat(course.get("endDate").asText()).isEqualTo("2024-12-15");
59+
assertThat(course.get("maxEnrollments").asInt()).isEqualTo(30);
5560
})
5661
.anySatisfy(course -> {
5762
assertThat(course.get("name").asText()).isEqualTo("General Physics I");
5863
assertThat(course.get("description").asText()).isEqualTo("Mechanics, motion, energy, and basic physical laws.");
5964
assertThat(course.get("instructor").asText()).isEqualTo("Brenda Stone");
65+
assertThat(course.get("startDate").asText()).isEqualTo("2024-09-01");
66+
assertThat(course.get("endDate").asText()).isEqualTo("2024-12-15");
67+
assertThat(course.get("maxEnrollments").asInt()).isEqualTo(28);
6068
});
6169
}
6270
}

src/test/java/net/hackyourfuture/coursehub/service/CourseServiceIntegrationTest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,13 @@ void setUp() {
5757
courseRepository.insertCourse(
5858
new CourseEntity(
5959
null, "Test Course 1", "Description 1", instructor1.instructorId(),
60-
LocalDate.now(), LocalDate.now().plusDays(30)
60+
LocalDate.now(), LocalDate.now().plusDays(30), 30
6161
));
6262

6363
courseRepository.insertCourse(
6464
new CourseEntity(
6565
null, "Test Course 2", "Description 2", instructor2.instructorId(),
66-
LocalDate.now(), LocalDate.now().plusDays(60)
66+
LocalDate.now(), LocalDate.now().plusDays(60), 50
6767
));
6868
}
6969

@@ -75,12 +75,18 @@ void shouldReturnCourses() {
7575
.contains(new CourseDto(
7676
"Test Course 1",
7777
"Description 1",
78-
"Test Instructor 1"
78+
"Test Instructor 1",
79+
LocalDate.now(),
80+
LocalDate.now().plusDays(30),
81+
30
7982
))
8083
.contains(new CourseDto(
8184
"Test Course 2",
8285
"Description 2",
83-
"Test Instructor 2"
86+
"Test Instructor 2",
87+
LocalDate.now(),
88+
LocalDate.now().plusDays(60),
89+
50
8490
));
8591
}
8692
}

src/test/java/net/hackyourfuture/coursehub/service/CourseServiceTest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,30 @@ void shouldReturnCoursesWithInstructorNames() {
3535
CourseService courseService = new CourseService(courseRepository, instructorRepository);
3636

3737
when(courseRepository.findAll()).thenReturn(List.of(
38-
new CourseEntity(1L, "Testing course", "A course about testing", 1L, LocalDate.of(2026, Month.JANUARY, 15), LocalDate.of(2026, Month.MARCH, 1)),
39-
new CourseEntity(2L, "Spring course", "A course about using Spring Boot", 2L, LocalDate.of(2026, Month.MAY, 1), LocalDate.of(2026, Month.SEPTEMBER, 1))
38+
new CourseEntity(1, "Testing course", "A course about testing", 1, LocalDate.of(2026, Month.JANUARY, 15), LocalDate.of(2026, Month.MARCH, 1), 30),
39+
new CourseEntity(2, "Spring course", "A course about using Spring Boot", 2, LocalDate.of(2026, Month.MAY, 1), LocalDate.of(2026, Month.SEPTEMBER, 1), 50)
4040
));
41-
when(instructorRepository.findById(1L))
42-
.thenReturn(new InstructorEntity(1L, "Alice", "Smith", "[email protected]"));
43-
when(instructorRepository.findById(2L))
44-
.thenReturn(new InstructorEntity(1L, "Bob", "Johnson", "[email protected]"));
41+
when(instructorRepository.findById(1))
42+
.thenReturn(new InstructorEntity(1, "Alice", "Smith", "[email protected]"));
43+
when(instructorRepository.findById(2))
44+
.thenReturn(new InstructorEntity(1, "Bob", "Johnson", "[email protected]"));
4545

4646
var courses = courseService.getAllCourses();
4747

4848
assertThat(courses)
4949
.hasSize(2)
50-
.contains(new CourseDto("Testing course", "A course about testing", "Alice Smith"))
51-
.contains(new CourseDto("Spring course", "A course about using Spring Boot", "Bob Johnson"));
50+
.contains(new CourseDto("Testing course", "A course about testing", "Alice Smith", LocalDate.of(2026, Month.JANUARY, 15), LocalDate.of(2026, Month.MARCH, 1), 30))
51+
.contains(new CourseDto("Spring course", "A course about using Spring Boot", "Bob Johnson", LocalDate.of(2026, Month.MAY, 1), LocalDate.of(2026, Month.SEPTEMBER, 1), 50));
5252
}
5353

5454
@Test
5555
void shouldFailIfCourseExistsWithoutAnInstructor() {
5656
CourseService courseService = new CourseService(courseRepository, instructorRepository);
5757

5858
when(courseRepository.findAll()).thenReturn(List.of(
59-
new CourseEntity(1L, "Testing course", "A course about testing", 1L, LocalDate.of(2026, Month.JANUARY, 15), LocalDate.of(2026, Month.MARCH, 1))
59+
new CourseEntity(1, "Testing course", "A course about testing", 1, LocalDate.of(2026, Month.JANUARY, 15), LocalDate.of(2026, Month.MARCH, 1), 30)
6060
));
61-
when(instructorRepository.findById(1L)).thenReturn(null);
61+
when(instructorRepository.findById(1)).thenReturn(null);
6262

6363
assertThatThrownBy(courseService::getAllCourses)
6464
.hasMessage("Instructor with id 1 not found");

0 commit comments

Comments
 (0)