Skip to content

Commit

Permalink
feat : add part of the buyProduct function code
Browse files Browse the repository at this point in the history
  • Loading branch information
tx2002 committed Oct 11, 2024
1 parent 546f85b commit aae203d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/casbin/casdoor/service/ProductService.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public CasdoorResponse<String, Object> updateProduct(Product product) throws IOE
return modifyProduct(ProductOperations.UPDATE_PRODUCT, product, null);
}

public Product buyProduct(String name, String providerName) throws IOException {
java.util.Map<String, String> queryMap = Map.of(
"id", config.organizationName + "/" + name,
"providerName", providerName
);
CasdoorResponse<Product, Object> response = doPost("buy-product", queryMap, "", new TypeReference<CasdoorResponse<Product, Object>>() {
});
return response.getData();
}

private <T1, T2> CasdoorResponse<T1, T2> modifyProduct(ProductOperations method, Product product, java.util.Map<String, String> queryMap) throws IOException {
String id = product.owner + "/" + product.name;
product.owner = config.organizationName;
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/casbin/casdoor/ProductTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,20 @@ public void testProduct() {
}
assertEquals(updatedDescription, updatedProduct.description, "Failed to update object, description mismatch");

// Test the buyProduct function
Product boughtProduct;
try {
boughtProduct = productService.buyProduct(name, "provider_payment_paypal");
} catch (Exception e) {
String errorMessage = e.getMessage();
if (errorMessage.contains("The user") && errorMessage.contains("doesn't exist")) {
assertTrue(true);
} else {
fail("Failed to buy product: " + e.getMessage());
}
return;
}

// Delete the object
assertDoesNotThrow(() -> productService.deleteProduct(product));

Expand Down

0 comments on commit aae203d

Please sign in to comment.