Java Code Examples for org.springframework.mock.web.MockMultipartFile

The following examples show how to use org.springframework.mock.web.MockMultipartFile. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: AthenaServing   Source File: GrayConfigControllerTest.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 新增灰度配置
 *
 * @throws Exception
 */
@Test
public void test_quick_start_3_addGrayConfig() throws Exception {
    ClassPathResource classPathResource1 = new ClassPathResource("1.yml");
    MockMultipartFile multipartFile1 = new MockMultipartFile("file", "1.yml", "application/octet-stream", classPathResource1.getInputStream());

    ClassPathResource classPathResource2 = new ClassPathResource("2.yml");
    MockMultipartFile multipartFile2 = new MockMultipartFile("file", "2.yml", "application/octet-stream", classPathResource2.getInputStream());

    ClassPathResource classPathResource3 = new ClassPathResource("3.yml");
    MockMultipartFile multipartFile3 = new MockMultipartFile("file", "3.yml", "application/octet-stream", classPathResource3.getInputStream());
    String result = this.mockMvc.perform(MockMvcRequestBuilders.fileUpload("/api/v1/grayConfig/addGrayConfig")
            .file(multipartFile1)
            .file(multipartFile2)
            .file(multipartFile3)
            .param("project", "测试项目")
            .param("cluster", "测试集群")
            .param("service", "测试服务")
            .param("version", "测试版本")
            .param("versionId", "3776343244166660096")
            .param("desc", "测试sdk")
            .param("grayId", "3780699522712207360")
            .param("name", "test")
            .contentType(MediaType.MULTIPART_FORM_DATA)
            .session((MockHttpSession) Utils.getLoginSession(this.mockMvc))
            .accept(MediaType.APPLICATION_JSON)).andExpect(MockMvcResultMatchers.status().isOk())
            .andReturn().getResponse().getContentAsString();
    Utils.assertResult(result);
}
 
Example 2
Source Project: flow-platform-x   Source File: OpenRestServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void should_save_job_report() {
    // given:
    Flow flow = flowService.create("user-test");

    Job job = new Job();
    job.setId("12345");
    job.setFlowId(flow.getId());
    job.setBuildNumber(1L);
    Mockito.when(jobDao.findByKey(Mockito.any())).thenReturn(Optional.of(job));

    MockMultipartFile uploadFile = new MockMultipartFile("jacoco-report.zip", "content".getBytes());

    CreateJobReport report = new CreateJobReport()
            .setName("jacoco")
            .setZipped(true)
            .setType(ContentType.HTML);

    // when:
    openRestService.saveJobReport(flow.getName(), job.getBuildNumber(), report, uploadFile);

    // then:
    List<JobReport> reports = jobReportDao.findAllByJobId(job.getId());
    Assert.assertNotNull(reports);
    Assert.assertEquals(1, reports.size());
}
 
Example 3
Source Project: flow-platform-x   Source File: OpenRestServiceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void should_save_job_report() {
    // given:
    Flow flow = flowService.create("user-test");

    Job job = new Job();
    job.setId("12345");
    job.setFlowId(flow.getId());
    job.setBuildNumber(1L);
    Mockito.when(jobDao.findByKey(Mockito.any())).thenReturn(Optional.of(job));

    MockMultipartFile uploadFile = new MockMultipartFile("jacoco-report.zip", "content".getBytes());

    CreateJobReport report = new CreateJobReport()
            .setName("jacoco")
            .setZipped(true)
            .setType(ContentType.HTML);

    // when:
    openRestService.saveJobReport(flow.getName(), job.getBuildNumber(), report, uploadFile);

    // then:
    List<JobReport> reports = jobReportDao.findAllByJobId(job.getId());
    Assert.assertNotNull(reports);
    Assert.assertEquals(1, reports.size());
}
 
Example 4
Source Project: Mahuta   Source File: IndexControllerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void indexFile() throws Exception {
    
    BuilderAndResponse<IndexingRequest, IndexingResponse> builderAndResponse = indexingRequestUtils.generateRandomInputStreamIndexingRequest();
    InputStreamIndexingRequest request = (InputStreamIndexingRequest) builderAndResponse.getBuilder().getRequest();
    
    // Create Index 
    mockMvc.perform(post("/config/index/" + request.getIndexName()).contentType(MediaType.APPLICATION_JSON).content(BytesUtils.readFile("index_mapping.json")))
        .andExpect(status().isOk())
        .andDo(print());

    MockMultipartFile mockMultipartFile = new MockMultipartFile("file", "test_file", request.getContentType(), IOUtils.toByteArray(request.getContent()));
    request.setContent(null);
    MockMultipartFile mockMultipartRequest = new MockMultipartFile("request", "", "application/json", mapper.writeValueAsBytes((AbstractIndexingRequest)request));

    MvcResult response = mockMvc.perform(multipart("/index/file")
            .file(mockMultipartFile)
            .file(mockMultipartRequest))
            .andDo(print())
            .andExpect(status().isOk())
            .andReturn();
    
    // Validate
    IndexingResponse result = mapper.readValue(response.getResponse().getContentAsString(), IndexingResponse.class);
    MahutaTestAbstract.validateMetadata(builderAndResponse, result);
}
 
Example 5
Source Project: flow-platform-x   Source File: AgentControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void should_save_log_from_agent() throws Throwable {
    MockMultipartFile log = new MockMultipartFile("file", "filename.txt", "application/octet-stream",
            "some xml".getBytes());

    mockMvc.perform(MockMvcRequestBuilders.multipart("/agents/logs/upload")
            .file(log)
            .header("AGENT-TOKEN", "12345")).andExpect(status().is(200));
}
 
Example 6
Source Project: Insights   Source File: BulkUploadTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(priority = 8, expectedExceptions = InsightsCustomException.class)
public void testUploadDataWithWrongInsightTimeFormatInDatabase() throws InsightsCustomException, IOException {
	FileInputStream input = new FileInputStream(fileWithNullEpochTime);
	MultipartFile multipartFile = new MockMultipartFile("file", fileWithNullEpochTime.getName(), "text/plain",
			IOUtils.toByteArray(input));
	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.label, bulkUploadTestData.insightTimeField,
			bulkUploadTestData.wrongInsightTimeFormat);
	String expectedOutcome = "Illegal pattern character 'c'";
	Assert.assertEquals(response, expectedOutcome);
	Assert.assertFalse(multipartFile.isEmpty());
	Assert.assertTrue(multipartFile.getSize() < filesizeMaxValue);
}
 
Example 7
Source Project: Insights   Source File: BulkUploadTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(priority = 10, expectedExceptions = InsightsCustomException.class)
public void testUploadDataWithNullLabelInDatabase() throws InsightsCustomException, IOException {
	FileInputStream input = new FileInputStream(fileWithVariedEpochTimes);
	MultipartFile multipartFile = new MockMultipartFile("file", fileWithVariedEpochTimes.getName(), "text/plain",
			IOUtils.toByteArray(input));
	String expectedOutcome = "Label cannot be empty";
	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.nullLabel, bulkUploadTestData.insightTimeField,
			bulkUploadTestData.nullInsightTimeFormat);
	Assert.assertEquals(response, expectedOutcome);
	Assert.assertFalse(multipartFile.isEmpty());
	Assert.assertTrue(multipartFile.getSize() < filesizeMaxValue);
}
 
Example 8
Source Project: XS2A-Sandbox   Source File: ParseServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void convertFileToTargetObject_failure() throws IOException {
    // When
    MockMultipartFile mock = mock(MockMultipartFile.class);
    when(mock.getInputStream()).thenThrow(IOException.class);

    // Then
    assertThrows(TppException.class, () -> parseService.convertFileToTargetObject(mock, UserTransaction.class));
}
 
Example 9
Source Project: Insights   Source File: BulkUploadTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(priority = 10, expectedExceptions = InsightsCustomException.class)
public void testFileSizeException() throws InsightsCustomException, IOException {

	FileInputStream input = new FileInputStream(fileSize);
	MultipartFile multipartFile = new MockMultipartFile("file", fileSize.getName(), "text/plain",
			IOUtils.toByteArray(input));

	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.label, bulkUploadTestData.insightTimeField,
			bulkUploadTestData.nullInsightTimeFormat);

}
 
Example 10
Source Project: tensorboot   Source File: TensorBootRestControllerIT.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRecognizeFile() throws Exception {
    mockMvc.perform(MockMvcRequestBuilders.multipart("/TensorApi/recognizeFile")
            .file(new MockMultipartFile("file", "banana.jpg", "image/jpeg", testImage)))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
            .andExpect(jsonPath("$", notNullValue()))
            .andExpect(jsonPath("$", hasSize(1)))
            .andExpect(jsonPath("$[0].name", is("fruit, banana")))
            .andExpect(jsonPath("$[0].confidence", is(0.9789109)));
}
 
