Skip to content

Commit e2836e5

Browse files
committed
adopt content to first day
1 parent 328cc8f commit e2836e5

15 files changed

Lines changed: 51 additions & 53 deletions

File tree

labs/lab-1/pom.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
<properties>
1818
<java.version>21</java.version>
19-
<mockito.version>5.5.0</mockito.version>
2019
</properties>
2120

2221
<dependencies>

labs/lab-1/src/test/java/pragmatech/digital/workshops/lab1/experiment/MockitoDemoTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.junit.jupiter.api.extension.ExtendWith;
88
import org.mockito.InjectMocks;
99
import org.mockito.Mock;
10+
import org.mockito.Mockito;
1011
import org.mockito.junit.jupiter.MockitoExtension;
1112
import pragmatech.digital.workshops.lab1.domain.Book;
1213
import pragmatech.digital.workshops.lab1.repository.BookRepository;
@@ -23,11 +24,13 @@ class MockitoDemoTest {
2324
@Mock
2425
private BookRepository bookRepository;
2526

27+
// public int count() -> int -> 0
28+
// public Integer count() -> null
29+
2630
@InjectMocks
2731
private BookService bookService;
2832

2933
@Test
30-
@Disabled("Disabled to prevent test failure due to Sunday restriction in BookService")
3134
void shouldReturnBookWhenFound() {
3235
// Arrange
3336
when(bookRepository.findByIsbn("1234")).thenReturn(Optional.empty());

labs/lab-1/src/test/java/pragmatech/digital/workshops/lab1/solutions/BookServiceWithClock.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.time.LocalDate;
66
import java.util.Optional;
77

8+
import org.springframework.context.annotation.Bean;
9+
import org.springframework.context.annotation.Configuration;
810
import pragmatech.digital.workshops.lab1.domain.Book;
911
import pragmatech.digital.workshops.lab1.repository.BookRepository;
1012
import pragmatech.digital.workshops.lab1.service.BookAlreadyExistsException;

labs/lab-1/src/test/java/pragmatech/digital/workshops/lab1/solutions/RefactoredBookService.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import pragmatech.digital.workshops.lab1.service.BookAlreadyExistsException;
99
import pragmatech.digital.workshops.lab1.util.TimeProvider;
1010

11+
import static java.time.DayOfWeek.SATURDAY;
1112
import static java.time.DayOfWeek.SUNDAY;
1213

1314
public class RefactoredBookService {

labs/lab-1/src/test/java/pragmatech/digital/workshops/lab1/solutions/Solution1UnitTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import java.time.ZoneId;
77
import java.util.Optional;
88

9-
import org.junit.jupiter.api.Disabled;
109
import org.junit.jupiter.api.DisplayName;
1110
import org.junit.jupiter.api.Test;
1211
import org.junit.jupiter.api.extension.ExtendWith;
@@ -25,7 +24,6 @@
2524
import static org.mockito.Mockito.mock;
2625
import static org.mockito.Mockito.verify;
2726
import static org.mockito.Mockito.when;
28-
import static tools.jackson.databind.ext.javatime.deser.JSR310StringParsableDeserializer.ZONE_ID;
2927

3028
@ExtendWith(MockitoExtension.class)
3129
class Solution1UnitTest {
@@ -53,7 +51,6 @@ void shouldThrowExceptionWhenBookWithIsbnAlreadyExists() {
5351

5452
@Test
5553
@DisplayName("Should create a book when ISBN does not exist")
56-
@Disabled("Disabled to prevent test failure due to Sunday restriction in BookService")
5754
void shouldCreateBookWhenIsbnDoesNotExist() {
5855
// Arrange
5956
BookService cut = new BookService(bookRepository);
@@ -87,7 +84,8 @@ void shouldCreateBookWhenIsbnDoesNotExist() {
8784
void shouldThrowExceptionWhenTryingToRegisterBookOnSunday() {
8885
// Arrange
8986
TimeProvider timeProvider = mock(TimeProvider.class);
90-
when(timeProvider.getCurrentDate()).thenReturn(LocalDate.of(2026, 3, 1)); // A Sunday
87+
when(timeProvider.getCurrentDate())
88+
.thenReturn(LocalDate.of(2026, 3, 1)); // A Sunday
9189

9290
RefactoredBookService cut = new RefactoredBookService(bookRepository, timeProvider);
9391
String isbn = "9780134685991";

labs/lab-2/src/test/java/pragmatech/digital/workshops/lab2/experiment/BookControllerTest.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -114,20 +114,21 @@ void shouldReturnNotFoundWhenBookDoesNotExistById() throws Exception {
114114
@Test
115115
void shouldReturnCreatedWithLocationWhenCreatingBookWithValidData() throws Exception {
116116
// Arrange
117-
BookCreationRequest request = new BookCreationRequest(
118-
"123-1234567890",
119-
"Test Book",
120-
"Test Author",
121-
LocalDate.of(2020, 1, 1)
122-
);
123-
124117
when(bookService.createBook(any(BookCreationRequest.class))).thenReturn(1L);
125118

126119
// Act & Assert
127120
mockMvc.perform(post("/api/books")
128121
.with(SecurityMockMvcRequestPostProcessors.user("user").roles("USER"))
129122
.contentType(MediaType.APPLICATION_JSON)
130-
.content(objectMapper.writeValueAsString(request)))
123+
.content("""
124+
{
125+
"isbn": "123-1234567890",
126+
"title": "Test Book",
127+
"author": "Test Author",
128+
"publishedDate": "2028-01-01"
129+
}
130+
131+
"""))
131132
.andExpect(status().isCreated())
132133
.andExpect(header().string("Location", containsString("/api/books/1")));
133134
}

labs/lab-2/src/test/java/pragmatech/digital/workshops/lab2/solutions/Solution1WebMvcTest.java

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,20 @@ void shouldReturnUnauthorizedWhenNoAuthenticationIsProvided() throws Exception {
5050
// Arrange - No authentication credentials provided
5151

5252
// Act & Assert
53-
mockMvc.perform(delete("/api/books/1"))
53+
mockMvc
54+
.perform(delete("/api/books/1"))
5455
.andExpect(status().isUnauthorized());
55-
56-
// Verify - Service should not be called
57-
verify(bookService, times(0)).deleteBook(any());
5856
}
5957

6058
@Test
61-
@WithMockUser(roles = "USER")
59+
@WithMockUser(roles = "DUKE")
6260
@DisplayName("Should return 403 Forbidden when authenticated with insufficient privileges")
6361
void shouldReturnForbiddenWhenAuthenticatedWithInsufficientPrivileges() throws Exception {
6462
// Arrange - User with insufficient privileges (USER role)
6563

6664
// Act & Assert
6765
mockMvc.perform(delete("/api/books/1"))
6866
.andExpect(status().isForbidden());
69-
70-
// Verify - Service should not be called
71-
verify(bookService, times(0)).deleteBook(any());
7267
}
7368

7469
@Test
@@ -81,9 +76,6 @@ void shouldReturnNoContentWhenAuthenticatedAsAdminAndBookExists() throws Excepti
8176
// Act & Assert
8277
mockMvc.perform(delete("/api/books/1"))
8378
.andExpect(status().isNoContent());
84-
85-
// Verify - Service should be called with the book ID
86-
verify(bookService, times(1)).deleteBook(1L);
8779
}
8880

8981
@Test
@@ -98,7 +90,7 @@ void shouldReturnNotFoundWhenAuthenticatedAsAdminButBookDoesntExist() throws Exc
9890
.andExpect(status().isNotFound());
9991

10092
// Verify - Service should be called with the book ID
101-
verify(bookService, times(1)).deleteBook(999L);
93+
//verify(bookService, times(1)).deleteBook(999L);
10294
}
10395
}
10496

@@ -129,9 +121,6 @@ void shouldReturnCreatedWhenValidBookDataIsProvided() throws Exception {
129121
.andExpect(status().isCreated())
130122
.andExpect(header().exists("Location"))
131123
.andExpect(header().string("Location", Matchers.containsString("/api/books/1")));
132-
133-
// Verify - Service should be called
134-
verify(bookService, times(1)).createBook(any());
135124
}
136125

137126
@Test
@@ -142,7 +131,7 @@ void shouldReturnBadRequestWhenInvalidBookDataIsProvided() throws Exception {
142131
String invalidBookJson = """
143132
{
144133
"isbn": "",
145-
"title": "",
134+
"title": "Test",
146135
"author": "Test Author",
147136
"publishedDate": "2025-01-01"
148137
}

labs/lab-3/src/main/java/pragmatech/digital/workshops/lab3/Lab3Application.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
66

7-
// @EnableJpaAuditing -> keep this class clean and outsource configuration to a separate class
7+
//@EnableJpaAuditing
88
@SpringBootApplication
99
public class Lab3Application {
1010

labs/lab-3/src/test/java/pragmatech/digital/workshops/lab3/experiment/BookRepositoryTest.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ class BookRepositoryTest {
3232
@Container
3333
@ServiceConnection
3434
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine")
35-
.withDatabaseName("testdb")
36-
.withUsername("test")
37-
.withPassword("test")
3835
.withInitScript("init-postgres.sql");
3936

4037
@Autowired

labs/lab-3/src/test/java/pragmatech/digital/workshops/lab3/solutions/Solution1DataJpaTest.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@
44
import java.util.List;
55
import java.util.Optional;
66

7-
import org.junit.jupiter.api.BeforeEach;
87
import org.junit.jupiter.api.DisplayName;
98
import org.junit.jupiter.api.Nested;
109
import org.junit.jupiter.api.Test;
1110
import org.springframework.beans.factory.annotation.Autowired;
1211
import org.springframework.boot.data.jpa.test.autoconfigure.DataJpaTest;
13-
import org.springframework.boot.jdbc.test.autoconfigure.AutoConfigureTestDatabase;
1412
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
15-
import org.testcontainers.containers.PostgreSQLContainer;
1613
import org.testcontainers.junit.jupiter.Container;
1714
import org.testcontainers.junit.jupiter.Testcontainers;
15+
import org.testcontainers.postgresql.PostgreSQLContainer;
1816
import pragmatech.digital.workshops.lab3.entity.Book;
1917
import pragmatech.digital.workshops.lab3.entity.BookStatus;
2018
import pragmatech.digital.workshops.lab3.repository.BookRepository;
@@ -30,15 +28,11 @@
3028
*/
3129
@DataJpaTest
3230
@Testcontainers
33-
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
3431
class Solution1DataJpaTest {
3532

3633
@Container
3734
@ServiceConnection
38-
static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:16-alpine")
39-
.withDatabaseName("testdb")
40-
.withUsername("test")
41-
.withPassword("test")
35+
static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:16-alpine")
4236
.withInitScript("init-postgres.sql");
4337

4438
@Autowired

0 commit comments

Comments
 (0)