### Question:
X509CertificateUtil { public static String getPropertyFromX500Principal(X509Certificate cert, String propertyName) { String propertyValue = null; String principalName = cert.getSubjectX500Principal().getName(); log.debug("Found principalName = {}", principalName); Pattern pattern = createPattern(propertyName); Matcher matcher = pattern.matcher(principalName); if (matcher.find()) { propertyValue = matcher.group(1); log.debug("Found principal property {} = {}", propertyName, propertyValue); } else { logAndThrowError("Principal property " + propertyName + " not found in Certificate"); } return propertyValue; } private  X509CertificateUtil(); static String getPropertyFromX500Principal(X509Certificate cert, String propertyName);  }### Answer:
@Test public void getPropertyFromX500Principal() { X509Certificate cert = mock(X509Certificate.class); X500Principal principal = new X500Principal("CN=Duke, OU=JavaSoft, O=Sun Microsystems, C=US"); when(cert.getSubjectX500Principal()).thenReturn(principal); String value = X509CertificateUtil.getPropertyFromX500Principal(cert, "CN"); assertNotNull(value); assertEquals("Duke", value); }
@Test public void getPropertyFromX500PrincipalNoPrincipalProperty() { X509Certificate cert = mock(X509Certificate.class); X500Principal principal = new X500Principal("OU=JavaSoft, O=Sun Microsystems, C=US"); when(cert.getSubjectX500Principal()).thenReturn(principal); try { X509CertificateUtil.getPropertyFromX500Principal(cert, "CN"); fail(); } catch (Exception e) { assertEquals("Principal property CN not found in Certificate", e.getMessage()); } }