minor formatting cleanup
This commit is contained in:
parent
c565173601
commit
bf95d0aa9d
@ -1,4 +1,5 @@
|
|||||||
package com.baeldung.sparkjava;
|
package com.baeldung.sparkjava;
|
||||||
|
|
||||||
import static spark.Spark.*;
|
import static spark.Spark.*;
|
||||||
|
|
||||||
public class HelloWorldService {
|
public class HelloWorldService {
|
||||||
|
@ -24,17 +24,13 @@ public class SparkRestExample {
|
|||||||
get("/users", (request, response) -> {
|
get("/users", (request, response) -> {
|
||||||
response.type("application/json");
|
response.type("application/json");
|
||||||
|
|
||||||
return new Gson().toJson(
|
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, new Gson().toJsonTree(userService.getUsers())));
|
||||||
new StandardResponse(StatusResponse.SUCCESS,new Gson()
|
|
||||||
.toJsonTree(userService.getUsers())));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
get("/users/:id", (request, response) -> {
|
get("/users/:id", (request, response) -> {
|
||||||
response.type("application/json");
|
response.type("application/json");
|
||||||
|
|
||||||
return new Gson().toJson(
|
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, new Gson().toJsonTree(userService.getUser(request.params(":id")))));
|
||||||
new StandardResponse(StatusResponse.SUCCESS,new Gson()
|
|
||||||
.toJsonTree(userService.getUser(request.params(":id")))));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
put("/users/:id", (request, response) -> {
|
put("/users/:id", (request, response) -> {
|
||||||
@ -44,13 +40,9 @@ public class SparkRestExample {
|
|||||||
User editedUser = userService.editUser(toEdit);
|
User editedUser = userService.editUser(toEdit);
|
||||||
|
|
||||||
if (editedUser != null) {
|
if (editedUser != null) {
|
||||||
return new Gson().toJson(
|
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, new Gson().toJsonTree(editedUser)));
|
||||||
new StandardResponse(StatusResponse.SUCCESS,new Gson()
|
|
||||||
.toJsonTree(editedUser)));
|
|
||||||
} else {
|
} else {
|
||||||
return new Gson().toJson(
|
return new Gson().toJson(new StandardResponse(StatusResponse.ERROR, new Gson().toJson("User not found or error in edit")));
|
||||||
new StandardResponse(StatusResponse.ERROR,new Gson()
|
|
||||||
.toJson("User not found or error in edit")));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -58,17 +50,13 @@ public class SparkRestExample {
|
|||||||
response.type("application/json");
|
response.type("application/json");
|
||||||
|
|
||||||
userService.deleteUser(request.params(":id"));
|
userService.deleteUser(request.params(":id"));
|
||||||
return new Gson().toJson(
|
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, "user deleted"));
|
||||||
new StandardResponse(StatusResponse.SUCCESS, "user deleted"));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
options("/users/:id", (request, response) -> {
|
options("/users/:id", (request, response) -> {
|
||||||
response.type("application/json");
|
response.type("application/json");
|
||||||
|
|
||||||
return new Gson().toJson(
|
return new Gson().toJson(new StandardResponse(StatusResponse.SUCCESS, (userService.userExist(request.params(":id"))) ? "User exists" : "User does not exists"));
|
||||||
new StandardResponse(StatusResponse.SUCCESS,
|
|
||||||
(userService.userExist(
|
|
||||||
request.params(":id"))) ? "User exists" : "User does not exists" ));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package com.baeldung.sparkjava;
|
package com.baeldung.sparkjava;
|
||||||
|
|
||||||
public enum StatusResponse {
|
public enum StatusResponse {
|
||||||
SUCCESS ("Success"),
|
SUCCESS("Success"), ERROR("Error");
|
||||||
ERROR ("Error");
|
|
||||||
|
|
||||||
final private String status;
|
final private String status;
|
||||||
|
|
||||||
|
@ -4,9 +4,14 @@ import java.util.Collection;
|
|||||||
|
|
||||||
public interface UserService {
|
public interface UserService {
|
||||||
public void addUser(User user);
|
public void addUser(User user);
|
||||||
|
|
||||||
public Collection<User> getUsers();
|
public Collection<User> getUsers();
|
||||||
|
|
||||||
public User getUser(String id);
|
public User getUser(String id);
|
||||||
|
|
||||||
public User editUser(User user) throws UserException;
|
public User editUser(User user) throws UserException;
|
||||||
|
|
||||||
public void deleteUser(String id);
|
public void deleteUser(String id);
|
||||||
|
|
||||||
public boolean userExist(String id);
|
public boolean userExist(String id);
|
||||||
}
|
}
|
||||||
|
@ -15,25 +15,11 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void configure(HttpSecurity http) throws Exception {
|
protected void configure(HttpSecurity http) throws Exception {
|
||||||
http
|
http.authorizeRequests().antMatchers("/css/**", "/js/**", "/loggedout").permitAll().anyRequest().authenticated().and().httpBasic().and().logout().disable().csrf().disable();
|
||||||
.authorizeRequests()
|
|
||||||
.antMatchers("/css/**", "/js/**", "/loggedout").permitAll()
|
|
||||||
.anyRequest().authenticated()
|
|
||||||
.and()
|
|
||||||
.httpBasic()
|
|
||||||
.and()
|
|
||||||
.logout().disable()
|
|
||||||
.csrf().disable();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
|
||||||
auth
|
auth.inMemoryAuthentication().withUser("jim").password("jim").roles("USER").and().withUser("pam").password("pam").roles("USER").and().withUser("michael").password("michael").roles("MANAGER");
|
||||||
.inMemoryAuthentication()
|
|
||||||
.withUser("jim").password("jim").roles("USER")
|
|
||||||
.and()
|
|
||||||
.withUser("pam").password("pam").roles("USER")
|
|
||||||
.and()
|
|
||||||
.withUser("michael").password("michael").roles("MANAGER");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,11 +37,8 @@ public class LiveTest {
|
|||||||
@Test
|
@Test
|
||||||
@WithMockUser(roles = "MANAGER")
|
@WithMockUser(roles = "MANAGER")
|
||||||
public void givenUserIsManager_whenGetTasks_thenAllTasks() throws Exception {
|
public void givenUserIsManager_whenGetTasks_thenAllTasks() throws Exception {
|
||||||
String allTasks = "[{'id':1,'description':'Send a fax','assignee':'pam'}," +
|
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':2,'description':'Print a document','assignee':'pam'}," +
|
+ "{'id':4,'description':'Call a client','assignee':'jim'}," + "{'id':5,'description':'Organize a meeting','assignee':'michael'}]";
|
||||||
"{'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));
|
mockMvc.perform(get("/api/tasks")).andExpect(status().isOk()).andExpect(content().json(allTasks));
|
||||||
}
|
}
|
||||||
@ -57,17 +54,16 @@ public class LiveTest {
|
|||||||
@Test
|
@Test
|
||||||
@WithMockUser(roles = "MANAGER")
|
@WithMockUser(roles = "MANAGER")
|
||||||
public void givenUserIsManager_whenPostTasks_thenIncludeAllTasks() throws Exception {
|
public void givenUserIsManager_whenPostTasks_thenIncludeAllTasks() throws Exception {
|
||||||
String newTasks = "[{\"description\":\"New to Michael\",\"assignee\":\"michael\"}," +
|
String newTasks = "[{\"description\":\"New to Michael\",\"assignee\":\"michael\"}," + "{\"description\":\"New to Pam\",\"assignee\":\"pam\"}]";
|
||||||
"{\"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'}]"));
|
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
|
@Test
|
||||||
@WithMockUser(username = "jim")
|
@WithMockUser(username = "jim")
|
||||||
public void givenUserNotManager_whenPostTasks_thenIncludeOnlyAssignedToMe() throws Exception {
|
public void givenUserNotManager_whenPostTasks_thenIncludeOnlyAssignedToMe() throws Exception {
|
||||||
String newTasks = "[{\"description\":\"New to Jim\",\"assignee\":\"jim\"}," +
|
String newTasks = "[{\"description\":\"New to Jim\",\"assignee\":\"jim\"}," + "{\"description\":\"New to Pam\",\"assignee\":\"pam\"}]";
|
||||||
"{\"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'}]"));
|
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…
x
Reference in New Issue
Block a user