#1172 - Provide static helpers to build TemplateVariable instance.
We now expose static factory methods to make code setting up a UriTemplate with TemplateVariable instances. Original pull request: #1192.
This commit is contained in:
committed by
Oliver Drotbohm
parent
f297bb28e5
commit
f1bc790a23
@@ -71,6 +71,66 @@ public final class TemplateVariable implements Serializable {
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper to fashion {@link VariableType#PATH_VARIABLE} variables.
|
||||
*
|
||||
* @param pathVariable
|
||||
* @return
|
||||
*/
|
||||
public static TemplateVariable pathVariable(String pathVariable) {
|
||||
return new TemplateVariable(pathVariable, VariableType.PATH_VARIABLE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper to fashion {@link VariableType#REQUEST_PARAM} variables.
|
||||
*
|
||||
* @param requestParam
|
||||
* @return
|
||||
*/
|
||||
public static TemplateVariable requestParam(String requestParam) {
|
||||
return new TemplateVariable(requestParam, VariableType.REQUEST_PARAM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper to fashion {@link VariableType#REQUEST_PARAM_CONTINUED} variables.
|
||||
*
|
||||
* @param requestParam
|
||||
* @return
|
||||
*/
|
||||
public static TemplateVariable requestParamContinued(String requestParam) {
|
||||
return new TemplateVariable(requestParam, VariableType.REQUEST_PARAM_CONTINUED);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper to fashion {@link VariableType#SEGMENT} variables.
|
||||
*
|
||||
* @param segment
|
||||
* @return
|
||||
*/
|
||||
public static TemplateVariable segment(String segment) {
|
||||
return new TemplateVariable(segment, VariableType.SEGMENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper to fashion {@link VariableType#FRAGMENT} variables.
|
||||
*
|
||||
* @param fragment
|
||||
* @return
|
||||
*/
|
||||
public static TemplateVariable fragment(String fragment) {
|
||||
return new TemplateVariable(fragment, VariableType.FRAGMENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Static helper to fashion {@link VariableType#COMPOSITE_PARAM} variables.
|
||||
*
|
||||
* @param compositeParam
|
||||
* @return
|
||||
*/
|
||||
public static TemplateVariable compositeParam(String compositeParam) {
|
||||
return new TemplateVariable(compositeParam, VariableType.COMPOSITE_PARAM);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns whether the variable has a description.
|
||||
*
|
||||
@@ -192,7 +252,7 @@ public final class TemplateVariable implements Serializable {
|
||||
.orElseThrow(() -> new IllegalArgumentException("Unsupported variable type " + key + "!"));
|
||||
}
|
||||
|
||||
/*
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see java.lang.Enum#toString()
|
||||
*/
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.hateoas;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.hateoas.TemplateVariable.*;
|
||||
import static org.springframework.hateoas.UriTemplateUnitTest.EncodingFixture.*;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
@@ -38,7 +39,7 @@ import org.apache.commons.io.output.ByteArrayOutputStream;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
import org.springframework.hateoas.TemplateVariable.VariableType;
|
||||
import org.springframework.hateoas.TemplateVariable.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link UriTemplate}.
|
||||
@@ -300,6 +301,25 @@ class UriTemplateUnitTest {
|
||||
assertThat(template.expand("value").toString()).isEqualTo("/foo?bar=value");
|
||||
}
|
||||
|
||||
@Test // 1172
|
||||
void useHelperMethodsToBuildUriTemplates() {
|
||||
|
||||
assertThat(UriTemplate.of("/foo").with(pathVariable("var")).getVariableNames()).containsExactly("var");
|
||||
|
||||
assertThat(UriTemplate.of("/foo").with(requestParam("var")).with(requestParamContinued("var2")).toString())
|
||||
.isEqualTo("/foo{?var,var2}");
|
||||
assertThat(UriTemplate.of("/foo").with(requestParam("var")).with(requestParam("var2")).toString())
|
||||
.isEqualTo("/foo{?var,var2}");
|
||||
|
||||
assertThat(UriTemplate.of("/foo").with(requestParamContinued("var2")).toString()).isEqualTo("/foo{&var2}");
|
||||
|
||||
assertThat(UriTemplate.of("/foo").with(segment("var")).toString()).isEqualTo("/foo{/var}");
|
||||
|
||||
assertThat(UriTemplate.of("/foo").with(fragment("var")).toString()).isEqualTo("/foo{#var}");
|
||||
|
||||
assertThat(UriTemplate.of("/foo").with(compositeParam("var")).toString()).isEqualTo("/foo{*var}");
|
||||
}
|
||||
|
||||
private static void assertVariables(UriTemplate template, TemplateVariable... variables) {
|
||||
assertVariables(template, Arrays.asList(variables));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user