Class PageContractAssert

java.lang.Object
org.apache.juneau.console.test.PageContractAssert

public final class PageContractAssert extends Object
Test helper that extracts the #juneau-page contract and <template> ids from a rendered console page, validates them against PageContractSchema, and offers fluent assertions so adopters assert on the contract instead of on rendered chrome HTML.

Failures throw AssertionError, so the class works under any test framework.

assertPage(String) also fails immediately if the contract still carries the pre-rename top-level version key (now contractVersion), even when isValid() is never called.

Example:

String html = client.get("/slo").run().getContent().asString(); PageContractAssert.assertPage(html) .isValid() .hasContractVersion("1") .hasActiveNav("slo") .hasNavPath("home", "about") .hasNavHref("slo", "/slo") .hasFooterText("Sandbox Support Console") .hasHeaderSlot("banner") .hasCard("releases", "datatables") .hasCardKey("releases", "/table/dataUrl", "/rest/releases/data") .templateContains("jc-seg-1", "id=\"ssc-table-slot\""); PageContractAssert.assertPage(html) .subscribes("tasks", "selection:changes", "params") .declaresTopic("ops.jobs") .hasBridge("ops") .hasNoWiringErrors();

Since:
10.0.0
  • Method Details

    • assertPage

      public static PageContractAssert assertPage(String html)
      Parses the contract and templates out of a rendered page.
      Parameters:
      html - The full rendered page.
      Returns:
      A new assertion object.
      Throws:
      AssertionError - If the page has no parseable contract island.
    • isValid

      Asserts the contract passes the schema and rules R-1..R-4.
      Returns:
      This object.
    • hasContractVersion

      Parameters:
      v - The expected contract version. @return This object.
    • hasTitle

      Parameters:
      v - The expected page title. @return This object.
    • hasTheme

      Parameters:
      name - The expected theme name. @return This object.
    • hasActiveNav

      Parameters:
      idPath - The expected active path, root first. @return This object.
    • hasNoActiveNav

      Asserts activeNav is empty (the JS prefix fallback applies). @return This object.
    • hasNavPath

      public PageContractAssert hasNavPath(String... idPath)
      Parameters:
      idPath - A root-first id path that must exist in nav. @return This object.
    • hasNavHref

      Parameters:
      id - A node id, or a /-separated id path such as "home/about".
      href - The expected href.
      Returns:
      This object.
    • hasNavChildren

      public PageContractAssert hasNavChildren(String idPath, String... childIds)
      Parameters:
      idPath - A /-separated id path.
      childIds - The expected child ids, in order.
      Returns:
      This object.
    • hasHeaderTitle

      Parameters:
      v - The expected header.title. @return This object.
    • hasHeaderSlot

      Parameters:
      name - A header slot name (brand, actions, replace, banner). @return This object.
    • hasFooterText

      Parameters:
      v - The expected footer.text. @return This object.
    • hasFooterSlot

      Parameters:
      name - A footer slot name (content). @return This object.
    • hasCard

      Parameters:
      id - The card id.
      type - The expected card type.
      Returns:
      This object.
    • hasCardOrder

      Parameters:
      ids - The expected card ids, in contract order. @return This object.
    • subscribes

      public PageContractAssert subscribes(String cardId, String topic)
      Parameters:
      cardId - The card id. @param topic A topic the card must subscribe to. @return This object.
    • subscribes

      public PageContractAssert subscribes(String cardId, String topic, String role)
      Parameters:
      cardId - The card id.
      topic - A topic the card must subscribe to.
      role - The role it must be wired to.
      Returns:
      This object.
    • publishes

      public PageContractAssert publishes(String cardId, String topic)
      Parameters:
      cardId - The card id. @param topic A custom topic the card must declare in publishes. @return This object.
    • declaresTopic

      Parameters:
      topic - A top-level topics entry that must exist. @return This object.
    • hasBridge

      Parameters:
      id - A top-level bridges id that must exist. @return This object.
    • hasNoWiringErrors

      Runs R-10 and R-11 against the standard card-type registry. Pair it with BusPolicy.check(contract()) (juneau-console-bus) for the server-grant side (E-55). A page with custom card types needs the hasNoWiringErrors(CardTypeRegistry) overload with the registry that knows them.
      Returns:
      This object.
    • hasNoWiringErrors

      Parameters:
      registry - The registry that knows the page's card types. @return This object.
    • hasCardSrc

      Parameters:
      id - The card id.
      src - The expected src value.
      Returns:
      This object.
    • hasCardKey

      public PageContractAssert hasCardKey(String id, String jsonPointer, Object expected)
      Parameters:
      id - The card id.
      jsonPointer - A /-separated path into the card's JSON, for example "/table/dataUrl".
      expected - The expected value at that path.
      Returns:
      This object.
    • templateContains

      public PageContractAssert templateContains(String templateId, String fragment)
      Parameters:
      templateId - A data-card or data-slot template id.
      fragment - Markup that must appear in the template.
      Returns:
      This object.
    • isTwinOf

      Asserts this page and another render the same contract and the same template markup.

      Use it to prove two ways of producing a page (for example a hand-written template and its builder twin) agree. The contracts are compared as key-sorted JSON; list order is kept, because card, nav and option order is part of the page. Templates are compared by id on their stripped markup, so the whitespace a template author's layout puts around a card body does not count.

      The failure message names the first difference, for example page contracts differ at $.cards[0].bare: this=true other=false.

      Parameters:
      other - The page this one must equal.
      Returns:
      This object.
    • assertTwins

      public static void assertTwins(Map<?,?> contract, Map<String,String> templates, Map<?,?> otherContract, Map<String,String> otherTemplates)
      Asserts two contract and template pairs are twins, as isTwinOf(PageContractAssert) does.

      Exposed so a test that must first adjust either side (for example remove a card known to differ) can still use the shared comparison.

      Parameters:
      contract - The first contract.
      templates - The first page's templates, by id.
      otherContract - The second contract.
      otherTemplates - The second page's templates, by id.
      Throws:
      AssertionError - If the contracts or templates differ.
    • contract

      public JsonMap contract()
      Escape hatch for asserts this class does not offer.
      Returns:
      The parsed contract.
    • templates

      Returns:
      Every <template data-card|data-slot> id mapped to its inner markup, in document order.
    • template

      public String template(String templateId)
      Parameters:
      templateId - A data-card or data-slot template id.
      Returns:
      The template's inner markup.
    • card

      public JsonMap card(String id)
      Parameters:
      id - The card id.
      Returns:
      The card's parsed JSON.