Skip to content

Commit

Permalink
fixed sonar issue
Browse files Browse the repository at this point in the history
  • Loading branch information
AnuchitO committed Mar 21, 2024
1 parent 2066e69 commit 5afb685
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -22,50 +21,43 @@ public ProductController(ProductService productService) {
this.productService = productService;
}

@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "list all products",
content = {
@ApiResponse(
responseCode = "200",
description = "list all products",
content = {
@Content(
mediaType = "application/json",
array =
@ArraySchema(
schema = @Schema(implementation = ProductResponse.class)))
})
@ApiResponse(
responseCode = "500",
description = "internal server error",
content =
@Content(
mediaType = "application/json",
array =
@ArraySchema(
schema =
@Schema(
implementation =
ProductResponse.class)))
}),
@ApiResponse(
responseCode = "500",
description = "internal server error",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = NotFoundException.class)))
})
schema = @Schema(implementation = NotFoundException.class)))
@GetMapping("/products")
public List<ProductResponse> getProducts() {
return productService.getAll();
}

@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "get product by sku",
content = {
@ApiResponse(
responseCode = "200",
description = "get product by sku",
content = {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = ProductResponse.class))
})
@ApiResponse(
responseCode = "404",
description = "product not found",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = ProductResponse.class))
}),
@ApiResponse(
responseCode = "404",
description = "product not found",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = NotFoundException.class)))
})
schema = @Schema(implementation = NotFoundException.class)))
@GetMapping("/products/{sku}")
public ProductResponse getProductById(@PathVariable String sku) {
return productService.getBySku(sku);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public ProductService(ProductRepository productRepository) {
}

public List<ProductResponse> getAll() {
return productRepository.findAll().stream().map(p -> p.toResponse()).toList();
return productRepository.findAll().stream().map(Product::toResponse).toList();
}

public ProductResponse getBySku(String sku) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -20,53 +20,46 @@ public PromotionController(PromotionService promotionService) {
this.promotionService = promotionService;
}

@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "list all promotions",
content = {
@ApiResponse(
responseCode = "200",
description = "list all promotions",
content = {
@Content(
mediaType = "application/json",
array =
@ArraySchema(
schema = @Schema(implementation = PromotionResponse.class)))
})
@ApiResponse(
responseCode = "500",
description = "internal server error",
content =
@Content(
mediaType = "application/json",
array =
@ArraySchema(
schema =
@Schema(
implementation =
PromotionResponse.class)))
}),
@ApiResponse(
responseCode = "500",
description = "internal server error",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = NotFoundException.class)))
})
schema = @Schema(implementation = NotFoundException.class)))
@GetMapping("/promotions")
public List<PromotionResponse> getAllPromotions() {
return promotionService.getAll();
}

@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "get promotion by code",
content = {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = PromotionResponse.class))
}),
@ApiResponse(
responseCode = "404",
description = "promotion not found",
content = {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = NotFoundException.class))
})
})
@ApiResponse(
responseCode = "200",
description = "get promotion by code",
content = {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = PromotionResponse.class))
})
@ApiResponse(
responseCode = "404",
description = "promotion not found",
content = {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = NotFoundException.class))
})
@GetMapping("/promotions/{code}")
public PromotionResponse getPromotionByCode(String code) {
public PromotionResponse getPromotionByCode(@PathVariable String code) {
return promotionService.getPromotionByCode(code);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public PromotionService(PromotionRepository promotionRepository) {
}

public List<PromotionResponse> getAll() {
return promotionRepository.findAll().stream().map(p -> p.toResponse()).toList();
return promotionRepository.findAll().stream().map(Promotion::toResponse).toList();
}

public PromotionResponse getPromotionByCode(String code) {
return promotionRepository
.findByCode(code)
.map(p -> p.toResponse())
.map(Promotion::toResponse)
.orElseThrow(() -> new NotFoundException("Promotion not found"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -22,52 +21,45 @@ public ShopperController(ShopperService shopperService) {
this.shopperService = shopperService;
}

@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "list all shoppers",
content = {
@ApiResponse(
responseCode = "200",
description = "list all shoppers",
content = {
@Content(
mediaType = "application/json",
array =
@ArraySchema(
schema = @Schema(implementation = ShopperResponse.class)))
})
@ApiResponse(
responseCode = "500",
description = "internal server error",
content =
@Content(
mediaType = "application/json",
array =
@ArraySchema(
schema =
@Schema(
implementation =
ShopperResponse.class)))
}),
@ApiResponse(
responseCode = "500",
description = "internal server error",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = NotFoundException.class)))
})
schema = @Schema(implementation = NotFoundException.class)))
@GetMapping("/shopper")
public List<ShopperResponse> getAllUsers() {
return shopperService.getAll();
}

@ApiResponses({
@ApiResponse(
responseCode = "200",
description = "get shopper by id",
content = {
@ApiResponse(
responseCode = "200",
description = "get shopper by id",
content = {
@Content(
mediaType = "application/json",
schema = @Schema(implementation = ShopperResponse.class))
})
@ApiResponse(
responseCode = "500",
description = "internal server error",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = ShopperResponse.class))
}),
@ApiResponse(
responseCode = "500",
description = "internal server error",
content =
@Content(
mediaType = "application/json",
schema = @Schema(implementation = NotFoundException.class)))
})
@GetMapping("/shopper/{id}")
public ShopperResponse getUserById(@PathVariable String id) {
return shopperService.getById(id);
schema = @Schema(implementation = NotFoundException.class)))
@GetMapping("/shopper/{username}")
public ShopperResponse getUserByUsername(@PathVariable String username) {
return shopperService.getByUsername(username);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface ShopperRepository extends JpaRepository<Shopper, Integer> {}
public interface ShopperRepository extends JpaRepository<Shopper, Integer> {
Optional<Shopper> findByUsername(String username);
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ public ShopperService(ShopperRepository shopperRepository) {
}

public List<ShopperResponse> getAll() {
return shopperRepository.findAll().stream().map(s -> s.toResponse()).toList();
return shopperRepository.findAll().stream().map(Shopper::toResponse).toList();
}

public ShopperResponse getById(String id) {
return shopperRepository
.findById(Integer.parseInt(id))
.map(s -> s.toResponse())
.map(Shopper::toResponse)
.orElseThrow(() -> new NotFoundException("Shopper not found"));
}

public ShopperResponse getByUsername(String username) {
return shopperRepository
.findByUsername(username)
.map(Shopper::toResponse)
.orElseThrow(() -> new NotFoundException("Shopper not found"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,30 @@ void testGetById_ShouldThrowNotFoundException() {
// Assertions
assertThrows(NotFoundException.class, () -> shopperService.getById("1"));
}

@Test
void testGetByUsername_ShouldReturnShopper() {
// Mock data
Shopper shopper = new Shopper(1L, "DataGuru", "[email protected]");

// Mock repository method
when(shopperRepository.findByUsername("DataGuru")).thenReturn(Optional.of(shopper));

// Call service method
ShopperResponse result = shopperService.getByUsername("DataGuru");

// Assertions
assertEquals("DataGuru", result.username());
}

@Test
void testGetByUsername_ShouldThrowNotFoundException() {
// Mock repository method returning empty optional
when(shopperRepository.findByUsername("DataGuru")).thenReturn(Optional.empty());

// Assertions
assertThrows(NotFoundException.class, () -> shopperService.getByUsername("DataGuru"));
}


}

0 comments on commit 5afb685

Please sign in to comment.