### Question:
AttachmentResolver { @VisibleForTesting static Map<String,Uri> buildCidToAttachmentUriMap(AttachmentInfoExtractor attachmentInfoExtractor, Part rootPart) { HashMap<String,Uri> result = new HashMap<>(); Stack<Part> partsToCheck = new Stack<>(); partsToCheck.push(rootPart); while (!partsToCheck.isEmpty()) { Part part = partsToCheck.pop(); Body body = part.getBody(); if (body instanceof Multipart) { Multipart multipart = (Multipart) body; for (Part bodyPart : multipart.getBodyParts()) { partsToCheck.push(bodyPart); } } else { try { String contentId = part.getContentId(); if (contentId != null) { AttachmentViewInfo attachmentInfo = attachmentInfoExtractor.extractAttachmentInfo(part); result.put(contentId, attachmentInfo.internalUri); } } catch (MessagingException e) { Timber.e(e, "Error extracting attachment info"); } } } return Collections.unmodifiableMap(result); } private  AttachmentResolver(Map<String, Uri> contentIdToAttachmentUriMap); @Nullable Uri getAttachmentUriForContentId(String cid); @WorkerThread static AttachmentResolver createFromPart(Part part);  }### Answer:
@Test public void buildCidMap__onPartWithNoBody__shouldReturnEmptyMap() throws Exception { Part part = new MimeBodyPart(); Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, part); assertTrue(result.isEmpty()); }
@Test public void buildCidMap__onMultipartWithNoParts__shouldReturnEmptyMap() throws Exception { Multipart multipartBody = MimeMultipart.newInstance(); Part multipartPart = new MimeBodyPart(multipartBody); Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, multipartPart); assertTrue(result.isEmpty()); }
@Test public void buildCidMap__onMultipartWithEmptyBodyPart__shouldReturnEmptyMap() throws Exception { Multipart multipartBody = MimeMultipart.newInstance(); BodyPart bodyPart = spy(new MimeBodyPart()); Part multipartPart = new MimeBodyPart(multipartBody); multipartBody.addBodyPart(bodyPart); Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, multipartPart); verify(bodyPart).getContentId(); assertTrue(result.isEmpty()); }
@Test public void buildCidMap__onTwoPart__shouldReturnBothUris() throws Exception { Multipart multipartBody = MimeMultipart.newInstance(); Part multipartPart = new MimeBodyPart(multipartBody); BodyPart subPart1 = new MimeBodyPart(); BodyPart subPart2 = new MimeBodyPart(); multipartBody.addBodyPart(subPart1); multipartBody.addBodyPart(subPart2); subPart1.setHeader(MimeHeader.HEADER_CONTENT_ID, "cid-1"); subPart2.setHeader(MimeHeader.HEADER_CONTENT_ID, "cid-2"); when(attachmentInfoExtractor.extractAttachmentInfo(subPart1)).thenReturn(new AttachmentViewInfo( null, null, AttachmentViewInfo.UNKNOWN_SIZE, ATTACHMENT_TEST_URI_1, false, subPart1, true)); when(attachmentInfoExtractor.extractAttachmentInfo(subPart2)).thenReturn(new AttachmentViewInfo( null, null, AttachmentViewInfo.UNKNOWN_SIZE, ATTACHMENT_TEST_URI_2, false, subPart2, true)); Map<String,Uri> result = AttachmentResolver.buildCidToAttachmentUriMap(attachmentInfoExtractor, multipartPart); assertEquals(2, result.size()); assertEquals(ATTACHMENT_TEST_URI_1, result.get("cid-1")); assertEquals(ATTACHMENT_TEST_URI_2, result.get("cid-2")); }