Skip to content

Commit

Permalink
SDK:309-Switching to the new Date and Time API (#226)
Browse files Browse the repository at this point in the history
* SDK:309-Switching to the new Date and Time API

* SDK-308: Switching to the new Date and Time API

* SDK-309: Adding tests

* SDK-309: Changing tests

* SDK-309: Refactoring tests
  • Loading branch information
wikumChamith committed Jun 28, 2023
1 parent 7999593 commit c7db8a1
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package org.openmrs.maven.plugins.model;

import static org.openmrs.maven.plugins.utility.PropertiesUtils.loadPropertiesFromFile;
import static org.openmrs.maven.plugins.utility.PropertiesUtils.loadPropertiesFromResource;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
Expand All @@ -16,13 +13,16 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Properties;
import java.util.UUID;

import static org.openmrs.maven.plugins.utility.PropertiesUtils.loadPropertiesFromFile;
import static org.openmrs.maven.plugins.utility.PropertiesUtils.loadPropertiesFromResource;

/**
*
*/
Expand All @@ -34,7 +34,7 @@ public class SdkStatistics {

private Properties statistics = new Properties();

private final DateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DATE_FORMAT);

public SdkStatistics createSdkStatsFile(boolean agree) throws MojoExecutionException {
loadStatsFromResource(SDK_STATS_FILE_NAME);
Expand Down Expand Up @@ -64,7 +64,7 @@ private SdkStatistics(Properties statistics) {
}

/**
* Checks if there is more then 7 days since last sendReport. If yes, send statistics
* Checks if there is more than 7 days since last sendReport. If yes, send statistics
* @throws MojoExecutionException
*/
public void sendReport(Wizard wizard) throws MojoExecutionException {
Expand All @@ -85,17 +85,11 @@ public void sendReport(Wizard wizard) throws MojoExecutionException {
* @return
*/
private boolean checkIfOneWeekFromLastReport(){
Date lastReport;
try {
lastReport = getLastReported();
} catch (ParseException e) {
lastReport = null;
}

Date currentDate = new Date();
LocalDate lastReport = getLastReported();
LocalDate currentDate = LocalDate.now();

if (lastReport != null) {
return (currentDate.getTime() - lastReport.getTime()) / (60 * 60 * 1000 * 24) % 60 > 7;
return ChronoUnit.DAYS.between(lastReport, currentDate) > 7;
} else {
return true;
}
Expand Down Expand Up @@ -226,13 +220,12 @@ public boolean getStatsEnabled(){
* Get last date that statistics were reported
* @return
*/
private Date getLastReported() throws ParseException {
private LocalDate getLastReported() {
String statsLastReported = statistics.getProperty("statsLastReported");
if (StringUtils.isBlank(statsLastReported)){
return null;
}

return dateFormat.parse(statsLastReported);
return LocalDate.parse(statsLastReported, dateTimeFormatter);
}

public int getGoalCalls(String goal) {
Expand All @@ -243,17 +236,17 @@ public int getGoalCalls(String goal) {
* Get last date that sdk was used
* @return
*/
private Date getLastUsed() throws ParseException {
private LocalDate getLastUsed() throws ParseException {
String statsLastUsed = statistics.getProperty("statsLastUsed");
return dateFormat.parse(statsLastUsed);
return LocalDate.parse(statsLastUsed, dateTimeFormatter);
}

/**
* Get current date as String
* @return String
*/
private String getDate() {
return dateFormat.format(new Date());
return LocalDate.now().format(dateTimeFormatter);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,37 @@
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;

/**
*
*/
public class SdkStatisticsTest {

private final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("dd-M-yyyy");
private SdkStatistics sdkStatistics;
private Properties statistics;
private Method checkIfOneWeekFromLastReportMethod;

@Before
public void loadStatsFile() throws Exception {
public void before() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
sdkStatistics = new SdkStatistics(new File(classLoader.getResource("sdk-stats.properties").getFile()));
Field statisticsField = sdkStatistics.getClass().getDeclaredField("statistics");
statisticsField.setAccessible(true);
statistics = (Properties) statisticsField.get(sdkStatistics);
checkIfOneWeekFromLastReportMethod = sdkStatistics.getClass().getDeclaredMethod("checkIfOneWeekFromLastReport");
checkIfOneWeekFromLastReportMethod.setAccessible(true);
}

@Test
Expand All @@ -31,10 +47,30 @@ public void shouldIncrementGoal() {
}

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

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

@Test
public void checkIfOneWeekApart_LessThanOneWeekApart() throws IllegalAccessException, InvocationTargetException {
statistics.setProperty("statsLastReported", LocalDate.now().minusDays(6).format(dateTimeFormatter));
assertFalse((Boolean) checkIfOneWeekFromLastReportMethod.invoke(sdkStatistics));
}


@Test
public void checkIfOneWeekApart_MoreThanOneWeekApart() throws IllegalAccessException, InvocationTargetException {
statistics.setProperty("statsLastReported", LocalDate.now().minusDays(8).format(dateTimeFormatter));
assertTrue((Boolean) checkIfOneWeekFromLastReportMethod.invoke(sdkStatistics));
}

@Test
public void checkIfOneWeekApart_OneWeekApart() throws IllegalAccessException, InvocationTargetException {
statistics.setProperty("statsLastReported", LocalDate.now().minusDays(7).format(dateTimeFormatter));
assertFalse((Boolean) checkIfOneWeekFromLastReportMethod.invoke(sdkStatistics));
}


}

0 comments on commit c7db8a1

Please sign in to comment.