BAEL-5715 Updated Code
This commit is contained in:
parent
97dbab9102
commit
083b1b42d7
|
@ -0,0 +1,15 @@
|
||||||
|
package com.baeldung.postman;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@EnableWebMvc
|
||||||
|
public class PostmanUploadDemo {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(PostmanUploadDemo.class, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package com.baeldung.postman.controller;
|
||||||
|
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import com.baeldung.postman.model.JsonRequest;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class PostmanUploadController {
|
||||||
|
|
||||||
|
@PostMapping("/uploadFile")
|
||||||
|
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file){
|
||||||
|
return ResponseEntity.ok().body("file received successfully");
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping("/uploadJson")
|
||||||
|
public ResponseEntity<String> handleJsonInput(@RequestBody JsonRequest json){
|
||||||
|
return ResponseEntity.ok().body(json.getId()+json.getName());
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,30 @@
|
||||||
|
package com.baeldung.postman.model;
|
||||||
|
|
||||||
|
public class JsonRequest {
|
||||||
|
int id;
|
||||||
|
String name;
|
||||||
|
|
||||||
|
public JsonRequest() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public JsonRequest(int id, String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(int id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,43 @@
|
||||||
|
package com.baeldung.postman.controller;
|
||||||
|
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||||
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.mock.web.MockMultipartFile;
|
||||||
|
import org.springframework.test.web.servlet.MockMvc;
|
||||||
|
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
|
||||||
|
|
||||||
|
import com.baeldung.postman.PostmanUploadDemo;
|
||||||
|
import com.baeldung.postman.model.JsonRequest;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
@SpringBootTest(classes = PostmanUploadDemo.class)
|
||||||
|
@AutoConfigureMockMvc
|
||||||
|
public class PostmanUploadControllerTest {
|
||||||
|
@Autowired
|
||||||
|
private MockMvc mockMvc;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenJson_whenUploaded_thenSuccessReturned() throws Exception {
|
||||||
|
JsonRequest request = new JsonRequest(1, "John Doe");
|
||||||
|
this.mockMvc.perform(MockMvcRequestBuilders.post("/uploadJson")
|
||||||
|
.contentType(MediaType.APPLICATION_JSON)
|
||||||
|
.content(new ObjectMapper().writeValueAsString(request)))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string("1John Doe"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenFile_whenUploaded_thenSuccessReturned() throws Exception {
|
||||||
|
MockMultipartFile request = new MockMultipartFile("dummy", "{\"key\": \"value\"}".getBytes());
|
||||||
|
this.mockMvc.perform(MockMvcRequestBuilders.multipart("/uploadFile")
|
||||||
|
.file("file", request.getBytes()))
|
||||||
|
.andExpect(status().isOk())
|
||||||
|
.andExpect(content().string("file received successfully"));
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue