Skip to content

Commit 43fab0d

Browse files
authored
Added unit test to usage metrics endpoints (#1133)
* Added unit test to usage metrics endpoints
1 parent db3de2d commit 43fab0d

File tree

5 files changed

+1034
-28
lines changed

5 files changed

+1034
-28
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.mskcc.cbio.oncokb.config;
2+
3+
import java.time.Clock;
4+
import org.springframework.context.annotation.Bean;
5+
import org.springframework.context.annotation.Configuration;
6+
7+
@Configuration
8+
public class ClockConfig {
9+
10+
@Bean
11+
public Clock clock() {
12+
return Clock.systemUTC();
13+
}
14+
}

src/main/java/org/mskcc/cbio/oncokb/config/Constants.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public final class Constants {
3535

3636
public static final String MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX = "public-website/usage-analysis/month-user-summary_";
3737
public static final String YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX = "public-website/usage-analysis/year-user-summary_";
38+
public static final String MONTH_RESOURCES_USAGE_SUMMARY_FILE_PREFIX = "public-website/usage-analysis/month-resource-summary_";
3839
public static final String YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX = "public-website/usage-analysis/year-resource-summary_";
3940
public static final String TOKEN_STATS_STORAGE_FILE_PREFIX = "public-website/token-usage/token-stats_";
4041

@@ -43,7 +44,7 @@ public final class Constants {
4344
public static final String TESTING_TOKEN = "faketoken";
4445

4546
public static final String ONCOKB_S3_BUCKET = "oncokb-v2";
46-
47+
4748
private Constants() {
4849
}
4950

Original file line numberDiff line numberDiff line change
@@ -1,35 +1,37 @@
11
package org.mskcc.cbio.oncokb.util;
22

3-
import org.mskcc.cbio.oncokb.config.Constants;
4-
3+
import java.time.Clock;
54
import java.time.Instant;
65
import java.time.ZoneId;
76
import java.time.ZonedDateTime;
87
import java.time.format.DateTimeFormatter;
98
import java.time.format.FormatStyle;
109
import java.util.Locale;
10+
import org.mskcc.cbio.oncokb.config.Constants;
1111

1212
/**
1313
* Created by Hongxin Zhang on 4/6/21.
1414
*/
1515
public class TimeUtil {
16-
public static String toSystemDefaultZoneTime(Instant time) {
17-
DateTimeFormatter formatter =
18-
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
19-
.withLocale(Locale.US)
20-
.withZone(ZoneId.systemDefault());
21-
return formatter.format(time);
22-
}
2316

24-
public static String toNYZoneTime(Instant time) {
25-
DateTimeFormatter formatter =
26-
DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL)
27-
.withLocale(Locale.US)
28-
.withZone(ZoneId.of(Constants.NY_ZONE_ID));
29-
return formatter.format(time);
30-
}
17+
public static String toSystemDefaultZoneTime(Instant time) {
18+
DateTimeFormatter formatter = DateTimeFormatter
19+
.ofLocalizedDateTime(FormatStyle.FULL)
20+
.withLocale(Locale.US)
21+
.withZone(ZoneId.systemDefault());
22+
return formatter.format(time);
23+
}
24+
25+
public static String toNYZoneTime(Instant time) {
26+
DateTimeFormatter formatter = DateTimeFormatter
27+
.ofLocalizedDateTime(FormatStyle.FULL)
28+
.withLocale(Locale.US)
29+
.withZone(ZoneId.of(Constants.NY_ZONE_ID));
30+
return formatter.format(time);
31+
}
3132

32-
public static ZonedDateTime getCurrentNYTime() {
33-
return ZonedDateTime.now(ZoneId.of(Constants.NY_ZONE_ID));
34-
}
33+
// TODO: Convert TimeUtil into a service class so we don't have autowire a clock into every class
34+
public static ZonedDateTime getCurrentNYTime(Clock clock) {
35+
return ZonedDateTime.now(clock.withZone(ZoneId.of(Constants.NY_ZONE_ID)));
36+
}
3537
}

src/main/java/org/mskcc/cbio/oncokb/web/rest/UsageAnalysisController.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.io.IOException;
44
import java.io.InputStreamReader;
55
import java.io.UnsupportedEncodingException;
6+
import java.time.Clock;
67
import java.time.format.DateTimeFormatter;
78
import java.time.temporal.ChronoUnit;
89
import java.util.*;
@@ -52,6 +53,9 @@ public class UsageAnalysisController {
5253
@Autowired
5354
private UserMapper userMapper;
5455

56+
@Autowired
57+
private Clock clock;
58+
5559
private JSONObject requestData(String file)
5660
throws UnsupportedEncodingException, IOException, ParseException {
5761
Optional<ResponseInputStream<GetObjectResponse>> s3object = s3Service.getObject(Constants.ONCOKB_S3_BUCKET, file);
@@ -66,7 +70,7 @@ private JSONObject requestData(String file)
6670
/**
6771
* API to get the detail usage info for specific user
6872
* @param userId
69-
* @return user usage infomation of given user
73+
* @return user usage information of given user
7074
* @throws IOException
7175
* @throws ParseException
7276
*/
@@ -76,13 +80,13 @@ public ResponseEntity<UserUsage> userUsageGet(@PathVariable @NotNull Long userId
7680
HttpStatus status = HttpStatus.OK;
7781

7882
if (userId != null) {
79-
int year = TimeUtil.getCurrentNYTime().getYear();
83+
int year = TimeUtil.getCurrentNYTime(clock).getYear();
8084
JSONObject yearSummary = requestData(YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
8185
Map<String, JSONObject> monthSummaries = new HashMap<>();
8286
int monthsBack = 0;
8387
JSONObject monthSummary;
8488
do {
85-
String month = TimeUtil.getCurrentNYTime().minus(monthsBack, ChronoUnit.MONTHS).format(DateTimeFormatter.ofPattern("yyyy-MM"));
89+
String month = TimeUtil.getCurrentNYTime(clock).minus(monthsBack, ChronoUnit.MONTHS).format(DateTimeFormatter.ofPattern("yyyy-MM"));
8690
monthSummary = requestData(MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX + month + FileExtension.JSON_FILE.getExtension());
8791
if (monthSummary != null) {
8892
monthSummaries.put(month, monthSummary);
@@ -143,13 +147,13 @@ public ResponseEntity<List<UserOverviewUsage>> userOverviewUsageGet(@RequestPara
143147
throws IOException, ParseException {
144148
HttpStatus status = HttpStatus.OK;
145149

146-
int year = TimeUtil.getCurrentNYTime().getYear();
150+
int year = TimeUtil.getCurrentNYTime(clock).getYear();
147151
JSONObject yearSummary = requestData(YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
148152
Map<String, JSONObject> monthSummaries = new HashMap<>();
149153
int monthsBack = 0;
150154
JSONObject monthSummary;
151155
do {
152-
String month = TimeUtil.getCurrentNYTime().minus(monthsBack, ChronoUnit.MONTHS).format(DateTimeFormatter.ofPattern("yyyy-MM"));
156+
String month = TimeUtil.getCurrentNYTime(clock).minus(monthsBack, ChronoUnit.MONTHS).format(DateTimeFormatter.ofPattern("yyyy-MM"));
153157
monthSummary = requestData(MONTH_USERS_USAGE_SUMMARY_FILE_PREFIX + month + FileExtension.JSON_FILE.getExtension());
154158
if (monthSummary != null) {
155159
monthSummaries.put(month, monthSummary);
@@ -251,7 +255,7 @@ public ResponseEntity<UsageSummary> resourceUsageGet()
251255
throws IOException, ParseException {
252256
HttpStatus status = HttpStatus.OK;
253257

254-
int year = TimeUtil.getCurrentNYTime().getYear();
258+
int year = TimeUtil.getCurrentNYTime(clock).getYear();
255259
JSONObject jsonObject = requestData(YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
256260

257261
Gson gson = new Gson();
@@ -263,7 +267,7 @@ public ResponseEntity<UsageSummary> resourceUsageGet()
263267
}
264268

265269
/**
266-
* API to get the usage of a sepcific resource
270+
* API to get the usage of a specific resource
267271
* @param endpoint
268272
* @return usage of a specific endpoint
269273
* @throws UnsupportedEncodingException
@@ -275,7 +279,7 @@ public ResponseEntity<UsageSummary> resourceDetailGet(@RequestParam String endpo
275279
throws UnsupportedEncodingException, IOException, ParseException {
276280
HttpStatus status = HttpStatus.OK;
277281

278-
int year = TimeUtil.getCurrentNYTime().getYear();
282+
int year = TimeUtil.getCurrentNYTime(clock).getYear();
279283
JSONObject resourceSummary = requestData(YEAR_RESOURCES_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
280284
JSONObject userSummary = requestData(YEAR_USERS_USAGE_SUMMARY_FILE_PREFIX + year + FileExtension.JSON_FILE.getExtension());
281285
if (resourceSummary != null && userSummary != null ){

0 commit comments

Comments
 (0)