### Question:
PopTokenBuilderUtils { public static RSAPrivateKey keyPemStringToRsaPrivateKey(String privateKeyPemString) throws PopPrivateKeyParseException { if (StringUtils.isBlank(privateKeyPemString)) { throw new IllegalArgumentException("The privateKeyPemString should not be null or empty"); } try { if (privateKeyPemString.startsWith(PKCS_8_PRIVATE_KEY_PREFIX) && privateKeyPemString.contains(PKCS_8_PRIVATE_KEY_SUFFIX)) { privateKeyPemString = privateKeyPemString.substring(PKCS_8_PRIVATE_KEY_PREFIX.length(), privateKeyPemString.indexOf(PKCS_8_PRIVATE_KEY_SUFFIX)); privateKeyPemString = sanitizePrivateKeyPemString(privateKeyPemString); byte[] keyBytes = toBase64DecodedBytes(privateKeyPemString); return buildRsaPrivateKey(keyBytes); } else { throw new PopPrivateKeyParseException( "The privateKeyPemString contains unsupported format, only PKCS#8 format is currently supported"); } } catch (PopPrivateKeyParseException ex) { throw ex; } catch (Exception ex) { throw new PopPrivateKeyParseException( "Error occurred while converting privateKeyPemString to RSAPrivateKey, error: " + ex.toString(), ex); } }  static RSAPrivateKey keyPemStringToRsaPrivateKey(String privateKeyPemString); static RSAPrivateKey encryptedKeyPemStringToRsaPrivateKey(String encryptedPrivateKeyPemString, String privateKeyPassword);  }### Answer:
@Test public void keyPemStringToRsaPrivateKey__validPrivateKeyPemStringInput__successfullyReturnsRsaPrivateKey() throws Exception { byte[] privateKeyBytes = PopTokenBuilderTestUtils.createNewRsaPrivateKey().getEncoded(); String privateKeyPemString = PopTokenBuilderTestUtils.generatePrivateKeyPemString(privateKeyBytes); RSAPrivateKey rsaPrivateKey = PopTokenBuilderUtils.keyPemStringToRsaPrivateKey(privateKeyPemString); assertNotNull("The rsaPrivateKey should not be null", rsaPrivateKey); }
@Test public void keyPemStringToRsaPrivateKey__validPrivateKeyPemStringWithoutLineSeparators__successfullyReturnsRsaPrivateKey() throws Exception { byte[] privateKeyBytes = PopTokenBuilderTestUtils.createNewRsaPrivateKey().getEncoded(); String privatePemKeyFileString = PopTokenBuilderTestUtils.generatePrivateKeyPemString(privateKeyBytes); String privatePemKeyFileStringWithoutLineSeparators = removeNewLineChars(privatePemKeyFileString); RSAPrivateKey rsaPrivateKey = PopTokenBuilderUtils.keyPemStringToRsaPrivateKey(privatePemKeyFileStringWithoutLineSeparators); assertNotNull("The rsaPrivateKey should not be null", rsaPrivateKey); }
@Test public void keyPemStringToRsaPrivateKey__nullPrivateKeyPemStringInput__throwsIllegalArgumentException() throws Exception { try { PopTokenBuilderUtils.keyPemStringToRsaPrivateKey(null); fail("Should have thrown IllegalArgumentException"); } catch (Exception ex) { logger.error("Error occurred while executing the test case, error: " + ex.toString(), ex); assertEquals(IllegalArgumentException.class, ex.getClass()); assertEquals("The privateKeyPemString should not be null or empty", ex.getMessage()); } }
@Test public void keyPemStringToRsaPrivateKey__invalidPrivateKeyPemString__throwsPopPrivateKeyParseException() { try { PopTokenBuilderUtils.keyPemStringToRsaPrivateKey("INVALID KEY PEM STRING"); fail("Should have thrown PopPrivateKeyParseException"); } catch (Exception ex) { logger.error("Error occurred while executing the test case, error: " + ex.toString(), ex); assertEquals(PopPrivateKeyParseException.class, ex.getClass()); assertEquals("The privateKeyPemString contains unsupported format, only PKCS#8 format is currently supported", ex.getMessage()); } }