Skip to content

Commit 3d5ddc1

Browse files
committed
MET-5960 update unit tests, fix detect state, add debias client
1 parent 153e929 commit 3d5ddc1

File tree

10 files changed

+118
-58
lines changed

10 files changed

+118
-58
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@
266266
</exclusions>
267267
</dependency>
268268

269+
<dependency>
270+
<groupId>eu.europeana.metis</groupId>
271+
<artifactId>metis-debias-detect-service</artifactId>
272+
<version>${version.metis}</version>
273+
</dependency>
274+
269275
<dependency>
270276
<groupId>eu.europeana.metis</groupId>
271277
<artifactId>metis-media-service</artifactId>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package eu.europeana.metis.sandbox.config;
2+
3+
4+
import eu.europeana.metis.debias.detect.client.DeBiasClient;
5+
import eu.europeana.metis.sandbox.repository.DatasetRepository;
6+
import eu.europeana.metis.sandbox.repository.debias.DetectRepository;
7+
import eu.europeana.metis.sandbox.service.debias.DeBiasDetectService;
8+
import eu.europeana.metis.sandbox.service.debias.DetectService;
9+
import org.springframework.beans.factory.annotation.Value;
10+
import org.springframework.context.annotation.Bean;
11+
import org.springframework.context.annotation.Configuration;
12+
13+
/**
14+
* The type DeBias config.
15+
*/
16+
@Configuration
17+
public class DeBiasConfig {
18+
19+
@Value("${sandbox.debias.url}")
20+
private String apiUrl;
21+
22+
@Value("${sandbox.debias.connectTimeout}")
23+
private int connectTimeout;
24+
25+
@Value("${sandbox.debias.requestTimeout}")
26+
private int requestTimeout;
27+
28+
/**
29+
* Debias machine detect service.
30+
*
31+
* @param detectRepository the detect repository
32+
* @return the detect service
33+
*/
34+
@Bean
35+
public DetectService debiasMachine(DetectRepository detectRepository, DatasetRepository datasetRepository) {
36+
return new DeBiasDetectService(detectRepository, datasetRepository);
37+
}
38+
39+
@Bean
40+
public DeBiasClient deBiasClient() {
41+
return new DeBiasClient(apiUrl, connectTimeout, requestTimeout);
42+
}
43+
}
44+

src/main/java/eu/europeana/metis/sandbox/config/DebiasConfig.java

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

