Class MocksApi

All Implemented Interfaces:
MocksConfig, MocksInt

public class MocksApi extends ApiAbstract implements MocksInt, MocksConfig
Class for API integration with mock-server

For one instance run recommended: MocksApi mockApi = MocksApi.getInstance();

Create @Step for every Created and Verified mock in your mock Class for allure report

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

Await for some assert default: awaitMs, can be changed by: setAwaitMs(int)

  • 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 mock-server client 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.

  • 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 singleton instance of MocksApi
      See Also:
    • setAwaitMs

      public MocksApi setAwaitMs(int awaitMs)
      Description copied from interface: MocksConfig
      Configure global await for asserts with await
      Specified by:
      setAwaitMs in interface MocksConfig
      Parameters:
      awaitMs - await ms
      Returns:
      this instance for method chaining
    • 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 Mocks") 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
      Add attachment with mock server logs (requests/responses)

      On DEBUG level also print to 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
      Add attachment with mock requests list (only method and path)
      Specified by:
      showBaseRequestsList in interface MocksInt
    • createMock

      @Step("(MOCK)[CREATE] Create Mock") public void createMock(@Param(mode=HIDDEN) String mockExpectation)
      Description copied from interface: MocksInt
      Create mock expectation
      Specified by:
      createMock in interface MocksInt
      Parameters:
      mockExpectation - - String with mock expectation Json
    • createMockCustom

      public void createMockCustom(String description, String path)
      Description copied from interface: MocksInt
      Create mock expectation
      Specified by:
      createMockCustom in interface MocksInt
      Parameters:
      description - - description part for step
      path - - path to file in resources
    • assertAllMocksCount

      @Step("(MOCK)[VERIFY] Assert count of ALL requests to Mock-server {receivedCount}") public void assertAllMocksCount(int receivedCount)
      Description copied from interface: MocksInt
      Assert of all requests to mock-server count
      Specified by:
      assertAllMocksCount in interface MocksInt
      Parameters:
      receivedCount - - expected EXACTLY count of requests
    • assertAllMocksCount

      @Step("(MOCK)(VERIFY) Assert count of ALL requests to Mock-server from:{from} to{to}") public void assertAllMocksCount(int from, int to)
      Description copied from interface: MocksInt
      Assert of all requests to mock-server count from to
      Specified by:
      assertAllMocksCount in interface MocksInt
      Parameters:
      from - int expected from
      to - int expected to
    • verifyMockCustom

      public void verifyMockCustom(String description, String path)
      Description copied from interface: MocksInt
      Verify requests to mock-server by setup from file
      Specified by:
      verifyMockCustom in interface MocksInt
      Parameters:
      description - - description part for step
      path - - path to file in resources
    • verifyMock

      @Step("(MOCK)[VERIFY] Verify mock") public void verifyMock(@Param(mode=HIDDEN) String verifySetup)
      Description copied from interface: MocksInt
      Verify requests to mock-server by verify setup
      Specified by:
      verifyMock in interface MocksInt
      Parameters:
      verifySetup - - String with mock verify Json
    • verifyMockSequence

      @Step("(MOCK)[VERIFY] Verify sequence") public void verifyMockSequence(String verifySetup)
      Description copied from interface: MocksInt
      Verify sequence of requests to mock-server by verify setup
      Specified by:
      verifyMockSequence in interface MocksInt
      Parameters:
      verifySetup - - String with mock verify Json
    • verifyMockCustomWithAwait

      public void verifyMockCustomWithAwait(String description, String path)
      Description copied from interface: MocksInt
      Verify requests to mock-server by setup from file

      with await

      Specified by:
      verifyMockCustomWithAwait in interface MocksInt
      Parameters:
      description - - description part for step
      path - - path to file in resources
    • verifyMockSequenceWithAwait

      @Step("(MOCK)[VERIFY] Verify sequence with await") public void verifyMockSequenceWithAwait(@Param(mode=HIDDEN) String verifySetup)
      Description copied from interface: MocksInt
      Verify sequence of requests to mock server by verify setup

      with await

      Specified by:
      verifyMockSequenceWithAwait in interface MocksInt
      Parameters:
      verifySetup - - String with mock verify Json
    • verifyMockWithAwait

      @Step("(MOCK)[VERIFY] Verify mock with await") public void verifyMockWithAwait(@Param(mode=HIDDEN) String verifySetup)
      Description copied from interface: MocksInt
      Verify requests to mock-server by verify setup

      with await

      Specified by:
      verifyMockWithAwait in interface MocksInt
      Parameters:
      verifySetup - - String with mock verify Json
    • assertMocksCountWithAwait

      @Step("(MOCK)(VERIFY) Assert count of ALL requests to Mock-server from:{from} to{to} with await") public void assertMocksCountWithAwait(int from, int to)
      Description copied from interface: MocksInt
      Assert of all requests to mock-server count from to

      with await

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

      @Step("[MOCK]: Get value from request {extractPath}") public Object getRequestValue(String mockSetup, String extractPath)
      Description copied from interface: MocksInt
      Return value from request (can be body, header, other)

      Can be used to get some info (id, hash, base64...) for next check

      
       var value = mocksApi.getRequestValue(
            """
            {
                 "method": "POST",
                 "path": "/api/user"
            }""",
            "[0].body.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.parameters.name")
      Returns:
      Object (any types)
    • baseAuthGenerate

      public static String baseAuthGenerate(String user, String pass)