-
Notifications
You must be signed in to change notification settings - Fork 16
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
Showing
8 changed files
with
172 additions
and
41 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
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
3 changes: 3 additions & 0 deletions
3
kbazaar/src/main/java/com/kampus/kbazaar/product/ProductResponse.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,3 @@ | ||
package com.kampus.kbazaar.product; | ||
|
||
public record ProductResponse(Long id, String name, String sku, double price, int quantity) {} |
35 changes: 35 additions & 0 deletions
35
kbazaar/src/main/java/com/kampus/kbazaar/product/ProductService.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,35 @@ | ||
package com.kampus.kbazaar.product; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class ProductService { | ||
|
||
private ProductRepository productRepository; | ||
|
||
public ProductService(ProductRepository productRepository) { | ||
this.productRepository = productRepository; | ||
} | ||
|
||
public List<ProductResponse> getAll() { | ||
return productRepository | ||
.findAll() | ||
.stream() | ||
.map(p -> new ProductResponse(p.getId(), p.getName(), p.getSku(), p.getPrice(), p.getQuantity())) | ||
.toList(); | ||
} | ||
|
||
public ProductResponse getBySku(String sku) { | ||
Optional<Product> product = productRepository.findBySku(sku); | ||
if (product.isEmpty()) { | ||
// throw new ProductNotFoundException("Product not found"); | ||
// TODO: handle exception | ||
return null; | ||
} | ||
|
||
return product.get().toResponse(); | ||
} | ||
} |
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,5 @@ | ||
INSERT INTO users (username, email, password) VALUES ('TechNinja', '[email protected]', 'password123') ON CONFLICT DO NOTHING; | ||
INSERT INTO users (username, email, password) VALUES ('CodeMaster', '[email protected]', 'securepassword') ON CONFLICT DO NOTHING; | ||
INSERT INTO users (username, email, password) VALUES ('DataGuru', '[email protected]', 'strongpassword') ON CONFLICT DO NOTHING; | ||
INSERT INTO users (username, email, password) VALUES ('CyberSavvy', '[email protected]', 'password2022') ON CONFLICT DO NOTHING; | ||
INSERT INTO users (username, email, password) VALUES ('GeekChic', '[email protected]', 'geeky123') ON CONFLICT DO NOTHING; |
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,6 @@ | ||
CREATE TABLE IF NOT EXISTS users ( | ||
id SERIAL PRIMARY KEY, | ||
username VARCHAR (255) UNIQUE NOT NULL, | ||
email VARCHAR (255) NOT NULL, | ||
password VARCHAR (255) NOT NULL | ||
); |
90 changes: 90 additions & 0 deletions
90
kbazaar/src/test/java/com/kampus/kbazaar/product/ProductServiceTest.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,90 @@ | ||
package com.kampus.kbazaar.product; | ||
|
||
import jdk.jfr.Name; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.MockitoAnnotations; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.when; | ||
|
||
class ProductServiceTest { | ||
|
||
@Mock | ||
private ProductRepository productRepository; | ||
|
||
@InjectMocks | ||
private ProductService productService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
MockitoAnnotations.openMocks(this); | ||
} | ||
|
||
@Test | ||
void testShouldBeAbleToGetAllProducts() { | ||
// Mock data | ||
Product product1 = new Product(1L, "Google Pixel 5", "MOBILE-GOOGLE-PIXEL-5", 12990.00, 100); | ||
Product product2 = new Product(2L, "Coca-Cola", "BEV-COCA-COLA", 20.00, 150); | ||
List<Product> productList = Arrays.asList(product1, product2); | ||
|
||
// Mock repository method | ||
when(productRepository.findAll()).thenReturn(productList); | ||
|
||
// Call service method | ||
List<ProductResponse> result = productService.getAll(); | ||
|
||
// Assertions | ||
assertEquals(2, result.size()); | ||
assertEquals("Google Pixel 5", result.get(0).name()); | ||
assertEquals("MOBILE-GOOGLE-PIXEL-5", result.get(1).sku()); | ||
} | ||
|
||
@Test | ||
void testShouldReturnEmptyListWhenNoProductFoundGetAllProducts() { | ||
// Mock repository method returning empty list | ||
when(productRepository.findAll()).thenReturn(Arrays.asList()); | ||
|
||
// Call service method | ||
List<ProductResponse> result = productService.getAll(); | ||
|
||
// Assertions | ||
assertTrue(result.isEmpty()); | ||
} | ||
|
||
@Test | ||
void testShouldBeAbleToGetProductBySku() { | ||
// Mock data | ||
Product product = new Product(1L, "Pens", "STATIONERY-PEN-BIC-BALLPOINT", 14.99, 100); | ||
|
||
// Mock repository method | ||
when(productRepository.findBySku("STATIONERY-PEN-BIC-BALLPOINT")).thenReturn(Optional.of(product)); | ||
|
||
// Call service method | ||
ProductResponse result = productService.getBySku("STATIONERY-PEN-BIC-BALLPOINT"); | ||
|
||
// Assertions | ||
assertEquals("Pens", result.name()); | ||
assertEquals(14.99, result.price()); | ||
} | ||
|
||
@Test | ||
void testShouldReturnNullWhenGetProductNonExistingSKU() { | ||
// Mock repository method returning empty optional | ||
when(productRepository.findBySku(anyString())).thenReturn(Optional.empty()); | ||
|
||
// Call service method | ||
ProductResponse result = productService.getBySku("NonExistingSKU"); | ||
|
||
// Assertions | ||
assertNull(result); | ||
} | ||
} |