Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions src/main/java/org/mybatis/jpetstore/web/CatalogApiAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
* Copyright 2010-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.jpetstore.web;

import java.util.List;

import org.mybatis.jpetstore.domain.Category;
import org.mybatis.jpetstore.domain.Item;
import org.mybatis.jpetstore.domain.Product;
import org.mybatis.jpetstore.service.CatalogService;
import org.springframework.http.ResponseEntity;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
* REST API endpoints for catalog data (categories, products, items).
* Provides JSON responses for mobile apps and external integrations.
*
* @author Generated
*/
@RestController
@RequestMapping("/api/catalog")
public class CatalogApiAction {

private final CatalogService catalogService;

public CatalogApiAction(CatalogService catalogService) {
this.catalogService = catalogService;
}

/**
* Get all categories.
*
* @return list of all categories as JSON
*/
@GetMapping("/categories")
public ResponseEntity<List<Category>> getCategories() {
List<Category> categories = catalogService.getCategoryList();
return ResponseEntity.ok(categories);
}

/**
* Get a specific category by ID.
*
* @param categoryId the category ID
* @return category details as JSON, or 404 if not found
*/
@GetMapping("/categories/{categoryId}")
public ResponseEntity<Category> getCategory(@PathVariable String categoryId) {
Category category = catalogService.getCategory(categoryId);
if (category == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(category);
}

/**
* Get products by category.
*
* @param categoryId the category ID
* @return list of products in that category as JSON
*/
@GetMapping("/categories/{categoryId}/products")
public ResponseEntity<List<Product>> getProductsByCategory(@PathVariable String categoryId) {
List<Product> products = catalogService.getProductListByCategory(categoryId);
return ResponseEntity.ok(products);
}

/**
* Get a specific product by ID.
*
* @param productId the product ID
* @return product details as JSON, or 404 if not found
*/
@GetMapping("/products/{productId}")
public ResponseEntity<Product> getProduct(@PathVariable String productId) {
Product product = catalogService.getProduct(productId);
if (product == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(product);
}

/**
* Search products by keyword.
*
* @param keywords search keywords (space-separated)
* @return list of matching products as JSON
*/
@GetMapping("/products/search")
public ResponseEntity<List<Product>> searchProducts(@RequestParam String keywords) {
if (keywords == null || keywords.trim().isEmpty()) {
return ResponseEntity.badRequest().build();
}
List<Product> products = catalogService.searchProductList(keywords);
return ResponseEntity.ok(products);
}

/**
* Get items by product.
*
* @param productId the product ID
* @return list of items for that product as JSON
*/
@GetMapping("/products/{productId}/items")
public ResponseEntity<List<Item>> getItemsByProduct(@PathVariable String productId) {
List<Item> items = catalogService.getItemListByProduct(productId);
return ResponseEntity.ok(items);
}

/**
* Get a specific item by ID.
*
* @param itemId the item ID
* @return item details as JSON, or 404 if not found
*/
@GetMapping("/items/{itemId}")
public ResponseEntity<Item> getItem(@PathVariable String itemId) {
Item item = catalogService.getItem(itemId);
if (item == null) {
return ResponseEntity.notFound().build();
}
return ResponseEntity.ok(item);
}

/**
* Check if an item is in stock.
*
* @param itemId the item ID
* @return JSON object with stock status
*/
@GetMapping("/items/{itemId}/stock")
public ResponseEntity<StockStatus> checkStock(@PathVariable String itemId) {
boolean inStock = catalogService.isItemInStock(itemId);
return ResponseEntity.ok(new StockStatus(itemId, inStock));
}

/**
* Helper class for stock status response.
*/
public static class StockStatus {
public String itemId;
public boolean inStock;

public StockStatus(String itemId, boolean inStock) {
this.itemId = itemId;
this.inStock = inStock;
}

public String getItemId() {
return itemId;
}

public boolean isInStock() {
return inStock;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 the original author or authors.
* Copyright 2010-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 the original author or authors.
* Copyright 2010-2026 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,119 +24,120 @@
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Message;
import net.sourceforge.stripes.action.Resolution;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mybatis.jpetstore.domain.Cart;

class CartActionBeanTest {

private CartActionBean cartActionBean;
private ActionBeanContext mockContext;
private CartActionBean cartActionBean;
private ActionBeanContext mockContext;

@BeforeEach
void setUp() {
cartActionBean = new CartActionBean();
cartActionBean.setCart(new Cart());
@BeforeEach
void setUp() {
cartActionBean = new CartActionBean();
cartActionBean.setCart(new Cart());

// Mock ActionBeanContext to avoid NPE in setMessage()
mockContext = mock(ActionBeanContext.class);
when(mockContext.getMessages()).thenReturn(new ArrayList<Message>());
cartActionBean.setContext(mockContext);
}
// Mock ActionBeanContext to avoid NPE in setMessage()
mockContext = mock(ActionBeanContext.class);
when(mockContext.getMessages()).thenReturn(new ArrayList<Message>());
cartActionBean.setContext(mockContext);
}

@Test
void constructorOutputNotNull() {
final CartActionBean actual = new CartActionBean();
@Test
void constructorOutputNotNull() {
final CartActionBean actual = new CartActionBean();

assertThat(actual).isNotNull();
assertThat(actual.getCart()).isNotNull();
assertThat(actual.getContext()).isNull();
}
assertThat(actual).isNotNull();
assertThat(actual.getCart()).isNotNull();
assertThat(actual.getContext()).isNull();
}

@Test
void getCartOutputNotNull() {
final CartActionBean bean = new CartActionBean();
@Test
void getCartOutputNotNull() {
final CartActionBean bean = new CartActionBean();

assertThat(bean.getCart()).isNotNull();
}
assertThat(bean.getCart()).isNotNull();
}

@Test
void addItemToCart_WithNullWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId(null);
@Test
void addItemToCart_WithNullWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId(null);

Resolution resolution = cartActionBean.addItemToCart();
Resolution resolution = cartActionBean.addItemToCart();

assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}
assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}

@Test
void addItemToCart_WithEmptyWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId("");
@Test
void addItemToCart_WithEmptyWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId("");

Resolution resolution = cartActionBean.addItemToCart();
Resolution resolution = cartActionBean.addItemToCart();

assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}
assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}

@Test
void addItemToCart_WithBlankWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId(" ");
@Test
void addItemToCart_WithBlankWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId(" ");

Resolution resolution = cartActionBean.addItemToCart();
Resolution resolution = cartActionBean.addItemToCart();

assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}
assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}

