Skip to content

Commit

Permalink
Move default params function to static
Browse files Browse the repository at this point in the history
  • Loading branch information
leung018 committed Nov 19, 2024
1 parent ca8de69 commit 1b38f7c
Showing 1 changed file with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void clearAccessToken() {
}

private String useNewUserAccessToken() throws Exception {
UserCredentials userCredentials = sampleUserCredentials();
UserCredentials userCredentials = UserCredentials.sample();

signup(userCredentials).andExpect(status().isCreated());
User user = userRepository.findByUsername(userCredentials.username).orElseThrow();
Expand All @@ -71,7 +71,7 @@ private String useNewUserAccessToken() throws Exception {
public void shouldCreateAndGetProduct() throws Exception {
useNewUserAccessToken();

CreateProductParams params = validParams();
CreateProductParams params = CreateProductParams.sample();
MvcResult mvcResult =
createProduct(params)
.andExpect(status().isCreated())
Expand Down Expand Up @@ -107,11 +107,11 @@ public void shouldIgnoreIdWhenCreateProduct() throws Exception {
public void shouldRejectCreateProductWithInvalidData() throws Exception {
useNewUserAccessToken();

CreateProductParams params = validParams();
CreateProductParams params = CreateProductParams.sample();
params.name = "";
createProduct(params).andExpect(status().isBadRequest());

params = validParams();
params = CreateProductParams.sample();
params.price = -1;
createProduct(params).andExpect(status().isBadRequest());
}
Expand Down Expand Up @@ -150,7 +150,7 @@ public void shouldRejectLoginWithNonexistentUsername() throws Exception {
@Test
public void shouldRejectNonAuthApiCallWithoutToken() throws Exception {
clearAccessToken();
createProduct(validParams()).andExpect(status().isForbidden());
createProduct(CreateProductParams.sample()).andExpect(status().isForbidden());
}

@Test
Expand All @@ -162,23 +162,23 @@ public void shouldRejectIfAuthHeaderIsNotSetCorrectly() throws Exception {
post("/products")
.contentType("application/json")
.header("Authorization", "NotBearer " + accessToken.orElseThrow())
.content(validParams().toContent()))
.content(CreateProductParams.sample().toContent()))
.andExpect(status().isForbidden());

mockMvc
.perform(
post("/products")
.contentType("application/json")
.header("Authorization", "Bearer ")
.content(validParams().toContent()))
.content(CreateProductParams.sample().toContent()))
.andExpect(status().isForbidden());
}

@Test
public void shouldCreateProductWithUserIdSameAsCreator() throws Exception {
String userId = useNewUserAccessToken();

CreateProductParams params = validParams();
CreateProductParams params = CreateProductParams.sample();
createProduct(params)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.userId").value(userId));
Expand All @@ -189,6 +189,10 @@ private static class CreateProductParams {
double price;
int quantity;

private static CreateProductParams sample() {
return new CreateProductParams("Product 1", 1.0, 50);
}

private CreateProductParams(String name, double price, int quantity) {
this.name = name;
this.price = price;
Expand All @@ -206,10 +210,6 @@ public String toContent() {
}
}

private CreateProductParams validParams() {
return new CreateProductParams("Product 1", 1.0, 50);
}

private ResultActions createProduct(CreateProductParams params) throws Exception {
MockHttpServletRequestBuilder builder =
post("/products").contentType("application/json").content(params.toContent());
Expand All @@ -227,6 +227,10 @@ private static class UserCredentials {
String username;
String password;

private static UserCredentials sample() {
return new UserCredentials("sample-user", "sample-password");
}

private UserCredentials(String username, String password) {
this.username = username;
this.password = password;
Expand All @@ -237,10 +241,6 @@ public String toContent() {
}
}

private UserCredentials sampleUserCredentials() {
return new UserCredentials("sample-user", "sample-password");
}

private ResultActions signup(UserCredentials userCredentials) throws Exception {
return mockMvc.perform(
post("/signup").contentType("application/json").content(userCredentials.toContent()));
Expand Down

0 comments on commit 1b38f7c

Please sign in to comment.