### Question:
MultipartUtils { public static List<MultiPartSpecification> convertToMultipart(List<Map<String, String>> parts) { return parts.stream() .map(part -> { final MultiPartSpecification specification; final String name = part.get(TestCaseUtils.JSON_FIELD_MULTIPART_NAME); Preconditions.checkNotNull(name, "'%s' for a part is mandatory", TestCaseUtils.JSON_FIELD_MULTIPART_NAME); if (part.containsKey(TestCaseUtils.JSON_FIELD_MULTIPART_FILE)) { final String filepath = part.get(TestCaseUtils.JSON_FIELD_MULTIPART_FILE); Preconditions.checkState(StringUtils.isNotBlank(filepath), "'%s' is mandatory. Missing for part [%s]", TestCaseUtils.JSON_FIELD_MULTIPART_FILE, name); final File content = resolve(filepath); Preconditions.checkNotNull(content.exists(), "Can't find file: ", filepath); final String contentType = Optional.ofNullable(part.get(TestCaseUtils.JSON_FIELD_MULTIPART_CONTENT_TYPE)) .orElse(URLConnection.guessContentTypeFromName(filepath)); Preconditions.checkArgument(StringUtils.isNotBlank(contentType), "'%s' isn't specified and can't be automatically detected. Missing for part [%s]", TestCaseUtils.JSON_FIELD_MULTIPART_CONTENT_TYPE, name); specification = new MultiPartSpecBuilder(content) .fileName(content.getName()) .mimeType(contentType) .controlName(name) .build(); } else { final String value = part.get(TestCaseUtils.JSON_FIELD_MULTIPART_VALUE); Preconditions.checkNotNull(StringUtils.isNotBlank(value), "'%s' is mandatory. Missing for part [%s]", TestCaseUtils.JSON_FIELD_MULTIPART_VALUE, name); specification = new MultiPartSpecBuilder(value) .controlName(name) .build(); } return specification; }) .collect(Collectors.toList()); }  static List<MultiPartSpecification> convertToMultipart(List<Map<String, String>> parts); static File resolve(String filepath);  }### Answer:
@Test public void testSimpleValue() throws Exception { final List<Map<String, String>> parts = ImmutableList.of( ImmutableMap.of( TestCaseUtils.JSON_FIELD_MULTIPART_NAME, "simpleField", TestCaseUtils.JSON_FIELD_MULTIPART_VALUE, "simpleValue" ) ); final List<MultiPartSpecification> spec = MultipartUtils.convertToMultipart(parts); assertEquals(spec.size(), 1); assertEquals(spec.get(0).getContent(), "simpleValue"); assertEquals(spec.get(0).getControlName(), "simpleField"); assertNull(spec.get(0).getCharset()); assertNull(spec.get(0).getFileName()); assertNull(spec.get(0).getMimeType()); }
@Test public void testMultipleValues() throws Exception { final List<Map<String, String>> parts = ImmutableList.of( ImmutableMap.of( TestCaseUtils.JSON_FIELD_MULTIPART_NAME, "simpleField", TestCaseUtils.JSON_FIELD_MULTIPART_VALUE, "simpleValue" ), ImmutableMap.of( TestCaseUtils.JSON_FIELD_MULTIPART_NAME, "simpleField", TestCaseUtils.JSON_FIELD_MULTIPART_VALUE, "simpleValue" ) ); final List<MultiPartSpecification> spec = MultipartUtils.convertToMultipart(parts); assertEquals(spec.size(), 2); }
@Test public void testFileWithNoMimetype() throws Exception { final List<Map<String, String>> parts = ImmutableList.of( ImmutableMap.of( TestCaseUtils.JSON_FIELD_MULTIPART_NAME, "upload", TestCaseUtils.JSON_FIELD_MULTIPART_FILE, "/files/multipart.something" ) ); try { MultipartUtils.convertToMultipart(parts); fail("It should fail for missing mimetype"); } catch (IllegalArgumentException e) { assertEquals(e.getMessage(), "'contentType' isn't specified and can't be automatically detected. Missing for part [upload]"); } }