Skip to content

Commit

Permalink
E-commerce
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrecpedro committed Sep 23, 2022
1 parent cb81dfd commit 2c9fb05
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 8 deletions.
6 changes: 0 additions & 6 deletions Ecommerce/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,6 @@
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ protected void configure(HttpSecurity http) throws Exception {
// Disable navigator login screen (only uses the Spring Boot's one)
http.csrf().disable()
.authorizeRequests()
.antMatchers("/user", "/user/authenticate").permitAll()
.antMatchers("/user", "/user/**").permitAll()
.antMatchers(HttpMethod.GET, "/product").hasAnyRole("USER", "ADMIN")
.antMatchers("/product", "/category").hasAnyRole("ADMIN")
.antMatchers("/category", "/category/**").hasAnyRole("ADMIN")
.antMatchers("/product").hasAnyRole("ADMIN")
.anyRequest()
.authenticated().and()
//.formLogin();
Expand Down
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();
}
}
}
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());
}
}
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());
}
}

0 comments on commit 2c9fb05

Please sign in to comment.