-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jans-auth-server): add support for ranges in statistic endpoint …
…(UI team request) #1089
- Loading branch information
Showing
6 changed files
with
181 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
jans-auth-server/server/src/main/java/io/jans/as/server/ws/rs/stat/Months.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package io.jans.as.server.ws.rs.stat; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.time.LocalDate; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
|
||
import static java.time.temporal.TemporalAdjusters.firstDayOfMonth; | ||
|
||
/** | ||
* @author Yuriy Zabrovarnyy | ||
*/ | ||
public class Months { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(Months.class); | ||
|
||
public static final DateTimeFormatter YYYYMMDD = DateTimeFormatter.ofPattern("yyyyMMdd"); | ||
public static final DateTimeFormatter YYYYMM = DateTimeFormatter.ofPattern("yyyyMM"); | ||
|
||
private Months() { | ||
} | ||
|
||
public static boolean isValid(String months, String startMonth, String endMonth) { | ||
boolean hasMonths = StringUtils.isNotBlank(months); | ||
boolean hasRange = StringUtils.isNotBlank(startMonth) && StringUtils.isNotBlank(endMonth); | ||
if (hasMonths && hasRange) { // if both are present then invalid | ||
return false; | ||
} | ||
return hasMonths || hasRange; | ||
} | ||
|
||
public static Set<String> getMonths(String months, String startMonth, String endMonth) { | ||
if (!isValid(months, startMonth, endMonth)) { | ||
return new LinkedHashSet<>(); | ||
} | ||
|
||
boolean hasMonths = StringUtils.isNotBlank(months); | ||
if (hasMonths) { | ||
return getMonths(months); | ||
} | ||
return getMonths(startMonth, endMonth); | ||
} | ||
|
||
public static LocalDate parse(String month) { | ||
// append first day of month -> "01" | ||
return LocalDate.parse(month + "01", YYYYMMDD).with(firstDayOfMonth()); | ||
} | ||
|
||
public static Set<String> getMonths(String startMonth, String endMonth) { | ||
Set<String> monthList = new LinkedHashSet<>(); | ||
if (!checkMonthFormat(startMonth) || !checkMonthFormat(endMonth)) { | ||
return monthList; | ||
} | ||
|
||
LocalDate start = parse(startMonth); | ||
LocalDate end = parse(endMonth); | ||
|
||
LocalDate date = start; | ||
|
||
while (date.isBefore(end)) { | ||
monthList.add(date.format(YYYYMM)); | ||
|
||
date = date.plusMonths(1).with(firstDayOfMonth()); | ||
} | ||
|
||
if (!monthList.isEmpty()) { // add last month | ||
monthList.add(date.format(YYYYMM)); | ||
} | ||
return monthList; | ||
} | ||
|
||
public static boolean checkMonthFormat(String month) { | ||
if (month.length() == 6) { | ||
return true; | ||
} | ||
|
||
log.error("Invalid month `{}`, month must be 6 chars length in format yyyyMM, e.g. 202212", month); | ||
return false; | ||
} | ||
|
||
public static Set<String> getMonths(String months) { | ||
Set<String> monthList = new LinkedHashSet<>(); | ||
if (StringUtils.isBlank(months)) { | ||
return monthList; | ||
} | ||
|
||
for (String m : months.split(" ")) { | ||
m = m.trim(); | ||
if (checkMonthFormat(m)) { | ||
monthList.add(m); | ||
} | ||
} | ||
return monthList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
jans-auth-server/server/src/test/java/io/jans/as/server/ws/rs/stat/MonthsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package io.jans.as.server.ws.rs.stat; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import java.util.Arrays; | ||
import java.util.Set; | ||
|
||
import static org.testng.Assert.assertEquals; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
/** | ||
* @author Yuriy Zabrovarnyy | ||
*/ | ||
public class MonthsTest { | ||
|
||
@Test | ||
public void getMonths_forValidStartAndEndDates_shouldReturnCorrectResult() { | ||
final Set<String> months = Months.getMonths("202112", "202205"); | ||
assertEquals(months.size(), 6); | ||
assertTrue(months.containsAll(Arrays.asList("202112", "202201", "202202", "202203", "202204", "202205"))); | ||
} | ||
|
||
@Test | ||
public void getMonths_whenStartIsAfterEndDates_shouldReturnEmptyList() { | ||
final Set<String> months = Months.getMonths("202212", "202205"); | ||
assertTrue(months.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void isValid_checkDifferentCases() { | ||
assertTrue(Months.isValid(null, "202112", "202205")); | ||
assertTrue(Months.isValid("", "202112", "202205")); | ||
assertTrue(Months.isValid("", "202012", "202205")); | ||
assertTrue(Months.isValid("202012", "", "")); | ||
assertTrue(Months.isValid("202012", null, null)); | ||
assertTrue(Months.isValid("202012 202101", null, null)); | ||
|
||
assertFalse(Months.isValid(null, null, null)); | ||
assertFalse(Months.isValid("", "", "")); | ||
assertFalse(Months.isValid("202012 202101", "202012", "202205")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters