Class InterceptorManager

  • All Implemented Interfaces:
    java.lang.AutoCloseable

    public class InterceptorManager
    extends java.lang.Object
    implements java.lang.AutoCloseable
    A class that keeps track of network calls by either reading the data from an existing test session record or recording the network calls in memory. Test session records are saved or read from: "session-records/ testName.json"
    • If the testMode is TestMode.PLAYBACK, the manager tries to find an existing test session record to read network calls from.
    • If the testMode is TestMode.RECORD, the manager creates a new test session record and saves all the network calls to it.
    • If the testMode is TestMode.LIVE, the manager won't attempt to read or create a test session record.
    When the InterceptorManager is disposed, if the testMode is TestMode.RECORD, the network calls that were recorded are persisted to: "session-records/testName.json"
    • Constructor Summary

      Constructors 
      Constructor Description
      InterceptorManager​(TestContextManager testContextManager)
      Creates a new InterceptorManager that either replays test-session records or saves them.
      InterceptorManager​(java.lang.String testName, java.net.URL unitTestOutDir, java.util.Map<java.lang.String,​java.lang.String> textReplacementRules, boolean doNotRecord, java.lang.String playbackRecordName)
      Creates a new InterceptorManager that replays test session records.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addTextReplacementRule​(java.lang.String regex, java.lang.String replacement)
      Add text replacement rule (regex as key, the replacement text as value) into textReplacementRules
      void close()
      Disposes of resources used by this InterceptorManager.
      com.azure.android.core.http.HttpClient getPlaybackClient()
      Gets a new HTTP client that plays back test session records managed by InterceptorManager.
      RecordedData getRecordedData()
      Gets the recorded data InterceptorManager is keeping track of.
      com.azure.android.core.http.HttpPipelinePolicy getRecordPolicy()
      Gets a new HTTP pipeline policy that records network calls and its data is managed by InterceptorManager.
      boolean isLiveMode()
      Gets whether this InterceptorManager is in live mode.
      boolean isPlaybackMode()
      Gets whether this InterceptorManager is in playback mode.
      • Methods inherited from class java.lang.Object

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

      • InterceptorManager

        public InterceptorManager​(TestContextManager testContextManager)
        Creates a new InterceptorManager that either replays test-session records or saves them.
        • If testMode is TestMode.PLAYBACK, the manager tries to find an existing test session record to read network calls from.
        • If testMode is TestMode.RECORD, the manager creates a new test session record and saves all the network calls to it.
        • If testMode is TestMode.LIVE, the manager won't attempt to read or create a test session record.
        The test session records are persisted in the path: "session-records/testName.json"
        Parameters:
        testContextManager - Contextual information about the test being ran, such as test name, TestMode, and others.
        Throws:
        java.io.UncheckedIOException - If testMode is TestMode.PLAYBACK and an existing test session record could not be located or the data could not be deserialized into an instance of RecordedData.
        java.lang.NullPointerException - If testName is null.
      • InterceptorManager

        public InterceptorManager​(java.lang.String testName,
                                  java.net.URL unitTestOutDir,
                                  java.util.Map<java.lang.String,​java.lang.String> textReplacementRules,
                                  boolean doNotRecord,
                                  java.lang.String playbackRecordName)
        Creates a new InterceptorManager that replays test session records. It takes a set of textReplacementRules, that can be used by PlaybackClient to replace values in a NetworkCallRecord.getResponse(). The test session records are read from: "session-records/testName.json"
        Parameters:
        testName - Name of the test.
        unitTestOutDir - The unit test output directory
        textReplacementRules - A set of rules to replace text in NetworkCallRecord.getResponse() when playing back network calls.
        doNotRecord - Flag indicating whether network calls should be record or played back.
        playbackRecordName - Full name of the test including its iteration, used as the playback record name.
        Throws:
        java.io.UncheckedIOException - An existing test session record could not be located or the data could not be deserialized into an instance of RecordedData.
        java.lang.NullPointerException - If testName or textReplacementRules is null.
    • Method Detail

      • isPlaybackMode

        public boolean isPlaybackMode()
        Gets whether this InterceptorManager is in playback mode.
        Returns:
        true if the InterceptorManager is in playback mode and false otherwise.
      • isLiveMode

        public boolean isLiveMode()
        Gets whether this InterceptorManager is in live mode.
        Returns:
        true if the InterceptorManager is in live mode and false otherwise.
      • getRecordedData

        public RecordedData getRecordedData()
        Gets the recorded data InterceptorManager is keeping track of.
        Returns:
        The recorded data managed by InterceptorManager.
      • getRecordPolicy

        public com.azure.android.core.http.HttpPipelinePolicy getRecordPolicy()
        Gets a new HTTP pipeline policy that records network calls and its data is managed by InterceptorManager.
        Returns:
        HttpPipelinePolicy to record network calls.
      • getPlaybackClient

        public com.azure.android.core.http.HttpClient getPlaybackClient()
        Gets a new HTTP client that plays back test session records managed by InterceptorManager.
        Returns:
        An HTTP client that plays back network calls from its recorded data.
      • close

        public void close()
        Disposes of resources used by this InterceptorManager. If testMode is TestMode.RECORD, all the network calls are persisted to: "session-records/testName.json"
        Specified by:
        close in interface java.lang.AutoCloseable
      • addTextReplacementRule

        public void addTextReplacementRule​(java.lang.String regex,
                                           java.lang.String replacement)
        Add text replacement rule (regex as key, the replacement text as value) into textReplacementRules
        Parameters:
        regex - the pattern to locate the position of replacement
        replacement - the replacement text