Class InMemoryClientInfoPort

java.lang.Object
com.fayupable.logged.test.InMemoryClientInfoPort
All Implemented Interfaces:
IClientInfoPort

public final class InMemoryClientInfoPort extends Object implements IClientInfoPort
An IClientInfoPort that always returns a fixed, test-controlled caller identity, instead of resolving one from Spring Security or the current HTTP request.

A test asserting on MethodInvocationEvent.callerIdentity() would otherwise need a real authenticated SecurityContext or a mock HTTP request just to control this one field. This class lets the caller identity be set directly instead.

  • Constructor Details

    • InMemoryClientInfoPort

      public InMemoryClientInfoPort()
      Creates an instance that reports the caller identity as "unknown" until setCallerIdentity(String) is called.
    • InMemoryClientInfoPort

      public InMemoryClientInfoPort(String callerIdentity)
      Creates an instance that reports the given caller identity.
      Parameters:
      callerIdentity - the caller identity to report
  • Method Details

    • resolveCallerIdentity

      public String resolveCallerIdentity()
      Description copied from interface: IClientInfoPort
      Resolves an identifier describing who triggered the current invocation.
      Specified by:
      resolveCallerIdentity in interface IClientInfoPort
      Returns:
      a caller identity such as "user:42" or "ip:203.0.113.10", or an implementation-defined placeholder such as "unknown" if no identity can be resolved
    • setCallerIdentity

      public void setCallerIdentity(String callerIdentity)
      Changes the caller identity reported by subsequent calls to resolveCallerIdentity().
      Parameters:
      callerIdentity - the caller identity to report from now on
    • resolveCallerIp

      public String resolveCallerIp()
      Description copied from interface: IClientInfoPort
      Resolves the IP address of the caller that triggered the current invocation, independent of whatever IClientInfoPort.resolveCallerIdentity() itself resolves to.

      Unlike IClientInfoPort.resolveCallerIdentity(), which resolves a single identity from an either/or chain of tiers (authenticated principal, then IP, then a placeholder), this method exists so that a caller's IP can be recorded alongside an authenticated identity rather than only as a fallback used when no identity is available — valuable for security-sensitive operations (login, password reset, admin actions) where the IP remains useful for audit and rate-limiting purposes even when the call also resolves to an authenticated user.

      This is a default method, not an abstract one, so that adding it does not break existing implementations of this interface compiled against an earlier version of this library. The default returns null, meaning "this adapter does not support resolving an IP independent of caller identity" — consistent with this library's convention of using null for "not available" on structured fields, rather than a placeholder string.

      Specified by:
      resolveCallerIp in interface IClientInfoPort
      Returns:
      the caller's IP address, or null if it cannot be resolved (for example, no HTTP request is available on the current thread, or this adapter does not implement IP resolution)
    • setCallerIp

      public void setCallerIp(String callerIp)
      Changes the IP address reported by subsequent calls to resolveCallerIp(), for testing a @Logged(includeIp = true) method's MethodInvocationEvent.callerIp() without wiring a real HTTP request. Defaults to null (not resolved) until this is called.
      Parameters:
      callerIp - the IP address to report from now on