Class ItemTracker

  • All Implemented Interfaces:
    org.junit.rules.TestRule

    public class ItemTracker
    extends org.junit.rules.ExternalResource
    A JUnit Rule for tracking items created in the course of an integration test execution. The rule must be created with a set of item types to keep track of. Before the tests run, this rule will capture pre-existing items for each of the types specified; after the tests it cleans all items that were created by the test, ie. they are not pre-existent to the test.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void after()  
      protected void before()  
      <T extends ItemModel>
      java.util.List<T>
      getCreatedItems​(java.lang.Class<T> type)
      Retrieves items created by the test
      <T extends ItemModel>
      boolean
      isCreated​(java.lang.Class<T> type, java.util.function.Predicate<T> condition)
      Checks whether item specified by the condition was created during the test execution.
      static ItemTracker track​(java.lang.Class<? extends ItemModel>... types)
      Creates this rule by specifying types of the items to be tracked.
      • Methods inherited from class org.junit.rules.ExternalResource

        apply
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • track

        @SafeVarargs
        public static ItemTracker track​(java.lang.Class<? extends ItemModel>... types)
        Creates this rule by specifying types of the items to be tracked. Be mindful of the number of types included because this rules keeps all items pre-existing for each of the types in memory.
        Parameters:
        types - types, for which items should be tracked by this rule. Sometimes, items of one type cannot be deleted while items of other types exist. In this case make sure the dependent items are cleaned last, e.g. "Product", "CatalogVersion", "Catalog"
        Returns:
        a rule initialized with the item types.
      • before

        protected void before()
        Overrides:
        before in class org.junit.rules.ExternalResource
      • after

        protected void after()
        Overrides:
        after in class org.junit.rules.ExternalResource
      • isCreated

        public <T extends ItemModel> boolean isCreated​(java.lang.Class<T> type,
                                                       java.util.function.Predicate<T> condition)
        Checks whether item specified by the condition was created during the test execution.
        Type Parameters:
        T - ItemModel subclass.
        Parameters:
        type - type of the item to check.
        condition - a condition the created item should match, e.g. a unique field match, etc.
        Returns:
        true, if there is at least one item that did not exist before the test but exists at the time of this method call; false otherwise.
        Throws:
        java.lang.IllegalArgumentException - if the specified type is not tracked
      • getCreatedItems

        public <T extends ItemModel> java.util.List<T> getCreatedItems​(java.lang.Class<T> type)
        Retrieves items created by the test
        Parameters:
        type - type of the items to retrieve
        Returns:
        a list of all items of the specified time created by the test or an empty list, if no items created.
        Throws:
        java.lang.IllegalArgumentException - if the specified type is not tracked