### Question:
AutocryptHeaderParser { @Nullable AutocryptHeader getValidAutocryptHeader(Message currentMessage) { String[] headers = currentMessage.getHeader(AutocryptHeader.AUTOCRYPT_HEADER); ArrayList<AutocryptHeader> autocryptHeaders = parseAllAutocryptHeaders(headers); boolean isSingleValidHeader = autocryptHeaders.size() == 1; return isSingleValidHeader ? autocryptHeaders.get(0) : null; } private  AutocryptHeaderParser(); static AutocryptHeaderParser getInstance();  }### Answer:
@Test public void getValidAutocryptHeader__withNoHeader__shouldReturnNull() throws Exception { MimeMessage message = parseFromResource("autocrypt/no_autocrypt.eml"); AutocryptHeader autocryptHeader = autocryptHeaderParser.getValidAutocryptHeader(message); assertNull(autocryptHeader); }
@Test public void getValidAutocryptHeader__withBrokenBase64__shouldReturnNull() throws Exception { MimeMessage message = parseFromResource("autocrypt/rsa2048-broken-base64.eml"); AutocryptHeader autocryptHeader = autocryptHeaderParser.getValidAutocryptHeader(message); assertNull(autocryptHeader); }
@Test public void getValidAutocryptHeader__withSimpleAutocrypt() throws Exception { MimeMessage message = parseFromResource("autocrypt/rsa2048-simple.eml"); AutocryptHeader autocryptHeader = autocryptHeaderParser.getValidAutocryptHeader(message); assertNotNull(autocryptHeader); assertEquals("alice@testsuite.autocrypt.org", autocryptHeader.addr); assertEquals(0, autocryptHeader.parameters.size()); assertEquals(1225, autocryptHeader.keyData.length); }
@Test public void getValidAutocryptHeader__withExplicitType() throws Exception { MimeMessage message = parseFromResource("autocrypt/rsa2048-explicit-type.eml"); AutocryptHeader autocryptHeader = autocryptHeaderParser.getValidAutocryptHeader(message); assertNotNull(autocryptHeader); assertEquals("alice@testsuite.autocrypt.org", autocryptHeader.addr); assertEquals(0, autocryptHeader.parameters.size()); }
@Test public void getValidAutocryptHeader__withUnknownType__shouldReturnNull() throws Exception { MimeMessage message = parseFromResource("autocrypt/unknown-type.eml"); AutocryptHeader autocryptHeader = autocryptHeaderParser.getValidAutocryptHeader(message); assertNull(autocryptHeader); }
@Test public void getValidAutocryptHeader__withUnknownCriticalHeader__shouldReturnNull() throws Exception { MimeMessage message = parseFromResource("autocrypt/rsa2048-unknown-critical.eml"); AutocryptHeader autocryptHeader = autocryptHeaderParser.getValidAutocryptHeader(message); assertNull(autocryptHeader); }
@Test public void getValidAutocryptHeader__withUnknownNonCriticalHeader() throws Exception { MimeMessage message = parseFromResource("autocrypt/rsa2048-unknown-non-critical.eml"); AutocryptHeader autocryptHeader = autocryptHeaderParser.getValidAutocryptHeader(message); assertNotNull(autocryptHeader); assertEquals("alice@testsuite.autocrypt.org", autocryptHeader.addr); assertEquals(1, autocryptHeader.parameters.size()); assertEquals("ignore", autocryptHeader.parameters.get("_monkey")); }