Search in sources :

Example 36 with Product

use of com.google.cloud.retail.v2.Product in project java-retail by googleapis.

the class ProductServiceClientTest method listProductsTest.

@Test
public void listProductsTest() throws Exception {
    Product responsesElement = Product.newBuilder().build();
    ListProductsResponse expectedResponse = ListProductsResponse.newBuilder().setNextPageToken("").addAllProducts(Arrays.asList(responsesElement)).build();
    mockProductService.addResponse(expectedResponse);
    BranchName parent = BranchName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[BRANCH]");
    ListProductsPagedResponse pagedListResponse = client.listProducts(parent);
    List<Product> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getProductsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockProductService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListProductsRequest actualRequest = ((ListProductsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListProductsPagedResponse(com.google.cloud.retail.v2.ProductServiceClient.ListProductsPagedResponse) Test(org.junit.Test)

Example 37 with Product

use of com.google.cloud.retail.v2.Product in project java-retail by googleapis.

the class CrudProduct method getProduct.

// get product
public static Product getProduct(String productName) throws IOException {
    Product product = Product.newBuilder().build();
    GetProductRequest getProductRequest = GetProductRequest.newBuilder().setName(productName).build();
    try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
        product = serviceClient.getProduct(getProductRequest);
        System.out.println("Get product response: " + product);
        return product;
    } catch (NotFoundException e) {
        System.out.printf("Product %s not found", productName);
        return product;
    }
}
Also used : GetProductRequest(com.google.cloud.retail.v2.GetProductRequest) Product(com.google.cloud.retail.v2.Product) ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) NotFoundException(com.google.api.gax.rpc.NotFoundException)

Example 38 with Product

use of com.google.cloud.retail.v2.Product in project java-retail by googleapis.

the class CrudProduct method updateProduct.

// update product
public static void updateProduct(Product originalProduct, String productName) throws IOException {
    UpdateProductRequest updateProductRequest = UpdateProductRequest.newBuilder().setProduct(generateProductForUpdate(originalProduct.getId(), productName)).setAllowMissing(true).build();
    System.out.printf("Update product request: %s%n", updateProductRequest);
    try (ProductServiceClient serviceClient = ProductServiceClient.create()) {
        Product updatedProduct = serviceClient.updateProduct(updateProductRequest);
        System.out.printf("Updated product: %s%n", updatedProduct);
    }
}
Also used : ProductServiceClient(com.google.cloud.retail.v2.ProductServiceClient) Product(com.google.cloud.retail.v2.Product) UpdateProductRequest(com.google.cloud.retail.v2.UpdateProductRequest)

Example 39 with Product

use of com.google.cloud.retail.v2.Product in project java-retail by googleapis.

the class CrudProduct method createProduct.

// call the Retail API to create product
public static Product createProduct(String productId, String branchName) throws IOException {
    CreateProductRequest createProductRequest = CreateProductRequest.newBuilder().setProduct(generateProduct()).setProductId(productId).setParent(branchName).build();
    System.out.printf("Create product request: %s%n", createProductRequest);
    Product createdProduct = ProductServiceClient.create().createProduct(createProductRequest);
    System.out.printf("Created product: %s%n", createdProduct);
    return createdProduct;
}
Also used : CreateProductRequest(com.google.cloud.retail.v2.CreateProductRequest) Product(com.google.cloud.retail.v2.Product)

Example 40 with Product

use of com.google.cloud.retail.v2.Product in project java-retail by googleapis.

the class CrudProduct method main.

public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = ServiceOptions.getDefaultProjectId();
    String generatedProductId = UUID.randomUUID().toString();
    String branchName = String.format("projects/%s/locations/global/catalogs/default_catalog/branches/0", projectId);
    String productName = String.format("%s/products/%s", branchName, generatedProductId);
    Product createdProduct = createProduct(generatedProductId, branchName);
    getProduct(productName);
    updateProduct(createdProduct, productName);
    deleteProduct(productName);
}
Also used : Product(com.google.cloud.retail.v2.Product)

Aggregations

Product (com.google.cloud.retail.v2.Product)20 ProductServiceClient (com.google.cloud.retail.v2.ProductServiceClient)12 Product (com.google.cloud.vision.v1.Product)10 SetupCleanup.deleteProduct (setup.SetupCleanup.deleteProduct)9 PriceInfo (com.google.cloud.retail.v2.PriceInfo)7 Test (org.junit.Test)6 SetupCleanup.createProduct (setup.SetupCleanup.createProduct)6 DeleteProductRequest (com.google.cloud.retail.v2.DeleteProductRequest)5 ImportProductsRequest (com.google.cloud.retail.v2.ImportProductsRequest)5 ProductSearchClient (com.google.cloud.vision.v1.ProductSearchClient)5 Product (com.haulmont.cuba.testmodel.sales_1.Product)5 ListProductsPagedResponse (com.google.cloud.retail.v2.ProductServiceClient.ListProductsPagedResponse)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 PrintStream (java.io.PrintStream)4 Before (org.junit.Before)4 Test (org.junit.jupiter.api.Test)4 NotFoundException (com.google.api.gax.rpc.NotFoundException)3 CreateProductRequest (com.google.cloud.retail.v2.CreateProductRequest)3 FulfillmentInfo (com.google.cloud.retail.v2.FulfillmentInfo)3 GetProductRequest (com.google.cloud.retail.v2.GetProductRequest)3