Skip to content

Commit d5bd001

Browse files
authored
Merge pull request #18 from gushakov/aitest
merging unit tests from AI
2 parents d8645e1 + 84a8582 commit d5bd001

2 files changed

Lines changed: 107 additions & 1 deletion

File tree

‎src/test/java/com/github/cargoclean/core/usecase/AbstractUseCaseTestSupport.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected void commonSetUp() {
5151
}).when(txOps).doInTransaction(any(TransactionRunnableWithoutResult.class));
5252

5353
lenient().doAnswer(invocation -> {
54-
((TransactionRunnableWithoutResult) invocation.getArgument(0)).run();
54+
((TransactionRunnableWithoutResult) invocation.getArgument(1)).run();
5555
return null;
5656
}).when(txOps).doInTransaction(anyBoolean(), any(TransactionRunnableWithoutResult.class));
5757

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.github.cargoclean.core.usecase.report;
2+
3+
import com.github.cargoclean.core.model.report.ExpectedArrivals;
4+
import com.github.cargoclean.core.port.ErrorHandlingPresenterOutputPort;
5+
import com.github.cargoclean.core.port.persistence.PersistenceGatewayOutputPort;
6+
import com.github.cargoclean.core.usecase.AbstractUseCaseTestSupport;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.Test;
9+
import org.junit.jupiter.api.extension.ExtendWith;
10+
import org.mockito.ArgumentCaptor;
11+
import org.mockito.Captor;
12+
import org.mockito.Mock;
13+
import org.mockito.junit.jupiter.MockitoExtension;
14+
15+
import java.util.Collections;
16+
import java.util.List;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.mockito.ArgumentMatchers.any;
20+
import static org.mockito.Mockito.*;
21+
22+
/*
23+
Written with Continue.dev and Gemini 2.0 Flash.
24+
*/
25+
26+
@ExtendWith(MockitoExtension.class)
27+
class ReportUseCaseTest extends AbstractUseCaseTestSupport {
28+
29+
@Mock
30+
private ReportPresenterOutputPort presenter;
31+
32+
@Mock
33+
private PersistenceGatewayOutputPort gatewayOps;
34+
35+
private ReportUseCase useCase;
36+
37+
@Captor
38+
private ArgumentCaptor<List<ExpectedArrivals>> expectedArrivalsCaptor;
39+
40+
@BeforeEach
41+
void setUp() {
42+
super.commonSetUp();
43+
useCase = new ReportUseCase(presenter, gatewayOps, txOps);
44+
}
45+
46+
@Override
47+
protected ErrorHandlingPresenterOutputPort getPresenter() {
48+
return presenter;
49+
}
50+
51+
@Test
52+
void reportExpectedArrivals_success() {
53+
// Arrange
54+
List<ExpectedArrivals> expectedArrivalsList = Collections.singletonList(
55+
new ExpectedArrivals("USNYC", 10) // Corrected: Using constructor
56+
);
57+
when(gatewayOps.queryForExpectedArrivals()).thenReturn(expectedArrivalsList);
58+
59+
// Act
60+
useCase.reportExpectedArrivals();
61+
62+
// Assert
63+
verify(gatewayOps, times(1)).queryForExpectedArrivals();
64+
verify(presenter, times(1)).presentExpectedArrivals(expectedArrivalsCaptor.capture());
65+
66+
List<ExpectedArrivals> capturedArrivals = expectedArrivalsCaptor.getValue();
67+
assertEquals(1, capturedArrivals.size());
68+
assertEquals("USNYC", capturedArrivals.get(0).getCity()); // Corrected: Using getCity()
69+
assertEquals(10, capturedArrivals.get(0).getNumberOfArrivals()); // Corrected: Using getNumberOfArrivals()
70+
71+
noErrorsWerePresented();
72+
}
73+
74+
@Test
75+
void reportExpectedArrivals_noArrivals() {
76+
// Arrange
77+
when(gatewayOps.queryForExpectedArrivals()).thenReturn(Collections.emptyList());
78+
79+
// Act
80+
useCase.reportExpectedArrivals();
81+
82+
// Assert
83+
verify(gatewayOps, times(1)).queryForExpectedArrivals();
84+
verify(presenter, times(1)).presentExpectedArrivals(expectedArrivalsCaptor.capture());
85+
86+
List<ExpectedArrivals> capturedArrivals = expectedArrivalsCaptor.getValue();
87+
assertEquals(0, capturedArrivals.size());
88+
89+
noErrorsWerePresented();
90+
}
91+
92+
@Test
93+
void reportExpectedArrivals_gatewayException() {
94+
// Arrange
95+
when(gatewayOps.queryForExpectedArrivals()).thenThrow(new RuntimeException("Database error"));
96+
97+
// Act
98+
useCase.reportExpectedArrivals();
99+
100+
// Assert
101+
verify(gatewayOps, times(1)).queryForExpectedArrivals();
102+
verify(presenter, times(1)).presentError(any(RuntimeException.class));
103+
104+
noErrorsWerePresented();
105+
}
106+
}

0 commit comments

Comments
 (0)