Skip to content

Commit

Permalink
Use ObjectProvider for service dependency (#261)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Peels <[email protected]>
  • Loading branch information
mpeels and Michael Peels authored Dec 19, 2024
1 parent ffce196 commit 02f877c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -47,14 +48,14 @@ public PatientMatchingService(
CachingValueService cachingValueService,
PrepareAssocModelHelper prepareAssocModelHelper,
@Value("${features.modernizedMatching.enabled:false}") boolean modernizedMatchingEnabled,
DeduplicationService deduplicationService) {
ObjectProvider<DeduplicationService> deduplicationService) {
super(edxPatientMatchRepositoryUtil,
entityHelper,
patientRepositoryUtil,
cachingValueService,
prepareAssocModelHelper);
this.modernizedMatchingEnabled = modernizedMatchingEnabled;
this.deduplicationService = deduplicationService;
this.deduplicationService = deduplicationService.getIfAvailable();
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package gov.cdc.dataprocessing.service.implementation.person.matching;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;

@Configuration
@ConditionalOnProperty(name = "${features.modernizedMatching.enabled}", havingValue = "true")
public class DeduplicationClient {

@Bean("deduplicationRestClient")
public RestClient restClient(@Value("${features.modernizedMatching.url}") String modernizedMatchingUrl) {
return RestClient.builder().baseUrl(modernizedMatchingUrl).build();
@Bean
public DeduplicationService deduplicationService(
@Value("${features.modernizedMatching.url}") String modernizedMatchingUrl) {
return new DeduplicationService(RestClient.builder().baseUrl(modernizedMatchingUrl).build());
}

}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package gov.cdc.dataprocessing.service.implementation.person.matching;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestClient;

@Component
public class DeduplicationService {

private RestClient restClient;

public DeduplicationService(
@Qualifier("deduplicationRestClient") RestClient restClient) {
public DeduplicationService(RestClient restClient) {
this.restClient = restClient;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,26 @@
package gov.cdc.dataprocessing.service.implementation.person;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.sql.Timestamp;
import java.util.List;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.ObjectProvider;

import gov.cdc.dataprocessing.constant.elr.EdxELRConstant;
import gov.cdc.dataprocessing.constant.elr.NEDSSConstant;
import gov.cdc.dataprocessing.exception.DataProcessingException;
Expand All @@ -14,29 +35,12 @@
import gov.cdc.dataprocessing.service.implementation.person.matching.LinkResponse;
import gov.cdc.dataprocessing.service.implementation.person.matching.LinkResponse.Results;
import gov.cdc.dataprocessing.service.implementation.person.matching.MatchResponse;
import gov.cdc.dataprocessing.service.implementation.person.matching.PersonMatchRequest;
import gov.cdc.dataprocessing.service.implementation.person.matching.MatchResponse.MatchType;
import gov.cdc.dataprocessing.service.implementation.person.matching.PersonMatchRequest;
import gov.cdc.dataprocessing.utilities.component.entity.EntityHelper;
import gov.cdc.dataprocessing.utilities.component.generic_helper.PrepareAssocModelHelper;
import gov.cdc.dataprocessing.utilities.component.patient.EdxPatientMatchRepositoryUtil;
import gov.cdc.dataprocessing.utilities.component.patient.PatientRepositoryUtil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

import java.sql.Timestamp;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class PatientMatchingServiceTest {

Expand All @@ -51,21 +55,24 @@ class PatientMatchingServiceTest {
@Mock
private PrepareAssocModelHelper prepareAssocModelHelper;
@Mock
private ObjectProvider<DeduplicationService> serviceProvider;
@Mock
private DeduplicationService deduplicationService;

private PatientMatchingService patientMatchingService;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
when(serviceProvider.getIfAvailable()).thenReturn(null);
patientMatchingService = new PatientMatchingService(
edxPatientMatchRepositoryUtil,
entityHelper,
patientRepositoryUtil,
cachingValueService,
prepareAssocModelHelper,
false,
deduplicationService);
serviceProvider);
}

@AfterEach
Expand All @@ -87,14 +94,15 @@ void shouldReturnNullIfRoleIsNotPat() throws DataProcessingException {

@Test
void shouldPerformModernizedMatching() throws DataProcessingException {
when(serviceProvider.getIfAvailable()).thenReturn(deduplicationService);
patientMatchingService = new PatientMatchingService(
edxPatientMatchRepositoryUtil,
entityHelper,
patientRepositoryUtil,
cachingValueService,
prepareAssocModelHelper,
true,
deduplicationService);
serviceProvider);
when(deduplicationService.match(Mockito.any(PersonMatchRequest.class))).thenReturn(new MatchResponse(
1l,
MatchType.EXACT,
Expand All @@ -120,14 +128,15 @@ void shouldPerformModernizedMatching() throws DataProcessingException {

@Test
void modernizedMatchingThrowsException() {
when(serviceProvider.getIfAvailable()).thenReturn(deduplicationService);
patientMatchingService = new PatientMatchingService(
edxPatientMatchRepositoryUtil,
entityHelper,
patientRepositoryUtil,
cachingValueService,
prepareAssocModelHelper,
true,
deduplicationService);
serviceProvider);
when(deduplicationService.match(Mockito.any(PersonMatchRequest.class)))
.thenReturn(null);
PersonContainer container = new PersonContainer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.springframework.web.client.RestClient;

class DeduplicationClientTest {

@Test
void setsBaseUrl() {
DeduplicationClient client = new DeduplicationClient();

RestClient restClient = client.restClient("someUrl");
DeduplicationService service = client.deduplicationService("someUrl");

assertThat(restClient).isNotNull();
assertThat(service).isNotNull();
}

@Test
void setsNullBaseUrl() {
DeduplicationClient client = new DeduplicationClient();

RestClient restClient = client.restClient(null);
DeduplicationService service = client.deduplicationService(null);

assertThat(restClient).isNotNull();
assertThat(service).isNotNull();
}
}

0 comments on commit 02f877c

Please sign in to comment.