src/main/java/eu/europeana/metis/sandbox/entity/debias/DetectionEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public class DetectionEntity {
2323
@GeneratedValue(strategy = GenerationType.IDENTITY)
2424
private Long id;
2525

26-
@OneToOne(cascade = CascadeType.PERSIST)
26+
@OneToOne(cascade = CascadeType.MERGE)
2727
@JoinColumn(name = "dataset_id", referencedColumnName = "datasetId")
2828
private DatasetEntity datasetId;
2929

src/main/java/eu/europeana/metis/sandbox/repository/debias/DetectRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface DetectRepository extends JpaRepository<DetectionEntity, Long> {
2525
* @param state the state
2626
*/
2727
@Modifying
28-
@Query("UPDATE DetectionEntity dec SET dec.state = '?2' WHERE dec.datasetId = ?1")
28+
@Query("UPDATE DetectionEntity dec SET dec.state = ?2 WHERE dec.datasetId.datasetId = ?1")
2929
void updateState(Integer datasetId, String state);
3030

3131
}

src/main/java/eu/europeana/metis/sandbox/service/debias/DebiasDetectService.java renamed to src/main/java/eu/europeana/metis/sandbox/service/debias/DeBiasDetectService.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
import eu.europeana.metis.sandbox.dto.debias.DetectionInfoDto;
44
import eu.europeana.metis.sandbox.entity.debias.DetectionEntity;
5+
import eu.europeana.metis.sandbox.repository.DatasetRepository;
56
import eu.europeana.metis.sandbox.repository.debias.DetectRepository;
67
import java.time.ZonedDateTime;
8+
import org.springframework.transaction.annotation.Transactional;
79

810
/**
911
* The type DeBias detect service.
1012
*/
11-
public class DebiasDetectService implements DetectService {
13+
public class DeBiasDetectService implements DetectService {
1214

1315
private static final String INITIAL_STATE = "READY";
1416
private final Stateful ready;
@@ -23,8 +25,8 @@ public class DebiasDetectService implements DetectService {
2325
*
2426
* @param detectRepository the detect repository
2527
*/
26-
public DebiasDetectService(DetectRepository detectRepository) {
27-
this.ready = new ReadyState(this, detectRepository);
28+
public DeBiasDetectService(DetectRepository detectRepository, DatasetRepository datasetRepository) {
29+
this.ready = new ReadyState(this, detectRepository, datasetRepository);
2830
this.processing = new ProcessingState(this, detectRepository);
2931
this.completed = new CompletedState(this, detectRepository);
3032
this.error = new ErrorState(this, detectRepository);
@@ -42,6 +44,7 @@ public void success(Integer datasetId) {
4244
state.success(datasetId);
4345
}
4446

47+
@Transactional
4548
@Override
4649
public boolean process(Integer datasetId) {
4750
return state.process(datasetId);

src/main/java/eu/europeana/metis/sandbox/service/debias/ReadyState.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import eu.europeana.metis.sandbox.entity.DatasetEntity;
44
import eu.europeana.metis.sandbox.entity.debias.DetectionEntity;
5+
import eu.europeana.metis.sandbox.repository.DatasetRepository;
56
import eu.europeana.metis.sandbox.repository.debias.DetectRepository;
67
import org.slf4j.Logger;
78
import org.slf4j.LoggerFactory;
@@ -14,6 +15,7 @@ public class ReadyState extends State implements Stateful {
1415

1516
private static final Logger LOGGER = LoggerFactory.getLogger(ReadyState.class);
1617
private static final String STATE_NAME = "READY";
18+
private final DatasetRepository datasetRepository;
1719

1820
/**
1921
* Instantiates a new Ready state.
@@ -22,10 +24,11 @@ public class ReadyState extends State implements Stateful {
2224
* @param detectRepository the detect repository
2325
*/
2426
public ReadyState(DetectService debiasMachine,
25-
DetectRepository detectRepository) {
27+
DetectRepository detectRepository, DatasetRepository datasetRepository) {
2628
this.stateMachine = debiasMachine;
2729
this.name = STATE_NAME;
2830
this.detectRepository = detectRepository;
31+
this.datasetRepository = datasetRepository;
2932
this.terminalState = false;
3033
}
3134

@@ -44,13 +47,10 @@ public void success(Integer datasetId) {
4447
public boolean process(Integer datasetId) {
4548
LOGGER.info("{} {}", STATE_NAME, datasetId);
4649
try {
50+
DatasetEntity dataset = datasetRepository.findById(datasetId).orElseThrow();
4751
DetectionEntity detectionEntity = detectRepository.findDetectionEntityByDatasetId_DatasetId(datasetId);
4852
if (detectionEntity == null) {
49-
detectionEntity = new DetectionEntity();
50-
detectionEntity.setState(STATE_NAME);
51-
DatasetEntity dataset = new DatasetEntity();
52-
dataset.setDatasetId(datasetId);
53-
detectionEntity.setDatasetId(dataset);
53+
detectionEntity = new DetectionEntity(dataset, STATE_NAME);
5454
detectRepository.save(detectionEntity);
5555
} else {
5656
detectRepository.updateState(datasetId, STATE_NAME);

src/main/resources/sample.application.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ sandbox:
9292
core-pool-size: 10
9393
max-pool-size: 40
9494
thread-prefix: sandbox-
95+
debias:
96+
url:
97+
connectTimeout: 300
98+
requestTimeout: 300
99+
batchSize: 20
100+
workers: 2
95101
metrics:
96102
frequency: '*/5 * * * * *' # every five seconds.
97103
validation:

src/test/java/eu/europeana/metis/sandbox/service/debias/DebiasDetectServiceTest.java renamed to src/test/java/eu/europeana/metis/sandbox/service/debias/DeBiasDetectServiceTest.java

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,46 @@
1515
import eu.europeana.metis.sandbox.dto.debias.DetectionInfoDto;
1616
import eu.europeana.metis.sandbox.entity.DatasetEntity;
1717
import eu.europeana.metis.sandbox.entity.debias.DetectionEntity;
18+
import eu.europeana.metis.sandbox.repository.DatasetRepository;
1819
import eu.europeana.metis.sandbox.repository.debias.DetectRepository;
1920
import java.time.ZonedDateTime;
21+
import java.util.NoSuchElementException;
22+
import java.util.Optional;
2023
import org.junit.jupiter.api.Test;
2124
import org.junit.jupiter.api.extension.ExtendWith;
2225
import org.mockito.InjectMocks;
2326
import org.mockito.Mock;
2427
import org.mockito.junit.jupiter.MockitoExtension;
2528

2629
@ExtendWith(MockitoExtension.class)
27-
class DebiasDetectServiceTest {
30+
class DeBiasDetectServiceTest {
2831

2932
@Mock
3033
DetectRepository detectRepository;
3134

35+
@Mock
36+
DatasetRepository datasetRepository;
37+
3238
@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+
}
3458

3559

3660
@Test
@@ -42,7 +66,7 @@ void processWhenNewHappyPath_Ready_Processing_Completed_expectSuccess() {
4266
final DatasetEntity datasetEntity = new DatasetEntity();
4367
datasetEntity.setDatasetId(datasetId);
4468
detectionEntity.setDatasetId(datasetEntity);
45-
69+
when(datasetRepository.findById(anyInt())).thenReturn(Optional.of(datasetEntity));
4670
when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt()))
4771
.thenReturn(null)
4872
.thenReturn(detectionEntity)
@@ -66,7 +90,7 @@ void processWhenNewHappyPath_Ready_andError_expectSuccess() {
6690
final DatasetEntity datasetEntity = new DatasetEntity();
6791
datasetEntity.setDatasetId(datasetId);
6892
detectionEntity.setDatasetId(datasetEntity);
69-
93+
when(datasetRepository.findById(anyInt())).thenReturn(Optional.of(datasetEntity));
7094
when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt()))
7195
.thenThrow(new RuntimeException("Error"));
7296

@@ -88,7 +112,7 @@ void processWhenNewHappyPath_Ready_Processing_Completed_andError_expectSuccess()
88112
final DatasetEntity datasetEntity = new DatasetEntity();
89113
datasetEntity.setDatasetId(datasetId);
90114
detectionEntity.setDatasetId(datasetEntity);
91-
115+
when(datasetRepository.findById(anyInt())).thenReturn(Optional.of(datasetEntity));
92116
when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt()))
93117
.thenReturn(null)
94118
.thenReturn(detectionEntity)
@@ -113,7 +137,7 @@ void processWhenNewHappyPath_Ready_Processing_Completed_andException_expectSucce
113137
final DatasetEntity datasetEntity = new DatasetEntity();
114138
datasetEntity.setDatasetId(datasetId);
115139
detectionEntity.setDatasetId(datasetEntity);
116-
140+
when(datasetRepository.findById(anyInt())).thenReturn(Optional.of(datasetEntity));
117141
when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt()))
118142
.thenReturn(null)
119143
.thenReturn(detectionEntity)
@@ -138,7 +162,7 @@ void processWhenNewHappyPath_Ready_Processing_Error_Ready_Processing_Completed_e
138162
final DatasetEntity datasetEntity = new DatasetEntity();
139163
datasetEntity.setDatasetId(datasetId);
140164
detectionEntity.setDatasetId(datasetEntity);
141-
165+
when(datasetRepository.findById(anyInt())).thenReturn(Optional.of(datasetEntity));
142166
when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt()))
143167
.thenReturn(null)
144168
.thenThrow(new RuntimeException("Error"))
@@ -165,7 +189,7 @@ void processWhenNewHappyPath_Ready_Processing_andError_expectSuccess() {
165189
final DatasetEntity datasetEntity = new DatasetEntity();
166190
datasetEntity.setDatasetId(datasetId);
167191
detectionEntity.setDatasetId(datasetEntity);
168-
192+
when(datasetRepository.findById(anyInt())).thenReturn(Optional.of(datasetEntity));
169193
when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt()))
170194
.thenReturn(null)
171195
.thenReturn(null)
@@ -184,9 +208,10 @@ void processWhenNewHappyPath_Ready_Processing_andError_expectSuccess() {
184208
void processWhenDatasetAlreadyExists_Ready_Processing_Completed_expectSuccess() {
185209
final Integer datasetId = 1;
186210
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);
190215
detectionEntity.setCreatedDate(ZonedDateTime.now());
191216
when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt()))
192217
.thenReturn(detectionEntity)
@@ -204,9 +229,10 @@ void processWhenDatasetAlreadyExists_Ready_Processing_Completed_expectSuccess()
204229
void processWhenDatasetAlreadyExists_Ready_Processing_andError_expectSuccess() {
205230
final Integer datasetId = 1;
206231
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);
210236
detectionEntity.setCreatedDate(ZonedDateTime.now());
211237
when(detectRepository.findDetectionEntityByDatasetId_DatasetId(anyInt()))
212238
.thenReturn(detectionEntity)
@@ -216,13 +242,14 @@ void processWhenDatasetAlreadyExists_Ready_Processing_andError_expectSuccess() {
216242

217243
assertFalse(result);
218244
assertInstanceOf(ErrorState.class, debiasDetectService.getState());
245+
219246
verify(detectRepository, times(3)).findDetectionEntityByDatasetId_DatasetId(datasetId);
220247
verify(detectRepository, times(1)).updateState(anyInt(), anyString());
221248
}
222249

223250
@Test
224251
void set_and_get_State() {
225-
debiasDetectService.setState(new ReadyState(debiasDetectService, detectRepository));
252+
debiasDetectService.setState(new ReadyState(debiasDetectService, detectRepository, datasetRepository));
226253
assertInstanceOf(ReadyState.class, debiasDetectService.getState());
227254

228255
debiasDetectService.setState(new ProcessingState(debiasDetectService, detectRepository));

src/test/resources/application.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ sandbox:
9292
core-pool-size: 10
9393
max-pool-size: 40
9494
thread-prefix: sandbox-
95+
debias:
96+
url: http://localhost:8080/metis-debias
97+
connectTimeout: 300
98+
requestTimeout: 300
99+
batchSize: 20
100+
workers: 2
95101
metrics:
96102
frequency: '*/5 * * * * *' # every five seconds.
97103
validation:

0 commit comments

Comments
 (0)