diff --git a/pom.xml b/pom.xml index 873f8e7..d283f89 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,11 @@ spring-boot-starter-test test + + org.json + json + 20240303 + org.jacoco jacoco-maven-plugin diff --git a/src/main/java/com/consola/lis/configuration/OpenAPIConfig.java b/src/main/java/com/consola/lis/configuration/OpenAPIConfig.java index b791d55..4630b0d 100644 --- a/src/main/java/com/consola/lis/configuration/OpenAPIConfig.java +++ b/src/main/java/com/consola/lis/configuration/OpenAPIConfig.java @@ -1,7 +1,6 @@ package com.consola.lis.configuration; -import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Info; @@ -26,14 +25,14 @@ public OpenAPI swagger() { .bearerFormat("JWT"); // Define security requirement for endpoints that require Bearer token - SecurityRequirement securityRequirement = new SecurityRequirement() - .addList("bearerAuth"); + // SecurityRequirement securityRequirement = new SecurityRequirement() + // .addList("bearerAuth"); // Create OpenAPI object and configure servers, info, security schemes, and security requirements return new OpenAPI() .info(info) - .components(new io.swagger.v3.oas.models.Components().addSecuritySchemes("bearerAuth", securityScheme)) - .addSecurityItem(securityRequirement); + .components(new io.swagger.v3.oas.models.Components().addSecuritySchemes("bearerAuth", securityScheme)); + //.addSecurityItem(securityRequirement); } diff --git a/src/main/java/com/consola/lis/controller/CategoryController.java b/src/main/java/com/consola/lis/controller/CategoryController.java index 5b6a52c..f29d09d 100644 --- a/src/main/java/com/consola/lis/controller/CategoryController.java +++ b/src/main/java/com/consola/lis/controller/CategoryController.java @@ -1,7 +1,7 @@ package com.consola.lis.controller; -import com.consola.lis.constans.EndpointConstant; +import com.consola.lis.util.constans.EndpointConstant; import com.consola.lis.dto.CategoryDTO; import com.consola.lis.model.entity.Category; import com.consola.lis.service.CategoryService; diff --git a/src/main/java/com/consola/lis/controller/ControllerAdvice.java b/src/main/java/com/consola/lis/controller/ControllerAdvice.java index 187a8db..d7f7f89 100644 --- a/src/main/java/com/consola/lis/controller/ControllerAdvice.java +++ b/src/main/java/com/consola/lis/controller/ControllerAdvice.java @@ -1,7 +1,7 @@ package com.consola.lis.controller; import com.consola.lis.dto.ErrorDTO; -import com.consola.lis.exception.*; +import com.consola.lis.util.exception.*; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; diff --git a/src/main/java/com/consola/lis/controller/Holi.java b/src/main/java/com/consola/lis/controller/Holi.java deleted file mode 100644 index 0d89197..0000000 --- a/src/main/java/com/consola/lis/controller/Holi.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.consola.lis.controller; - -import io.swagger.v3.oas.annotations.security.SecurityRequirement; -import lombok.RequiredArgsConstructor; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -@RestController -@RequestMapping("/api/admi/holi") -@RequiredArgsConstructor -public class Holi { - - - @SecurityRequirement(name = "bearerAuth") - @GetMapping - public String hole() { - return "holi"; - } -} diff --git a/src/main/java/com/consola/lis/controller/InventoryController.java b/src/main/java/com/consola/lis/controller/InventoryController.java index 6c8173d..aca392a 100644 --- a/src/main/java/com/consola/lis/controller/InventoryController.java +++ b/src/main/java/com/consola/lis/controller/InventoryController.java @@ -1,6 +1,6 @@ package com.consola.lis.controller; -import com.consola.lis.constans.EndpointConstant; +import com.consola.lis.util.constans.EndpointConstant; import com.consola.lis.dto.GeneralItemDTO; import com.consola.lis.dto.QuantizableItemDTO; import com.consola.lis.model.entity.GeneralItem; @@ -12,6 +12,7 @@ import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.Map; @Tag(name = "Manage for Inventory", description = "something") @RestController @@ -50,8 +51,9 @@ public void deleteCategory(@PathVariable("itemId") String itemId){ } @GetMapping - public List inventoryItems() { - return inventoryItemService.getAllItems(); + public Map inventoryItems() { + System.out.println("al menos me dan click"); + return inventoryItemService.getAllItemsMapped(); } @GetMapping(EndpointConstant.ENDPOINT_ONE_GENERAL_ITEM) diff --git a/src/main/java/com/consola/lis/controller/LoginRequestController.java b/src/main/java/com/consola/lis/controller/LoginRequestController.java index dc3e569..58a8668 100644 --- a/src/main/java/com/consola/lis/controller/LoginRequestController.java +++ b/src/main/java/com/consola/lis/controller/LoginRequestController.java @@ -1,6 +1,6 @@ package com.consola.lis.controller; -import com.consola.lis.constans.EndpointConstant; +import com.consola.lis.util.constans.EndpointConstant; import com.consola.lis.dto.AuthResponseDTO; import com.consola.lis.dto.LoginRequestDTO; import com.consola.lis.service.LoginService; diff --git a/src/main/java/com/consola/lis/controller/RegisterRequestController.java b/src/main/java/com/consola/lis/controller/RegisterRequestController.java index c2c5036..e464e42 100644 --- a/src/main/java/com/consola/lis/controller/RegisterRequestController.java +++ b/src/main/java/com/consola/lis/controller/RegisterRequestController.java @@ -1,7 +1,7 @@ package com.consola.lis.controller; -import com.consola.lis.constans.EndpointConstant; +import com.consola.lis.util.constans.EndpointConstant; import com.consola.lis.dto.AuthResponseDTO; import com.consola.lis.dto.RegisterRequestDTO; import com.consola.lis.service.RegisterService; diff --git a/src/main/java/com/consola/lis/controller/UserController.java b/src/main/java/com/consola/lis/controller/UserController.java index 58f2149..8f4bb39 100644 --- a/src/main/java/com/consola/lis/controller/UserController.java +++ b/src/main/java/com/consola/lis/controller/UserController.java @@ -1,7 +1,7 @@ package com.consola.lis.controller; -import com.consola.lis.constans.EndpointConstant; +import com.consola.lis.util.constans.EndpointConstant; import com.consola.lis.dto.AuthResponseDTO; import com.consola.lis.dto.UserDTO; import com.consola.lis.model.entity.User; @@ -21,24 +21,24 @@ public class UserController { private final UserService userService; - @GetMapping("/{userName}") - public User oneUser(@PathVariable("userName") String userId){ - return userService.getUser(userId); + @GetMapping(EndpointConstant.ENDPOINT_ONE_USER) + public ResponseEntity oneUser(@PathVariable("username") String userId){ + return ResponseEntity.ok(userService.getUser(userId)); } @Operation(summary = "This method is used to login the options valid are ADMIN, AUXPROG, AUXADMI") - @PatchMapping("/role/{username}") + @PatchMapping(EndpointConstant.ENDPOINT_CHANGE_ROLE_USER) public ResponseEntity changeRole(@PathVariable("username") String username, @RequestBody UserRole userRole){ return ResponseEntity.ok(userService.changeUserRole(username, userRole)); } - @GetMapping("/ldapUser/{username}") + @GetMapping(EndpointConstant.ENDPOINT_USER_LDAP) public ResponseEntity userLDAP(@PathVariable("username") String username){ return ResponseEntity.ok(userService.getUserLDAP(username)); } - @DeleteMapping("/{username}") + @DeleteMapping(EndpointConstant.ENDPOINT_DELETE_USER) public void deleteUser(@PathVariable("username") String username){ userService.deleteUser(username); } diff --git a/src/main/java/com/consola/lis/dto/ItemInfoDTO.java b/src/main/java/com/consola/lis/dto/ItemInfoDTO.java new file mode 100644 index 0000000..20865da --- /dev/null +++ b/src/main/java/com/consola/lis/dto/ItemInfoDTO.java @@ -0,0 +1,19 @@ +package com.consola.lis.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@Builder +@AllArgsConstructor +@NoArgsConstructor +public class ItemInfoDTO { + private String id; + private String state; + private String category; + private String wallet; + private String[] attributes; + private boolean quantizable; +} diff --git a/src/main/java/com/consola/lis/jwt/JwtAuthenticationFilter.java b/src/main/java/com/consola/lis/jwt/JwtAuthenticationFilter.java index 9ced74b..18351ef 100644 --- a/src/main/java/com/consola/lis/jwt/JwtAuthenticationFilter.java +++ b/src/main/java/com/consola/lis/jwt/JwtAuthenticationFilter.java @@ -1,6 +1,6 @@ package com.consola.lis.jwt; -import com.consola.lis.exception.JwtExpiredException; +import com.consola.lis.util.exception.JwtExpiredException; import io.jsonwebtoken.ExpiredJwtException; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletException; diff --git a/src/main/java/com/consola/lis/service/CategoryService.java b/src/main/java/com/consola/lis/service/CategoryService.java index 5c68224..21bd0a4 100644 --- a/src/main/java/com/consola/lis/service/CategoryService.java +++ b/src/main/java/com/consola/lis/service/CategoryService.java @@ -2,8 +2,8 @@ import com.consola.lis.dto.CategoryDTO; -import com.consola.lis.exception.AlreadyExistsException; -import com.consola.lis.exception.NotExistingException; +import com.consola.lis.util.exception.AlreadyExistsException; +import com.consola.lis.util.exception.NotExistingException; import com.consola.lis.model.entity.Category; import com.consola.lis.model.repository.CategoryRepository; import com.fasterxml.jackson.core.JsonProcessingException; diff --git a/src/main/java/com/consola/lis/service/InventoryItemService.java b/src/main/java/com/consola/lis/service/InventoryItemService.java index c4ecbe0..ac1cd69 100644 --- a/src/main/java/com/consola/lis/service/InventoryItemService.java +++ b/src/main/java/com/consola/lis/service/InventoryItemService.java @@ -1,12 +1,13 @@ package com.consola.lis.service; -import com.consola.lis.constans.Util; +import com.consola.lis.dto.ItemInfoDTO; +import com.consola.lis.util.constans.Util; import com.consola.lis.dto.GeneralItemDTO; import com.consola.lis.dto.QuantizableItemDTO; -import com.consola.lis.exception.AlreadyExistsException; -import com.consola.lis.exception.IllegalParameterInRequest; -import com.consola.lis.exception.NotExistingException; +import com.consola.lis.util.exception.AlreadyExistsException; +import com.consola.lis.util.exception.IllegalParameterInRequest; +import com.consola.lis.util.exception.NotExistingException; import com.consola.lis.model.entity.Category; import com.consola.lis.model.entity.GeneralItem; import com.consola.lis.model.entity.QuantizableItem; @@ -15,6 +16,7 @@ import com.consola.lis.model.repository.CategoryRepository; import com.consola.lis.model.repository.GeneralItemRepository; import com.consola.lis.model.repository.QuantizableItemRepository; +import com.consola.lis.util.mapper.InventoryItemMapper; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.transaction.Transactional; @@ -23,6 +25,7 @@ import java.util.*; + @Service public class InventoryItemService { @@ -32,6 +35,7 @@ public class InventoryItemService { private final QuantizableItemRepository quantizableItemRepository; private final CategoryRepository categoryRepository; + private final Map>> validStatesMap = new HashMap<>(); public InventoryItemService(ObjectMapper objectMapper, @@ -183,17 +187,44 @@ public boolean existItem(String itemId) { return generalItemRepository.existsById(itemId); } - public List getAllItems() { + public Map getAllItemsMapped() { + List inventoryItems = getAllItems(); + Map result = new HashMap<>(); + result.put("header", createHeader()); + result.put("allRegisters", inventoryItems); + return result; + } + + private List createHeader() { + List header = new ArrayList<>(); + header.add("Id"); + header.add("Estado"); + header.add("Categoría"); + header.add("Wallet"); + header.add("Atributos"); + return header; + } + private List getAllItems() { List generalItems = getAllGeneralItems(); - List inventoryItems = new ArrayList<>(generalItems); List quantizableItems = getAllQuantizableItems(); - inventoryItems.addAll(quantizableItems); - return inventoryItems; + List inventoryItems = new ArrayList<>(); - } + generalItems.forEach(generalItem -> { + Category category = categoryRepository.findCategoryById(generalItem.getCategoryId()); + inventoryItems.add(InventoryItemMapper.mapToItemInfoDTO(generalItem, category)); + }); + // Mapear los QuantizableItems + quantizableItems.forEach(quantizableItem -> { + GeneralItem generalItem = quantizableItem.getGeneralItem(); + Category category = categoryRepository.findCategoryById(generalItem.getCategoryId()); + inventoryItems.add(InventoryItemMapper.mapToItemInfoDTO(generalItem, category)); + }); + return inventoryItems; + } + } diff --git a/src/main/java/com/consola/lis/service/LoginService.java b/src/main/java/com/consola/lis/service/LoginService.java index 8e707d1..c02bffb 100644 --- a/src/main/java/com/consola/lis/service/LoginService.java +++ b/src/main/java/com/consola/lis/service/LoginService.java @@ -2,7 +2,7 @@ import com.consola.lis.dto.AuthResponseDTO; import com.consola.lis.dto.LoginRequestDTO; -import com.consola.lis.exception.UserAuthenticationException; +import com.consola.lis.util.exception.UserAuthenticationException; import com.consola.lis.jwt.JwtService; import com.consola.lis.model.entity.User; import com.consola.lis.model.repository.UserRepository; diff --git a/src/main/java/com/consola/lis/service/RegisterService.java b/src/main/java/com/consola/lis/service/RegisterService.java index fdd1d04..2513b0f 100644 --- a/src/main/java/com/consola/lis/service/RegisterService.java +++ b/src/main/java/com/consola/lis/service/RegisterService.java @@ -2,7 +2,7 @@ import com.consola.lis.dto.AuthResponseDTO; import com.consola.lis.dto.RegisterRequestDTO; -import com.consola.lis.exception.AlreadyExistsException; +import com.consola.lis.util.exception.AlreadyExistsException; import com.consola.lis.jwt.JwtService; import com.consola.lis.model.entity.User; import com.consola.lis.model.repository.UserRepository; diff --git a/src/main/java/com/consola/lis/service/UserService.java b/src/main/java/com/consola/lis/service/UserService.java index dbb343d..81afc66 100644 --- a/src/main/java/com/consola/lis/service/UserService.java +++ b/src/main/java/com/consola/lis/service/UserService.java @@ -2,7 +2,8 @@ import com.consola.lis.dto.AuthResponseDTO; import com.consola.lis.dto.UserDTO; -import com.consola.lis.exception.NotExistingException; +import com.consola.lis.util.mapper.UserMapper; +import com.consola.lis.util.exception.NotExistingException; import com.consola.lis.jwt.JwtService; import com.consola.lis.model.entity.User; import com.consola.lis.model.enums.UserRole; @@ -25,13 +26,12 @@ public class UserService { public final PasswordEncoder passwordEncoder; public final JwtService jwtService; - public User getUser(String username) { + + public UserDTO getUser(String username) { Optional user = userRepository.findByUsername(username); if (user.isEmpty()) { throw new NotExistingException("404", HttpStatus.NOT_FOUND, "the category whit id " + username + " not exist "); - } else { - return user.get(); - } + } else return UserMapper.mapToUserDTO(user.get()); } diff --git a/src/main/java/com/consola/lis/constans/EndpointConstant.java b/src/main/java/com/consola/lis/util/constans/EndpointConstant.java similarity index 79% rename from src/main/java/com/consola/lis/constans/EndpointConstant.java rename to src/main/java/com/consola/lis/util/constans/EndpointConstant.java index aa86652..49f90f6 100644 --- a/src/main/java/com/consola/lis/constans/EndpointConstant.java +++ b/src/main/java/com/consola/lis/util/constans/EndpointConstant.java @@ -1,4 +1,4 @@ -package com.consola.lis.constans; +package com.consola.lis.util.constans; import lombok.experimental.UtilityClass; @@ -14,7 +14,7 @@ public class EndpointConstant { public static final String ENDPOINT_CATEGORY = "/api/console-lis/auth/category"; public static final String ENDPOINT_DELETE_CATEGORY = "/{categoryName}"; public static final String ENDPOINT_ONE_CATEGORY = "/{categoryName}"; - public static final String ENDPOINT_ALL_NAMES_CATEGORYS = "/CategoriesNames"; + public static final String ENDPOINT_ALL_NAMES_CATEGORYS = "/categoriesNames"; //Endpoints Inventory Item @@ -34,6 +34,14 @@ public class EndpointConstant { //user public static final String ENDPOINT_USER = "/api/console-lis/auth/user"; + public static final String ENDPOINT_ONE_USER ="/{username}"; + public static final String ENDPOINT_USER_LDAP = "/ldapUser/{username}"; + public static final String ENDPOINT_DELETE_USER = "/delete/{username}"; + public static final String ENDPOINT_CHANGE_ROLE_USER = "/role/{username}"; + + + + diff --git a/src/main/java/com/consola/lis/constans/Util.java b/src/main/java/com/consola/lis/util/constans/Util.java similarity index 95% rename from src/main/java/com/consola/lis/constans/Util.java rename to src/main/java/com/consola/lis/util/constans/Util.java index a2d35b2..3862137 100644 --- a/src/main/java/com/consola/lis/constans/Util.java +++ b/src/main/java/com/consola/lis/util/constans/Util.java @@ -1,4 +1,4 @@ -package com.consola.lis.constans; +package com.consola.lis.util.constans; import lombok.experimental.UtilityClass; diff --git a/src/main/java/com/consola/lis/exception/AlreadyExistsException.java b/src/main/java/com/consola/lis/util/exception/AlreadyExistsException.java similarity index 92% rename from src/main/java/com/consola/lis/exception/AlreadyExistsException.java rename to src/main/java/com/consola/lis/util/exception/AlreadyExistsException.java index c1f553f..a85dce5 100644 --- a/src/main/java/com/consola/lis/exception/AlreadyExistsException.java +++ b/src/main/java/com/consola/lis/util/exception/AlreadyExistsException.java @@ -1,4 +1,4 @@ -package com.consola.lis.exception; +package com.consola.lis.util.exception; import lombok.Data; import lombok.EqualsAndHashCode; diff --git a/src/main/java/com/consola/lis/exception/IllegalParameterInRequest.java b/src/main/java/com/consola/lis/util/exception/IllegalParameterInRequest.java similarity index 92% rename from src/main/java/com/consola/lis/exception/IllegalParameterInRequest.java rename to src/main/java/com/consola/lis/util/exception/IllegalParameterInRequest.java index 9956e76..78975a1 100644 --- a/src/main/java/com/consola/lis/exception/IllegalParameterInRequest.java +++ b/src/main/java/com/consola/lis/util/exception/IllegalParameterInRequest.java @@ -1,4 +1,4 @@ -package com.consola.lis.exception; +package com.consola.lis.util.exception; import lombok.Data; diff --git a/src/main/java/com/consola/lis/exception/JwtExpiredException.java b/src/main/java/com/consola/lis/util/exception/JwtExpiredException.java similarity index 92% rename from src/main/java/com/consola/lis/exception/JwtExpiredException.java rename to src/main/java/com/consola/lis/util/exception/JwtExpiredException.java index a731970..589140d 100644 --- a/src/main/java/com/consola/lis/exception/JwtExpiredException.java +++ b/src/main/java/com/consola/lis/util/exception/JwtExpiredException.java @@ -1,4 +1,4 @@ -package com.consola.lis.exception; +package com.consola.lis.util.exception; import lombok.Data; import lombok.EqualsAndHashCode; diff --git a/src/main/java/com/consola/lis/exception/NotExistingException.java b/src/main/java/com/consola/lis/util/exception/NotExistingException.java similarity index 92% rename from src/main/java/com/consola/lis/exception/NotExistingException.java rename to src/main/java/com/consola/lis/util/exception/NotExistingException.java index 85e36ae..fd49ff9 100644 --- a/src/main/java/com/consola/lis/exception/NotExistingException.java +++ b/src/main/java/com/consola/lis/util/exception/NotExistingException.java @@ -1,4 +1,4 @@ -package com.consola.lis.exception; +package com.consola.lis.util.exception; import lombok.Data; import lombok.EqualsAndHashCode; diff --git a/src/main/java/com/consola/lis/exception/UserAuthenticationException.java b/src/main/java/com/consola/lis/util/exception/UserAuthenticationException.java similarity index 92% rename from src/main/java/com/consola/lis/exception/UserAuthenticationException.java rename to src/main/java/com/consola/lis/util/exception/UserAuthenticationException.java index 9fbc926..bd1302e 100644 --- a/src/main/java/com/consola/lis/exception/UserAuthenticationException.java +++ b/src/main/java/com/consola/lis/util/exception/UserAuthenticationException.java @@ -1,4 +1,4 @@ -package com.consola.lis.exception; +package com.consola.lis.util.exception; import lombok.Data; import lombok.EqualsAndHashCode; diff --git a/src/main/java/com/consola/lis/util/mapper/InventoryItemMapper.java b/src/main/java/com/consola/lis/util/mapper/InventoryItemMapper.java new file mode 100644 index 0000000..32f36f6 --- /dev/null +++ b/src/main/java/com/consola/lis/util/mapper/InventoryItemMapper.java @@ -0,0 +1,31 @@ +package com.consola.lis.util.mapper; + +import com.consola.lis.dto.ItemInfoDTO; +import com.consola.lis.model.entity.Category; +import com.consola.lis.model.entity.GeneralItem; +import org.json.JSONObject; + +import java.util.Set; + +public class InventoryItemMapper { + private InventoryItemMapper () { + } + public static ItemInfoDTO mapToItemInfoDTO(GeneralItem generalItem, Category category) { + + return ItemInfoDTO.builder() + .id(generalItem.getItemId()) + .state(String.valueOf(generalItem.getState())) + .category(category.getCategoryName()) + .wallet(String.valueOf(generalItem.getWallet()).replace("_"," ")) + .quantizable(category.getQuantizable()) + .attributes(convertAttributes(generalItem.getAttributes())) + .build(); + } + + + private static String[] convertAttributes(String attributes){ + JSONObject jsonObject = new JSONObject(attributes); + Set keySet = jsonObject.keySet(); + return keySet.toArray(new String[0]); + } +} diff --git a/src/main/java/com/consola/lis/util/mapper/UserMapper.java b/src/main/java/com/consola/lis/util/mapper/UserMapper.java new file mode 100644 index 0000000..a7562b7 --- /dev/null +++ b/src/main/java/com/consola/lis/util/mapper/UserMapper.java @@ -0,0 +1,22 @@ +package com.consola.lis.util.mapper; + +import com.consola.lis.dto.UserDTO; +import com.consola.lis.model.entity.User; + + + + +public class UserMapper { + private UserMapper() { + } + public static UserDTO mapToUserDTO(User user) { + return UserDTO.builder() + .username(user.getUsername()) + .role(String.valueOf(user.getRole())) + .id(user.getId()) + .name(user.getName()) + .lastname(user.getLastname()) + .build(); + } +} + diff --git a/src/test/java/com/consola/lis/service/CategoryServiceTest.java b/src/test/java/com/consola/lis/service/CategoryServiceTest.java index c6419a8..80de5a6 100644 --- a/src/test/java/com/consola/lis/service/CategoryServiceTest.java +++ b/src/test/java/com/consola/lis/service/CategoryServiceTest.java @@ -2,8 +2,8 @@ import com.consola.lis.dto.CategoryDTO; import com.consola.lis.dto.ListAttributeDTO; -import com.consola.lis.exception.AlreadyExistsException; -import com.consola.lis.exception.NotExistingException; +import com.consola.lis.util.exception.AlreadyExistsException; +import com.consola.lis.util.exception.NotExistingException; import com.consola.lis.model.entity.Category; import com.consola.lis.model.repository.CategoryRepository; import com.fasterxml.jackson.core.JsonProcessingException; diff --git a/src/test/java/com/consola/lis/service/InventoryItemServiceTest.java b/src/test/java/com/consola/lis/service/InventoryItemServiceTest.java index 18bcd97..e4358e9 100644 --- a/src/test/java/com/consola/lis/service/InventoryItemServiceTest.java +++ b/src/test/java/com/consola/lis/service/InventoryItemServiceTest.java @@ -1,10 +1,9 @@ package com.consola.lis.service; import com.consola.lis.dto.GeneralItemDTO; -import com.consola.lis.dto.ListAttributeDTO; import com.consola.lis.dto.QuantizableItemDTO; -import com.consola.lis.exception.AlreadyExistsException; -import com.consola.lis.exception.IllegalParameterInRequest; +import com.consola.lis.util.exception.AlreadyExistsException; +import com.consola.lis.util.exception.IllegalParameterInRequest; import com.consola.lis.model.entity.Category; import com.consola.lis.model.entity.GeneralItem; import com.consola.lis.model.entity.QuantizableItem; @@ -21,7 +20,6 @@ import java.util.Arrays; import java.util.List; -import java.util.Objects; import java.util.Optional; import static org.junit.jupiter.api.Assertions.*; diff --git a/src/test/java/com/consola/lis/service/LoginServiceTest.java b/src/test/java/com/consola/lis/service/LoginServiceTest.java index 6808398..d55c765 100644 --- a/src/test/java/com/consola/lis/service/LoginServiceTest.java +++ b/src/test/java/com/consola/lis/service/LoginServiceTest.java @@ -2,7 +2,7 @@ import com.consola.lis.dto.AuthResponseDTO; import com.consola.lis.dto.LoginRequestDTO; -import com.consola.lis.exception.UserAuthenticationException; +import com.consola.lis.util.exception.UserAuthenticationException; import com.consola.lis.jwt.JwtService; import com.consola.lis.model.entity.User; import com.consola.lis.model.enums.UserRole; diff --git a/src/test/java/com/consola/lis/service/RegisterServiceTest.java b/src/test/java/com/consola/lis/service/RegisterServiceTest.java index af3b5d9..0a0242d 100644 --- a/src/test/java/com/consola/lis/service/RegisterServiceTest.java +++ b/src/test/java/com/consola/lis/service/RegisterServiceTest.java @@ -2,7 +2,7 @@ import com.consola.lis.dto.AuthResponseDTO; import com.consola.lis.dto.RegisterRequestDTO; -import com.consola.lis.exception.AlreadyExistsException; +import com.consola.lis.util.exception.AlreadyExistsException; import com.consola.lis.jwt.JwtService; import com.consola.lis.model.entity.User; import com.consola.lis.model.enums.UserRole; diff --git a/src/test/java/com/consola/lis/service/UserServiceTest.java b/src/test/java/com/consola/lis/service/UserServiceTest.java index c0c12e8..eb3f036 100644 --- a/src/test/java/com/consola/lis/service/UserServiceTest.java +++ b/src/test/java/com/consola/lis/service/UserServiceTest.java @@ -2,18 +2,16 @@ import com.consola.lis.dto.AuthResponseDTO; import com.consola.lis.dto.UserDTO; -import com.consola.lis.exception.NotExistingException; +import com.consola.lis.util.exception.NotExistingException; import com.consola.lis.jwt.JwtService; import com.consola.lis.model.entity.User; import com.consola.lis.model.enums.UserRole; import com.consola.lis.model.repository.UserRepository; -import jakarta.transaction.Transactional; import org.junit.jupiter.api.BeforeEach; import org.mockito.InjectMocks; import org.mockito.Mock; import org.junit.jupiter.api.Test; import org.mockito.MockitoAnnotations; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.web.client.RestTemplate; @@ -30,6 +28,8 @@ class UserServiceTest { @Mock RestTemplate restTemplate; + + @Mock PasswordEncoder passwordEncoder; @@ -45,27 +45,18 @@ void setUp() { } @Test - void testGetUser_ExistingUser() { + void testGetUser_UserNotExists() { + String username = "nonExistingUsername"; + Optional optionalUser = Optional.empty(); + when(userRepository.findByUsername(username)).thenReturn(optionalUser); - String username = "testUser"; - User user = new User(); - user.setUsername(username); - when(userRepository.findByUsername(username)).thenReturn(Optional.of(user)); + assertThrows(NotExistingException.class, () -> userService.getUser(username)); - User result = userService.getUser(username); + verify(userRepository, times(1)).findByUsername(username); - assertEquals(user, result); } - @Test - void testGetUser_NonExistingUser() { - // Arrange - String username = "nonExistingUser"; - when(userRepository.findByUsername(username)).thenReturn(Optional.empty()); - // Act and Assert - assertThrows(NotExistingException.class, () -> userService.getUser(username)); - } @Test void testDeleteUser_UserExist() {