Example 11
Source Project: flow-platform-x   Source File: ArtifactServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void should_get_artifact_with_src_stream() throws IOException {
    Job job = new Job();
    job.setFlowId("1111");
    job.setBuildNumber(1L);

    ByteArrayInputStream content = new ByteArrayInputStream("content".getBytes());
    MockMultipartFile file = new MockMultipartFile("file", "test.jar", null, content);

    Mockito.when(fileManager.save(eq(file.getOriginalFilename()), any(), any()))
            .thenReturn("artifact/file/path");

    Mockito.when(fileManager.read(eq(file.getOriginalFilename()), any()))
            .thenReturn(content);

    // when: save artifact
    artifactService.save(job, "foo/boo", "md5..", file);

    // then: fetch
    List<JobArtifact> list = artifactService.list(job);
    Assert.assertEquals(1, list.size());

    JobArtifact fetched = artifactService.fetch(job, list.get(0).getId());
    Assert.assertNotNull(fetched);
    Assert.assertNotNull(fetched.getSrc());
    Assert.assertEquals("test.jar", fetched.getFileName());
    Assert.assertEquals("artifact/file/path", fetched.getPath());
}
 
Example 12
Source Project: spring-analysis-note   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithSingleFile() throws Exception {
	byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile filePart = new MockMultipartFile("file", "orig", null, fileContent);

	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/multipartfile").file(filePart).file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attribute("fileContent", fileContent))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 13
Source Project: spring-analysis-note   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithFileArray() throws Exception {
	byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
	MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);

	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/multipartfilearray").file(filePart1).file(filePart2).file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attribute("fileContent", fileContent))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 14
Source Project: spring-analysis-note   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithFileList() throws Exception {
	byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
	MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);

	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/multipartfilelist").file(filePart1).file(filePart2).file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attribute("fileContent", fileContent))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 15
Source Project: kafka-webview   Source File: MessageFormatControllerTest.java    License: MIT License 5 votes vote down vote up
/**
 * Test attempting to create a new message format, but fail to load the deserializer.
 */
@Test
@Transactional
public void testPostUpdate_createNewMessageFormatInvalidJar() throws Exception {
    final String expectedName = "MyMessageFormat" + System.currentTimeMillis();
    final String expectedClassPath = "examples.deserializer.ExampleDeserializer";

    // This isn't a real jar, will fail validation.
    final MockMultipartFile jarUpload = new MockMultipartFile("file", "contents".getBytes(Charsets.UTF_8));

    // Hit page.
    mockMvc
        .perform(multipart("/configuration/messageFormat/update")
            .file(jarUpload)
            .with(user(adminUserDetails))
            .with(csrf())
            .param("name", expectedName)
            .param("classpath", expectedClassPath))
        //.andDo(print())
        .andExpect(model().hasErrors())
        .andExpect(model().attributeHasFieldErrors("messageFormatForm", "file"))
        .andExpect(status().isOk());

    // Validate
    final MessageFormat messageFormat = messageFormatRepository.findByName(expectedName);
    assertNull("Should NOT have message format", messageFormat);
}
 
Example 16
Source Project: spring-analysis-note   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithOptionalFileNotPresent() throws Exception {
	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/optionalfile").file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attributeDoesNotExist("fileContent"))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 17
Source Project: Insights   Source File: BulkUploadTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test(priority = 2)
public void testUploadDataInDatabase() throws InsightsCustomException, IOException {
	FileInputStream input = new FileInputStream(file);
	MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain",
			IOUtils.toByteArray(input));
	boolean response = bulkUploadService.uploadDataInDatabase(multipartFile, bulkUploadTestData.toolName,
			bulkUploadTestData.label, bulkUploadTestData.insightTimeField,
			bulkUploadTestData.nullInsightTimeFormat);
	Assert.assertEquals(response, true);
	Assert.assertFalse(multipartFile.isEmpty());
	Assert.assertTrue(multipartFile.getSize() < filesizeMaxValue);
}
 
Example 18
Source Project: spring-analysis-note   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithOptionalFileArrayNotPresent() throws Exception {
	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/optionalfilearray").file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attributeDoesNotExist("fileContent"))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 19
Source Project: spring-analysis-note   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithOptionalFileList() throws Exception {
	byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
	MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);

	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/optionalfilelist").file(filePart1).file(filePart2).file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attribute("fileContent", fileContent))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 20
Source Project: spring-analysis-note   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithOptionalFileListNotPresent() throws Exception {
	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/optionalfilelist").file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attributeDoesNotExist("fileContent"))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 21
Source Project: spring-analysis-note   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test  // SPR-13317
public void multipartRequestWrapped() throws Exception {
	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	Filter filter = new RequestWrappingFilter();
	MockMvc mockMvc = standaloneSetup(new MultipartController()).addFilter(filter).build();

	Map<String, String> jsonMap = Collections.singletonMap("name", "yeeeah");
	mockMvc.perform(multipart("/json").file(jsonPart)).andExpect(model().attribute("json", jsonMap));
}
 
Example 22
Source Project: alcor   Source File: NodeControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test_getNodeInfoFromUpload() throws Exception {
    MockMultipartFile file = new MockMultipartFile("file", "./machine.json", "application/json", "{\"host_info\":{\"node_id\":\"ephost_0\",\"node_name\":\"ephost_0\",\"local_ip\":\"172.17.0.6\",\"mac_address\":\"02:42:ac:11:00:06\",\"veth\":\"\",\"server_port\":50001}}".getBytes());
    List<NodeInfo> nodeList = new ArrayList<NodeInfo>();
    doNothing().when(mockNodeRepository).addItemBulkTransaction(nodeList);
    this.mockMvc.perform(MockMvcRequestBuilders.multipart("/nodes/upload")
            .file(file))
            .andDo(print())
            .andExpect(status().isOk());
}
 
Example 23
Source Project: alcor   Source File: NodeControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test_getNodeInfoFromUpload_wrongFile() throws Exception {
    MockMultipartFile file = new MockMultipartFile("file2", "", "application/json", "".getBytes());
    MvcResult result = this.mockMvc.perform(MockMvcRequestBuilders.multipart("/nodes/upload")
            .file(file))
            .andDo(print())
            .andExpect(status().isBadRequest())
            .andExpect((rslt) -> assertTrue(rslt.getResolvedException().getClass() != null))
            .andReturn();
}
 
Example 24
Source Project: java-technology-stack   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithSingleFile() throws Exception {
	byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile filePart = new MockMultipartFile("file", "orig", null, fileContent);

	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/multipartfile").file(filePart).file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attribute("fileContent", fileContent))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 25
Source Project: java-technology-stack   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithFileArray() throws Exception {
	byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
	MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);

	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/multipartfilearray").file(filePart1).file(filePart2).file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attribute("fileContent", fileContent))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 26
Source Project: java-technology-stack   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithFileList() throws Exception {
	byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
	MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);

	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/multipartfilelist").file(filePart1).file(filePart2).file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attribute("fileContent", fileContent))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 27
Source Project: java-technology-stack   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithOptionalFile() throws Exception {
	byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile filePart = new MockMultipartFile("file", "orig", null, fileContent);

	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/optionalfile").file(filePart).file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attribute("fileContent", fileContent))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 28
Source Project: java-technology-stack   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithOptionalFileNotPresent() throws Exception {
	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/optionalfile").file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attributeDoesNotExist("fileContent"))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 29
Source Project: java-technology-stack   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithOptionalFileArray() throws Exception {
	byte[] fileContent = "bar".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile filePart1 = new MockMultipartFile("file", "orig", null, fileContent);
	MockMultipartFile filePart2 = new MockMultipartFile("file", "orig", null, fileContent);

	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/optionalfilearray").file(filePart1).file(filePart2).file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attribute("fileContent", fileContent))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}
 
Example 30
Source Project: java-technology-stack   Source File: MultipartControllerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void multipartRequestWithOptionalFileArrayNotPresent() throws Exception {
	byte[] json = "{\"name\":\"yeeeah\"}".getBytes(StandardCharsets.UTF_8);
	MockMultipartFile jsonPart = new MockMultipartFile("json", "json", "application/json", json);

	standaloneSetup(new MultipartController()).build()
			.perform(multipart("/optionalfilearray").file(jsonPart))
			.andExpect(status().isFound())
			.andExpect(model().attributeDoesNotExist("fileContent"))
			.andExpect(model().attribute("jsonContent", Collections.singletonMap("name", "yeeeah")));
}