Merge branch 'master' into update-links
This commit is contained in:
@@ -1,38 +0,0 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.ResponseTransfer;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
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 com.baeldung.transfer.LoginForm;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@Controller
|
||||
@RequestMapping("/post")
|
||||
public class ExamplePostController {
|
||||
|
||||
private static Logger log = LoggerFactory.getLogger(ExamplePostController.class);
|
||||
|
||||
@Autowired ExampleService exampleService;
|
||||
|
||||
@PostMapping("/request")
|
||||
public ResponseEntity postController(@RequestBody LoginForm loginForm) {
|
||||
log.debug("POST received - serializing LoginForm: " + loginForm.getPassword() + " " + loginForm.getUsername());
|
||||
exampleService.fakeAuthenticate(loginForm);
|
||||
return ResponseEntity.ok(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@PostMapping("/response")
|
||||
@ResponseBody
|
||||
public ResponseTransfer postResponseController(@RequestBody LoginForm loginForm) {
|
||||
log.debug("POST received - serializing LoginForm: " + loginForm.getPassword() + " " + loginForm.getUsername());
|
||||
return new ResponseTransfer("Thanks For Posting!!!");
|
||||
}
|
||||
}
|
||||
@@ -1,12 +0,0 @@
|
||||
package com.baeldung.services;
|
||||
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class ExampleService {
|
||||
|
||||
public boolean fakeAuthenticate(LoginForm lf) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,18 +0,0 @@
|
||||
package com.baeldung.transfer;
|
||||
|
||||
public class ResponseTransfer {
|
||||
|
||||
private String text;
|
||||
|
||||
public ResponseTransfer(String text) {
|
||||
this.setText(text);
|
||||
}
|
||||
|
||||
public String getText() {
|
||||
return text;
|
||||
}
|
||||
|
||||
public void setText(String text) {
|
||||
this.text = text;
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.sampleapp.config.MainApplication;
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
public class ExamplePostControllerRequestIntegrationTest {
|
||||
|
||||
MockMvc mockMvc;
|
||||
@Mock private ExampleService exampleService;
|
||||
@InjectMocks private ExamplePostController exampleController;
|
||||
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
|
||||
private LoginForm lf = new LoginForm();
|
||||
|
||||
@Before
|
||||
public void preTest() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders
|
||||
.standaloneSetup(exampleController)
|
||||
.build();
|
||||
lf.setPassword("password");
|
||||
lf.setUsername("username");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestBodyTest() {
|
||||
try {
|
||||
when(exampleService.fakeAuthenticate(lf)).thenReturn(true);
|
||||
mockMvc
|
||||
.perform(post("/post/request")
|
||||
.content(jsonBody)
|
||||
.contentType("application/json"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk());
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,59 +0,0 @@
|
||||
package com.baeldung.controllers;
|
||||
|
||||
import com.baeldung.sampleapp.config.MainApplication;
|
||||
import com.baeldung.services.ExampleService;
|
||||
import com.baeldung.transfer.LoginForm;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = MainApplication.class)
|
||||
public class ExamplePostControllerResponseIntegrationTest {
|
||||
|
||||
MockMvc mockMvc;
|
||||
@Mock private ExampleService exampleService;
|
||||
@InjectMocks private ExamplePostController exampleController;
|
||||
private final String jsonBody = "{\"username\": \"username\", \"password\": \"password\"}";
|
||||
private LoginForm lf = new LoginForm();
|
||||
|
||||
@Before
|
||||
public void preTest() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders
|
||||
.standaloneSetup(exampleController)
|
||||
.build();
|
||||
lf.setPassword("password");
|
||||
lf.setUsername("username");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void requestBodyTest() {
|
||||
try {
|
||||
when(exampleService.fakeAuthenticate(lf)).thenReturn(true);
|
||||
mockMvc
|
||||
.perform(post("/post/response")
|
||||
.content(jsonBody)
|
||||
.contentType("application/json"))
|
||||
.andDo(print())
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().json("{\"text\":\"Thanks For Posting!!!\"}"));
|
||||
} catch (Exception e) {
|
||||
System.out.println("Exception: " + e);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user