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

Implement support for LocalDate and OffsetDateTime in RequestInformation #1586

Merged
2 changes: 1 addition & 1 deletion components/abstractions/gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ dependencies {
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'

// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'io.github.std-uritemplate:std-uritemplate:1.0.6'
implementation 'io.github.std-uritemplate:std-uritemplate:2.0.0'
implementation 'io.opentelemetry:opentelemetry-api:1.42.1'
implementation 'jakarta.annotation:jakarta.annotation-api:2.1.1'
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -473,6 +474,14 @@ private static Object getSanitizedValues(Object value) {
return ((ValuedEnum) value).getValue();
} else if (value instanceof UUID) {
return value.toString();
} else if (value instanceof OffsetDateTime) {
return ((OffsetDateTime) value).format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
} else if (value instanceof LocalDate) {
espenvis marked this conversation as resolved.
Show resolved Hide resolved
return ((LocalDate) value).format(DateTimeFormatter.ISO_LOCAL_DATE);
} else if (value instanceof LocalTime) {
return ((LocalTime) value).format(DateTimeFormatter.ISO_LOCAL_TIME);
} else if (value instanceof PeriodAndDuration) {
return ((PeriodAndDuration) value).toString();
} else {
return value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@

import java.net.URI;
import java.net.URISyntaxException;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.time.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -73,6 +72,62 @@ void SetsPathParametersOfDateTimeOffsetType() {
assertTrue(uriResult.toString().contains("toDateTime='2022-08-02T00%3A00%3A00Z'"));
}

@Test
void SetsPathParametersOfLocalDateType() {
final RequestInformation requestInfo = new RequestInformation();
requestInfo.httpMethod = HttpMethod.GET;
requestInfo.urlTemplate = "http://localhost/getCalendarView(startLocalDate='{startLocalDate}',endLocalDate='{endLocalDate}')";

final LocalDate startLocalDate = LocalDate.of(2022, 8, 1);
final LocalDate endLocalDate = LocalDate.of(2022, 8, 2);

requestInfo.pathParameters.put("startLocalDate", startLocalDate);
requestInfo.pathParameters.put("endLocalDate", endLocalDate);

var uriResult = assertDoesNotThrow(() -> requestInfo.getUri());
assertTrue(uriResult.toString().contains("startLocalDate='2022-08-01'"));
assertTrue(uriResult.toString().contains("endLocalDate='2022-08-02'"));
}

@Test
void SetsPathParametersOfLocalTimeType() {
// Arrange as the request builders would
final RequestInformation requestInfo = new RequestInformation();
requestInfo.httpMethod = HttpMethod.GET;
requestInfo.urlTemplate = "http://localhost/getDailyCalendarView(startTime='{startTime}',endTime='{endTime}')";

// Act
final LocalTime startLocalTime = LocalTime.of(0, 1, 1);
final LocalTime endLocalTime = LocalTime.of(2, 2, 0);
requestInfo.pathParameters.put("startTime", startLocalTime);
requestInfo.pathParameters.put("endTime", endLocalTime);

// Assert
var uriResult = assertDoesNotThrow(() -> requestInfo.getUri());
assertTrue(uriResult.toString().contains("startTime='00%3A01%3A01'"));
assertTrue(uriResult.toString().contains("endTime='02%3A02%3A00'"));
}

@Test
void SetsPathParametersOfPeriodAndDurationType() {
// Arrange as the request builders would
final RequestInformation requestInfo = new RequestInformation();
requestInfo.httpMethod = HttpMethod.GET;
requestInfo.urlTemplate = "http://localhost/getRestaurantSeating(mealDuration='{mealDuration}',seatingDuration='{seatingDuration}')";

// Act
final PeriodAndDuration startLocalTime = PeriodAndDuration.of(Period.of(1, 2, 3), Duration.ofHours(10)) ;
final PeriodAndDuration endLocalTime = PeriodAndDuration.parse("PT30M");
requestInfo.pathParameters.put("mealDuration", startLocalTime);
requestInfo.pathParameters.put("seatingDuration", endLocalTime);

// Assert
var uriResult = assertDoesNotThrow(() -> requestInfo.getUri());
assertTrue(uriResult.toString().contains("mealDuration='P1Y2M3DT10H'"));
assertTrue(uriResult.toString().contains("seatingDuration='PT30M'"));
}


@Test
void ExpandQueryParametersAfterPathParams() {
// Arrange as the request builders would
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ public class UriTemplateTest {
"async",
true,
"page",
10,
"date",
OffsetDateTime.of(2023, 2, 13, 18, 49, 00, 00, ZoneOffset.UTC));
10);

// Tests inspired from:
// https://github.com/micronaut-projects/micronaut-core/blob/02992a905cf9a2279b7fe8e49927ff080cb937d5/http/src/test/groovy/io/micronaut/http/uri/UriTemplateSpec.groovy
@ParameterizedTest
@CsvFileSource(resources = "/data.csv", numLinesToSkip = 1, delimiter = ';')
@CsvFileSource(resources = "/data.csv", numLinesToSkip = 1, delimiter = ',')
void shouldProduceExpectedOutput(String template, String expected) {
String result = StdUriTemplate.expand(template, params);

Expand Down
13 changes: 6 additions & 7 deletions components/abstractions/src/test/resources/data.csv
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
template, result
{+baseurl}/users; http://localhost:8080/users
{+baseurl}/users/{id}/list; http://localhost:8080/users/1/list
{+baseurl}/users/{id}/list{?async*}; http://localhost:8080/users/1/list?async=true
{+baseurl}/users/{id}/list{?async*,unmatched*}; http://localhost:8080/users/1/list?async=true
{+baseurl}/users/{id}/list{?async*,unmatched*,page*}; http://localhost:8080/users/1/list?async=true&page=10
{+baseurl}/users/{id}/name/{name}/list{?async*,unmatched*,page*}; http://localhost:8080/users/1/name/foo/list?async=true&page=10
{+baseurl}/users/{id}/list{?async*,unmatched*,page*,date*}; http://localhost:8080/users/1/list?async=true&page=10&date=2023-02-13T18%3A49%3A00Z
"{+baseurl}/users", http://localhost:8080/users
"{+baseurl}/users/{id}/list", http://localhost:8080/users/1/list
"{+baseurl}/users/{id}/list{?async*}", http://localhost:8080/users/1/list?async=true
"{+baseurl}/users/{id}/list{?async*,unmatched*}", http://localhost:8080/users/1/list?async=true
"{+baseurl}/users/{id}/list{?async*,unmatched*,page*}", http://localhost:8080/users/1/list?async=true&page=10
"{+baseurl}/users/{id}/name/{name}/list{?async*,unmatched*,page*}", http://localhost:8080/users/1/name/foo/list?async=true&page=10
Loading