@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")); }
@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"); }
@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 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 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); }
@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 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 resolveLinkNoVariables() { Link link = Link.create("reference", "https://example.com/2"); link.resolve(); assertThat(link.isTemplated()).isFalse(); assertThat(link.getTemplateVariables()).isEmpty(); }
@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); }
@Test void expandLinkMissingVariable() { Link link = Link.create("reference", "https://example.com/{a}/2/{b}"); link.resolve(); assertThatIllegalArgumentException() .isThrownBy(() -> link.expand(Collections.singletonMap("a", "test"))) .withMessageContaining("missing value for 'b'"); }