Skip to content

Commit

Permalink
#593 - Polishing.
Browse files Browse the repository at this point in the history
Refactored existing unit tests to use the newly introduced test fixture type. Refactored ticket references to use the current style.
  • Loading branch information
odrotbohm committed Nov 28, 2019
1 parent d98e678 commit 5ae89bc
Showing 1 changed file with 39 additions and 136 deletions.
175 changes: 39 additions & 136 deletions src/test/java/org/springframework/hateoas/UriTemplateUnitTest.java
Expand Up @@ -48,10 +48,8 @@
*/
class UriTemplateUnitTest {

/**
* @see #137
*/
@Test
@Test // #137
@SuppressWarnings("null")
void discoversTemplate() {

assertThat(UriTemplate.isTemplate("/foo{?bar}")).isTrue();
Expand All @@ -60,65 +58,47 @@ void discoversTemplate() {
assertThat(UriTemplate.isTemplate("")).isFalse();
}

/**
* @see #137
*/
@Test
@Test // #137
void discoversRequestParam() {

UriTemplate template = UriTemplate.of("/foo{?bar}");

assertVariables(template, new TemplateVariable("bar", VariableType.REQUEST_PARAM));
}

/**
* @see #137
*/
@Test
@Test // #137
void discoversRequestParamCntinued() {

UriTemplate template = UriTemplate.of("/foo?bar{&foobar}");

assertVariables(template, new TemplateVariable("foobar", VariableType.REQUEST_PARAM_CONTINUED));
}

/**
* @see #137
*/
@Test
@Test // #137
void discoversOptionalPathVariable() {

UriTemplate template = UriTemplate.of("/foo{/bar}");

assertVariables(template, new TemplateVariable("bar", VariableType.SEGMENT));
}

/**
* @see #137
*/
@Test
@Test // #137
void discoversPathVariable() {

UriTemplate template = UriTemplate.of("/foo/{bar}");

assertVariables(template, new TemplateVariable("bar", VariableType.PATH_VARIABLE));
}

/**
* @see #137
*/
@Test
@Test // #137
void discoversFragment() {

UriTemplate template = UriTemplate.of("/foo{#bar}");

assertVariables(template, new TemplateVariable("bar", VariableType.FRAGMENT));
}

/**
* @see #137
*/
@Test
@Test // #137
void discoversMultipleRequestParam() {

UriTemplate template = UriTemplate.of("/foo{?bar,foobar}");
Expand All @@ -127,10 +107,7 @@ void discoversMultipleRequestParam() {
new TemplateVariable("foobar", VariableType.REQUEST_PARAM));
}

/**
* @see #137
*/
@Test
@Test // #137
void expandsRequestParameter() {

UriTemplate template = UriTemplate.of("/foo{?bar}");
Expand All @@ -139,10 +116,7 @@ void expandsRequestParameter() {
assertThat(uri.toString()).isEqualTo("/foo?bar=myBar");
}

/**
* @see #137
*/
@Test
@Test // #137
void expandsMultipleRequestParameters() {

Map<String, Object> parameters = new HashMap<>();
Expand All @@ -155,10 +129,7 @@ void expandsMultipleRequestParameters() {
assertThat(uri.toString()).isEqualTo("/foo?bar=myBar&fooBar=myFooBar");
}

/**
* @see #137
*/
@Test
@Test // #137
void rejectsMissingRequiredPathVariable() {

UriTemplate template = UriTemplate.of("/foo/{bar}");
Expand All @@ -168,48 +139,33 @@ void rejectsMissingRequiredPathVariable() {
});
}

/**
* @see #137
*/
@Test
@Test // #137
void expandsMultipleVariablesViaArray() {

UriTemplate template = UriTemplate.of("/foo{/bar}{?firstname,lastname}{#anchor}");
URI uri = template.expand("path", "Dave", "Matthews", "discography");
assertThat(uri.toString()).isEqualTo("/foo/path?firstname=Dave&lastname=Matthews#discography");
}

/**
* @see #137
*/
@Test
@Test // #137
void expandsTemplateWithoutVariablesCorrectly() {
assertThat(UriTemplate.of("/foo").expand().toString()).isEqualTo("/foo");
}

/**
* @see #137
*/
@Test
@Test // #137
void correctlyExpandsFullUri() {
assertThat(UriTemplate.of("http://localhost:8080/foo{?bar}").expand().toString())
.isEqualTo("http://localhost:8080/foo");
}

/**
* @see #137
*/
@Test
@Test // #137
void rendersUriTempalteWithPathVariable() {

UriTemplate template = UriTemplate.of("/{foo}/bar{?page}");
assertThat(template.toString()).isEqualTo("/{foo}/bar{?page}");
}

/**
* #@see 137
*/
@Test
@Test // #137
void addsTemplateVariables() {

UriTemplate source = UriTemplate.of("/{foo}/bar{?page}");
Expand All @@ -222,10 +178,7 @@ void addsTemplateVariables() {
assertVariables(source.with(new TemplateVariables(toAdd)), expected);
}

/**
* @see #217
*/
@Test
@Test // #217
void doesNotAddVariablesForAlreadyExistingRequestParameters() {

UriTemplate template = UriTemplate.of("/?page=2");
Expand All @@ -236,53 +189,32 @@ void doesNotAddVariablesForAlreadyExistingRequestParameters() {
assertThat(result.getVariableNames()).isEmpty();
}

/**
* @see #217
*/
@Test
@Test // #217
void doesNotAddVariablesForAlreadyExistingFragment() {

UriTemplate template = UriTemplate.of("/#fragment");
UriTemplate result = template.with(new TemplateVariables(new TemplateVariable("fragment", VariableType.FRAGMENT)));
assertThat(result.getVariableNames()).isEmpty();
}

/**
* @see #271
*/
@Test
void expandASimplePathVariable() {

UriTemplate template = UriTemplate.of("/foo/{id}");
assertThat(template.expand(2).toString()).isEqualTo("/foo/2");
}

/**
* @see #273
*/
@Test
@Test // #273
@SuppressWarnings("null")
void rejectsEmptyBaseUri() {

assertThatIllegalArgumentException().isThrownBy(() -> {
new UriTemplate(null, TemplateVariables.NONE);
UriTemplate.of(null, TemplateVariables.NONE);
});
}

/**
* @see #281
*/
@Test
@Test // #281
void allowsAddingTemplateVariable() {

UriTemplate template = UriTemplate.of("/").with("q", VariableType.REQUEST_PARAM);

assertThat(template.toString()).isEqualTo("/{?q}");
}

/**
* @see #483
*/
@Test
@Test // #483
void compositveValuesAreRecognisedAsVariableType() {

UriTemplate template = UriTemplate.of("/foo{&bar,foobar*}");
Expand All @@ -291,66 +223,37 @@ void compositveValuesAreRecognisedAsVariableType() {
new TemplateVariable("foobar", VariableType.COMPOSITE_PARAM));
}

/**
* @see #483
*/
@Test
@Test // #483
@SuppressWarnings("serial")
void expandsCompositeValueAsAssociativeArray() {

UriTemplate template = UriTemplate.of("/foo{&bar,foobar*}");

String expandedTemplate = template.expand(new HashMap<String, Object>() {
{
put("bar", "barExpanded");
put("foobar", new HashMap<String, String>() {
of("/foo{&bar,foobar*}", "/foo?bar=barExpanded&city=Clarksville&state=TN") //
.param("bar", "barExpanded") //
.param("foobar", new HashMap<String, String>() {
{
put("city", "Clarksville");
put("state", "TN");
}
});
}
}).toString();

assertThat(expandedTemplate).isEqualTo("/foo?bar=barExpanded&city=Clarksville&state=TN");
}) //
.verify();
}

/**
* @see #483
*/
@Test
@SuppressWarnings("serial")
@Test // #483
void expandsCompositeValueAsList() {

UriTemplate template = UriTemplate.of("/foo{&bar,foobar*}");

String expandedTemplate = template.expand(new HashMap<String, Object>() {
{
put("bar", "barExpanded");
put("foobar", Arrays.asList("foo1", "foo2"));
}
}).toString();

assertThat(expandedTemplate).isEqualTo("/foo?bar=barExpanded&foobar=foo1&foobar=foo2");
of("/foo{&bar,foobar*}", "/foo?bar=barExpanded&foobar=foo1&foobar=foo2") //
.param("bar", "barExpanded") //
.param("foobar", Arrays.asList("foo1", "foo2")) //
.verify();
}

/**
* @see #483
*/
@Test
@SuppressWarnings("serial")
@Test // #483
void handlesCompositeValueAsSingleValue() {

UriTemplate template = UriTemplate.of("/foo{&bar,foobar*}");

String expandedTemplate = template.expand(new HashMap<String, Object>() {
{
put("bar", "barExpanded");
put("foobar", "singleValue");
}
}).toString();

assertThat(expandedTemplate).isEqualTo("/foo?bar=barExpanded&foobar=singleValue");
of("/foo{&bar,foobar*}", "/foo?bar=barExpanded&foobar=singleValue") //
.param("bar", "barExpanded") //
.param("foobar", "singleValue") //
.verify();
}

@Test // #1127
Expand Down

0 comments on commit 5ae89bc

Please sign in to comment.