@Override public Affordance withSelfRel() { if (!linkParams.get(REL.paramName) .contains(Link.REL_SELF)) { linkParams.add(REL.paramName, Link.REL_SELF); } return new Affordance(this.getHref(), linkParams, actionDescriptors); }
@Override public Affordance withSelfRel() { if (!linkParams.get(REL.paramName) .contains(Link.REL_SELF)) { linkParams.add(REL.paramName, Link.REL_SELF); } return new Affordance(this.getHref(), linkParams, actionDescriptors); }
/** * Expands template variables as far as possible, unsatisfied variables will remain variables. This is primarily for * manually created affordances. If the Affordance has been created with linkTo-methodOn, it should not be necessary * to expand the affordance again. * * @param arguments for expansion * @return partially expanded affordance */ public Affordance expandPartially(Map<String, ? extends Object> arguments) { return new Affordance(partialUriTemplate.expand((Map<String, Object>) arguments) .toString(), linkParams, actionDescriptors); }
/** * Expands template variables as far as possible, unsatisfied variables will remain variables. This is primarily for * manually created affordances. If the Affordance has been created with linkTo-methodOn, it should not be necessary * to expand the affordance again. * * @param arguments for expansion, in the order they appear in the template * @return partially expanded affordance */ public Affordance expandPartially(Object... arguments) { return new Affordance(partialUriTemplate.expand(arguments) .toString(), linkParams, actionDescriptors); }
/** * Expands template variables as far as possible, unsatisfied variables will remain variables. This is primarily for * manually created affordances. If the Affordance has been created with linkTo-methodOn, it should not be necessary * to expand the affordance again. * * @param arguments for expansion, in the order they appear in the template * @return partially expanded affordance */ public Affordance expandPartially(Object... arguments) { return new Affordance(partialUriTemplate.expand(arguments) .toString(), linkParams, actionDescriptors); }
@Override public Affordance withRel(String rel) { linkParams.set(REL.paramName, rel); return new Affordance(this.getHref(), linkParams, actionDescriptors); }
/** * Expands template variables as far as possible, unsatisfied variables will remain variables. This is primarily for * manually created affordances. If the Affordance has been created with linkTo-methodOn, it should not be necessary * to expand the affordance again. * * @param arguments for expansion * @return partially expanded affordance */ public Affordance expandPartially(Map<String, ? extends Object> arguments) { return new Affordance(partialUriTemplate.expand((Map<String, Object>) arguments) .toString(), linkParams, actionDescriptors); }
@Override public Affordance withRel(String rel) { linkParams.set(REL.paramName, rel); return new Affordance(this.getHref(), linkParams, actionDescriptors); }
/** * Expands template variables, arguments must satisfy all required template variables, unsatisfied optional * arguments will be removed. * * @param arguments to expansion * @return expanded affordance */ @Override public Affordance expand(Map<String, ? extends Object> arguments) { UriTemplate template = new UriTemplate(partialUriTemplate.asComponents() .toString()); String expanded = template.expand(arguments) .toASCIIString(); return new Affordance(expanded, linkParams, actionDescriptors); }
@Test public void testExpandWithArgumentsMap() { final Affordance affordance = new Affordance("http://localhost/things{?id}", "thing"); Map<String, Object> arguments = new HashMap<String, Object>(); arguments.put("id", 101); assertEquals("http://localhost/things?id=101", affordance.expand(101) .getHref()); }
@Test public void testGetVariables() { final Affordance affordance = new Affordance("http://localhost/things/{id}", "thing"); Assert.assertThat(affordance.getVariableNames(), Matchers.contains("id")); }
@Test public void expandsSimpleStringVariablesPartially() { final Affordance affordance = new Affordance("/protected/res/documents/index.html?focus={contractId}&caller=BLUE&referrer=/protected/res/my_contracts/index.html&fragment=/contractDetails/{ref}", "thing"); Map<String, Object> args = new HashMap<String, Object>(); args.put("ref", 1234567890); assertEquals("/protected/res/documents/index.html?focus={contractId}&caller=BLUE&referrer=/protected/res/my_contracts/index.html&fragment=/contractDetails/1234567890", affordance.expandPartially(args).getHref()); }
@Test public void testAffordanceAsHeader() { final Affordance affordance = new Affordance("http://localhost/things/{id}", "thing", "http://example.net/relation/other"); assertEquals("<http://localhost/things/{id}>; rel=\"thing http://example.net/relation/other\"", affordance.asHeader()); }
@Test public void preservesSimpleStringVariables() { final Affordance affordance = new Affordance("/protected/res/documents/index.html?focus={contractId}&caller=BLUE&referrer=/protected/res/my_contracts/index.html&fragment=/contractDetails/{ref}", "thing"); assertEquals("/protected/res/documents/index.html?focus={contractId}&caller=BLUE&referrer=/protected/res/my_contracts/index.html&fragment=/contractDetails/{ref}", affordance.getHref()); }
@Test public void testExpand() { final Affordance affordance = new Affordance("http://localhost/things{/id}", "thing"); assertEquals("http://localhost/things/100", affordance.expand(100) .getHref()); }
@Test public void testConstructorWithoutRels() { final Affordance affordance = new Affordance("http://localhost/things/{id}"); assertEquals("http://localhost/things/{id}", affordance.getHref()); Assert.assertNull("rel must be null", affordance.getRel()); assertEquals(0, affordance.getRels() .size()); Assert.assertThat(affordance.getRels(), Matchers.is(Matchers.empty())); }
@Test public void testConstructorWithRels() { final Affordance affordance = new Affordance("http://localhost/things/{id}", "start", "http://example.net/relation/other"); assertEquals("http://localhost/things/{id}", affordance.getHref()); assertEquals("start", affordance.getRel()); Assert.assertThat(affordance.getRels(), Matchers.contains("start", "http://example.net/relation/other")); }
@Test public void testConstructorWithSingleRel() { final Affordance affordance = new Affordance("http://localhost/things/{id}", "thing"); assertEquals("http://localhost/things/{id}", affordance.getHref()); assertEquals("thing", affordance.getRel()); Assert.assertThat(affordance.getRels(), Matchers.contains("thing")); }
@Test public void testIsTemplated() { final Affordance affordance = new Affordance("http://localhost/things/{id}", "thing", "http://example.net/relation/other"); assertEquals("http://localhost/things/{id}", affordance.getHref()); Assert.assertTrue("must recognize template", affordance.isTemplated()); }
@Test public void testLinkExtensionParams() { final Affordance affordance = new Affordance("http://example.com"); affordance.addLinkParam("name", "name-to-distinguish-links-with-same-rel"); affordance.addLinkParam("deprecation", "http://example.com/why/this/is/deprecated"); affordance.addLinkParam("type", "application/json"); Affordance.DynaBean linkExtensions = affordance.getLinkExtensions(); assertEquals("application/json", affordance.getType()); assertEquals("must only contain link extension params", "{name=name-to-distinguish-links-with-same-rel, " + "deprecation=http://example.com/why/this/is/deprecated}", linkExtensions.toString()); }