Class KiwiServletMocks
- java.lang.Object
-
- org.kiwiproject.beta.test.servlet.KiwiServletMocks
-
@Beta public final class KiwiServletMocks extends Object
Static utilities to create Mockito-based mocks for servlet API code.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.mockito.ArgumentMatcher<X509Certificate[]>matchesCertArrayByX500PrincipalName(String name)Argument matcher that matches a certificate array containing exactly oneX509Certificatehaving anX500Principalwith the given name.static org.mockito.ArgumentMatcher<X509Certificate[]>matchesExpectedCertArrayBySubjectDN(String subjectDn)Argument matcher that matches a certificate array containing exactly oneX509Certificatehaving the given subject DN.static org.mockito.ArgumentMatcher<X509Certificate>matchesExpectedCertBySubjectDN(String subjectDn)Argument matcher that matches a certificate having the given subject DN.static org.mockito.ArgumentMatcher<X509Certificate>matchesExpectedCertByX500PrincipalName(String name)Argument matcher that matches a certificate having anX500Principalwith the given name.static javax.servlet.http.HttpServletRequestmockHttpServletRequestWithCertificate(String dn)Create a mockHttpServletRequestwith the given subject distinguished name.static javax.servlet.http.HttpServletRequestmockHttpServletRequestWithNoCertificate()Create a mockHttpServletRequestthat contains no X509 certificate.static X509CertificatemockX509Certificate(String dn)Create a mockX509Certificatewith the given subject distinguished name.
-
-
-
Method Detail
-
mockHttpServletRequestWithCertificate
public static javax.servlet.http.HttpServletRequest mockHttpServletRequestWithCertificate(String dn)
Create a mockHttpServletRequestwith the given subject distinguished name.- Parameters:
dn- the subject distinguished name- Returns:
- a mocked HttpServletRequest with the given subject distinguished name
- Implementation Note:
- Don't inline the 'certificate' in the thenReturn() call on the mock request.
For some reason that I have not fully investigated, Mockito gets really upset and throws
a
UnfinishedStubbingException.
-
mockHttpServletRequestWithNoCertificate
public static javax.servlet.http.HttpServletRequest mockHttpServletRequestWithNoCertificate()
Create a mockHttpServletRequestthat contains no X509 certificate.- Returns:
- a mocked HttpServletRequest with no X.509 certificate
- Implementation Note:
- This is not strictly necessary since a Mockito mock will return null for methods that return a reference type if not provided any expectations. But, it makes test code more explicit about the intent of the code, so that's why this exists.
-
mockX509Certificate
public static X509Certificate mockX509Certificate(String dn)
Create a mockX509Certificatewith the given subject distinguished name.- Parameters:
dn- the subject distinguished name- Returns:
- a mocked X509Certificate with the given subject distinguished name
- Implementation Note:
- Has to mock the
Principalreturned byX509Certificate.getSubjectDN()so this actually creates two mocks. Also, sinceX509Certificate.getSubjectX500Principal()returns an instance of the final classX500Principal, we can't mock it and instead a "real" instance ofX500Principalhaving the given distinguished name is returned. Also see StackOverflow entry regarding the getSubjectDN method being "denigrated". And, Java 16 deprecatedX509Certificate.getSubjectDN()though (as of this writing on Nov. 8, 2022) not for removal.
-
matchesExpectedCertArrayBySubjectDN
public static org.mockito.ArgumentMatcher<X509Certificate[]> matchesExpectedCertArrayBySubjectDN(String subjectDn)
Argument matcher that matches a certificate array containing exactly oneX509Certificatehaving the given subject DN. Uses theX509Certificate.getSubjectDN()to obtain thePrincipaland then matches againstPrincipal.getName().- Parameters:
subjectDn- the subject distinguished name- Returns:
- a Mockito argument matcher for an array of X509Certificate objects
-
matchesExpectedCertBySubjectDN
public static org.mockito.ArgumentMatcher<X509Certificate> matchesExpectedCertBySubjectDN(String subjectDn)
Argument matcher that matches a certificate having the given subject DN.- Parameters:
subjectDn- the subject distinguished name- Returns:
- a Mockito argument matcher for an array of X509Certificate objects
-
matchesCertArrayByX500PrincipalName
public static org.mockito.ArgumentMatcher<X509Certificate[]> matchesCertArrayByX500PrincipalName(String name)
Argument matcher that matches a certificate array containing exactly oneX509Certificatehaving anX500Principalwith the given name. UsesX509Certificate.getSubjectX500Principal()and to obtain theX500Principaland then matches againstX500Principal.getName().- Parameters:
name- the name for the X500Principal- Returns:
- a Mockito argument matcher for an array of X509Certificate objects
-
matchesExpectedCertByX500PrincipalName
public static org.mockito.ArgumentMatcher<X509Certificate> matchesExpectedCertByX500PrincipalName(String name)
Argument matcher that matches a certificate having anX500Principalwith the given name.- Parameters:
name- the name for the X500Principal- Returns:
- a Mockito argument matcher for an array of X509Certificate objects
-
-