Search in sources :

Example 1 with ProductDTO

use of com.rafaelvieira.letmebuy.dto.ProductDTO in project letmebuy by rafaelrok.

the class ProductControllerIT method updateShouldReturnProductDTOWhenIdExists.

@Test
public void updateShouldReturnProductDTOWhenIdExists() throws Exception {
    String accessToken = tokenUtil.obtainAccessToken(mockMvc, username, password);
    ProductDTO productDTO = Factory.createProductDTO();
    String jsonBody = objectMapper.writeValueAsString(productDTO);
    String expectedName = productDTO.getName();
    String expectedDescription = productDTO.getDescription();
    ResultActions result = mockMvc.perform(put("/products/{id}", existingId).header("Authorization", "Bearer " + accessToken).content(jsonBody).contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON));
    result.andExpect(status().isOk());
    result.andExpect(jsonPath("$.id").value(existingId));
    result.andExpect(jsonPath("$.name").value(expectedName));
    result.andExpect(jsonPath("$.description").value(expectedDescription));
}
Also used : ResultActions(org.springframework.test.web.servlet.ResultActions) ProductDTO(com.rafaelvieira.letmebuy.dto.ProductDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 2 with ProductDTO

use of com.rafaelvieira.letmebuy.dto.ProductDTO in project letmebuy by rafaelrok.

the class ProductServiceIT method findAllPagedShouldReturnEmptyPageWhenPageDoesNotExist.

@Test
public void findAllPagedShouldReturnEmptyPageWhenPageDoesNotExist() {
    PageRequest pageRequest = PageRequest.of(50, 10);
    Page<ProductDTO> result = service.findAllPaged(0L, "", pageRequest);
    Assertions.assertTrue(result.isEmpty());
}
Also used : PageRequest(org.springframework.data.domain.PageRequest) ProductDTO(com.rafaelvieira.letmebuy.dto.ProductDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with ProductDTO

use of com.rafaelvieira.letmebuy.dto.ProductDTO in project letmebuy by rafaelrok.

the class ProductService method findById.

@Transactional(readOnly = true)
public ProductDTO findById(Long id) {
    Optional<Product> obj = productRepo.findById(id);
    Product entity = obj.orElseThrow(() -> new ResourceNotFoundException("Produto não encontrada"));
    return new ProductDTO(entity, entity.getCategories());
}
Also used : Product(com.rafaelvieira.letmebuy.entities.Product) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) ProductDTO(com.rafaelvieira.letmebuy.dto.ProductDTO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with ProductDTO

use of com.rafaelvieira.letmebuy.dto.ProductDTO in project letmebuy by rafaelrok.

the class ProductService method findAllPaged.

/*
    @Transactional(readOnly = true)
    public Page<ProductDTO> findAllPaged(Pageable pageable) {
        Page<Product> list = productRepo.findAll(pageable);
        return list.map(x -> new ProductDTO(x));
    }
     */
@Transactional(readOnly = true)
public Page<ProductDTO> findAllPaged(Long categoryId, String name, Pageable pageable) {
    List<Category> categories = (categoryId == 0) ? null : Arrays.asList(categoryRepo.getOne(categoryId));
    Page<Product> page = productRepo.find(categories, name, pageable);
    productRepo.findProductsWithCategories(page.getContent());
    return page.map(x -> new ProductDTO(x, x.getCategories()));
}
Also used : Category(com.rafaelvieira.letmebuy.entities.Category) Product(com.rafaelvieira.letmebuy.entities.Product) ProductDTO(com.rafaelvieira.letmebuy.dto.ProductDTO) Transactional(org.springframework.transaction.annotation.Transactional)

Example 5 with ProductDTO

use of com.rafaelvieira.letmebuy.dto.ProductDTO in project letmebuy by rafaelrok.

the class ProductService method update.

@Transactional
public ProductDTO update(Long id, ProductDTO dto) {
    try {
        // getOne salva em memoria o objeto para não acessr 2 vezes a base
        Product entity = productRepo.getOne(id);
        copyDtoToEntity(dto, entity);
        entity = productRepo.save(entity);
        return new ProductDTO(entity);
    } catch (EntityNotFoundException e) {
        throw new ResourceNotFoundException("Produto não encontrada " + id);
    }
}
Also used : Product(com.rafaelvieira.letmebuy.entities.Product) EntityNotFoundException(javax.persistence.EntityNotFoundException) ResourceNotFoundException(com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException) ProductDTO(com.rafaelvieira.letmebuy.dto.ProductDTO) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ProductDTO (com.rafaelvieira.letmebuy.dto.ProductDTO)11 Test (org.junit.jupiter.api.Test)7 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 Product (com.rafaelvieira.letmebuy.entities.Product)4 Transactional (org.springframework.transaction.annotation.Transactional)4 PageRequest (org.springframework.data.domain.PageRequest)3 ResourceNotFoundException (com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException)2 ResultActions (org.springframework.test.web.servlet.ResultActions)2 Category (com.rafaelvieira.letmebuy.entities.Category)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 Pageable (org.springframework.data.domain.Pageable)1