Skip to content

Commit

Permalink
refactor: Use InjectMocks for unit tests [DHIS2-126705] (#19295)
Browse files Browse the repository at this point in the history
  • Loading branch information
larshelge authored Nov 26, 2024
1 parent 7306b5b commit e8e8e0b
Show file tree
Hide file tree
Showing 12 changed files with 88 additions and 153 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
* @author Lars Helge Overland
*/
class AnalyticsTableColumnTest {

@Test
void testIsNotNull() {
AnalyticsTableColumn colA =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,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.junit.jupiter.MockitoExtension;

Expand All @@ -48,11 +49,12 @@ class ContinuousAnalyticsTableJobTest {
@Mock private AnalyticsTableGenerator analyticsTableGenerator;

@Mock private SystemSettingsService settingsService;

@Mock private SystemSettings settings;

@Mock private TableInfoReader tableInfoReader;

private ContinuousAnalyticsTableJob job;
@InjectMocks private ContinuousAnalyticsTableJob job;

private final Date dateA = getDate(2024, 1, 4, 23, 0);
private final Date dateB = getDate(2024, 1, 5, 2, 0);
Expand All @@ -61,8 +63,6 @@ class ContinuousAnalyticsTableJobTest {
@BeforeEach
public void beforeEach() {
when(settingsService.getCurrentSettings()).thenReturn(settings);
job =
new ContinuousAnalyticsTableJob(analyticsTableGenerator, settingsService, tableInfoReader);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
* @author maikel arabori
*/
class TrackedEntityQueryRequestValidatorTest {

@Test
void testValidateWhenTrackedEntityTypeIsInvalid() {
String teiUid = CodeGenerator.generateUid() + "invalid";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ void testWhenNoLegendSet() {
SqlParameterManager sqlParameterManager = new SqlParameterManager();
QueryContext queryContext = QueryContext.of(null, sqlParameterManager);

// SETUP
DimensionParam dimensionParam = mock(DimensionParam.class);
QueryItem queryItem = mock(QueryItem.class);
DimensionParamItem dimensionParamItem = mock(DimensionParamItem.class);
Expand All @@ -78,13 +77,11 @@ void testWhenNoLegendSet() {
when(dimensionParamItem.getOperator()).thenReturn(AnalyticsQueryOperator.of(QueryOperator.EQ));
when(dimensionParamItem.getValues()).thenReturn(List.of("value"));

// CALL
DataElementCondition dataElementCondition =
DataElementCondition.of(queryContext, dimensionIdentifier);

String rendered = dataElementCondition.render();

// ASSERT
assertEquals("(\"eventdatavalues\" -> 'uid' ->> 'value')::TEXT = :1", rendered);
assertEquals("value", queryContext.getParametersPlaceHolder().get("1"));
}
Expand All @@ -94,7 +91,6 @@ void testWhenLegendSet() {
SqlParameterManager sqlParameterManager = new SqlParameterManager();
QueryContext queryContext = QueryContext.of(null, sqlParameterManager);

// SETUP
DimensionParam dimensionParam = mock(DimensionParam.class);
QueryItem queryItem = mock(QueryItem.class);
DimensionParamItem dimensionParamItem = mock(DimensionParamItem.class);
Expand All @@ -105,7 +101,6 @@ void testWhenLegendSet() {
when(dimensionParam.getUid()).thenReturn("uid");
when(queryItem.hasLegendSet()).thenReturn(true);

// in the assertion we will check that the type is force to DECIMAL
when(dimensionParam.getValueType()).thenReturn(ValueType.TEXT);

when(dimensionParam.getItems()).thenReturn(List.of(dimensionParamItem));
Expand All @@ -121,13 +116,11 @@ void testWhenLegendSet() {
when(legend.getStartValue()).thenReturn(1.0);
when(legend.getEndValue()).thenReturn(2.0);

// CALL
DataElementCondition dataElementCondition =
DataElementCondition.of(queryContext, dimensionIdentifier);

String rendered = dataElementCondition.render();

// ASSERT
assertEquals(
"(\"eventdatavalues\" -> 'uid' ->> 'value')::DECIMAL >= :1 "
+ "and (\"eventdatavalues\" -> 'uid' ->> 'value')::DECIMAL < :2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,9 @@
import org.hisp.dhis.trackedentity.TrackedEntityType;
import org.junit.jupiter.api.Test;

/** Unit tests for {@link OrganisationUnitCondition}. */
class OrganisationUnitConditionTest {
@Test
void testTeiOuMultipleOusProduceCorrectSql() {
// Given
List<String> ous = List.of("ou1", "ou2");
DimensionIdentifier<DimensionParam> dimensionIdentifier =
stubDimensionIdentifier(ous, null, null);
Expand All @@ -82,17 +80,14 @@ void testTeiOuMultipleOusProduceCorrectSql() {
OrganisationUnitCondition organisationUnitCondition =
OrganisationUnitCondition.of(dimensionIdentifier, queryContext);

// When
String render = organisationUnitCondition.render();

// Then
assertEquals("t_1.\"ou\" in (:1)", render);
assertEquals(ous, queryContext.getParametersPlaceHolder().get("1"));
}

@Test
void testChildrenOuModeProduceCorrectSql() {
// Given
List<String> ous = List.of("ou1", "ou2");

DimensionIdentifier<DimensionParam> dimensionIdentifier =
Expand Down Expand Up @@ -125,13 +120,11 @@ void testChildrenOuModeProduceCorrectSql() {
OrganisationUnitCondition organisationUnitCondition =
OrganisationUnitCondition.of(dimensionIdentifier, queryContext);

// When
String render = organisationUnitCondition.render();

List<String> expected =
ous.stream().flatMap(ouId -> Stream.of(ouId + "_children1", ouId + "_children2")).toList();

// Then
assertEquals("t_1.\"ou\" in (:1)", render);
assertTrue(
isEqualCollection(
Expand All @@ -140,7 +133,6 @@ void testChildrenOuModeProduceCorrectSql() {

@Test
void testDescendantOuModeProduceCorrectSql() {
// Given
List<String> ous = List.of("ou1", "ou2");

DimensionIdentifier<DimensionParam> dimensionIdentifier =
Expand All @@ -155,7 +147,6 @@ void testDescendantOuModeProduceCorrectSql() {
return organisationUnit;
});

// Descendant is the default ouMode.
CommonRequestParams requestParams = new CommonRequestParams();

ContextParams<TrackedEntityRequestParams, TrackedEntityQueryParams> contextParams =
Expand All @@ -169,16 +160,13 @@ void testDescendantOuModeProduceCorrectSql() {
OrganisationUnitCondition organisationUnitCondition =
OrganisationUnitCondition.of(dimensionIdentifier, queryContext);

// When
String render = organisationUnitCondition.render();

// Then
assertEquals("(t_1.\"uidlevel1\" = :1 or t_1.\"uidlevel1\" = :2)", render);
}

@Test
void testEmptyOuProduceFalse() {
// Given
List<String> ous = List.of();
DimensionIdentifier<DimensionParam> dimensionIdentifier =
stubDimensionIdentifier(ous, null, null);
Expand All @@ -197,17 +185,14 @@ void testEmptyOuProduceFalse() {
OrganisationUnitCondition organisationUnitCondition =
OrganisationUnitCondition.of(dimensionIdentifier, queryContext);

// When
String render = organisationUnitCondition.render();

// Then
assertEquals("false", render);
assertTrue(queryContext.getParametersPlaceHolder().isEmpty());
}

@Test
void testTeiOuSingleOusProduceCorrectSql() {
// Given
List<String> ous = List.of("ou1");

DimensionIdentifier<DimensionParam> dimensionIdentifier =
Expand All @@ -227,17 +212,14 @@ void testTeiOuSingleOusProduceCorrectSql() {
OrganisationUnitCondition organisationUnitCondition =
OrganisationUnitCondition.of(dimensionIdentifier, queryContext);

// When
String render = organisationUnitCondition.render();

// Then
assertEquals("t_1.\"ou\" = :1", render);
assertEquals(ous.get(0), queryContext.getParametersPlaceHolder().get("1"));
}

@Test
void testEventOuSingleOusProduceCorrectSql() {
// Given
List<String> ous = List.of("ou1");

DimensionIdentifier<DimensionParam> dimensionIdentifier =
Expand All @@ -257,17 +239,14 @@ void testEventOuSingleOusProduceCorrectSql() {
OrganisationUnitCondition organisationUnitCondition =
OrganisationUnitCondition.of(dimensionIdentifier, queryContext);

// When
String statement = organisationUnitCondition.render();

// Then
assertEquals("\"Z8z5uu61HAb.tO8L1aBitDm\".\"ou\" = :1", statement);
assertEquals(ous.get(0), queryContext.getParametersPlaceHolder().get("1"));
}

@Test
void testEventOuMultipleOusProduceCorrectSql() {
// Given
List<String> ous = List.of("ou1", "ou2");

DimensionIdentifier<DimensionParam> dimensionIdentifier =
Expand All @@ -291,17 +270,14 @@ void testEventOuMultipleOusProduceCorrectSql() {
OrganisationUnitCondition organisationUnitCondition =
OrganisationUnitCondition.of(dimensionIdentifier, queryContext);

// When
String statement = organisationUnitCondition.render();

// Then
assertEquals("\"Z8z5uu61HAb.tO8L1aBitDm\".\"ou\" in (:1)", statement);
assertEquals(ous, queryContext.getParametersPlaceHolder().get("1"));
}

@Test
void testEnrollmentOuSingleOusProduceCorrectSql() {
// Given
List<String> ous = List.of("ou1");

DimensionIdentifier<DimensionParam> dimensionIdentifier =
Expand All @@ -325,17 +301,14 @@ void testEnrollmentOuSingleOusProduceCorrectSql() {
OrganisationUnitCondition organisationUnitCondition =
OrganisationUnitCondition.of(dimensionIdentifier, queryContext);

// When
String statement = organisationUnitCondition.render();

// Then
assertEquals("\"Z8z5uu61HAb\".\"ou\" = :1", statement);
assertEquals(ous.get(0), queryContext.getParametersPlaceHolder().get("1"));
}

@Test
void testEnrollmentOuMultipleOusProduceCorrectSql() {
// Given
List<String> ous = List.of("ou1", "ou2");

DimensionIdentifier<DimensionParam> dimensionIdentifier =
Expand All @@ -358,10 +331,8 @@ void testEnrollmentOuMultipleOusProduceCorrectSql() {
OrganisationUnitCondition organisationUnitCondition =
OrganisationUnitCondition.of(dimensionIdentifier, queryContext);

// When
String statement = organisationUnitCondition.render();

// Then
assertEquals("\"Z8z5uu61HAb\".\"ou\" in (:1)", statement);
assertEquals(ous, queryContext.getParametersPlaceHolder().get("1"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import org.junit.jupiter.api.Test;

class RenderableDataValueTest {

@Test
void testRender() {
RenderableDataValue renderableDataValue =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,26 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

/** Tests for {@link OffsetHelper}. */
class OffsetHelperTest {

@ParameterizedTest(name = "testGetItemBasedOnOffset - {index}")
@CsvSource({"2,d", "1,e", "0,a", "-1,b", "-2,c"})
void testGetItemBasedOnOffset(String offsetParam, String expectedResponse) {
// Given
Stream<String> stream = Stream.of("a", "b", "c", "d", "e");
Comparator<String> comparator = Comparator.naturalOrder();
int offset = Integer.parseInt(offsetParam);

// When
Optional<String> result = OffsetHelper.getItemBasedOnOffset(stream, comparator, offset);

// Then
Assertions.assertTrue(result.isPresent());
Assertions.assertEquals(expectedResponse, result.get());
}

@ParameterizedTest
@CsvSource({"1,1,asc", "2,2,asc", "0,1,desc", "-1,2,desc", "-2,3,desc"})
void testGetOffset(String offsetParam, String expectedOffset, String expectedDirection) {
// When
Offset offset = OffsetHelper.getOffset(Integer.parseInt(offsetParam));

// Then
Assertions.assertEquals(expectedOffset, offset.offset());
Assertions.assertEquals(expectedDirection, offset.direction());
}
Expand Down
Loading

0 comments on commit e8e8e0b

Please sign in to comment.