Skip to content

Commit

Permalink
refactor: Use @Injectmocks for unit tests (#19283)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Nov 25, 2024
1 parent 7b15092 commit 3a43013
Show file tree
Hide file tree
Showing 19 changed files with 75 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ class DefaultTrackedEntityAnalyticsDimensionsService
implements TrackedEntityAnalyticsDimensionsService {
private final TrackedEntityTypeService trackedEntityTypeService;

private final EnrollmentAnalyticsDimensionsService enrollmentAnalyticsDimensionsService;

private final ProgramService programService;

private final EnrollmentAnalyticsDimensionsService enrollmentAnalyticsDimensionsService;

@Override
public List<PrefixedDimension> getQueryDimensionsByTrackedEntityTypeId(
String trackedEntityTypeId, Set<String> programUids) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,14 @@
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.YearlyPeriodType;
import org.hisp.dhis.test.TestBase;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.jdbc.core.JdbcTemplate;

Expand All @@ -80,9 +81,9 @@ class AnalyticsManagerTest extends TestBase {

@Mock private ExecutionPlanStore executionPlanStore;

private final SqlBuilder sqlBuilder = new PostgreSqlBuilder();
@Spy private final SqlBuilder sqlBuilder = new PostgreSqlBuilder();

private JdbcAnalyticsManager analyticsManager;
@InjectMocks private JdbcAnalyticsManager analyticsManager;

private static Stream<Arguments> data() {
return Stream.of(
Expand All @@ -92,12 +93,6 @@ private static Stream<Arguments> data() {
arguments("2017Nov", 26.5D));
}

@BeforeEach
void before() {
analyticsManager =
new JdbcAnalyticsManager(queryPlanner, jdbcTemplate, executionPlanStore, sqlBuilder);
}

@ParameterizedTest
@MethodSource("data")
public void testWeightedAverage(String financialYear, Double weightedAverage) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -103,14 +104,6 @@ class DataQueryServiceDimensionItemKeywordTest {
OrganisationUnit.class,
Set.of("geometry", "parent", "groups", "children")));

private DimensionalObjectProducer dimensionalObjectProducer;

private RequestBuilder rb;

private OrganisationUnit rootOu;

private DefaultDataQueryService target;

@Mock private IdentifiableObjectManager idObjectManager;

@Mock private OrganisationUnitService organisationUnitService;
Expand All @@ -129,18 +122,18 @@ class DataQueryServiceDimensionItemKeywordTest {

@Mock private I18n i18n;

@InjectMocks private DimensionalObjectProducer dimensionalObjectProducer;

private DefaultDataQueryService target;

private RequestBuilder rb;

private OrganisationUnit rootOu;

@BeforeEach
public void setUp() {
lenient().when(settingsService.getCurrentSettings()).thenReturn(SystemSettings.of(Map.of()));

dimensionalObjectProducer =
new DimensionalObjectProducer(
idObjectManager,
organisationUnitService,
settingsService,
i18nManager,
dimensionService,
aclService);
target =
new DefaultDataQueryService(dimensionalObjectProducer, idObjectManager, securityManager);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,40 @@
import java.util.Set;
import org.hisp.dhis.analytics.AnalyticsSecurityManager;
import org.hisp.dhis.analytics.DataQueryParams;
import org.hisp.dhis.analytics.DataQueryService;
import org.hisp.dhis.common.BaseIdentifiableObject;
import org.hisp.dhis.common.IdentifiableObjectManager;
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.user.User;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

/** Unit tests for {@link DataQueryService}. */
@ExtendWith(MockitoExtension.class)
class DataQueryServiceTest {

@Mock private DimensionalObjectProducer dimensionalObjectProducer;

@Mock private IdentifiableObjectManager idObjectManager;

@Mock private AnalyticsSecurityManager analyticsSecurityManager;

@InjectMocks private DefaultDataQueryService dataQueryService;

@Test
void testGetUserOrgUnitsWithExplicitlyDefinedAnalyticsOrganisationUnits() {
// given
OrganisationUnit ouB = createOrganisationUnit('B');
OrganisationUnit ouC = createOrganisationUnit('C');
OrganisationUnit ouD = createOrganisationUnit('D');
DataQueryParams dataQueryParams =
DataQueryParams.newBuilder().withUserOrgUnitType(DATA_OUTPUT).build();
User currentUser = mock(User.class);
AnalyticsSecurityManager analyticsSecurityManager = mock(AnalyticsSecurityManager.class);
when(currentUser.getDataViewOrganisationUnits()).thenReturn(Set.of(ouB, ouC, ouD));
when(analyticsSecurityManager.getCurrentUser(dataQueryParams)).thenReturn(currentUser);
DataQueryService dataQueryService =
new DefaultDataQueryService(
mock(DimensionalObjectProducer.class),
mock(IdentifiableObjectManager.class),
analyticsSecurityManager);

// when
List<OrganisationUnit> userOrgUnits = dataQueryService.getUserOrgUnits(dataQueryParams, null);

// then
assertEquals(3, userOrgUnits.size());
assertThat(
userOrgUnits.stream().map(BaseIdentifiableObject::getName).toList(),
Expand All @@ -82,25 +81,16 @@ void testGetUserOrgUnitsWithExplicitlyDefinedAnalyticsOrganisationUnits() {

@Test
void testGetUserOrgUnitsWithNoAnalyticsOrganisationUnitsDefined() {
// given
OrganisationUnit ouA = createOrganisationUnit('A');
DataQueryParams dataQueryParams =
DataQueryParams.newBuilder().withUserOrgUnitType(DATA_OUTPUT).build();
User currentUser = mock(User.class);
AnalyticsSecurityManager analyticsSecurityManager = mock(AnalyticsSecurityManager.class);
when(currentUser.getOrganisationUnits()).thenReturn(Set.of(ouA));
when(currentUser.getDataViewOrganisationUnits()).thenReturn(Set.of());
when(analyticsSecurityManager.getCurrentUser(dataQueryParams)).thenReturn(currentUser);
DataQueryService dataQueryService =
new DefaultDataQueryService(
mock(DimensionalObjectProducer.class),
mock(IdentifiableObjectManager.class),
analyticsSecurityManager);

// when
List<OrganisationUnit> userOrgUnits = dataQueryService.getUserOrgUnits(dataQueryParams, null);

// then
assertEquals(1, userOrgUnits.size());
assertThat(
userOrgUnits.stream().map(BaseIdentifiableObject::getName).toList(),
Expand All @@ -109,24 +99,11 @@ void testGetUserOrgUnitsWithNoAnalyticsOrganisationUnitsDefined() {

@Test
void testGetUserOrgUnitsWithNoneOrganisationUnitDefined() {
// given
DataQueryParams dataQueryParams =
DataQueryParams.newBuilder().withUserOrgUnitType(DATA_OUTPUT).build();
User currentUser = mock(User.class);
AnalyticsSecurityManager analyticsSecurityManager = mock(AnalyticsSecurityManager.class);
when(currentUser.getOrganisationUnits()).thenReturn(Set.of());
when(currentUser.getDataViewOrganisationUnits()).thenReturn(Set.of());
when(analyticsSecurityManager.getCurrentUser(dataQueryParams)).thenReturn(currentUser);
DataQueryService dataQueryService =
new DefaultDataQueryService(
mock(DimensionalObjectProducer.class),
mock(IdentifiableObjectManager.class),
analyticsSecurityManager);

// when
List<OrganisationUnit> userOrgUnits = dataQueryService.getUserOrgUnits(dataQueryParams, null);

// then
assertEquals(0, userOrgUnits.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

Expand All @@ -115,7 +116,6 @@
*/
@ExtendWith(MockitoExtension.class)
class DimensionalObjectProducerTest {
private DimensionalObjectProducer target;

@Mock private IdentifiableObjectManager idObjectManager;

Expand All @@ -124,6 +124,7 @@ class DimensionalObjectProducerTest {
@Mock private DimensionService dimensionService;

@Mock private SystemSettingsProvider settingsProvider;

@Mock private SystemSettings settings;

@Mock private AclService aclService;
Expand All @@ -134,17 +135,11 @@ class DimensionalObjectProducerTest {

@Mock private I18nFormat i18nFormat;

@InjectMocks private DimensionalObjectProducer target;

@BeforeEach
public void setUp() {
lenient().when(settingsProvider.getCurrentSettings()).thenReturn(settings);
target =
new DimensionalObjectProducer(
idObjectManager,
organisationUnitService,
settingsProvider,
i18nManager,
dimensionService,
aclService);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,13 @@
import org.hisp.dhis.period.Period;
import org.hisp.dhis.period.PeriodType;
import org.hisp.dhis.setting.SystemSettingsService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
Expand All @@ -82,20 +83,15 @@ class JdbcAnalyticsManagerTest {

@Mock private NestedIndicatorCyclicDependencyInspector nestedIndicatorCyclicDependencyInspector;

private final SqlBuilder sqlBuilder = new PostgreSqlBuilder();
@Mock private QueryPlanner queryPlanner;

@Captor private ArgumentCaptor<String> sql;
@Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder();

private JdbcAnalyticsManager subject;
@Captor private ArgumentCaptor<String> sql;

@Mock private ExecutionPlanStore executionPlanStore;

@BeforeEach
public void setUp() {
QueryPlanner queryPlanner = new DefaultQueryPlanner(partitionManager);

subject = new JdbcAnalyticsManager(queryPlanner, jdbcTemplate, executionPlanStore, sqlBuilder);
}
@InjectMocks private JdbcAnalyticsManager subject;

@Test
void verifyQueryGeneratedWhenDataElementHasLastAggregationType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.period.Period;
import org.hisp.dhis.subexpression.SubexpressionDimensionItem;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.jdbc.core.JdbcTemplate;

Expand All @@ -80,20 +81,15 @@ class JdbcSubexpressionQueryGeneratorTest {

@Mock private ExecutionPlanStore executionPlanStore;

private final SqlBuilder sqlBuilder = new PostgreSqlBuilder();
@Mock private QueryPlanner queryPlanner;

private JdbcAnalyticsManager jam;
@Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder();

@InjectMocks private JdbcAnalyticsManager manager;

/** Matches a UID with an initial single quote. */
private static final Pattern QUOTED_UID = Pattern.compile("'\\w{11}'");

@BeforeAll
public void setUp() {
QueryPlanner queryPlanner = new DefaultQueryPlanner(partitionManager);

jam = new JdbcAnalyticsManager(queryPlanner, jdbcTemplate, executionPlanStore, sqlBuilder);
}

@Test
void testGetSql() {
OrganisationUnit ouA = createOrganisationUnit('A');
Expand Down Expand Up @@ -147,7 +143,7 @@ ORGUNIT_DIM_ID, DimensionType.ORGANISATION_UNIT, getList(ouA)))
.build();

JdbcSubexpressionQueryGenerator target =
new JdbcSubexpressionQueryGenerator(jam, params, DATA_VALUE);
new JdbcSubexpressionQueryGenerator(manager, params, DATA_VALUE);

String expected =
"select ax.\"pe\",'subexprxUID' as \"dx\","
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@
import org.hisp.dhis.analytics.DataQueryGroups;
import org.hisp.dhis.analytics.DataQueryParams;
import org.hisp.dhis.analytics.DataType;
import org.hisp.dhis.analytics.QueryPlanner;
import org.hisp.dhis.analytics.QueryPlannerParams;
import org.hisp.dhis.analytics.partition.PartitionManager;
import org.hisp.dhis.category.CategoryCombo;
Expand All @@ -61,9 +60,9 @@
import org.hisp.dhis.organisationunit.OrganisationUnit;
import org.hisp.dhis.period.MonthlyPeriodType;
import org.joda.time.DateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

Expand All @@ -72,14 +71,9 @@
*/
@ExtendWith(MockitoExtension.class)
class QueryPlannerGroupByAggregationTypeTest {
private QueryPlanner subject;

@Mock private PartitionManager partitionManager;

@BeforeEach
public void setUp() {
subject = new DefaultQueryPlanner(partitionManager);
}
@InjectMocks private DefaultQueryPlanner subject;

@Test
void verifyMultipleDataElementIsAggregatedWithTwoQueryGroupWhenDataTypeIsDifferent() {
Expand Down
Loading

0 comments on commit 3a43013

Please sign in to comment.