diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/trackedentity/DefaultTrackedEntityAnalyticsDimensionsService.java b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/trackedentity/DefaultTrackedEntityAnalyticsDimensionsService.java index 2e2d0e64c819..c71c5441c46e 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/trackedentity/DefaultTrackedEntityAnalyticsDimensionsService.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/main/java/org/hisp/dhis/analytics/trackedentity/DefaultTrackedEntityAnalyticsDimensionsService.java @@ -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 getQueryDimensionsByTrackedEntityTypeId( String trackedEntityTypeId, Set programUids) { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsManagerTest.java index 42bf5627e0b5..e4c6b410c5e3 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/AnalyticsManagerTest.java @@ -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; @@ -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 data() { return Stream.of( @@ -92,12 +93,6 @@ private static Stream 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) { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceDimensionItemKeywordTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceDimensionItemKeywordTest.java index e0ab2257f7c1..399c9c4741fa 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceDimensionItemKeywordTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceDimensionItemKeywordTest.java @@ -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; @@ -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; @@ -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); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceTest.java index d798cfb0fe63..0ea3ea985f43 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DataQueryServiceTest.java @@ -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 userOrgUnits = dataQueryService.getUserOrgUnits(dataQueryParams, null); - // then assertEquals(3, userOrgUnits.size()); assertThat( userOrgUnits.stream().map(BaseIdentifiableObject::getName).toList(), @@ -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 userOrgUnits = dataQueryService.getUserOrgUnits(dataQueryParams, null); - // then assertEquals(1, userOrgUnits.size()); assertThat( userOrgUnits.stream().map(BaseIdentifiableObject::getName).toList(), @@ -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 userOrgUnits = dataQueryService.getUserOrgUnits(dataQueryParams, null); - // then assertEquals(0, userOrgUnits.size()); } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DimensionalObjectProducerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DimensionalObjectProducerTest.java index cabae51fb2e9..3389f894a488 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DimensionalObjectProducerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/DimensionalObjectProducerTest.java @@ -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; @@ -115,7 +116,6 @@ */ @ExtendWith(MockitoExtension.class) class DimensionalObjectProducerTest { - private DimensionalObjectProducer target; @Mock private IdentifiableObjectManager idObjectManager; @@ -124,6 +124,7 @@ class DimensionalObjectProducerTest { @Mock private DimensionService dimensionService; @Mock private SystemSettingsProvider settingsProvider; + @Mock private SystemSettings settings; @Mock private AclService aclService; @@ -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 diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManagerTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManagerTest.java index 75893f264588..37af80ed2508 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManagerTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/JdbcAnalyticsManagerTest.java @@ -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; @@ -82,20 +83,15 @@ class JdbcAnalyticsManagerTest { @Mock private NestedIndicatorCyclicDependencyInspector nestedIndicatorCyclicDependencyInspector; - private final SqlBuilder sqlBuilder = new PostgreSqlBuilder(); + @Mock private QueryPlanner queryPlanner; - @Captor private ArgumentCaptor sql; + @Spy private SqlBuilder sqlBuilder = new PostgreSqlBuilder(); - private JdbcAnalyticsManager subject; + @Captor private ArgumentCaptor 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() { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/JdbcSubexpressionQueryGeneratorTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/JdbcSubexpressionQueryGeneratorTest.java index 2c954f291ab1..f7f4c363b727 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/JdbcSubexpressionQueryGeneratorTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/JdbcSubexpressionQueryGeneratorTest.java @@ -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; @@ -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'); @@ -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\"," diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerGroupByAggregationTypeTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerGroupByAggregationTypeTest.java index 5b1731e89464..3cc54c5d8a81 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerGroupByAggregationTypeTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/data/QueryPlannerGroupByAggregationTypeTest.java @@ -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; @@ -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; @@ -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() { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityAnalyticsDimensionsServiceTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityAnalyticsDimensionsServiceTest.java index a0699faaa676..cb98c9e24b7c 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityAnalyticsDimensionsServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityAnalyticsDimensionsServiceTest.java @@ -32,49 +32,39 @@ import static org.hisp.dhis.analytics.common.DimensionServiceCommonTest.queryDisallowedValueTypesPredicate; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.Collection; -import org.hisp.dhis.analytics.event.data.DefaultEnrollmentAnalyticsDimensionsService; import org.hisp.dhis.common.BaseIdentifiableObject; import org.hisp.dhis.common.PrefixedDimension; import org.hisp.dhis.dataelement.DataElement; import org.hisp.dhis.program.ProgramService; -import org.hisp.dhis.security.acl.AclService; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.trackedentity.TrackedEntityTypeService; 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; @ExtendWith(MockitoExtension.class) class TrackedEntityAnalyticsDimensionsServiceTest { - @Mock private ProgramService programService; - @Mock private TrackedEntityTypeService trackedEntityTypeService; - private TrackedEntityAnalyticsDimensionsService trackedEntityAnalyticsDimensionsService; + @Mock private ProgramService programService; + + @InjectMocks private DefaultTrackedEntityAnalyticsDimensionsService service; @BeforeEach void setup() { when(trackedEntityTypeService.getTrackedEntityType(any())).thenReturn(trackedEntityType()); - - trackedEntityAnalyticsDimensionsService = - new DefaultTrackedEntityAnalyticsDimensionsService( - trackedEntityTypeService, - new DefaultEnrollmentAnalyticsDimensionsService(programService, mock(AclService.class)), - programService); } @Test void testQueryDoesNotContainDisallowedValueTypes() { Collection analyticsDimensions = - trackedEntityAnalyticsDimensionsService - .getQueryDimensionsByTrackedEntityTypeId("aTeiId", emptySet()) - .stream() + service.getQueryDimensionsByTrackedEntityTypeId("aTeiId", emptySet()).stream() .map(PrefixedDimension::getItem) .toList(); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java index 401d6acceae6..11e874343ee0 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityQueryRequestValidatorTest.java @@ -44,38 +44,32 @@ class TrackedEntityQueryRequestValidatorTest { @Test void testValidateWhenTrackedEntityTypeIsInvalid() { String teiUid = CodeGenerator.generateUid() + "invalid"; - // Given TrackedEntityRequestParams trackedEntityRequestParams = new TrackedEntityRequestParams(teiUid); TrackedEntityQueryRequestValidator trackedEntityQueryRequestValidator = new TrackedEntityQueryRequestValidator(); - // When IllegalQueryException exception = assertThrows( IllegalQueryException.class, () -> trackedEntityQueryRequestValidator.validate(trackedEntityRequestParams)); - // Then assertEquals( "Invalid UID `" + teiUid + "` for property `trackedEntityType`", exception.getMessage()); } @Test void testValidateWhenNoTrackedEntityType() { - // Given TrackedEntityRequestParams trackedEntityRequestParams = new TrackedEntityRequestParams(null); TrackedEntityQueryRequestValidator trackedEntityQueryRequestValidator = new TrackedEntityQueryRequestValidator(); - // When IllegalQueryException exception = assertThrows( IllegalQueryException.class, () -> trackedEntityQueryRequestValidator.validate(trackedEntityRequestParams)); - // Then assertEquals("Invalid UID `null` for property `trackedEntityType`", exception.getMessage()); } } diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityRequestParamsMapperTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityRequestParamsMapperTest.java index f6d7e6560966..3b6159970d1a 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityRequestParamsMapperTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/TrackedEntityRequestParamsMapperTest.java @@ -51,7 +51,7 @@ class TrackedEntityRequestParamsMapperTest { @Mock private ProgramService programService; - @InjectMocks private TrackedEntityQueryRequestMapper trackedEntityQueryRequestMapper; + @InjectMocks private TrackedEntityQueryRequestMapper mapper; @Test void testOneProgramFailing() { @@ -82,8 +82,7 @@ void testValidateTrackedEntityType(String trackedEntityTypeUid, String expectedM final IllegalQueryException thrown = assertThrows( - IllegalQueryException.class, - () -> trackedEntityQueryRequestMapper.map(trackedEntityTypeUid, requestParams)); + IllegalQueryException.class, () -> mapper.map(trackedEntityTypeUid, requestParams)); assertEquals(expectedMessage, thrown.getMessage()); } @@ -105,8 +104,7 @@ void testOK() { when(programService.getPrograms(Set.of("A", "B"))).thenReturn(Set.of(programA, programB)); - assertDoesNotThrow( - () -> trackedEntityQueryRequestMapper.map(trackedEntityTypeUid, requestParams)); + assertDoesNotThrow(() -> mapper.map(trackedEntityTypeUid, requestParams)); } @Test @@ -120,8 +118,7 @@ void testOKWhenNoPrograms() { when(trackedEntityTypeService.getTrackedEntityType(trackedEntityTypeUid)) .thenReturn(trackedEntityType); - assertDoesNotThrow( - () -> trackedEntityQueryRequestMapper.map(trackedEntityTypeUid, requestParams)); + assertDoesNotThrow(() -> mapper.map(trackedEntityTypeUid, requestParams)); } private Program stubProgram(String uid, String tetUid) { diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/StatusConditionTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/StatusConditionTest.java index 11e563192ba8..6be1c6d0a446 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/StatusConditionTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/StatusConditionTest.java @@ -46,7 +46,6 @@ class StatusConditionTest { @Test void testProgramStatusCompletedProduceCorrectSql() { - // SETUP List values = List.of("COMPLETED"); DimensionIdentifier dimensionIdentifier = @@ -56,7 +55,6 @@ void testProgramStatusCompletedProduceCorrectSql() { DimensionParam.StaticDimension.ENROLLMENT_STATUS, values); - // CALL SqlParameterManager sqlParameterManager = new SqlParameterManager(); QueryContext queryContext = QueryContext.of(null, sqlParameterManager); @@ -70,7 +68,6 @@ void testProgramStatusCompletedProduceCorrectSql() { @Test void testProgramStatusCompletedActiveProduceCorrectSql() { - // SETUP List values = List.of("COMPLETED", "ACTIVE"); DimensionIdentifier dimensionIdentifier = @@ -80,7 +77,6 @@ void testProgramStatusCompletedActiveProduceCorrectSql() { DimensionParam.StaticDimension.ENROLLMENT_STATUS, values); - // CALL SqlParameterManager sqlParameterManager = new SqlParameterManager(); QueryContext queryContext = QueryContext.of(null, sqlParameterManager); @@ -94,14 +90,12 @@ void testProgramStatusCompletedActiveProduceCorrectSql() { @Test void testEventStatusCompletedProduceCorrectSql() { - // SETUP List values = List.of("COMPLETED"); DimensionIdentifier dimensionIdentifier = getProgramAttributeDimensionIdentifier( "programUid", "programStageUid", DimensionParam.StaticDimension.EVENT_STATUS, values); - // CALL SqlParameterManager sqlParameterManager = new SqlParameterManager(); QueryContext queryContext = QueryContext.of(null, sqlParameterManager); @@ -115,14 +109,12 @@ void testEventStatusCompletedProduceCorrectSql() { @Test void testEventStatusCompletedScheduleProduceCorrectSql() { - // SETUP List values = List.of("COMPLETED", "SCHEDULE"); DimensionIdentifier dimensionIdentifier = getProgramAttributeDimensionIdentifier( "programUid", "programStageUid", DimensionParam.StaticDimension.EVENT_STATUS, values); - // CALL SqlParameterManager sqlParameterManager = new SqlParameterManager(); QueryContext queryContext = QueryContext.of(null, sqlParameterManager); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/TrackedEntityAttributeConditionTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/TrackedEntityAttributeConditionTest.java index d8f15c6cffa3..21bd12db5e88 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/TrackedEntityAttributeConditionTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/TrackedEntityAttributeConditionTest.java @@ -46,13 +46,11 @@ class TrackedEntityAttributeConditionTest { @Test void testProgramAttributeConditionProduceCorrectSql() { - // SETUP String attr = "attr"; List values = List.of("eq:v1"); DimensionIdentifier dimensionIdentifier = getProgramAttributeDimensionIdentifier("attr", values); - // CALL SqlParameterManager sqlParameterManager = new SqlParameterManager(); QueryContext queryContext = QueryContext.of(null, sqlParameterManager); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/SqlQueryHelperTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/SqlQueryHelperTest.java index 3bd06e1d8ae6..289140c447d3 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/SqlQueryHelperTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/analytics/trackedentity/query/context/querybuilder/SqlQueryHelperTest.java @@ -41,18 +41,27 @@ import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramStage; import org.hisp.dhis.trackedentity.TrackedEntityType; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; -/** Unit tests for {@link SqlQueryHelper}. */ +@ExtendWith(MockitoExtension.class) @SuppressWarnings("unchecked") class SqlQueryHelperTest { - @Test - void test_throws_when_undetected_type() { - DimensionParam dimensionParam = mock(DimensionParam.class); - DimensionIdentifier testedDimension = mock(DimensionIdentifier.class); + @Mock private DimensionParam dimensionParam; + + @Mock private DimensionIdentifier testedDimension; + + @BeforeEach + void beforeEach() { when(testedDimension.getDimension()).thenReturn(dimensionParam); + } + @Test + void test_throws_when_undetected_type() { assertThrows( IllegalArgumentException.class, () -> SqlQueryHelper.buildOrderSubQuery(testedDimension, () -> "field")); @@ -64,10 +73,6 @@ void test_throws_when_undetected_type() { @Test void test_subQuery_TE() { - DimensionParam dimensionParam = mock(DimensionParam.class); - DimensionIdentifier testedDimension = mock(DimensionIdentifier.class); - when(testedDimension.getDimension()).thenReturn(dimensionParam); - when(testedDimension.isTeDimension()).thenReturn(true); assertEquals( @@ -80,9 +85,6 @@ void test_subQuery_TE() { @Test void test_subQuery_enrollment() { - DimensionParam dimensionParam = mock(DimensionParam.class); - DimensionIdentifier testedDimension = mock(DimensionIdentifier.class); - when(testedDimension.getDimension()).thenReturn(dimensionParam); when(testedDimension.getPrefix()).thenReturn("prefix"); TrackedEntityType trackedEntityType = mock(TrackedEntityType.class); @@ -125,9 +127,6 @@ void test_subQuery_enrollment() { @Test void test_subQuery_event() { - DimensionParam dimensionParam = mock(DimensionParam.class); - DimensionIdentifier testedDimension = mock(DimensionIdentifier.class); - when(testedDimension.getDimension()).thenReturn(dimensionParam); when(testedDimension.getPrefix()).thenReturn("prefix"); TrackedEntityType trackedEntityType = mock(TrackedEntityType.class); @@ -190,12 +189,7 @@ where exists(select 1 @Test void test_subQuery_data_element() { - DimensionParam dimensionParam = mock(DimensionParam.class); - when(dimensionParam.isOfType(any())).thenReturn(true); - - DimensionIdentifier testedDimension = mock(DimensionIdentifier.class); - when(testedDimension.getDimension()).thenReturn(dimensionParam); when(testedDimension.getPrefix()).thenReturn("prefix"); TrackedEntityType trackedEntityType = mock(TrackedEntityType.class); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/db/model/ColumnTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/db/model/ColumnTest.java index a8079fdbbee7..e1d2f48443c5 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/db/model/ColumnTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/db/model/ColumnTest.java @@ -34,7 +34,6 @@ import org.junit.jupiter.api.Test; class ColumnTest { - @Test void testIsNotNull() { Column colA = new Column("dx", DataType.CHARACTER_11, Nullable.NOT_NULL); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/db/model/IndexTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/db/model/IndexTest.java index 53d77d00775e..c7d46a847a70 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/db/model/IndexTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/db/model/IndexTest.java @@ -60,13 +60,10 @@ void testIsUnique() { @Test void testDefaults() { - // given Index.IndexBuilder builder = Index.builder(); - // when Index index = builder.build(); - // then assertNull(index.getName()); assertNull(index.getTableName()); assertNull(index.getCondition()); diff --git a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/resourcetable/table/OrganisationUnitStructureResourceTableTest.java b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/resourcetable/table/OrganisationUnitStructureResourceTableTest.java index be8d39e20a8d..2080837b84e6 100644 --- a/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/resourcetable/table/OrganisationUnitStructureResourceTableTest.java +++ b/dhis-2/dhis-services/dhis-service-analytics/src/test/java/org/hisp/dhis/resourcetable/table/OrganisationUnitStructureResourceTableTest.java @@ -28,19 +28,18 @@ package org.hisp.dhis.resourcetable.table; import static org.hisp.dhis.test.TestBase.createOrganisationUnit; -import static org.junit.jupiter.api.Assertions.*; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; import java.util.ArrayList; import java.util.List; import org.hisp.dhis.organisationunit.OrganisationUnit; import org.junit.jupiter.api.Test; -/** Unit tests for {@link OrganisationUnitStructureResourceTable}. */ class OrganisationUnitStructureResourceTableTest { - @Test void testCreateBatchObjectsWhenLevelsAreSame() { - // Given int maxOrgUnitLevels = 3; int currentLevel = 3; @@ -65,13 +64,11 @@ void testCreateBatchObjectsWhenLevelsAreSame() { OrganisationUnitStructureResourceTable resourceTable = new OrganisationUnitStructureResourceTable(null, maxOrgUnitLevels, null); - // Then assertDoesNotThrow(() -> resourceTable.createBatchObjects(organisationUnits, currentLevel)); } @Test void testCreateBatchObjectsWhenHierarchyLevelIsLowerThanMaxLevel() { - // Given int maxOrgUnitLevels = 3; int currentLevel = 2; @@ -87,13 +84,11 @@ void testCreateBatchObjectsWhenHierarchyLevelIsLowerThanMaxLevel() { OrganisationUnitStructureResourceTable resourceTable = new OrganisationUnitStructureResourceTable(null, maxOrgUnitLevels, null); - // Then assertDoesNotThrow(() -> resourceTable.createBatchObjects(organisationUnits, currentLevel)); } @Test void testCreateBatchObjectsWhenCurrentLevelIsLargerThanMaxLevel() { - // Given int maxOrgUnitLevels = 2; int currentLevel = 3; @@ -110,7 +105,6 @@ void testCreateBatchObjectsWhenCurrentLevelIsLargerThanMaxLevel() { OrganisationUnitStructureResourceTable resourceTable = new OrganisationUnitStructureResourceTable(null, maxOrgUnitLevels, null); - // Then Exception ex = assertThrows( IllegalStateException.class, @@ -123,7 +117,6 @@ void testCreateBatchObjectsWhenCurrentLevelIsLargerThanMaxLevel() { @Test void testCreateBatchObjectsWhenCurrentLevelHasNoParent() { - // Given int maxOrgUnitLevels = 2; int currentLevel = 3; @@ -140,7 +133,6 @@ void testCreateBatchObjectsWhenCurrentLevelHasNoParent() { OrganisationUnitStructureResourceTable resourceTable = new OrganisationUnitStructureResourceTable(null, maxOrgUnitLevels, null); - // Then Exception ex = assertThrows( IllegalStateException.class, diff --git a/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataitem/query/ExpressionDimensionItemQueryTest.java b/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataitem/query/ExpressionDimensionItemQueryTest.java index 9d1098abed6e..70bdde9e0dcd 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataitem/query/ExpressionDimensionItemQueryTest.java +++ b/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/dataitem/query/ExpressionDimensionItemQueryTest.java @@ -40,14 +40,11 @@ class ExpressionDimensionItemQueryTest { @Test void testGetStatementContainsOwnerCheck() { - // Given MapSqlParameterSource anyMap = new MapSqlParameterSource(); ExpressionDimensionItemQuery query = new ExpressionDimensionItemQuery(); - // When String statement = query.getStatement(anyMap); - // Then assertTrue(statement.contains("(jsonb_extract_path_text(t.item_sharing, 'owner') = :userUid)")); } } diff --git a/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionService2Test.java b/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java similarity index 98% rename from dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionService2Test.java rename to dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java index e1c30e9ca515..b472290e850f 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionService2Test.java +++ b/dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java @@ -60,6 +60,7 @@ import com.google.common.collect.Sets; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -112,7 +113,7 @@ * @author Luciano Fiandesio */ @ExtendWith(MockitoExtension.class) -class ExpressionService2Test extends TestBase { +class ExpressionServiceTest extends TestBase { @Mock private HibernateGenericStore hibernateGenericStore; @Mock private ConstantService constantService; @@ -229,8 +230,6 @@ class ExpressionService2Test extends TestBase { private String expressionN; - private String expressionO; - private String expressionP; private String expressionR; @@ -392,7 +391,6 @@ public void setUp() { + SEPARATOR + cocA.getUid() + "}"; - expressionO = "#{" + opA.getDimensionItem() + "}+sum(#{" + opB.getDimensionItem() + "})"; expressionP = "#{" + deB.getUid() + SEPARATOR + coc.getUid() + "}"; expressionR = "#{" @@ -693,20 +691,17 @@ void testGetBaseExpressionParams() { when(idObjectManager.getNoAcl( eq(OrganisationUnitGroup.class), - (java.util.Collection) - argThat(containsInAnyOrder(groupA.getUid(), groupB.getUid())))) + (Collection) argThat(containsInAnyOrder(groupA.getUid(), groupB.getUid())))) .thenReturn(List.of(groupA, groupB)); when(idObjectManager.getNoAcl( eq(DataSet.class), - (java.util.Collection) - argThat(containsInAnyOrder(dataSetA.getUid(), dataSetB.getUid())))) + (Collection) argThat(containsInAnyOrder(dataSetA.getUid(), dataSetB.getUid())))) .thenReturn(List.of(dataSetA, dataSetB)); when(idObjectManager.getNoAcl( eq(Program.class), - (java.util.Collection) - argThat(containsInAnyOrder(programA.getUid(), programB.getUid())))) + (Collection) argThat(containsInAnyOrder(programA.getUid(), programB.getUid())))) .thenReturn(List.of(programA, programB)); ExpressionParams baseParams = target.getBaseExpressionParams(info);