Class MocksApi

java.lang.Object
net.bugreaper.modules.mocks.MocksApi
All Implemented Interfaces:
MocksConfig, MocksInt

public class MocksApi extends Object implements MocksInt, MocksConfig
API helper that provides a common API for operating with mock-server using RestAssured.

It is recommended to use a single instance: MocksApi mockApi = MocksApi.getInstance();

Enchanted Report for mocks verify default:ON: enchantedReport, can be disabled by: setEnchantedReport(boolean))

Default await timeout for assertions with await is configured by awaitMs. It can be changed using setAwaitMs(int) or configuration.

Since:
1.0.0
  • Field Details

    • awaitPollInterval

      protected volatile int awaitPollInterval
      Default await polling interval in milliseconds for tests.
  • Constructor Details

    • MocksApi

      public MocksApi(String url, int port)
      This constructor initializes client for interaction with mock-server
      Parameters:
      url - mock-server url ("http://localhost")
      port - mock-server port
    • MocksApi

      public MocksApi()
      Constructs a mock-server Api client using YAML configuration.

      Loads configuration values from a YAML file.

      Default file: bugreaper.yml

      Custom file: using -DbugreaperEnv=test loads bugreaper-test.yml

       modules:
         mocks:
           url: http://localhost
           port: 1082
           await: 440 # (optional)
           logging: true # (optional)
           enchanted-report: false # (optional)
       

      Missing required keys will result in configuration errors. Missing optional keys will fall back to predefined defaults.

      Throws:
      IllegalArgumentException - if the configuration contains invalid values
  • Method Details

    • getInstance

      public static MocksApi getInstance()
      Returns the instance of MocksApi with config builder MocksApi().

      This implementation is thread-safe using method-level synchronization.

      Returns:
      the shared instance of MocksApi
      Throws:
      IllegalArgumentException - if the configuration contains invalid values
      See Also:
    • setAwaitMs

      public MocksApi setAwaitMs(int awaitMs)
      Description copied from interface: MocksConfig
      Configures the global await timeout for assertions and operations that use await.
      Specified by:
      setAwaitMs in interface MocksConfig
      Parameters:
      awaitMs - await timeout in milliseconds
      Returns:
      this
    • withAwaitMs

      public MocksApi withAwaitMs(int specificAwaitMs)
      Description copied from interface: MocksConfig
      Configure await for next assert with await (than await rollback to global).

      global MocksConfig.setAwaitMs(int) will be ignored

      Specified by:
      withAwaitMs in interface MocksConfig
      Parameters:
      specificAwaitMs - ms await
      Returns:
      this instance for method chaining
    • setLogging

      public MocksApi setLogging(boolean enable)
      Description copied from interface: MocksConfig
      Enables or disables logging manually (debug log level will print logs anyway!).
      Specified by:
      setLogging in interface MocksConfig
      Parameters:
      enable - true=request/response logging (Allure on always!)
      Returns:
      this instance for method chaining
    • setEnchantedReport

      public MocksApi setEnchantedReport(boolean enchantedReport)
      Description copied from interface: MocksConfig
      Enabling or disabling enchanted mockVerify report.

      Same behavior as MockEnchantedDiffer.

      Specified by:
      setEnchantedReport in interface MocksConfig
      Parameters:
      enchantedReport - true=on/false=off
      Returns:
      this instance for method chaining
    • getConfigSummary

      public String getConfigSummary()
      Description copied from interface: MocksConfig
      Returns and logs (at INFO level) a human-readable summary of all resolved configuration values.

      The summary includes values loaded from the YAML configuration file as well as any fields overridden programmatically after construction. Optional fields that were not present in the configuration and resolved via default values may also be included.

      Specified by:
      getConfigSummary in interface MocksConfig
      Returns:
      String with summary
    • resetMocks

      @Step("(MOCK)[RESET]: reset mock-server") public void resetMocks()
      Description copied from interface: MocksInt
      Reset mock-server.

      Clean all: logs, requests, expectations

      Specified by:
      resetMocks in interface MocksInt
    • cleanMockLogs

      @Step("(MOCK)[CLEAN] Clean all recorded log messages from mock-server") public void cleanMockLogs()
      Description copied from interface: MocksInt
      Clean mock-server logs (requests/responses)
      Specified by:
      cleanMockLogs in interface MocksInt
    • showMockLogs

      @Step("(MOCK)[LOGS] Retrieve all recorded log messages from mock-server") public void showMockLogs()
      Description copied from interface: MocksInt
      Adds an attachment with mock-server logs (requests/responses).

      At DEBUG level, logs are also printed to the console.

      Specified by:
      showMockLogs in interface MocksInt
    • showMockRequests

      @Step("(MOCK)[LOGS] Retrieve all requests to mock-server") public void showMockRequests()
      Description copied from interface: MocksInt
      Add attachment with requests to mock-server
      Specified by:
      showMockRequests in interface MocksInt
    • showBaseRequestsList

      public void showBaseRequestsList()
      Description copied from interface: MocksInt
      Adds an attachment with a list of mock-server requests (only HTTP method and path).
      Specified by:
      showBaseRequestsList in interface MocksInt
    • createMock

      @Step("(MOCK)[CREATE] Create mock-server expectation") public void createMock(@Param(mode=HIDDEN) String mockExpectation)
      Description copied from interface: MocksInt
      Creates a mock-server expectation.
      Specified by:
      createMock in interface MocksInt
      Parameters:
      mockExpectation - JSON string containing the mock-server expectation definition
    • createMockCustom

      public void createMockCustom(String description, String path)
      Description copied from interface: MocksInt
      Creates a mock-server expectation loaded from a file.
      Specified by:
      createMockCustom in interface MocksInt
      Parameters:
      description - description part for step
      path - path to the resource file with mock-server expectation
    • assertAllMocksCount

      @Step("(MOCK)[VERIFY] Mock-server received exactly <{receivedCount}> requests") public void assertAllMocksCount(int receivedCount)
      Description copied from interface: MocksInt
      Asserts that the total number of requests to mock-server matches the expected count.
      Specified by:
      assertAllMocksCount in interface MocksInt
      Parameters:
      receivedCount - expected exact number of requests
    • assertAllMocksCount

      @Step("(MOCK)(VERIFY) Mock-server received requests from <{from}> to <{to}>") public void assertAllMocksCount(int from, int to)
      Description copied from interface: MocksInt
      Asserts that the total number of requests to mock-server is within the specified range.
      Specified by:
      assertAllMocksCount in interface MocksInt
      Parameters:
      from - minimum expected number of requests
      to - maximum expected number of requests
    • verifyMockCustom

      public void verifyMockCustom(String description, String path)
      Description copied from interface: MocksInt
      Verifies mock-server requests according to the verification setup loaded from a file.
      Specified by:
      verifyMockCustom in interface MocksInt
      Parameters:
      description - description part for step
      path - path to the resource file with mock-server verification setup
    • verifyMock

      @Step("(MOCK)[VERIFY] Verify mock-server request") public void verifyMock(@Param(mode=HIDDEN) String verifySetup)
      Description copied from interface: MocksInt
      Verifies mock-server requests according to the provided verification setup.
      Specified by:
      verifyMock in interface MocksInt
      Parameters:
      verifySetup - JSON string containing the mock-server verification setup
    • verifyMockSequence

      @Step("(MOCK)[VERIFY] Verify mock-server request sequence") public void verifyMockSequence(String verifySequenceSetup)
      Description copied from interface: MocksInt
      Verifies mock-server requests according to the verification setup loaded from a file.
      Specified by:
      verifyMockSequence in interface MocksInt
      Parameters:
      verifySequenceSetup - JSON string containing the mock-server sequence verification setup
    • verifyMockCustomWithAwait

      public void verifyMockCustomWithAwait(String description, String path)
      Description copied from interface: MocksInt
      Verifies mock-server requests according to the verification setup loaded from a file.

      Uses await.

      Specified by:
      verifyMockCustomWithAwait in interface MocksInt
      Parameters:
      description - description part for step
      path - path to the resource file with mock-server verification setup
    • verifyMockSequenceWithAwait

      @Step("(MOCK)[VERIFY] Verify mock-server request sequence using await") public void verifyMockSequenceWithAwait(@Param(mode=HIDDEN) String verifySequenceSetup)
      Description copied from interface: MocksInt
      Verifies mock-server requests according to the sequence verification setup

      Uses await.

      Specified by:
      verifyMockSequenceWithAwait in interface MocksInt
      Parameters:
      verifySequenceSetup - JSON string containing the mock-server sequence verification setup
    • verifyMockWithAwait

      @Step("(MOCK)[VERIFY] Verify mock-server request using await") public void verifyMockWithAwait(@Param(mode=HIDDEN) String verifySetup)
      Description copied from interface: MocksInt
      Verifies mock-server requests according to the provided verification setup.

      Uses await.

      Specified by:
      verifyMockWithAwait in interface MocksInt
      Parameters:
      verifySetup - JSON string containing the mock-server verification setup
    • assertMocksCountWithAwait

      @Step("(MOCK)(VERIFY) Mock-server received requests from <{from}> to <{to}> using await") public void assertMocksCountWithAwait(int from, int to)
      Description copied from interface: MocksInt
      Asserts that the total number of requests to mock-server is within the specified range.

      Uses await.

      Specified by:
      assertMocksCountWithAwait in interface MocksInt
      Parameters:
      from - minimum expected number of requests
      to - maximum expected number of requests
    • getRequestValue

      @Step("[MOCK]: Get value from request {extractPath}") public Object getRequestValue(String mockSetup, String extractPath)
      Description copied from interface: MocksInt
      Returns a value extracted from a mock-server request (body, header, or other source).

      Can be used to retrieve dynamic data (for example, ID, hash, or Base64 value) required for subsequent assertions.

      
       var value = mocksApi.getRequestValue(
            """
            {
                 "method": "POST",
                 "path": "/api/user"
            }""",
            "[0].body.json.name"); //grab value from key "name" from body of first request by condition
       
      Specified by:
      getRequestValue in interface MocksInt
      Parameters:
      mockSetup - - setup for request
      extractPath - - json path in request to mock (example: "[0].body.json.id")
      Returns:
      Object (any types)
    • baseAuthGenerate

      public static String baseAuthGenerate(String user, String pass)