Interface RedisHelperInt

All Known Implementing Classes:
Redis, RedisHashHelper, RedisHelper, RedisListHelper, RedisSetHelper, RedisStreamHelper, RedisStringHelper, RedisZSetHelper

public interface RedisHelperInt
Interface defines methods for facilitating helper interactions and assertions. Validates that all required methods are implemented.
  • Method Details

    • addRecord

      void addRecord(String key, String value)
      Adds a record to Redis using the configured Redis data type helper.

      The storage operation depends on the Redis data type

      • LIST - appends a new element to the list
      • HASH - stores a value in the hash by key:id
      • STRING - stores or replaces the string value
      • SET - appends a new element to the set (duplicates ignored)
      • ZSET - appends a new element to the zset by key:score (duplicates ignored)
      • STREAM - appends a new JSON element to the stream
      Parameters:
      key - Redis key
      value - value to store in Redis
      Throws:
      RedisHelperException - if no id provided in keyWithId (only for HASH) if no score provided or not numeric score in keyWithScore (only for ZSET)
      net.bugreaper.core.exceptions.JsonMappersException - if the provided JSON is invalid (only for STREAM)
    • addRecord

      void addRecord(String key, String value, long ttlSeconds)
      Adds a record to Redis with an expiration time.

      The storage operation depends on the Redis data type

      • LIST - appends a new element to the list, TTL for all list
      • HASH - stores a value in the hash by key:id, TTL for all hash
      • STRING - stores or replaces the string value, TTL for key
      • SET - stores a value in the hash by key, TTL for all set
      • ZSET - appends a new element to the zset by key:score, TTL for all zset
      • STREAM - appends a new JSON element to the stream, TTL for all stream
      Parameters:
      key - Redis key
      value - value to store in Redis
      ttlSeconds - time to live in seconds
      Throws:
      RedisHelperException - if no id provided in keyWithId (only for HASH) if no score provided or not numeric score in keyWithScore (only for ZSET)
      net.bugreaper.core.exceptions.JsonMappersException - if the provided JSON is invalid (only for STREAM)
    • cleanAll

      void cleanAll()
      Removes ALL data from current Redis database.

      WARNING: This operation deletes every key from selected Redis DB.

    • deleteKeysByPattern

      void deleteKeysByPattern(String keyPattern)
      Deletes Redis keys matching the specified pattern.

      Uses Redis SCAN to safely find matching keys without blocking the Redis server.

      Examples: users:admins (LIST) ├── 0 -> Alex └── 1 -> John users:developers (STRINGS) ├── 0 -> Michael └── 1 -> Max users:data (HASH) ├── 1 -> test1 └── 2 -> test2

       deleteByKeyPattern("users:*");
       

      Deletes:

       users:admins
       users:developers
       users:data
       

      Also supports exact key deletion:

       deleteByKeyPattern("users:admins");
       
      Parameters:
      keyPattern - Redis key pattern or exact key
    • grabRecordsByKey

      net.bugreaper.core.assertable.AssertableStringList grabRecordsByKey(String key)
      Retrieves all records stored under the specified Redis key.

      Uses await until the key exists.

      The returned records depend on the configured Redis data type:

      • LIST - returns list elements
      • HASH - returns hash values
      • STRING - returns a single value as a list
      • SET - returns set elements
      • ZSET - returns zset elements
      • STREAM - returns stream elements - records are represented as JSON strings

      Examples:

       grabRecordsByKey("users:admins")
        .seeListAnyEquals("John");
       
      Parameters:
      key - Redis key
      Returns:
      AssertableStringList list of stored records
      Throws:
      AssertionError - if the key does not exist or not a current Redis type
    • getRecordsCountByKey

      long getRecordsCountByKey(String key)
      Returns the number of records stored under the specified Redis key.

      The returned count depend on the configured Redis data type:

      • LIST - returns elements count in list
      • HASH - returns values count in hash
      • STRING - stores only a single value per key
      • SET - returns values count in set
      • ZSET - returns values count in zset
      • STREAM - returns values count in stream
      Parameters:
      key - Redis key
      Returns:
      number of records stored under the key
    • getKeysCountByPattern

      long getKeysCountByPattern(String keyPattern)
      Returns the number of Redis keys matching the specified pattern.

      This method counts keys, not records inside.

      Example:

       users:admins      -> LIST
       users:developers  -> HASH
       users:me          -> STRING
      
       getKeysCountByPattern("users:*") returns 3
       
      Parameters:
      keyPattern - Redis key pattern
      Returns:
      number of matching Redis keys
    • getKeyExistsStatus

      boolean getKeyExistsStatus(String key)
      Checks whether the specified Redis key exists.

      The method works with any Redis data type, including LIST, STRING, HASH, SET, ZSET, and STREAM.

      Parameters:
      key - Redis key
      Returns:
      true if the key exists, otherwise false
    • getTtlByKey

      long getTtlByKey(String key)
      Returns the remaining TTL (time-to-live) of the specified Redis key.
      Parameters:
      key - Redis key
      Returns:
      remaining TTL in seconds
      Throws:
      RedisHelperException - if the key does not exist or has no expiration
    • getKeyType

      String getKeyType(String key)
      Returns the Redis data type of the specified key.

      Possible return values:

      • string
      • list
      • set
      • zset
      • hash
      • stream
      • none (key does not exist)
      Parameters:
      key - Redis key
      Returns:
      Redis data type or none if the key does not exist
    • seeKeyExists

      void seeKeyExists(String key)
      Asserts that the specified Redis key exists.

      Uses await.

      The method works with any Redis data type, including LIST, STRING, HASH, SET, ZSET, and STREAM.

      Example:

       seeKeyExists("users:admins");
       seeKeyExists("users:1");
       
      Parameters:
      key - Redis key
      Throws:
      AssertionError - if the key does not exist
    • seeKeyTypeIs

      void seeKeyTypeIs(String key, String type)
      Asserts that the specified Redis key has the expected data type.

      Supported Redis data types:

      • string
      • list
      • set
      • zset
      • hash
      • stream

      Examples:

       seeKeyTypeIs("users:1", "string");
       seeKeyTypeIs("users:admins", "list");
       seeKeyTypeIs("users", "hash");
       
      Parameters:
      key - Redis key
      type - expected Redis data type
      Throws:
      AssertionError - if the key type does not match the expected type
    • seeRecordsCountByKeyIsExactly

      void seeRecordsCountByKeyIsExactly(String key, long expectedCount)
      Asserts that the number of records in the Redis key is exactly the expected value.

      Uses await.

      The returned records depend on the configured Redis data type:

      • LIST - number of records stored in the specified Redis LIST
      • HASH - number of records stored in the specified Redis HASH
      • STRING - contains only one value per key if exist
      • SET - number of records stored in the specified Redis SET
      • ZSET - number of records stored in the specified Redis ZSET
      • STREAM - number of records stored in the specified Redis STREAM
      Parameters:
      key - Redis key
      expectedCount - expected number of records
      Throws:
      AssertionError - if the assertion fails
    • seeKeysCountByPatternIsExactly

      void seeKeysCountByPatternIsExactly(String keyPattern, long expectedCount)
      Asserts that the number of Redis keys matching the specified pattern is exactly the expected count.

      Uses await.

      Parameters:
      keyPattern - Redis key pattern
      expectedCount - expected number of keys
      Throws:
      AssertionError - if the assertion fails
    • seeKeysCountByPatternIsGreaterThan

      void seeKeysCountByPatternIsGreaterThan(String keyPattern, long minCount)
      Asserts that the number of Redis keys matching the specified pattern is greater than the given value.

      Uses await.

      Parameters:
      keyPattern - Redis key pattern
      minCount - minimum expected number of keys
      Throws:
      AssertionError - if the assertion fails
    • seeKeysCountByPatternIsLessThan

      void seeKeysCountByPatternIsLessThan(String keyPattern, long maxCount)
      Asserts that the number of Redis keys matching the specified pattern is less than the given value.

      Uses await.

      Parameters:
      keyPattern - Redis key pattern
      maxCount - maximum expected number of keys
      Throws:
      AssertionError - if the assertion fails
    • seeTtlIsGreaterThan

      void seeTtlIsGreaterThan(String key, int minTtl)
      Asserts that the key TTL is greater than the specified value.
      Parameters:
      key - Redis key
      minTtl - minimum expected TTL in seconds
      Throws:
      AssertionError - if the TTL validation fails
      RedisHelperException - if the key does not exist or has no TTL
    • seeTtlIsLessThan

      void seeTtlIsLessThan(String key, int maxTtl)
      Asserts that the key TTL is less than the specified value.
      Parameters:
      key - Redis key
      maxTtl - maximum expected TTL in seconds
      Throws:
      AssertionError - if the TTL validation fails
      RedisHelperException - if the key does not exist or has no TTL