Skip to content

Commit

Permalink
api for getting a list of customer name matches
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanoy committed Jun 23, 2020
1 parent 57f6a6b commit d2b3e27
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,16 @@ public List<Engagement> findByModifiedAndAction(FileAction action) {
public List<Engagement> findByModified() {
return find("action is not null").list();
}

/**
* A case insensitive string to match against customer names.
* @param input
* @return
*/
public List<Engagement> findCustomerSuggestions(String input) {
String queryInput = String.format("(?i)%s", input);

return find("customerName like ?1", queryInput).list();
}

}
17 changes: 17 additions & 0 deletions src/main/java/com/redhat/labs/omp/resource/EngagementResource.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
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;
import javax.ws.rs.PUT;
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;
Expand Down Expand Up @@ -122,6 +125,20 @@ public Engagement get(@PathParam("customerName") String customerName,
public List<Engagement> 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<String> customerSuggestions = engagementService.getSuggestions(match);

return Response.ok(customerSuggestions).build();
}

@PUT
@Path("/launch")
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/com/redhat/labs/omp/service/EngagementService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -68,6 +71,7 @@ public class EngagementService {
* @param engagement
* @return
*/

public Engagement create(Engagement engagement) {

// check if engagement exists
Expand Down Expand Up @@ -182,6 +186,20 @@ public Optional<Engagement> get(String customerName, String projectName) {
public List<Engagement> 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<String> getSuggestions(String subString) {
List<Engagement> allEngagements = repository.findCustomerSuggestions(subString);

Set<String> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Long> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit d2b3e27

Please sign in to comment.