use of com.google.cloud.recommender.v1beta1.ListRecommendationsRequest 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()));
}
use of com.google.cloud.recommender.v1beta1.ListRecommendationsRequest 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()));
}
use of com.google.cloud.recommender.v1beta1.ListRecommendationsRequest 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()));
}
}
}
use of com.google.cloud.recommender.v1beta1.ListRecommendationsRequest in project java-recommender by googleapis.
the class ITSystemTest method listRecommendationsExceptionTest.
@Test(expected = InvalidArgumentException.class)
public void listRecommendationsExceptionTest() {
String filter = "invalid-filter";
ListRecommendationsRequest request = ListRecommendationsRequest.newBuilder().setParent(FORMATTED_PARENT).setFilter(filter).build();
client.listRecommendations(request);
}
use of com.google.cloud.recommender.v1beta1.ListRecommendationsRequest 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();
}
Aggregations