minor formatting cleanup
This commit is contained in:
parent
c565173601
commit
bf95d0aa9d
|
@ -8,7 +8,7 @@ import com.lmax.disruptor.EventHandler;
|
|||
public class MultiEventPrintConsumer implements EventConsumer {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public EventHandler<ValueEvent>[] getEventHandler() {
|
||||
|
|
|
@ -8,7 +8,7 @@ import com.lmax.disruptor.EventHandler;
|
|||
public class SingleEventPrintConsumer implements EventConsumer {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public EventHandler<ValueEvent>[] getEventHandler() {
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
package com.baeldung.sparkjava;
|
||||
|
||||
import static spark.Spark.*;
|
||||
|
||||
public class HelloWorldService {
|
||||
public static void main(String[] args) {
|
||||
get("/hello", (req,res)->"Hello, Baeldung");
|
||||
|
||||
get("/hello/:name", (req,res)->{
|
||||
return "Hello: "+ req.params(":name");
|
||||
get("/hello", (req, res) -> "Hello, Baeldung");
|
||||
|
||||
get("/hello/:name", (req, res) -> {
|
||||
return "Hello: " + req.params(":name");
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,10 +11,10 @@ import com.google.gson.Gson;
|
|||
public class SparkRestExample {
|
||||
public static void main(String[] args) {
|
||||
final UserService userService = new UserServiceMapImpl();
|
||||
|
||||
|
||||
post("/users", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
|
||||
User user = new Gson().fromJson(request.body(), User.class);
|
||||
userService.addUser(user);
|
||||
|
||||
|
@ -23,52 +23,40 @@ public class SparkRestExample {
|
|||
|
||||
get("/users", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.SUCCESS,new Gson()
|
||||
.toJsonTree(userService.getUsers())));
|
||||
|
||||
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, new Gson().toJsonTree(userService.getUsers())));
|
||||
});
|
||||
|
||||
get("/users/:id", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.SUCCESS,new Gson()
|
||||
.toJsonTree(userService.getUser(request.params(":id")))));
|
||||
|
||||
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, new Gson().toJsonTree(userService.getUser(request.params(":id")))));
|
||||
});
|
||||
|
||||
put("/users/:id", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
|
||||
User toEdit = new Gson().fromJson(request.body(), User.class);
|
||||
User editedUser = userService.editUser(toEdit);
|
||||
|
||||
|
||||
if (editedUser != null) {
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.SUCCESS,new Gson()
|
||||
.toJsonTree(editedUser)));
|
||||
}else {
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.ERROR,new Gson()
|
||||
.toJson("User not found or error in edit")));
|
||||
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, new Gson().toJsonTree(editedUser)));
|
||||
} else {
|
||||
return new Gson().toJson(new StandardResponse(StatusResponse.ERROR, new Gson().toJson("User not found or error in edit")));
|
||||
}
|
||||
});
|
||||
|
||||
delete("/users/:id", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
|
||||
userService.deleteUser(request.params(":id"));
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.SUCCESS, "user deleted"));
|
||||
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, "user deleted"));
|
||||
});
|
||||
|
||||
options("/users/:id", (request, response) -> {
|
||||
response.type("application/json");
|
||||
|
||||
return new Gson().toJson(
|
||||
new StandardResponse(StatusResponse.SUCCESS,
|
||||
(userService.userExist(
|
||||
request.params(":id"))) ? "User exists" : "User does not exists" ));
|
||||
|
||||
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, (userService.userExist(request.params(":id"))) ? "User exists" : "User does not exists"));
|
||||
});
|
||||
|
||||
}
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
package com.baeldung.sparkjava;
|
||||
|
||||
public enum StatusResponse {
|
||||
SUCCESS ("Success"),
|
||||
ERROR ("Error");
|
||||
|
||||
SUCCESS("Success"), ERROR("Error");
|
||||
|
||||
final private String status;
|
||||
|
||||
|
||||
StatusResponse(String status) {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
|
||||
public String getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
|
|
@ -51,7 +51,7 @@ class User {
|
|||
public void setEmail(String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringBuffer().append(getEmail()).toString();
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package com.baeldung.sparkjava;
|
||||
|
||||
public class UserException extends Exception{
|
||||
|
||||
public class UserException extends Exception {
|
||||
|
||||
public UserException() {
|
||||
super();
|
||||
}
|
||||
|
||||
|
||||
public UserException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
|
|
@ -3,10 +3,15 @@ package com.baeldung.sparkjava;
|
|||
import java.util.Collection;
|
||||
|
||||
public interface UserService {
|
||||
public void addUser (User user) ;
|
||||
public Collection<User> getUsers () ;
|
||||
public User getUser (String id) ;
|
||||
public User editUser (User user) throws UserException;
|
||||
public void deleteUser (String id) ;
|
||||
public boolean userExist (String id);
|
||||
public void addUser(User user);
|
||||
|
||||
public Collection<User> getUsers();
|
||||
|
||||
public User getUser(String id);
|
||||
|
||||
public User editUser(User user) throws UserException;
|
||||
|
||||
public void deleteUser(String id);
|
||||
|
||||
public boolean userExist(String id);
|
||||
}
|
||||
|
|
|
@ -3,65 +3,65 @@ package com.baeldung.sparkjava;
|
|||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class UserServiceMapImpl implements UserService{
|
||||
public class UserServiceMapImpl implements UserService {
|
||||
private HashMap<String, User> userMap;
|
||||
|
||||
|
||||
public UserServiceMapImpl() {
|
||||
userMap = new HashMap<>();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addUser (User user) {
|
||||
public void addUser(User user) {
|
||||
userMap.put(user.getId(), user);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Collection<User> getUsers () {
|
||||
return userMap.values();
|
||||
public Collection<User> getUsers() {
|
||||
return userMap.values();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public User getUser (String id) {
|
||||
public User getUser(String id) {
|
||||
return userMap.get(id);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public User editUser (User forEdit) throws UserException{
|
||||
try{
|
||||
if (forEdit.getId() == null)
|
||||
public User editUser(User forEdit) throws UserException {
|
||||
try {
|
||||
if (forEdit.getId() == null)
|
||||
throw new UserException("ID cannot be blank");
|
||||
|
||||
|
||||
User toEdit = userMap.get(forEdit.getId());
|
||||
|
||||
if (toEdit == null )
|
||||
|
||||
if (toEdit == null)
|
||||
throw new UserException("User not found");
|
||||
|
||||
if (forEdit.getEmail()!=null) {
|
||||
|
||||
if (forEdit.getEmail() != null) {
|
||||
toEdit.setEmail(forEdit.getEmail());
|
||||
}
|
||||
if (forEdit.getFirstName()!=null) {
|
||||
if (forEdit.getFirstName() != null) {
|
||||
toEdit.setFirstName(forEdit.getFirstName());
|
||||
}
|
||||
if (forEdit.getLastName()!=null) {
|
||||
if (forEdit.getLastName() != null) {
|
||||
toEdit.setLastName(forEdit.getLastName());
|
||||
}
|
||||
if (forEdit.getId()!=null) {
|
||||
if (forEdit.getId() != null) {
|
||||
toEdit.setId(forEdit.getId());
|
||||
}
|
||||
|
||||
return toEdit;
|
||||
}catch (Exception ex) {
|
||||
} catch (Exception ex) {
|
||||
throw new UserException(ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void deleteUser (String id) {
|
||||
public void deleteUser(String id) {
|
||||
userMap.remove(id);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean userExist (String id) {
|
||||
public boolean userExist(String id) {
|
||||
return userMap.containsKey(id);
|
||||
}
|
||||
|
||||
|
|
|
@ -15,25 +15,11 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http
|
||||
.authorizeRequests()
|
||||
.antMatchers("/css/**", "/js/**", "/loggedout").permitAll()
|
||||
.anyRequest().authenticated()
|
||||
.and()
|
||||
.httpBasic()
|
||||
.and()
|
||||
.logout().disable()
|
||||
.csrf().disable();
|
||||
http.authorizeRequests().antMatchers("/css/**", "/js/**", "/loggedout").permitAll().anyRequest().authenticated().and().httpBasic().and().logout().disable().csrf().disable();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
||||
auth
|
||||
.inMemoryAuthentication()
|
||||
.withUser("jim").password("jim").roles("USER")
|
||||
.and()
|
||||
.withUser("pam").password("pam").roles("USER")
|
||||
.and()
|
||||
.withUser("michael").password("michael").roles("MANAGER");
|
||||
auth.inMemoryAuthentication().withUser("jim").password("jim").roles("USER").and().withUser("pam").password("pam").roles("USER").and().withUser("michael").password("michael").roles("MANAGER");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -37,38 +37,34 @@ public class LiveTest {
|
|||
@Test
|
||||
@WithMockUser(roles = "MANAGER")
|
||||
public void givenUserIsManager_whenGetTasks_thenAllTasks() throws Exception {
|
||||
String allTasks = "[{'id':1,'description':'Send a fax','assignee':'pam'}," +
|
||||
"{'id':2,'description':'Print a document','assignee':'pam'}," +
|
||||
"{'id':3,'description':'Answer the phone','assignee':'pam'}," +
|
||||
"{'id':4,'description':'Call a client','assignee':'jim'}," +
|
||||
"{'id':5,'description':'Organize a meeting','assignee':'michael'}]";
|
||||
|
||||
String allTasks = "[{'id':1,'description':'Send a fax','assignee':'pam'}," + "{'id':2,'description':'Print a document','assignee':'pam'}," + "{'id':3,'description':'Answer the phone','assignee':'pam'},"
|
||||
+ "{'id':4,'description':'Call a client','assignee':'jim'}," + "{'id':5,'description':'Organize a meeting','assignee':'michael'}]";
|
||||
|
||||
mockMvc.perform(get("/api/tasks")).andExpect(status().isOk()).andExpect(content().json(allTasks));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@WithMockUser(username = "jim")
|
||||
public void givenUserNotManager_whenGetTasks_thenReturnAssignedToMe() throws Exception {
|
||||
String myTasks = "[{'id':4,'description':'Call a client','assignee':'jim'}]";
|
||||
|
||||
|
||||
mockMvc.perform(get("/api/tasks")).andExpect(status().isOk()).andExpect(content().json(myTasks));
|
||||
}
|
||||
|
||||
@Test
|
||||
@WithMockUser(roles = "MANAGER")
|
||||
public void givenUserIsManager_whenPostTasks_thenIncludeAllTasks() throws Exception {
|
||||
String newTasks = "[{\"description\":\"New to Michael\",\"assignee\":\"michael\"}," +
|
||||
"{\"description\":\"New to Pam\",\"assignee\":\"pam\"}]";
|
||||
|
||||
mockMvc.perform(post("/api/tasks").contentType(MediaType.APPLICATION_JSON).content(newTasks)).andExpect(status().isOk()).andExpect(content().json("[{'id': 6,'description':'New to Michael','assignee':'michael'}, {'id': 7,'description':'New to Pam','assignee':'pam'}]"));
|
||||
String newTasks = "[{\"description\":\"New to Michael\",\"assignee\":\"michael\"}," + "{\"description\":\"New to Pam\",\"assignee\":\"pam\"}]";
|
||||
|
||||
mockMvc.perform(post("/api/tasks").contentType(MediaType.APPLICATION_JSON).content(newTasks)).andExpect(status().isOk())
|
||||
.andExpect(content().json("[{'id': 6,'description':'New to Michael','assignee':'michael'}, {'id': 7,'description':'New to Pam','assignee':'pam'}]"));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@WithMockUser(username = "jim")
|
||||
public void givenUserNotManager_whenPostTasks_thenIncludeOnlyAssignedToMe() throws Exception {
|
||||
String newTasks = "[{\"description\":\"New to Jim\",\"assignee\":\"jim\"}," +
|
||||
"{\"description\":\"New to Pam\",\"assignee\":\"pam\"}]";
|
||||
|
||||
String newTasks = "[{\"description\":\"New to Jim\",\"assignee\":\"jim\"}," + "{\"description\":\"New to Pam\",\"assignee\":\"pam\"}]";
|
||||
|
||||
mockMvc.perform(post("/api/tasks").contentType(MediaType.APPLICATION_JSON).content(newTasks)).andExpect(status().isOk()).andExpect(content().json("[{'id': 8,'description':'New to Jim','assignee':'jim'}]"));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue