-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb81dfd
commit 2c9fb05
Showing
5 changed files
with
170 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
Ecommerce/src/main/java/com/example/ecommerce/utils/CommerceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.example.ecommerce.utils; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
public class CommerceUtils { | ||
/** Attributes **/ | ||
private static ObjectMapper mapper = new ObjectMapper(); | ||
|
||
/** Methods **/ | ||
// Convert Object to JSON | ||
public static String asJsonString(final Object obj) { | ||
try { | ||
return mapper.writeValueAsString(obj); | ||
} catch (Exception e) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
|
||
// Convert JSON to Object | ||
public static <T> T objectFromString(Class<T> aClass, String value) { | ||
try { | ||
return mapper.readValue(value, aClass); | ||
} catch (Exception e) { | ||
throw new RuntimeException(); | ||
} | ||
} | ||
} |
98 changes: 98 additions & 0 deletions
98
.../java/com/example/ecommerce/controller/integration/CategoryControllerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.example.ecommerce.controller.integration; | ||
|
||
import com.example.ecommerce.entity.dto.CategoryDTO; | ||
import org.junit.jupiter.api.BeforeEach; | ||
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.security.test.context.support.WithMockUser; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.springframework.web.context.WebApplicationContext; | ||
|
||
import static com.example.ecommerce.utils.CommerceUtils.asJsonString; | ||
import static com.example.ecommerce.utils.CommerceUtils.objectFromString; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
class CategoryControllerIntegrationTest { | ||
/** Attributes **/ | ||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private WebApplicationContext context; | ||
|
||
/** Before Test **/ | ||
@BeforeEach | ||
void init() { | ||
mockMvc = MockMvcBuilders.webAppContextSetup(context) | ||
.apply(springSecurity()) | ||
.build(); | ||
} | ||
|
||
/** Tests **/ | ||
// Registering an user | ||
@Test | ||
@WithMockUser(username = "potato", password = "potato123", roles = "ADMIN") | ||
void saveCategory() throws Exception { | ||
CategoryDTO categoryDTO = new CategoryDTO(); | ||
categoryDTO.setName("Electronics"); | ||
|
||
// Returns a MockHttpServletResponse object | ||
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/category") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.content(asJsonString(categoryDTO))) | ||
.andDo(MockMvcResultHandlers.print()) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andReturn(); | ||
|
||
String responseBody = mvcResult.getResponse().getContentAsString(); | ||
// Convert String to Object | ||
categoryDTO = objectFromString(CategoryDTO.class, responseBody); | ||
assertNotNull(categoryDTO.getId()); | ||
} | ||
|
||
// Registering an user | ||
@Test | ||
@WithMockUser(username = "potato", password = "potato123", roles = "ADMIN") | ||
void getCategoryByIdTest() throws Exception { | ||
CategoryDTO categoryDTO = new CategoryDTO(); | ||
categoryDTO.setName("Perfums"); | ||
|
||
// POST | ||
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/category") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.content(asJsonString(categoryDTO))) | ||
.andDo(MockMvcResultHandlers.print()) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andReturn(); | ||
|
||
String responseBody = mvcResult.getResponse().getContentAsString(); | ||
// Convert String to Object | ||
categoryDTO = objectFromString(CategoryDTO.class, responseBody); | ||
|
||
// GET | ||
mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/category/{id}", categoryDTO.getId()) | ||
.accept(MediaType.APPLICATION_JSON)) | ||
.andDo(MockMvcResultHandlers.print()) | ||
.andExpect(MockMvcResultMatchers.status().isOk()) | ||
.andReturn(); | ||
|
||
responseBody = mvcResult.getResponse().getContentAsString(); | ||
CategoryDTO categoryDTO2 = objectFromString(CategoryDTO.class, responseBody); | ||
assertEquals(categoryDTO.getId(), categoryDTO2.getId()); | ||
assertEquals(categoryDTO.getName(), categoryDTO2.getName()); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...test/java/com/example/ecommerce/controller/integration/UserControllerIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.example.ecommerce.controller.integration; | ||
|
||
import com.example.ecommerce.entity.dto.UserDTO; | ||
import com.example.ecommerce.enums.UserRoles; | ||
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.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; | ||
import org.springframework.test.web.servlet.result.MockMvcResultHandlers; | ||
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; | ||
|
||
import static com.example.ecommerce.utils.CommerceUtils.asJsonString; | ||
|
||
@SpringBootTest | ||
@AutoConfigureMockMvc | ||
class UserControllerIntegrationTest { | ||
/** Attributes **/ | ||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
/** Tests **/ | ||
// Registering an user | ||
@Test | ||
void createTest() throws Exception { | ||
UserDTO userDTO = new UserDTO(); | ||
userDTO.setName("Thiago Bellini"); | ||
userDTO.setUsername("thiagobellini"); | ||
userDTO.setEmail("thiagobellini@email.com"); | ||
userDTO.setPassword("1234567890"); | ||
userDTO.setUserRoles(UserRoles.ROLE_ADMIN); | ||
|
||
mockMvc.perform(MockMvcRequestBuilders.post("/user") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.accept(MediaType.APPLICATION_JSON) | ||
.content(asJsonString(userDTO))) | ||
.andDo(MockMvcResultHandlers.print()) | ||
.andExpect(MockMvcResultMatchers.status().isCreated()); | ||
} | ||
} |