Skip to content

Commit a1c6d49

Browse files
committed
Add region filter for use case and activity
1 parent d72fe0c commit a1c6d49

File tree

7 files changed

+77
-41
lines changed

7 files changed

+77
-41
lines changed

src/main/java/com/redhat/labs/lodestar/model/filter/ListFilterOptions.java

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.redhat.labs.lodestar.model.filter;
22

3-
import java.util.List;
4-
import java.util.Optional;
3+
import java.util.*;
54
import java.util.stream.Collectors;
65
import java.util.stream.Stream;
76

@@ -157,4 +156,25 @@ public Integer getNextPage() {
157156
return this.page == null ? null : this.page + 1;
158157
}
159158

159+
public Set<String> getV2Regions() {
160+
Set<String> region = new HashSet<>();
161+
162+
if(search == null) {
163+
return region;
164+
}
165+
166+
String[] params = search.split("&");
167+
168+
for (String param : params) {
169+
String[] keyValues = param.split("=");
170+
171+
if (keyValues[0].equals("engagement_region")) {
172+
String[] regionsArray = keyValues[1].split(",");
173+
Collections.addAll(region, regionsArray);
174+
}
175+
}
176+
177+
return region;
178+
}
179+
160180
}

src/main/java/com/redhat/labs/lodestar/rest/client/ActivityApiClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public interface ActivityApiClient {
3737

3838
@GET
3939
@Path("/latest")
40-
List<Commit> getLatestActivity(@QueryParam("page") int page, @QueryParam("pageSize") int pageSize);
40+
List<String> getLatestActivity(@QueryParam("page") int page, @QueryParam("pageSize") int pageSize, @QueryParam("regions") Set<String> regions);
4141

4242
@GET
4343
@Path("/uuid/{engagementUuid}")

src/main/java/com/redhat/labs/lodestar/rest/client/UseCaseApiClient.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import javax.ws.rs.*;
1010
import javax.ws.rs.core.*;
1111
import java.util.List;
12+
import java.util.Set;
1213

1314
@ApplicationScoped
1415
@RegisterRestClient(configKey = "lodestar.engagements.api")
@@ -17,7 +18,7 @@
1718
public interface UseCaseApiClient {
1819

1920
@GET
20-
Response getUseCases(@QueryParam("page") int page, @QueryParam("pageSize") int pageSize);//TODO sort????
21+
Response getUseCases(@QueryParam("page") int page, @QueryParam("pageSize") int pageSize, @QueryParam("regions") Set<String> regions);//TODO sort????
2122

2223
@GET
2324
@Path("{uuid}")

src/main/java/com/redhat/labs/lodestar/service/ActivityService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public Instant getLatestActivity(String engagementUuid) {
4747
* @param pageSize page size
4848
* @return A list of the last activity (commit) for each engagement
4949
*/
50-
public List<Commit> getLatestActivity(int page, int pageSize) {
51-
return activityApiClient.getLatestActivity(page, pageSize);
50+
public List<String> getLatestActivity(int page, int pageSize, Set<String> regions) {
51+
return activityApiClient.getLatestActivity(page, pageSize, regions);
5252
}
5353

5454
public Response getPaginatedActivityForUuid(String engagementUuid, int page, int pageSize) {

src/main/java/com/redhat/labs/lodestar/service/EngagementService.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import javax.enterprise.context.ApplicationScoped;
2121
import javax.inject.Inject;
2222
import javax.ws.rs.WebApplicationException;
23+
import javax.ws.rs.core.GenericType;
2324
import javax.ws.rs.core.Response;
2425
import java.time.Instant;
2526
import java.util.*;
@@ -81,6 +82,7 @@ public void flushCache() { //TODO
8182
//TODO cache this - btw - this caching will probably only work in a single pod sitch.
8283
// could continue to leverage the db or use distribute cache (back to Alpha!)
8384
public Engagement getEngagement(String uuid) {
85+
LOGGER.debug("getting uuid {}", uuid);
8486
Engagement engagement = engagementApiClient.getEngagement(uuid);
8587
List<HostingEnvironment> hes = hostingEnvironmentService.getHostingEnvironments(uuid);
8688
List<Artifact> artifacts = artifactService.getArtifacts(uuid);
@@ -242,7 +244,7 @@ private void nullToEmpty(Engagement engagement) {
242244
/**
243245
* Returns Optional containing {@link Engagement} if found with given subdomain.
244246
*
245-
* @param subdomain
247+
* @param subdomain check this
246248
* @return
247249
*/
248250
public Response getBySubdomain(String engagementUuid, String subdomain) {
@@ -264,14 +266,6 @@ public void updateStatusAndCommits(Hook hook) {
264266

265267
Engagement engagement = getByCustomerAndProjectName(hook.getCustomerName(), hook.getEngagementName());
266268

267-
// refresh entire engagement if requested
268-
if (hook.containsAnyMessage(commitFilteredMessages)) {
269-
activityService.postHook(hook);
270-
getEngagement(engagement.getUuid());
271-
LOGGER.debug("hook triggered refresh of engagement for project {}", hook.getProjectId());
272-
return;
273-
}
274-
275269
// send update status event
276270
if (hook.didFileChange(statusFile)) {
277271
LOGGER.debug("Status update {}", hook.getProjectId());
@@ -288,6 +282,14 @@ public void updateStatusAndCommits(Hook hook) {
288282
//TODO should return the uuid from the post as header
289283
//TODO invalidate cache + load new into cache
290284
}
285+
286+
// refresh entire engagement if requested
287+
if (hook.containsAnyMessage(commitFilteredMessages)) {
288+
//TODO Need to call a single refresh of engagement in every service
289+
LOGGER.debug("hook triggered refresh of engagement for project {}", hook.getProjectId());
290+
}
291+
292+
getEngagement(engagement.getUuid());
291293
}
292294

293295

@@ -319,29 +321,20 @@ public Response getEngagementsPaged(ListFilterOptions listFilterOptions) {
319321
// we can just filter on engagement instead of hitting the activity service
320322
String sort = listFilterOptions.getSortFields().orElse("");
321323
int pageSize = listFilterOptions.getPerPage().orElse(5);
324+
322325
if(pageSize == 5 && sort.equals("last_update")) {
323-
List<Commit> activity = activityService.getLatestActivity(0,5);
324-
List<Engagement> engagements = activity.stream().map(a -> getEngagement(a.getEngagementUuid())).collect(Collectors.toList());
326+
List<String> activity = activityService.getLatestActivity(0,5, listFilterOptions.getV2Regions());
327+
List<Engagement> engagements = activity.stream().map(this::getEngagement).collect(Collectors.toList());
325328
return Response.ok(engagements).build();
326329
}
327330

328331
int page = listFilterOptions.getPage().orElse(1) - 1;
329332
pageSize = listFilterOptions.getPerPage().orElse(1000);
330333

331-
String search = listFilterOptions.getSearch().orElse("");
332-
String[] params = search.split("&");
333-
334-
Set<String> region = new HashSet<>();
335-
for (String param : params) {
336-
String[] keyValues = param.split("=");
337-
338-
if (keyValues[0].equals("engagement_region")) {
339-
String[] regionsArray = keyValues[1].split(",");
340-
Collections.addAll(region, regionsArray);
341-
}
342-
}
343-
344-
return engagementApiClient.getEngagements(page, pageSize, region);
334+
Response response = engagementApiClient.getEngagements(page, pageSize, listFilterOptions.getV2Regions());
335+
List<Engagement> engagements = response.readEntity(new GenericType<>(){});
336+
//engagements.forEach(e ->LOGGER.debug(" {} --- {} ", e.getProjectName(), e.getName() ));
337+
return Response.ok(engagements).header("x-total-engagements", response.getHeaderString("x-total-engagements")).build();
345338
}
346339

347340
/**
@@ -385,7 +378,7 @@ public Response getCategories(List<String> regions, ListFilterOptions filterOpti
385378
public Response getUseCases(ListFilterOptions filterOptions) {
386379
int page = filterOptions.getPage().orElse(1) -1;
387380
int pageSize = filterOptions.getPerPage().orElse(500);
388-
return useCaseApiClient.getUseCases(page, pageSize);
381+
return useCaseApiClient.getUseCases(page, pageSize, filterOptions.getV2Regions());
389382
}
390383

391384
/**

src/test/java/com/redhat/labs/lodestar/resource/StatusResourceTest.java

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
import com.redhat.labs.lodestar.model.Engagement;
44
import com.redhat.labs.lodestar.model.Hook;
55
import com.redhat.labs.lodestar.model.Status;
6-
import com.redhat.labs.lodestar.rest.client.ActivityApiClient;
7-
import com.redhat.labs.lodestar.rest.client.EngagementApiClient;
8-
import com.redhat.labs.lodestar.rest.client.EngagementStatusApiClient;
9-
import com.redhat.labs.lodestar.rest.client.StatusApiClient;
6+
import com.redhat.labs.lodestar.model.filter.ArtifactOptions;
7+
import com.redhat.labs.lodestar.rest.client.*;
108
import com.redhat.labs.lodestar.utils.ResourceLoader;
119
import io.quarkus.test.junit.QuarkusTest;
1210
import io.quarkus.test.junit.mockito.InjectMock;
@@ -20,6 +18,8 @@
2018
import javax.ws.rs.WebApplicationException;
2119
import javax.ws.rs.core.Response;
2220

21+
import java.util.Collections;
22+
2323
import static io.restassured.RestAssured.given;
2424
import static org.hamcrest.CoreMatchers.is;
2525

@@ -43,6 +43,22 @@ class StatusResourceTest {
4343
@RestClient
4444
ActivityApiClient activityApiClient;
4545

46+
@InjectMock
47+
@RestClient
48+
HostingEnvironmentApiClient hostingEnvironmentApiClient;
49+
50+
@InjectMock
51+
@RestClient
52+
ArtifactApiClient artifactApiClient;
53+
54+
@InjectMock
55+
@RestClient
56+
ParticipantApiClient participantApiClient;
57+
58+
@InjectMock
59+
@RestClient
60+
CategoryApiClient categoryApiClient;
61+
4662
@BeforeEach
4763
void setUp() {
4864
String customer = "jello";
@@ -51,6 +67,7 @@ void setUp() {
5167
Engagement engagement = Engagement.builder().uuid(uuid1).customerName(customer).projectName(exists).build();
5268

5369
Mockito.when(engagementApiClient.getEngagement(customer, exists)).thenReturn(engagement);
70+
Mockito.when(engagementApiClient.getEngagement(uuid1)).thenReturn(engagement);
5471
Mockito.when(engagementApiClient.getEngagement(customer, "doesnotexist")).thenThrow(
5572
new WebApplicationException(404)
5673
);
@@ -59,6 +76,9 @@ void setUp() {
5976
Mockito.when(engagementStatusApiClient.getEngagementStatus(uuid1)).thenReturn(status);
6077

6178
Mockito.when(engagementStatusApiClient.updateEngagementStatus(uuid1)).thenReturn(Response.ok().build());
79+
80+
Mockito.when(artifactApiClient.getArtifacts(Mockito.any(ArtifactOptions.class)))
81+
.thenReturn(javax.ws.rs.core.Response.ok(Collections.emptyList()).build());
6282
}
6383

6484
@Test
@@ -76,9 +96,10 @@ void testStatusValid() {
7696
.statusCode(200);
7797

7898
Mockito.verify(engagementApiClient).getEngagement("jello", "exists");
99+
Mockito.verify(engagementApiClient).getEngagement("uuid1");
79100
Mockito.verify(engagementStatusApiClient).updateEngagementStatus("uuid1");
80101
Mockito.verify(engagementStatusApiClient).getEngagementStatus("uuid1");
81-
Mockito.verify(activityApiClient, Mockito.times(0)).getActivityForUuid("uuid1");
102+
Mockito.verify(activityApiClient).getActivityForUuid("uuid1");
82103
}
83104

84105
@Test

src/test/java/com/redhat/labs/lodestar/resource/UseCaseResourceTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
import org.junit.jupiter.api.Test;
1212
import org.mockito.Mockito;
1313

14-
import java.util.Arrays;
14+
import java.util.Collections;
15+
import java.util.List;
1516

1617
import static io.restassured.RestAssured.given;
1718
import static org.hamcrest.CoreMatchers.containsString;
@@ -27,13 +28,13 @@ public class UseCaseResourceTest {
2728
UseCaseApiClient useCaseApiClient;
2829

2930
@Test
30-
void testGetUseCase() throws Exception {
31+
void testGetUseCase() {
3132

3233
UseCase useCase = UseCase.builder().title("useCase1").description("use case 1").order(0).build();
33-
javax.ws.rs.core.Response response = javax.ws.rs.core.Response.ok(Arrays.asList(useCase))
34+
javax.ws.rs.core.Response response = javax.ws.rs.core.Response.ok(List.of(useCase))
3435
.header("x-total-use-cases", 1).build();
3536

36-
Mockito.when(useCaseApiClient.getUseCases(0, 500)).thenReturn(response);
37+
Mockito.when(useCaseApiClient.getUseCases(0, 500, Collections.emptySet())).thenReturn(response);
3738
given()
3839
.auth()
3940
.oauth2(validToken)

0 commit comments

Comments
 (0)