Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add initial e2e tests for outliers #16098

Merged
merged 15 commits into from
Jan 11, 2024
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
package org.hisp.dhis.analytics.outlier.service;

import static java.lang.Double.NaN;
import static org.apache.commons.math3.util.Precision.round;
import static org.hisp.dhis.analytics.OutlierDetectionAlgorithm.MOD_Z_SCORE;
import static org.hisp.dhis.analytics.outlier.OutlierHelper.withExceptionHandling;
import static org.hisp.dhis.period.PeriodType.getIsoPeriod;
Expand Down Expand Up @@ -142,18 +143,18 @@ private Outlier getOutlier(Calendar calendar, ResultSet rs) throws SQLException
*/
private void addZScoreBasedParamsToOutlier(Outlier outlier, ResultSet rs, boolean modifiedZ)
throws SQLException {

final int scale = 5;
if (modifiedZ) {
outlier.setMedian(rs.getDouble("middle_value"));
outlier.setStdDev(rs.getDouble("mad"));
outlier.setMedian(round(rs.getDouble("middle_value"), scale));
outlier.setStdDev(round(rs.getDouble("mad"), scale));
} else {
outlier.setMean(rs.getDouble("middle_value"));
outlier.setStdDev(rs.getDouble("std_dev"));
outlier.setMean(round(rs.getDouble("middle_value"), scale));
outlier.setStdDev(round(rs.getDouble("std_dev"), scale));
}

outlier.setAbsDev(rs.getDouble("middle_value_abs_dev"));
outlier.setZScore(outlier.getStdDev() == 0 ? NaN : rs.getDouble("z_score"));
outlier.setLowerBound(rs.getDouble("lower_bound"));
outlier.setUpperBound(rs.getDouble("upper_bound"));
outlier.setAbsDev(round(rs.getDouble("middle_value_abs_dev"), scale));
outlier.setZScore(round(outlier.getStdDev() == 0 ? NaN : rs.getDouble("z_score"), scale));
outlier.setLowerBound(round(rs.getDouble("lower_bound"), scale));
outlier.setUpperBound(round(rs.getDouble("upper_bound"), scale));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2004-2022, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.actions.analytics;

import org.hisp.dhis.actions.RestApiActions;

/**
* Provides tracked entities endpoints/operations associated to the parent
* "analytics/outlierDetection".
*
* @author maikel arabori
*/
public class AnalyticsOutlierDetectionActions extends RestApiActions {
public AnalyticsOutlierDetectionActions() {
super("/analytics/outlierDetection");
}

public AnalyticsOutlierDetectionActions(String endpoint) {
super("/analytics/outlierDetection" + endpoint);
}

public AnalyticsOutlierDetectionActions query() {
return new AnalyticsOutlierDetectionActions("");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.hisp.dhis.actions.RestApiActions;
import org.hisp.dhis.actions.analytics.AnalyticsEnrollmentsActions;
import org.hisp.dhis.actions.analytics.AnalyticsEventActions;
import org.hisp.dhis.actions.analytics.AnalyticsOutlierDetectionActions;
import org.hisp.dhis.actions.analytics.AnalyticsTeiActions;
import org.hisp.dhis.dto.ApiResponse;
import org.hisp.dhis.helpers.QueryParamsBuilder;
Expand All @@ -49,8 +50,6 @@
* missing analytics tables. For this reason they have to run first (before analytics tables are
* created), hence @Order(1).
*/

// TODO do not forget add e2e for DHIS2-16375
@Order(1)
@ExtendWith(ConfigurationExtension.class)
@Tag("analytics")
Expand All @@ -60,6 +59,8 @@ public class NoAnalyticsTablesErrorsScenariosTest {
new AnalyticsEnrollmentsActions();
private final AnalyticsEventActions analyticsEventActions = new AnalyticsEventActions();
private final AnalyticsTeiActions analyticsTeiActions = new AnalyticsTeiActions();
private final AnalyticsOutlierDetectionActions analyticsOutlierActions =
new AnalyticsOutlierDetectionActions();
private final RestApiActions analyticsAggregateActions = new RestApiActions("analytics");

@BeforeAll
Expand Down Expand Up @@ -140,6 +141,33 @@ public void queryWithProgramAndEnrollmentDateAndInvalidEnrollmentOffset() {
assertNoAnalyticsTableResponse(response);
}

@Test
public void testOutliersAnalyticsWhenOutliersDataAreMissing() {
// Given
QueryParamsBuilder params =
new QueryParamsBuilder()
.add("dx=Y7Oq71I3ASg")
.add("endDate=2024-01-02")
.add("ou=O6uvpzGd5pu,fdc6uOvgoji")
.add("maxResults=100")
.add("startDate=2020-10-01")
.add("algorithm=MOD_Z_SCORE");

// When
ApiResponse response = analyticsOutlierActions.query().get("", JSON, JSON, params);

// Then
response
.validate()
.statusCode(409)
.body("status", equalTo("ERROR"))
.body(
"message",
equalTo(
"The analytics outliers data does not exist. Please ensure analytics job was run and did not skip the outliers"))
.body("errorCode", equalTo("E7180"));
}

private void assertNoAnalyticsTableResponse(ApiResponse response) {
response
.validate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,11 @@ static String generateTestMethod(String name, String url) {
+ "().get(\""
+ dimensionUid
+ "\", JSON, JSON, params);\n";
} else {
testTarget =
"\n// When\n ApiResponse response = actions."
+ ACTION
+ "().get(\"\",JSON, JSON, params);\n";
}

String response = getResponse(url);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
*/
package org.hisp.dhis.analytics.generator;

import org.hisp.dhis.analytics.generator.impl.TeiQueryGenerator;
import org.hisp.dhis.analytics.generator.impl.OutlierDetectionGenerator;

/** This class simply hold the generator implementation to be used during the code generation. */
public class TestGenerator {
static Generator get() {
return new TeiQueryGenerator();
return new OutlierDetectionGenerator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2004-2023, University of Oslo
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* Neither the name of the HISP project nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.hisp.dhis.analytics.generator.impl;

import org.hisp.dhis.analytics.generator.Generator;

/**
* Set of behaviour and settings required by the test generation of
* "/analytics/outlierDetection?{PARAMS...}" endpoint.
*/
public class OutlierDetectionGenerator implements Generator {
@Override
public int getMaxTestsPerClass() {
return 4;
}

@Override
public String getAction() {
return "query";
}

@Override
public String getClassNamePrefix() {
return "OutliersDetection";
}

@Override
public String getActionDeclaration() {
return "private AnalyticsOutlierDetectionActions actions = new AnalyticsOutlierDetectionActions();";
}

@Override
public String getPackage() {
return "org.hisp.dhis.analytics.outlier";
}

@Override
public String getTopClassComment() {
return "Groups e2e tests for \"/analytics/outlierDetection\" endpoint.";
}

@Override
public boolean assertMetaData() {
return true;
}

@Override
public boolean assertRowIndex() {
return true;
}
}
Loading
Loading