public static Link create(String rel, String href, String description) { return new Link(rel, href, description); }
private static void mapLink(Link link, ObjectNode node) { node.put("href", link.getHref()); if (link.isTemplated()) { node.put("templated", true); } if (link.getDescription() != null) { node.put("title", link.getDescription()); } }
@Test void resolveLinkWithVariables() { Link link = Link.create("reference", "https://example.com/{a}/2/{b}"); link.resolve(); assertThat(link.isTemplated()).isTrue(); assertThat(link.getTemplateVariables()).containsExactlyInAnyOrder("a", "b"); }
@Test void expandLinkWithSameAttributeAtTwoPlaces() throws Exception { Link link = Link.create("reference", "https://example.com/{a}/2/{a}"); link.resolve(); Map<String, String> map = new LinkedHashMap<>(); map.put("a", "test"); map.put("b", "another"); assertThat(link.expand(map)) .isEqualTo(new URI("https://example.com/test/2/test")); }
@Test void keepOrderingWithMultipleUrlForSameRel() { List<Link> links = new ArrayList<>(); links.add(Link.create("first", "https://example.com")); links.add(Link.create("second", "https://example.com")); links.add(Link.create("first", "https://example.com")); ObjectNode model = LinkMapper.mapLinks(links); String json = model.toString(); assertThat(json.indexOf("first")).isLessThan(json.indexOf("second")); }
/** * Map the specified links to a json model. If several links share the same relation, * they are grouped together. * @param links the links to map * @return a model for the specified links */ public static ObjectNode mapLinks(List<Link> links) { ObjectNode result = nodeFactory.objectNode(); Map<String, List<Link>> byRel = new LinkedHashMap<>(); links.forEach((it) -> byRel.computeIfAbsent(it.getRel(), (k) -> new ArrayList<>()) .add(it)); byRel.forEach((rel, l) -> { if (l.size() == 1) { ObjectNode root = JsonNodeFactory.instance.objectNode(); mapLink(l.get(0), root); result.set(rel, root); } else { ArrayNode root = JsonNodeFactory.instance.arrayNode(); l.forEach((link) -> { ObjectNode node = JsonNodeFactory.instance.objectNode(); mapLink(link, node); root.add(node); }); result.set(rel, root); } }); return result; }
@Test void resolveLinkNoVariables() { Link link = Link.create("reference", "https://example.com/2"); link.resolve(); assertThat(link.isTemplated()).isFalse(); assertThat(link.getTemplateVariables()).isEmpty(); }
@Test void expandLink() throws Exception { Link link = Link.create("reference", "https://example.com/{a}/2/{b}"); link.resolve(); Map<String, String> map = new LinkedHashMap<>(); map.put("a", "test"); map.put("b", "another"); assertThat(link.expand(map)) .isEqualTo(new URI("https://example.com/test/2/another")); }
@Test void keepOrdering() { List<Link> links = new ArrayList<>(); links.add(Link.create("first", "https://example.com")); links.add(Link.create("second", "https://example.com")); ObjectNode model = LinkMapper.mapLinks(links); String json = model.toString(); assertThat(json.indexOf("first")).isLessThan(json.indexOf("second")); }
@Test void mapSimpleRel() { List<Link> links = new ArrayList<>(); links.add(Link.create("a", "https://example.com", "some description")); ObjectNode model = LinkMapper.mapLinks(links); assertThat(model).hasSize(1); assertThat(model.has("a")).isTrue(); ObjectNode linkModel = (ObjectNode) model.get("a"); assertThat(linkModel).hasSize(2); assertThat(linkModel.get("href").textValue()).isEqualTo("https://example.com"); assertThat(linkModel.get("title").textValue()).isEqualTo("some description"); }
public static Link create(String rel, String href, boolean templated) { return new Link(rel, href, templated); }
@Test void mapTemplatedRel() { List<Link> links = new ArrayList<>(); links.add(Link.create("a", "https://example.com/{bootVersion}/a", true)); ObjectNode model = LinkMapper.mapLinks(links); assertThat(model).hasSize(1); assertThat(model.has("a")).isTrue(); ObjectNode linkModel = (ObjectNode) model.get("a"); assertThat(linkModel).hasSize(2); assertThat(linkModel.get("href").textValue()) .isEqualTo("https://example.com/{bootVersion}/a"); assertThat(linkModel.get("templated").booleanValue()).isEqualTo(true); }
public static Link create(String rel, String href) { return new Link(rel, href); }
@Test void mergeSeveralLinksInArray() { List<Link> links = new ArrayList<>(); links.add(Link.create("a", "https://example.com", "some description")); links.add(Link.create("a", "https://example.com/2")); ObjectNode model = LinkMapper.mapLinks(links); assertThat(model).hasSize(1); assertThat(model.has("a")).isTrue(); ArrayNode linksModel = (ArrayNode) model.get("a"); assertThat(linksModel).hasSize(2); assertThat(linksModel.get(0).get("href").textValue()) .isEqualTo("https://example.com"); assertThat(linksModel.get(1).get("href").textValue()) .isEqualTo("https://example.com/2"); }
@Test void linksRendered() { Dependency dependency = Dependency.withId("foo", "com.example", "foo"); dependency.getLinks().add(Link.create("guide", "https://example.com/how-to")); dependency.getLinks().add(Link.create("reference", "https://example.com/doc")); InitializrMetadata metadata = InitializrMetadataTestBuilder.withDefaults() .addDependencyGroup("test", dependency).build(); String json = this.jsonMapper.write(metadata, null); int first = json.indexOf("https://example.com/how-to"); int second = json.indexOf("https://example.com/doc"); // JSON objects are not ordered assertThat(first).isGreaterThan(0); assertThat(second).isGreaterThan(0); }
@Test void resolveInvalidLinkNoHref() { Link link = Link.create("reference", null, "foo doc"); assertThatExceptionOfType(InvalidInitializrMetadataException.class) .isThrownBy(link::resolve); }
@Test void invalidLink() { Dependency dependency = Dependency.withId("foo"); dependency.getLinks().add(Link.create(null, "https://example.com")); assertThatExceptionOfType(InvalidInitializrMetadataException.class) .isThrownBy(dependency::resolve); }