@Test
void removeItemFromCart_WithNullWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId(null);
@Test
void removeItemFromCart_WithNullWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId(null);

Resolution resolution = cartActionBean.removeItemFromCart();
Resolution resolution = cartActionBean.removeItemFromCart();

assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}
assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}

@Test
void removeItemFromCart_WithEmptyWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId("");
@Test
void removeItemFromCart_WithEmptyWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId("");

Resolution resolution = cartActionBean.removeItemFromCart();
Resolution resolution = cartActionBean.removeItemFromCart();

assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}
assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}

@Test
void removeItemFromCart_WithBlankWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId(" ");
@Test
void removeItemFromCart_WithBlankWorkingItemId_ShouldReturnError() {
cartActionBean.setWorkingItemId(" ");

Resolution resolution = cartActionBean.removeItemFromCart();
Resolution resolution = cartActionBean.removeItemFromCart();

assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}
assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}

@Test
void removeItemFromCart_WithNonExistentItem_ShouldReturnError() {
cartActionBean.setWorkingItemId("NON_EXISTENT_ITEM");
@Test
void removeItemFromCart_WithNonExistentItem_ShouldReturnError() {
cartActionBean.setWorkingItemId("NON_EXISTENT_ITEM");

Resolution resolution = cartActionBean.removeItemFromCart();
Resolution resolution = cartActionBean.removeItemFromCart();

assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}
assertThat(resolution).isNotNull();
assertThat(resolution.toString()).contains("Error.jsp");
}

@Test
void clearShouldResetCartAndWorkingItemId() {
cartActionBean.setWorkingItemId("EST-1");
@Test
void clearShouldResetCartAndWorkingItemId() {
cartActionBean.setWorkingItemId("EST-1");

cartActionBean.clear();
cartActionBean.clear();

assertThat(cartActionBean.getCart()).isNotNull();
assertThat(cartActionBean.getCart().getNumberOfItems()).isZero();
}
assertThat(cartActionBean.getCart()).isNotNull();
assertThat(cartActionBean.getCart().getNumberOfItems()).isZero();
}
}