Skip to content

Commit

Permalink
SDK-309: Changing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wikumChamith committed Jun 28, 2023
1 parent 5b6fd89 commit ddfc1c5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,12 @@ public void sendReport(Wizard wizard) throws MojoExecutionException {
* @return
*/
private boolean checkIfOneWeekFromLastReport(){
return checkIfOneWeekApart(getLastReported());
}
LocalDate lastReport = getLastReported();
LocalDate currentDate = LocalDate.now();

public boolean checkIfOneWeekApart(LocalDate lastDate) {
if(lastDate != null) {
return ChronoUnit.DAYS.between(lastDate, LocalDate.now()) > 7;
}
else {
if (lastReport != null) {
return ChronoUnit.DAYS.between(lastReport, currentDate) > 7;
} else {
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package org.openmrs.maven.plugins.model;

import org.apache.commons.lang.time.DateUtils;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Properties;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.openmrs.maven.plugins.model.SdkStatistics.SDK_STATS_FILE_NAME;

/**
*
*/
public class SdkStatisticsTest {

private static final String DATE_FORMAT = "dd-M-yyyy";
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT);
private SdkStatistics sdkStatistics;

@Before
Expand All @@ -36,25 +41,45 @@ public void shouldIncrementGoal() {
}

@Test
public void shouldSetStatsEnabledMode(){
public void shouldSetStatsEnabledMode() {
sdkStatistics.setStatsEnabled(true);

assertThat(sdkStatistics.getStatsEnabled(), is(true));
}

@Test
public void testCheckIfOneWeekApart_LessThanOneWeekApart() {
assertFalse(sdkStatistics.checkIfOneWeekApart(LocalDate.now().minusDays(6)));
public void checkIfOneWeekApart_LessThanOneWeekApart() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Field statisticsField = sdkStatistics.getClass().getDeclaredField("statistics");
statisticsField.setAccessible(true);
Properties statistics = (Properties) statisticsField.get(sdkStatistics);
statistics.setProperty("statsLastReported", LocalDate.now().minusDays(6).format(dateTimeFormatter));
Method checkIfOneWeekFromLastReportMethod = sdkStatistics.getClass().getDeclaredMethod("checkIfOneWeekFromLastReport");
checkIfOneWeekFromLastReportMethod.setAccessible(true);
assertFalse((Boolean) checkIfOneWeekFromLastReportMethod.invoke(sdkStatistics));

}


@Test
public void testCheckIfOneWeekApart_MoreThanOneWeekApart() {
assertTrue(sdkStatistics.checkIfOneWeekApart(LocalDate.now().minusDays(8)));
public void checkIfOneWeekApart_MoreThanOneWeekApart() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Field statisticsField = sdkStatistics.getClass().getDeclaredField("statistics");
statisticsField.setAccessible(true);
Properties statistics = (Properties) statisticsField.get(sdkStatistics);
statistics.setProperty("statsLastReported", LocalDate.now().minusDays(8).format(dateTimeFormatter));
Method checkIfOneWeekFromLastReportMethod = sdkStatistics.getClass().getDeclaredMethod("checkIfOneWeekFromLastReport");
checkIfOneWeekFromLastReportMethod.setAccessible(true);
assertTrue((Boolean) checkIfOneWeekFromLastReportMethod.invoke(sdkStatistics));
}

@Test
public void testCheckIfOneWeekApart_OneWeekApart() {
assertFalse(sdkStatistics.checkIfOneWeekApart(LocalDate.now().minusDays(7)) );
public void checkIfOneWeekApart_OneWeekApart() throws NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
Field statisticsField = sdkStatistics.getClass().getDeclaredField("statistics");
statisticsField.setAccessible(true);
Properties statistics = (Properties) statisticsField.get(sdkStatistics);
statistics.setProperty("statsLastReported", LocalDate.now().minusDays(7).format(dateTimeFormatter));
Method checkIfOneWeekFromLastReportMethod = sdkStatistics.getClass().getDeclaredMethod("checkIfOneWeekFromLastReport");
checkIfOneWeekFromLastReportMethod.setAccessible(true);
assertFalse((Boolean) checkIfOneWeekFromLastReportMethod.invoke(sdkStatistics));
}


Expand Down

0 comments on commit ddfc1c5

Please sign in to comment.