From d2b3e271c81e6bf68e575aafb1b77a057d29993e Mon Sep 17 00:00:00 2001 From: Kevin McAnoy Date: Tue, 23 Jun 2020 11:36:45 -0700 Subject: [PATCH] api for getting a list of customer name matches --- .../omp/repository/EngagementRepository.java | 11 ++ .../labs/omp/resource/EngagementResource.java | 17 +++ .../labs/omp/service/EngagementService.java | 18 +++ .../omp/resources/CustomerSuggestionTest.java | 139 ++++++++++++++++++ .../omp/resources/EngagementResourceTest.java | 2 +- 5 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/redhat/labs/omp/resources/CustomerSuggestionTest.java diff --git a/src/main/java/com/redhat/labs/omp/repository/EngagementRepository.java b/src/main/java/com/redhat/labs/omp/repository/EngagementRepository.java index 73316145..b3440ca8 100644 --- a/src/main/java/com/redhat/labs/omp/repository/EngagementRepository.java +++ b/src/main/java/com/redhat/labs/omp/repository/EngagementRepository.java @@ -27,5 +27,16 @@ public List findByModifiedAndAction(FileAction action) { public List findByModified() { return find("action is not null").list(); } + + /** + * A case insensitive string to match against customer names. + * @param input + * @return + */ + public List findCustomerSuggestions(String input) { + String queryInput = String.format("(?i)%s", input); + + return find("customerName like ?1", queryInput).list(); + } } diff --git a/src/main/java/com/redhat/labs/omp/resource/EngagementResource.java b/src/main/java/com/redhat/labs/omp/resource/EngagementResource.java index d325fbc6..c5a75f12 100644 --- a/src/main/java/com/redhat/labs/omp/resource/EngagementResource.java +++ b/src/main/java/com/redhat/labs/omp/resource/EngagementResource.java @@ -1,11 +1,13 @@ package com.redhat.labs.omp.resource; +import java.util.Collection; import java.util.List; import java.util.Optional; import javax.enterprise.context.RequestScoped; import javax.inject.Inject; import javax.validation.Valid; +import javax.validation.constraints.NotBlank; import javax.ws.rs.Consumes; import javax.ws.rs.GET; import javax.ws.rs.POST; @@ -13,6 +15,7 @@ import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; import javax.ws.rs.core.Context; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -122,6 +125,20 @@ public Engagement get(@PathParam("customerName") String customerName, public List getAll() { return engagementService.getAll(); } + + @GET + @Path("/customers/suggest") + @SecurityRequirement(name = "jwt", scopes = {}) + @APIResponses(value = { + @APIResponse(responseCode = "401", description = "Missing or Invalid JWT"), + @APIResponse(responseCode = "200", description = "Customer data has been returned.") }) + @Operation(summary = "Returns customers list") + public Response findCustomers(@NotBlank @QueryParam("suggest") String match) { + + Collection customerSuggestions = engagementService.getSuggestions(match); + + return Response.ok(customerSuggestions).build(); + } @PUT @Path("/launch") diff --git a/src/main/java/com/redhat/labs/omp/service/EngagementService.java b/src/main/java/com/redhat/labs/omp/service/EngagementService.java index 7f2e4d79..0b7f98a3 100644 --- a/src/main/java/com/redhat/labs/omp/service/EngagementService.java +++ b/src/main/java/com/redhat/labs/omp/service/EngagementService.java @@ -1,8 +1,11 @@ package com.redhat.labs.omp.service; import java.time.LocalDateTime; +import java.util.Collection; import java.util.List; import java.util.Optional; +import java.util.Set; +import java.util.TreeSet; import javax.enterprise.context.ApplicationScoped; import javax.inject.Inject; @@ -68,6 +71,7 @@ public class EngagementService { * @param engagement * @return */ + public Engagement create(Engagement engagement) { // check if engagement exists @@ -182,6 +186,20 @@ public Optional get(String customerName, String projectName) { public List getAll() { return repository.listAll(); } + + /** + * Returns a {@link List} of all customer names in the data store that match the input + * @param subString - A string to match the customer name on. Can be all or part. case-insensitive + * @return a {@link List} of all customer names in the data store that match the input + */ + public Collection getSuggestions(String subString) { + List allEngagements = repository.findCustomerSuggestions(subString); + + Set customers = new TreeSet<>(); + allEngagements.stream().forEach(engagement -> customers.add(engagement.getCustomerName())); + + return customers; + } /** * Used by the {@link GitSyncService} to delete all {@link Engagement} from the diff --git a/src/test/java/com/redhat/labs/omp/resources/CustomerSuggestionTest.java b/src/test/java/com/redhat/labs/omp/resources/CustomerSuggestionTest.java new file mode 100644 index 00000000..a6fb03c7 --- /dev/null +++ b/src/test/java/com/redhat/labs/omp/resources/CustomerSuggestionTest.java @@ -0,0 +1,139 @@ +package com.redhat.labs.omp.resources; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.containsString; + +import java.util.HashMap; + +import javax.inject.Inject; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import com.redhat.labs.omp.model.Engagement; +import com.redhat.labs.omp.service.EngagementService; +import com.redhat.labs.utils.EmbeddedMongoTest; +import com.redhat.labs.utils.TokenUtils; + +import io.quarkus.test.junit.QuarkusTest; + +@EmbeddedMongoTest +@QuarkusTest +class CustomerSuggestionTest { + private static final String ANSWER = "Red Hat"; + private static final String SUGGESTION_URL = "/engagements/customers/suggest"; + + @Inject + EngagementService engagementService; + + String token; + + @BeforeEach + void setUp() throws Exception { + HashMap timeClaims = new HashMap<>(); + token = TokenUtils.generateTokenString("/JwtClaimsWriter.json", timeClaims); + + Engagement engagement = new EngagementResourceTest().mockEngagement(); + engagement.setCustomerName(ANSWER); + engagementService.create(engagement); + } + + @Test void testSuggestionsExactSuccess() throws Exception { + + given() + .when() + .auth().oauth2(token).queryParam("suggest", ANSWER).get(SUGGESTION_URL) + .then() + .statusCode(200).body(containsString(ANSWER)); + } + + @Test void testSuggestionsNoSuggestionFail() throws Exception { + + given() + .when() + .auth().oauth2(token) + .then() + .statusCode(400); + } + + @Test void testSuggestionsStartLowerSuccess() throws Exception { + + given() + .when() + .auth().oauth2(token).queryParam("suggest", "red").get(SUGGESTION_URL) + .then() + .statusCode(200).body(containsString(ANSWER)); + } + + @Test void testSuggestionsStartUpperSuccess() throws Exception { + + given() + .when() + .auth().oauth2(token).queryParam("suggest", "Red").get(SUGGESTION_URL) + .then() + .statusCode(200).body(containsString(ANSWER)); + } + + @Test void testSuggestionStartLowerThanUpperSuccess() throws Exception { + + given() + .when() + .auth().oauth2(token).queryParam("suggest", "rED").get(SUGGESTION_URL) + .then() + .statusCode(200).body(containsString(ANSWER)); + } + + @Test void testSuggestionsEndLowerSuccess() throws Exception { + + given() + .when() + .auth().oauth2(token).queryParam("suggest", "hat").get(SUGGESTION_URL) + .then() + .statusCode(200).body(containsString(ANSWER)); + } + + @Test void testSuggestionsEndUpperSuccess() throws Exception { + + given() + .when() + .auth().oauth2(token).queryParam("suggest", "haT").get(SUGGESTION_URL) + .then() + .statusCode(200).body(containsString(ANSWER)); + } + + @Test void testSuggestionEndLowerThanUpperSuccess() throws Exception { + + given() + .when() + .auth().oauth2(token).queryParam("suggest", "hAT").get(SUGGESTION_URL) + .then() + .statusCode(200).body(containsString(ANSWER)); + } + + @Test void testSuggestionsMidLowerSuccess() throws Exception { + + given() + .when() + .auth().oauth2(token).queryParam("suggest", "ed").get(SUGGESTION_URL) + .then() + .statusCode(200).body(containsString(ANSWER)); + } + + @Test void testSuggestionsMidUpperSuccess() throws Exception { + + given() + .when() + .auth().oauth2(token).queryParam("suggest", "ED").get(SUGGESTION_URL) + .then() + .statusCode(200).body(containsString(ANSWER)); + } + + @Test void testSuggestionMidLowerThanUpperSuccess() throws Exception { + + given() + .when() + .auth().oauth2(token).queryParam("suggest", "eD").get(SUGGESTION_URL) + .then() + .statusCode(200).body(containsString(ANSWER)); + } +} diff --git a/src/test/java/com/redhat/labs/omp/resources/EngagementResourceTest.java b/src/test/java/com/redhat/labs/omp/resources/EngagementResourceTest.java index 13f3299a..0184fe2e 100644 --- a/src/test/java/com/redhat/labs/omp/resources/EngagementResourceTest.java +++ b/src/test/java/com/redhat/labs/omp/resources/EngagementResourceTest.java @@ -826,7 +826,7 @@ public void testLaunchEngagementWithAuthAndRoleEngagementNotFound() throws Excep } - private Engagement mockEngagement() { + public Engagement mockEngagement() { Engagement engagement = Engagement.builder().customerName("TestCustomer").projectName("TestProject") .description("Test Description").location("Raleigh, NC").startDate("20170501").endDate("20170708")