15
15
import eu .europeana .metis .sandbox .dto .debias .DetectionInfoDto ;
16
16
import eu .europeana .metis .sandbox .entity .DatasetEntity ;
17
17
import eu .europeana .metis .sandbox .entity .debias .DetectionEntity ;
18
+ import eu .europeana .metis .sandbox .repository .DatasetRepository ;
18
19
import eu .europeana .metis .sandbox .repository .debias .DetectRepository ;
19
20
import java .time .ZonedDateTime ;
21
+ import java .util .NoSuchElementException ;
22
+ import java .util .Optional ;
20
23
import org .junit .jupiter .api .Test ;
21
24
import org .junit .jupiter .api .extension .ExtendWith ;
22
25
import org .mockito .InjectMocks ;
23
26
import org .mockito .Mock ;
24
27
import org .mockito .junit .jupiter .MockitoExtension ;
25
28
26
29
@ ExtendWith (MockitoExtension .class )
27
- class DebiasDetectServiceTest {
30
+ class DeBiasDetectServiceTest {
28
31
29
32
@ Mock
30
33
DetectRepository detectRepository ;
31
34
35
+ @ Mock
36
+ DatasetRepository datasetRepository ;
37
+
32
38
@ InjectMocks
33
- DebiasDetectService debiasDetectService ;
39
+ DeBiasDetectService debiasDetectService ;
40
+
41
+ @ Test
42
+ void processWhenDatasetNotExists_expectSuccess () {
43
+ final Integer datasetId = 1 ;
44
+ final DetectionEntity detectionEntity = new DetectionEntity ();
45
+ detectionEntity .setState ("READY" );
46
+ detectionEntity .setCreatedDate (ZonedDateTime .now ());
47
+
48
+ when (datasetRepository .findById (anyInt ())).thenThrow (NoSuchElementException .class );
49
+
50
+ boolean result = debiasDetectService .process (datasetId );
51
+
52
+ assertFalse (result );
53
+ assertInstanceOf (ReadyState .class , debiasDetectService .getState ());
54
+ verify (detectRepository , times (0 )).findDetectionEntityByDatasetId_DatasetId (datasetId );
55
+ verify (detectRepository , times (0 )).save (any (DetectionEntity .class ));
56
+ verify (detectRepository , times (0 )).updateState (anyInt (), anyString ());
57
+ }
34
58
35
59
36
60
@ Test
@@ -42,7 +66,7 @@ void processWhenNewHappyPath_Ready_Processing_Completed_expectSuccess() {
42
66
final DatasetEntity datasetEntity = new DatasetEntity ();
43
67
datasetEntity .setDatasetId (datasetId );
44
68
detectionEntity .setDatasetId (datasetEntity );
45
-
69
+ when ( datasetRepository . findById ( anyInt ())). thenReturn ( Optional . of ( datasetEntity ));
46
70
when (detectRepository .findDetectionEntityByDatasetId_DatasetId (anyInt ()))
47
71
.thenReturn (null )
48
72
.thenReturn (detectionEntity )
@@ -66,7 +90,7 @@ void processWhenNewHappyPath_Ready_andError_expectSuccess() {
66
90
final DatasetEntity datasetEntity = new DatasetEntity ();
67
91
datasetEntity .setDatasetId (datasetId );
68
92
detectionEntity .setDatasetId (datasetEntity );
69
-
93
+ when ( datasetRepository . findById ( anyInt ())). thenReturn ( Optional . of ( datasetEntity ));
70
94
when (detectRepository .findDetectionEntityByDatasetId_DatasetId (anyInt ()))
71
95
.thenThrow (new RuntimeException ("Error" ));
72
96
@@ -88,7 +112,7 @@ void processWhenNewHappyPath_Ready_Processing_Completed_andError_expectSuccess()
88
112
final DatasetEntity datasetEntity = new DatasetEntity ();
89
113
datasetEntity .setDatasetId (datasetId );
90
114
detectionEntity .setDatasetId (datasetEntity );
91
-
115
+ when ( datasetRepository . findById ( anyInt ())). thenReturn ( Optional . of ( datasetEntity ));
92
116
when (detectRepository .findDetectionEntityByDatasetId_DatasetId (anyInt ()))
93
117
.thenReturn (null )
94
118
.thenReturn (detectionEntity )
@@ -113,7 +137,7 @@ void processWhenNewHappyPath_Ready_Processing_Completed_andException_expectSucce
113
137
final DatasetEntity datasetEntity = new DatasetEntity ();
114
138
datasetEntity .setDatasetId (datasetId );
115
139
detectionEntity .setDatasetId (datasetEntity );
116
-
140
+ when ( datasetRepository . findById ( anyInt ())). thenReturn ( Optional . of ( datasetEntity ));
117
141
when (detectRepository .findDetectionEntityByDatasetId_DatasetId (anyInt ()))
118
142
.thenReturn (null )
119
143
.thenReturn (detectionEntity )
@@ -138,7 +162,7 @@ void processWhenNewHappyPath_Ready_Processing_Error_Ready_Processing_Completed_e
138
162
final DatasetEntity datasetEntity = new DatasetEntity ();
139
163
datasetEntity .setDatasetId (datasetId );
140
164
detectionEntity .setDatasetId (datasetEntity );
141
-
165
+ when ( datasetRepository . findById ( anyInt ())). thenReturn ( Optional . of ( datasetEntity ));
142
166
when (detectRepository .findDetectionEntityByDatasetId_DatasetId (anyInt ()))
143
167
.thenReturn (null )
144
168
.thenThrow (new RuntimeException ("Error" ))
@@ -165,7 +189,7 @@ void processWhenNewHappyPath_Ready_Processing_andError_expectSuccess() {
165
189
final DatasetEntity datasetEntity = new DatasetEntity ();
166
190
datasetEntity .setDatasetId (datasetId );
167
191
detectionEntity .setDatasetId (datasetEntity );
168
-
192
+ when ( datasetRepository . findById ( anyInt ())). thenReturn ( Optional . of ( datasetEntity ));
169
193
when (detectRepository .findDetectionEntityByDatasetId_DatasetId (anyInt ()))
170
194
.thenReturn (null )
171
195
.thenReturn (null )
@@ -184,9 +208,10 @@ void processWhenNewHappyPath_Ready_Processing_andError_expectSuccess() {
184
208
void processWhenDatasetAlreadyExists_Ready_Processing_Completed_expectSuccess () {
185
209
final Integer datasetId = 1 ;
186
210
final String stateName = "READY" ;
187
- final DatasetEntity dataset = new DatasetEntity ();
188
- dataset .setDatasetId (1 );
189
- final DetectionEntity detectionEntity = new DetectionEntity (dataset , stateName );
211
+ final DatasetEntity datasetEntity = new DatasetEntity ();
212
+ datasetEntity .setDatasetId (1 );
213
+ when (datasetRepository .findById (anyInt ())).thenReturn (Optional .of (datasetEntity ));
214
+ final DetectionEntity detectionEntity = new DetectionEntity (datasetEntity , stateName );
190
215
detectionEntity .setCreatedDate (ZonedDateTime .now ());
191
216
when (detectRepository .findDetectionEntityByDatasetId_DatasetId (anyInt ()))
192
217
.thenReturn (detectionEntity )
@@ -204,9 +229,10 @@ void processWhenDatasetAlreadyExists_Ready_Processing_Completed_expectSuccess()
204
229
void processWhenDatasetAlreadyExists_Ready_Processing_andError_expectSuccess () {
205
230
final Integer datasetId = 1 ;
206
231
final String stateName = "READY" ;
207
- final DatasetEntity dataset = new DatasetEntity ();
208
- dataset .setDatasetId (datasetId );
209
- final DetectionEntity detectionEntity = new DetectionEntity (dataset , stateName );
232
+ final DatasetEntity datasetEntity = new DatasetEntity ();
233
+ datasetEntity .setDatasetId (1 );
234
+ when (datasetRepository .findById (anyInt ())).thenReturn (Optional .of (datasetEntity ));
235
+ final DetectionEntity detectionEntity = new DetectionEntity (datasetEntity , stateName );
210
236
detectionEntity .setCreatedDate (ZonedDateTime .now ());
211
237
when (detectRepository .findDetectionEntityByDatasetId_DatasetId (anyInt ()))
212
238
.thenReturn (detectionEntity )
@@ -216,13 +242,14 @@ void processWhenDatasetAlreadyExists_Ready_Processing_andError_expectSuccess() {
216
242
217
243
assertFalse (result );
218
244
assertInstanceOf (ErrorState .class , debiasDetectService .getState ());
245
+
219
246
verify (detectRepository , times (3 )).findDetectionEntityByDatasetId_DatasetId (datasetId );
220
247
verify (detectRepository , times (1 )).updateState (anyInt (), anyString ());
221
248
}
222
249
223
250
@ Test
224
251
void set_and_get_State () {
225
- debiasDetectService .setState (new ReadyState (debiasDetectService , detectRepository ));
252
+ debiasDetectService .setState (new ReadyState (debiasDetectService , detectRepository , datasetRepository ));
226
253
assertInstanceOf (ReadyState .class , debiasDetectService .getState ());
227
254
228
255
debiasDetectService .setState (new ProcessingState (debiasDetectService , detectRepository ));
0 commit comments