Search in sources :

Example 1 with Recommendation

use of com.google.cloud.recommender.v1beta1.Recommendation in project java-recommender by googleapis.

the class RecommenderClientTest method listRecommendationsTest2.

@Test
public void listRecommendationsTest2() throws Exception {
    Recommendation responsesElement = Recommendation.newBuilder().build();
    ListRecommendationsResponse expectedResponse = ListRecommendationsResponse.newBuilder().setNextPageToken("").addAllRecommendations(Arrays.asList(responsesElement)).build();
    mockRecommender.addResponse(expectedResponse);
    String parent = "parent-995424086";
    String filter = "filter-1274492040";
    ListRecommendationsPagedResponse pagedListResponse = client.listRecommendations(parent, filter);
    List<Recommendation> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getRecommendationsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockRecommender.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListRecommendationsRequest actualRequest = ((ListRecommendationsRequest) actualRequests.get(0));
    Assert.assertEquals(parent, actualRequest.getParent());
    Assert.assertEquals(filter, actualRequest.getFilter());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListRecommendationsPagedResponse(com.google.cloud.recommender.v1beta1.RecommenderClient.ListRecommendationsPagedResponse) Test(org.junit.Test)

Example 2 with Recommendation

use of com.google.cloud.recommender.v1beta1.Recommendation in project java-recommender by googleapis.

the class RecommenderClientTest method listRecommendationsTest.

@Test
public void listRecommendationsTest() throws Exception {
    Recommendation responsesElement = Recommendation.newBuilder().build();
    ListRecommendationsResponse expectedResponse = ListRecommendationsResponse.newBuilder().setNextPageToken("").addAllRecommendations(Arrays.asList(responsesElement)).build();
    mockRecommender.addResponse(expectedResponse);
    RecommenderName parent = RecommenderName.ofProjectLocationRecommenderName("[PROJECT]", "[LOCATION]", "[RECOMMENDER]");
    String filter = "filter-1274492040";
    ListRecommendationsPagedResponse pagedListResponse = client.listRecommendations(parent, filter);
    List<Recommendation> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getRecommendationsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockRecommender.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListRecommendationsRequest actualRequest = ((ListRecommendationsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertEquals(filter, actualRequest.getFilter());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : AbstractMessage(com.google.protobuf.AbstractMessage) ListRecommendationsPagedResponse(com.google.cloud.recommender.v1beta1.RecommenderClient.ListRecommendationsPagedResponse) Test(org.junit.Test)

Example 3 with Recommendation

use of com.google.cloud.recommender.v1beta1.Recommendation in project java-recommender by googleapis.

the class ListRecommendations method listRecommendations.

// List recommendations for a specified project, location, and recommender
public static void listRecommendations(String projectId, String location, String recommender) throws IOException {
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RecommenderClient recommenderClient = RecommenderClient.create()) {
        // / Build the request
        String parent = String.format("projects/%s/locations/%s/recommenders/%s", projectId, location, recommender);
        ListRecommendationsRequest request = ListRecommendationsRequest.newBuilder().setParent(parent).build();
        try {
            // Send the request
            ListRecommendationsPagedResponse response = recommenderClient.listRecommendations(request);
            // Print out each recommendation
            for (Recommendation responseItem : response.iterateAll()) {
                Recommendation recommendation = responseItem;
                System.out.println("Recommendation name: " + recommendation.getName());
                System.out.println("- description: " + recommendation.getDescription());
                System.out.println("- primary_impact.category: " + recommendation.getPrimaryImpact().getCategory());
                System.out.println("- state_info.state: " + recommendation.getStateInfo().getState());
                System.out.println();
            }
            // Indicate the request was successful
            System.out.println("List recommendations successful");
        } catch (PermissionDeniedException e) {
            System.out.println("Permission denied for project '" + projectId + "'. Ensure you have the appropriate permissions to list recommendations: \n" + e.toString());
        } catch (InvalidArgumentException e) {
            System.out.println(("Invalid argument for projectId. Ensure you have 'GOOGLE_CLOUD_PROJECT' set: \n" + e.toString()));
        }
    }
}
Also used : InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) RecommenderClient(com.google.cloud.recommender.v1beta1.RecommenderClient) PermissionDeniedException(com.google.api.gax.rpc.PermissionDeniedException) ListRecommendationsRequest(com.google.cloud.recommender.v1beta1.ListRecommendationsRequest) ListRecommendationsPagedResponse(com.google.cloud.recommender.v1beta1.RecommenderClient.ListRecommendationsPagedResponse) Recommendation(com.google.cloud.recommender.v1beta1.Recommendation)

Example 4 with Recommendation

use of com.google.cloud.recommender.v1beta1.Recommendation in project java-recommender by googleapis.

the class ITSystemTest method listRecommendationsTest.

@Test
public void listRecommendationsTest() {
    ListRecommendationsRequest request = ListRecommendationsRequest.newBuilder().setParent(FORMATTED_PARENT).setFilter("").build();
    List<Recommendation> recommendations = Lists.newArrayList(client.listRecommendations(request).iterateAll());
    assertThat(recommendations.size() > 0).isTrue();
    assertThat(recommendations.contains(null)).isFalse();
}
Also used : ListRecommendationsRequest(com.google.cloud.recommender.v1beta1.ListRecommendationsRequest) Recommendation(com.google.cloud.recommender.v1beta1.Recommendation) Test(org.junit.Test)

Aggregations

ListRecommendationsPagedResponse (com.google.cloud.recommender.v1beta1.RecommenderClient.ListRecommendationsPagedResponse)3 Test (org.junit.Test)3 ListRecommendationsRequest (com.google.cloud.recommender.v1beta1.ListRecommendationsRequest)2 Recommendation (com.google.cloud.recommender.v1beta1.Recommendation)2 AbstractMessage (com.google.protobuf.AbstractMessage)2 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)1 PermissionDeniedException (com.google.api.gax.rpc.PermissionDeniedException)1 RecommenderClient (com.google.cloud.recommender.v1beta1.RecommenderClient)1