Commit 4abed227 authored by Bhargava Rellu's avatar Bhargava Rellu

Changes according to the review comments,

Added test cases.
parent e3fd38fc
...@@ -31,14 +31,22 @@ public class OrderManagement { ...@@ -31,14 +31,22 @@ public class OrderManagement {
* functionality regarding the selected option in the console. * functionality regarding the selected option in the console.
*/ */
public void menu() { public void menu() {
String option1 = "1. Add product to your stock";
String option2 = "2. Order Product";
String option3 = "3. Get order details";
String option4 = "4. Exit";
String toExit = "To exit please enter 4";
String toAddMore = "To Add more enter 1";
String enterForMenu = "Please enter 5 for menu";
boolean flag = true; boolean flag = true;
logger.info("Welcome to RetailStore Business Management"); logger.info("Welcome to RetailStore Business Management");
RetailStore gap = new RetailStoreService(); RetailStore gap = new RetailStoreService();
String menu = "\n------------------------\nMenu\n------------------------\n" + String menu = "\n------------------------\nMenu\n------------------------\n" +
"1. Add product to your stock\n------------------------\n" + option1 + "\n------------------------\n" +
"2. Order Product\n------------------------\n" + option2 + "\n------------------------\n" +
"3. Get order details\n------------------------\n" + option3 + "\n------------------------\n" +
"4. Exit"; option4;
logger.info(menu); logger.info(menu);
while (flag) { while (flag) {
int menuOption; int menuOption;
......
package com.retail_store.order_management.model; package com.retail_store.order_management.model;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter; import lombok.Getter;
import java.time.LocalDate; import java.time.LocalDate;
...@@ -13,6 +14,7 @@ import java.util.List; ...@@ -13,6 +14,7 @@ import java.util.List;
*/ */
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
@Builder
public class Order { public class Order {
private String orderId; private String orderId;
private LocalDate orderDate; private LocalDate orderDate;
......
package com.retail_store.order_management.service; package com.retailstore.ordermanagement.service;
import com.retail_store.order_management.constants.Category; import com.retailstore.ordermanagement.constants.Category;
import com.retail_store.order_management.constants.Color; import com.retailstore.ordermanagement.constants.Color;
import com.retail_store.order_management.constants.Gender; import com.retailstore.ordermanagement.constants.Gender;
import com.retail_store.order_management.model.Order; import com.retailstore.ordermanagement.model.Order;
import com.retail_store.order_management.model.Product; import com.retailstore.ordermanagement.model.Product;
import java.sql.Connection; import java.sql.Connection;
import java.util.Map; import java.util.Map;
......
package com.retail_store.order_management.service; package com.retailstore.ordermanagement.service;
import com.retail_store.order_management.model.OrderedProduct; import com.retailstore.ordermanagement.model.OrderedProduct;
import com.retail_store.order_management.model.Product; import com.retailstore.ordermanagement.model.Product;
import java.sql.Connection; import java.sql.Connection;
import java.util.Collection; import java.util.Collection;
......
package com.retail_store.order_management.utils; package com.retailstore.ordermanagement.utils;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
......
package com.retail_store.order_management.utils; package com.retailstore.ordermanagement.utils;
import com.retail_store.order_management.application.OrderManagement; import com.retailstore.ordermanagement.application.OrderManagement;
import com.retail_store.order_management.constants.Category; import com.retailstore.ordermanagement.constants.Category;
import com.retail_store.order_management.constants.Color; import com.retailstore.ordermanagement.constants.Color;
import com.retail_store.order_management.constants.Gender; import com.retailstore.ordermanagement.constants.Gender;
import com.retail_store.order_management.constants.Size; import com.retailstore.ordermanagement.constants.Size;
import com.retail_store.order_management.model.Order; import com.retailstore.ordermanagement.model.Order;
import com.retail_store.order_management.service.RetailStore; import com.retailstore.ordermanagement.service.RetailStoreService;
import java.util.*; import java.util.*;
import java.util.logging.Logger; import java.util.logging.Logger;
...@@ -17,7 +17,7 @@ import java.util.logging.Logger; ...@@ -17,7 +17,7 @@ import java.util.logging.Logger;
* Class used to import the values of the option given in the console and print accordingly * Class used to import the values of the option given in the console and print accordingly
* of the required choice chosen * of the required choice chosen
*/ */
public final class Utility { public class Utility {
private static final Scanner SCANNER = null; private static final Scanner SCANNER = null;
private static final Logger LOGGER = null; private static final Logger LOGGER = null;
...@@ -40,7 +40,7 @@ public final class Utility { ...@@ -40,7 +40,7 @@ public final class Utility {
if (check) { if (check) {
getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + categoryOptions + "\n------------------------\n"); getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + categoryOptions + "\n------------------------\n");
} }
int option = getInt(); int option = validateInputNumber();
categoryValue = Arrays.stream(Category.values()).filter(category -> category.getOption() == option).findFirst().orElseGet(() -> { categoryValue = Arrays.stream(Category.values()).filter(category -> category.getOption() == option).findFirst().orElseGet(() -> {
getLogger().info("\n------------------------\n+Please enter valid category\n------------------------\n" + categoryOptions + "\n------------------------\n"); getLogger().info("\n------------------------\n+Please enter valid category\n------------------------\n" + categoryOptions + "\n------------------------\n");
return getCategory(false, message); return getCategory(false, message);
...@@ -61,7 +61,7 @@ public final class Utility { ...@@ -61,7 +61,7 @@ public final class Utility {
if (check) { if (check) {
getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + categoryOptions + "\n------------------------\n"); getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + categoryOptions + "\n------------------------\n");
} }
int option = getInt(); int option = validateInputNumber();
sizeValue = Arrays.stream(Size.values()).filter(size -> size.getOption() == option).findFirst().orElseGet(() -> { sizeValue = Arrays.stream(Size.values()).filter(size -> size.getOption() == option).findFirst().orElseGet(() -> {
getLogger().info("\n------------------------\nPlease enter valid category\n------------------------\n" + categoryOptions + "\n------------------------\n"); getLogger().info("\n------------------------\nPlease enter valid category\n------------------------\n" + categoryOptions + "\n------------------------\n");
return getSize(false, message); return getSize(false, message);
...@@ -82,7 +82,7 @@ public final class Utility { ...@@ -82,7 +82,7 @@ public final class Utility {
if (check) { if (check) {
getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + genderOptions + "\n------------------------\n"); getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + genderOptions + "\n------------------------\n");
} }
int option = getInt(); int option = validateInputNumber();
genderValue = Arrays.stream(Gender.values()).filter(gender -> gender.getOption() == option).findFirst().orElseGet(() -> { genderValue = Arrays.stream(Gender.values()).filter(gender -> gender.getOption() == option).findFirst().orElseGet(() -> {
getLogger().info("\n------------------------\nPlease enter valid gender\n------------------------\n" + genderOptions + "\n------------------------\n"); getLogger().info("\n------------------------\nPlease enter valid gender\n------------------------\n" + genderOptions + "\n------------------------\n");
return getGender(false, message); return getGender(false, message);
...@@ -103,7 +103,7 @@ public final class Utility { ...@@ -103,7 +103,7 @@ public final class Utility {
if (check) { if (check) {
getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + colorOptions + "\n------------------------\n"); getLogger().info("\n------------------------\n" + message + "\n------------------------\n" + colorOptions + "\n------------------------\n");
} }
int option = getInt(); int option = validateInputNumber();
colorValue = Arrays.stream(Color.values()).filter(color -> color.getOption() == option).findFirst().orElseGet(() -> { colorValue = Arrays.stream(Color.values()).filter(color -> color.getOption() == option).findFirst().orElseGet(() -> {
getLogger().info("\n------------------------\n+Please enter valid color\n------------------------\n" + colorOptions + "\n------------------------\n"); getLogger().info("\n------------------------\n+Please enter valid color\n------------------------\n" + colorOptions + "\n------------------------\n");
return getColor(false, message); return getColor(false, message);
...@@ -119,7 +119,7 @@ public final class Utility { ...@@ -119,7 +119,7 @@ public final class Utility {
* @param message * @param message
* @return double * @return double
*/ */
public static double getDouble(boolean flag, String message) { public static double validateInputPrice(boolean flag, String message) {
double number; double number;
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
if (flag) { if (flag) {
...@@ -134,7 +134,7 @@ public final class Utility { ...@@ -134,7 +134,7 @@ public final class Utility {
} catch (InputMismatchException e) { } catch (InputMismatchException e) {
getLogger().warning("\n------------------------\nPlease enter valid price\n------------------------\n"); getLogger().warning("\n------------------------\nPlease enter valid price\n------------------------\n");
scanner.reset(); scanner.reset();
return getDouble(false, message); return validateInputPrice(false, message);
} }
} }
...@@ -144,19 +144,19 @@ public final class Utility { ...@@ -144,19 +144,19 @@ public final class Utility {
* *
* @return int * @return int
*/ */
public static int getInt() { public static int validateInputNumber() {
int number; int number;
Scanner scanner = new Scanner(System.in); Scanner scanner = new Scanner(System.in);
try { try {
number = scanner.nextInt(); number = scanner.nextInt();
if (number < 0) { if (number < 0) {
throw new RuntimeException(); throw new RuntimeException("Number cannot be negative");
} }
return number; return number;
} catch (Exception e) { } catch (Exception e) {
getLogger().warning("\n------------------------\nPlease enter valid number\n------------------------\n"); getLogger().warning(e.getMessage() + "\n------------------------\nPlease enter valid number\n------------------------\n");
scanner.reset(); scanner.reset();
return getInt(); return validateInputNumber();
} }
} }
...@@ -178,7 +178,7 @@ public final class Utility { ...@@ -178,7 +178,7 @@ public final class Utility {
getLogger().warning("Please enter valid phone number"); getLogger().warning("Please enter valid phone number");
return phoneNumberValidator(); return phoneNumberValidator();
} }
return phoneNumber.toString(); return phoneNumber;
} }
/** /**
...@@ -206,18 +206,18 @@ public final class Utility { ...@@ -206,18 +206,18 @@ public final class Utility {
* *
* @return integer according to key type size * @return integer according to key type size
*/ */
public static LinkedHashMap<Size, Integer> getProductSizes() { public static Map<Size, Integer> getProductSizes() {
LinkedHashMap<Size, Integer> productSizes = new LinkedHashMap<>(); Map<Size, Integer> productSizes = new LinkedHashMap<>();
getLogger().info("Please enter the available stock for particular sizes \n Number of products with size S"); getLogger().info("Please enter the available stock for particular sizes \n Number of products with size S");
productSizes.put(Size.S, getInt()); productSizes.put(Size.S, validateInputNumber());
getLogger().info("Number of products with size M"); getLogger().info("Number of products with size M");
productSizes.put(Size.M, getInt()); productSizes.put(Size.M, validateInputNumber());
getLogger().info("Number of products with size L"); getLogger().info("Number of products with size L");
productSizes.put(Size.L, getInt()); productSizes.put(Size.L, validateInputNumber());
getLogger().info("Number of products with size XL"); getLogger().info("Number of products with size XL");
productSizes.put(Size.XL, getInt()); productSizes.put(Size.XL, validateInputNumber());
getLogger().info("Number of products with size XXL"); getLogger().info("Number of products with size XXL");
productSizes.put(Size.XXL, getInt()); productSizes.put(Size.XXL, validateInputNumber());
return productSizes; return productSizes;
} }
...@@ -233,7 +233,7 @@ public final class Utility { ...@@ -233,7 +233,7 @@ public final class Utility {
* @return integer according to key type size * @return integer according to key type size
*/ */
public static Map<Size, Integer> getProductSizes(int s, int m, int l, int xl, int xxl) { public static Map<Size, Integer> getProductSizes(int s, int m, int l, int xl, int xxl) {
LinkedHashMap<Size, Integer> productSizes = new LinkedHashMap<>(); Map<Size, Integer> productSizes = new LinkedHashMap<>();
productSizes.put(Size.S, s); productSizes.put(Size.S, s);
productSizes.put(Size.M, m); productSizes.put(Size.M, m);
productSizes.put(Size.L, l); productSizes.put(Size.L, l);
...@@ -249,7 +249,7 @@ public final class Utility { ...@@ -249,7 +249,7 @@ public final class Utility {
* @return string * @return string
*/ */
public static String productIdGenerator() { public static String productIdGenerator() {
return Arrays.stream(UUID.randomUUID().toString().split("-")).skip(1).findFirst().get() + "#" + RetailStore.class.getSimpleName(); return Arrays.stream(UUID.randomUUID().toString().split("-")).skip(1).findFirst().get() + "#" + RetailStoreService.class.getSimpleName();
} }
/** /**
......
package com.retailstore.ordermanagement.service;
import com.retailstore.ordermanagement.constants.Category;
import com.retailstore.ordermanagement.constants.Color;
import com.retailstore.ordermanagement.constants.Gender;
import com.retailstore.ordermanagement.model.Order;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import java.sql.Connection;
import java.util.*;
import java.util.stream.Collectors;
class ProductFilterTest {
@Mock
Connection connection;
RetailStoreService service;
@BeforeEach
void createretailsStoreObject() {
service = new RetailStoreService();
}
@AfterEach
void removeretailsStoreObject() {
service = null;
}
@Test
void getCategoriesByGenderTest() {
Map<Integer, Category> categoryMap = new HashMap<>();
categoryMap.put(1, Category.SWEATSHIRTS);
categoryMap.put(2, Category.T_SHIRTS);
categoryMap.put(3, Category.SHIRTS);
categoryMap.put(4, Category.SHORTS);
categoryMap.put(5, Category.HOODIES);
categoryMap.put(6, Category.PANTS);
categoryMap.put(7, Category.JEANS);
categoryMap.put(8, Category.POLOS);
Assertions.assertEquals(categoryMap, service.getCategoriesByGender(Gender.MEN));
}
@Test
void getProductsByGenderAndCategoryTest() {
List<String> productsList = new ArrayList<>();
productsList.add("14e0#Gap");
productsList.add("86f0#Gap");
productsList.add("9064#Gap");
productsList.add("b715#Gap");
Assertions.assertEquals(productsList, service.getProductsByGenderAndCategory(Gender.MEN, Category.HOODIES).entrySet().stream().map(product -> product.getValue().getId()).collect(Collectors.toList()));
productsList.clear();
Assertions.assertEquals(productsList, service.getProductsByGenderAndCategory(Gender.WOMEN, Category.PANTS).entrySet().stream().map(product -> product.getValue().getId()).collect(Collectors.toList()));
}
@Test
void getColorsByGenderAndCategoryTest() {
Map<Integer, Color> colorMap = new HashMap<>();
colorMap.put(1, Color.BLUE);
colorMap.put(2, Color.GREY);
colorMap.put(3, Color.BLACK);
Assertions.assertEquals(colorMap, service.getColorsByGenderAndCategory(Gender.MEN, Category.JEANS));
colorMap.clear();
Assertions.assertEquals(colorMap, service.getColorsByGenderAndCategory(Gender.MEN, Category.DRESS));
}
@Test
void getOrdersTest() {
List<Order> orderList = new ArrayList<>();
Map<Integer, Order> ordersMap = new HashMap<>();
Assertions.assertEquals(ordersMap, service.getOrders(connection));
}
}
\ No newline at end of file
package com.retailstore.ordermanagement.service;
import com.retailstore.ordermanagement.constants.Category;
import com.retailstore.ordermanagement.constants.Color;
import com.retailstore.ordermanagement.constants.Gender;
import com.retailstore.ordermanagement.constants.Size;
import com.retailstore.ordermanagement.model.Order;
import com.retailstore.ordermanagement.model.OrderedProduct;
import com.retailstore.ordermanagement.utils.Utility;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import java.sql.Connection;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
class RetailStoreTest {
@Mock
Connection connection;
RetailStoreService service;
@Spy
@InjectMocks
Utility utility;
@BeforeEach
void createretailsStoreObject() {
service = new RetailStoreService();
}
@AfterEach
void removeretailsStoreObject() {
service = null;
}
@Test
void addProducts() {
LinkedHashMap<Size, Integer> sizeMap = new LinkedHashMap<>();
sizeMap.put(Size.S, 2);
sizeMap.put(Size.M, 2);
sizeMap.put(Size.L, 2);
sizeMap.put(Size.XL, 2);
sizeMap.put(Size.XXL, 2);
Mockito.when(utility.productIdGenerator()).thenReturn("TestMethod" + LocalDate.now() + LocalTime.now());
Mockito.when(Utility.getScanner().nextLine()).thenReturn("TestProduct");
Mockito.when(Utility.getCategory(true, "Test check")).thenReturn(Category.JEANS);
Mockito.when(Utility.getColor(true, "Test check")).thenReturn(Color.BLACK);
Mockito.when(Utility.getGender(true, "Test check")).thenReturn(Gender.WOMEN);
Mockito.when(Utility.getDouble(true, "Test price")).thenReturn(1350.00);
Mockito.when(Utility.getProductSizes()).thenReturn(sizeMap);
Assertions.assertEquals(true, service.addProducts());
}
@Test
void placeOrders() {
}
@Test
void showOrdersTest() {
Map<Integer, Order> ordersMap = Mockito.mock(LinkedHashMap.class);
List<OrderedProduct> orderedProductList = Mockito.mock(ArrayList.class);
ordersMap.put(1, new Order("10bc46f3#Order", LocalDate.now(), LocalTime.now(), "Customer", "9633214574", 2500.00, new ArrayList<OrderedProduct>()));
Mockito.when(service.getOrders(connection)).thenReturn(ordersMap);
}
@Test
void createOrder() {
ArrayList<OrderedProduct> orderedProducts = new ArrayList<>();
orderedProducts.add(new OrderedProduct("test1", "testname", Category.JEANS, Color.BLACK, 1200.00, Gender.MEN, Size.L, 2));
// Mockito.when(getGender(true, "Test check")).thenReturn(Gender.WOMEN);
}
@Test
void addProductToCart() {
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment