Skip to content

Commit

Permalink
refactor: rename findById to findByIdAndStatus and add status parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul-tyagi08 committed Dec 6, 2024
1 parent 7c3e9dd commit d1f0f2e
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.gravitee.apim.core.subscription.use_case.CloseSubscriptionUseCase;
import io.gravitee.common.data.domain.Page;
import io.gravitee.common.http.MediaType;
import io.gravitee.repository.management.model.ApplicationStatus;
import io.gravitee.rest.api.management.v2.rest.mapper.*;
import io.gravitee.rest.api.management.v2.rest.model.*;
import io.gravitee.rest.api.management.v2.rest.model.Error;
Expand Down Expand Up @@ -496,7 +497,7 @@ private void expandData(List<Subscription> subscriptions, Set<String> expands) {
.map(subscription -> (subscription.getApplication()).getId())
.collect(Collectors.toSet());
final Collection<BaseApplication> applications = applicationMapper.mapToBaseApplicationList(
applicationService.findByIds(executionContext, applicationIds)
applicationService.findByIdsAndStatus(executionContext, applicationIds, ApplicationStatus.ACTIVE)
);
applications.forEach(application ->
subscriptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import fixtures.PlanFixtures;
import fixtures.SubscriptionFixtures;
import fixtures.UserFixtures;
import io.gravitee.repository.management.model.ApplicationStatus;
import io.gravitee.rest.api.management.v2.rest.model.Error;
import io.gravitee.rest.api.management.v2.rest.model.Subscription;
import io.gravitee.rest.api.model.SubscriptionEntity;
Expand Down Expand Up @@ -139,9 +140,10 @@ public void should_return_subscription_with_expands() {
.thenReturn(Set.of(PlanFixtures.aPlanEntityV4().toBuilder().id(PLAN).build()));

when(
applicationService.findByIds(
applicationService.findByIdsAndStatus(
eq(GraviteeContext.getExecutionContext()),
argThat(argument -> List.of(APPLICATION).containsAll(argument))
argThat(argument -> List.of(APPLICATION).containsAll(argument)),
ApplicationStatus.ACTIVE
)
)
.thenReturn(Set.of(ApplicationFixtures.anApplicationListItem().toBuilder().id(APPLICATION).build()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import fixtures.PlanFixtures;
import fixtures.SubscriptionFixtures;
import io.gravitee.common.data.domain.Page;
import io.gravitee.repository.management.model.ApplicationStatus;
import io.gravitee.rest.api.management.v2.rest.model.Error;
import io.gravitee.rest.api.management.v2.rest.model.Links;
import io.gravitee.rest.api.management.v2.rest.model.Pagination;
Expand Down Expand Up @@ -211,9 +212,10 @@ public void should_return_list_of_subscriptions_with_expands() {
)
);
when(
applicationService.findByIds(
applicationService.findByIdsAndStatus(
eq(GraviteeContext.getExecutionContext()),
argThat(argument -> List.of("application-1", "application-2").containsAll(argument))
argThat(argument -> List.of("application-1", "application-2").containsAll(argument)),
ApplicationStatus.ACTIVE
)
)
.thenReturn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import io.gravitee.common.http.MediaType;
import io.gravitee.repository.management.api.search.Order;
import io.gravitee.repository.management.model.ApplicationStatus;
import io.gravitee.rest.api.model.ApiKeyMode;
import io.gravitee.rest.api.model.ApplicationEntity;
import io.gravitee.rest.api.model.NewApplicationEntity;
Expand Down Expand Up @@ -242,7 +243,11 @@ protected List<Application> transformPageContent(final ExecutionContext executio
return Collections.emptyList();
}

Set<ApplicationListItem> applicationListItems = applicationService.findByIds(executionContext, pageContent);
Set<ApplicationListItem> applicationListItems = applicationService.findByIdsAndStatus(
executionContext,
pageContent,
ApplicationStatus.ACTIVE
);
Comparator<String> orderingComparator = Comparator.comparingInt(pageContent::indexOf);

return applicationListItems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import io.gravitee.common.http.HttpStatusCode;
import io.gravitee.repository.management.api.search.Order;
import io.gravitee.repository.management.model.ApplicationStatus;
import io.gravitee.rest.api.model.*;
import io.gravitee.rest.api.model.application.ApplicationListItem;
import io.gravitee.rest.api.model.application.ApplicationSettings;
Expand All @@ -37,7 +38,6 @@
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.Response;
import java.util.*;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
Expand Down Expand Up @@ -73,14 +73,14 @@ public void init() {
.findIdsByUser(eq(GraviteeContext.getExecutionContext()), any(), any());
doReturn(new HashSet<>(Arrays.asList(applicationA, applicationB)))
.when(applicationService)
.findByIds(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("A", "B")));
.findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("A", "B")), ApplicationStatus.ACTIVE);
doReturn(new HashSet<>(Arrays.asList(applicationB, applicationA)))
.when(applicationService)
.findByIds(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("B", "A")));
.findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("B", "A")), ApplicationStatus.ACTIVE);

doReturn(new HashSet<>(Arrays.asList(applicationB)))
.when(applicationService)
.findByIds(eq(GraviteeContext.getExecutionContext()), eq(List.of("B")));
.findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(List.of("B")), ApplicationStatus.ACTIVE);

doReturn(new Application().id("A").name("A"))
.when(applicationMapper)
Expand Down Expand Up @@ -149,7 +149,7 @@ public void shouldGetApplicationsOrderByName() {
.findIdsByUser(eq(GraviteeContext.getExecutionContext()), any(), eq(sort));
doReturn(mockApplications)
.when(applicationService)
.findByIds(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("A", "B", "C", "D")));
.findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("A", "B", "C", "D")), ApplicationStatus.ACTIVE);

doReturn(new Application().id("A").name("A"))
.when(applicationMapper)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
public interface ApplicationService {
ApplicationEntity findById(final ExecutionContext executionContext, String applicationId);

Set<ApplicationListItem> findByIds(final ExecutionContext executionContext, Collection<String> applicationIds);
Set<ApplicationListItem> findByIdsAndStatus(
final ExecutionContext executionContext,
Collection<String> applicationIds,
ApplicationStatus applicationStatus
);

default Set<ApplicationListItem> findByUser(final ExecutionContext executionContext, String username) {
return findByUser(executionContext, username, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import io.gravitee.alert.api.trigger.Trigger;
import io.gravitee.common.event.Event;
import io.gravitee.notifier.api.Notification;
import io.gravitee.repository.management.api.AlertTriggerRepository;
import io.gravitee.repository.management.model.ApplicationStatus;
import io.gravitee.rest.api.model.ApplicationEntity;
import io.gravitee.rest.api.model.MembershipEntity;
import io.gravitee.rest.api.model.MembershipReferenceType;
Expand All @@ -51,7 +51,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -320,7 +319,7 @@ private void updateAlertsRecipients(ApplicationAlertMembershipEvent alertMembers

// get recipients for each application
final Map<String, List<String>> recipientsByApplicationId = applicationService
.findByIds(executionContext, new ArrayList<>(applicationIds))
.findByIdsAndStatus(executionContext, new ArrayList<>(applicationIds), ApplicationStatus.ACTIVE)
.stream()
.collect(
Collectors.toMap(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,11 @@ public ApplicationEntity findById(final ExecutionContext executionContext, Strin
}

@Override
public Set<ApplicationListItem> findByIds(final ExecutionContext executionContext, Collection<String> applicationIds) {
public Set<ApplicationListItem> findByIdsAndStatus(
final ExecutionContext executionContext,
Collection<String> applicationIds,
ApplicationStatus applicationStatus
) {
try {
LOGGER.debug("Find application by IDs: {}", applicationIds);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.gravitee.repository.management.api.SubscriptionRepository;
import io.gravitee.repository.management.api.search.SubscriptionCriteria;
import io.gravitee.repository.management.model.Api;
import io.gravitee.repository.management.model.ApplicationStatus;
import io.gravitee.repository.management.model.Audit;
import io.gravitee.repository.management.model.Subscription;
import io.gravitee.rest.api.model.*;
Expand Down Expand Up @@ -322,7 +323,7 @@ private Set<String> getIndirectMemberIds(ExecutionContext context, List<String>

// get all indirect members
List<String> applicationsGroups = applicationService
.findByIds(context, applicationIds)
.findByIdsAndStatus(context, applicationIds, ApplicationStatus.ACTIVE)
.stream()
.filter(application -> application.getGroups() != null)
.flatMap((ApplicationListItem applicationListItem) -> applicationListItem.getGroups().stream())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1595,7 +1595,11 @@ public Metadata getMetadata(ExecutionContext executionContext, SubscriptionMetad
.map(withApplications -> {
Set<String> appIds = subscriptions.stream().map(SubscriptionEntity::getApplication).collect(toSet());
return applicationService
.findByIds(new ExecutionContext(query.getOrganization(), query.getEnvironment()), appIds)
.findByIdsAndStatus(
new ExecutionContext(query.getOrganization(), query.getEnvironment()),
appIds,
ApplicationStatus.ACTIVE
)
.stream()
.collect(toMap(ApplicationListItem::getId, Function.identity()));
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
* @author GraviteeSource Team
*/
@RunWith(MockitoJUnitRunner.class)
public class ApplicationService_FindByIdsTest {
public class ApplicationService_FindByIdsAndStatusTest {

private static final List<String> APPLICATION_IDS = Arrays.asList("id-app-1", "id-app-2");

Expand Down Expand Up @@ -114,7 +114,7 @@ public void tearDown() {
}

@Test
public void shouldFindByIds() throws TechnicalException {
public void shouldFindByIdsAndStatus() throws TechnicalException {
ExecutionContext executionContext = GraviteeContext.getExecutionContext();
ApplicationCriteria criteria = new ApplicationCriteria.Builder()
.ids(Sets.newHashSet(APPLICATION_IDS))
Expand All @@ -124,14 +124,18 @@ public void shouldFindByIds() throws TechnicalException {
doReturn(new Page(Arrays.asList(app1, app2), 1, 2, 2)).when(applicationRepository).search(criteria, null);
doReturn(2).when(primaryOwners).size();

final Set<ApplicationListItem> applications = applicationService.findByIds(executionContext, APPLICATION_IDS);
final Set<ApplicationListItem> applications = applicationService.findByIdsAndStatus(
executionContext,
APPLICATION_IDS,
ApplicationStatus.ACTIVE
);

assertNotNull(applications);
assertEquals(APPLICATION_IDS, applications.stream().map(ApplicationListItem::getId).collect(Collectors.toList()));
}

@Test
public void shouldFindByIdsWithDuplicatedIds() throws TechnicalException {
public void shouldFindByIdsAndStatusWithDuplicatedIdsAndStatus() throws TechnicalException {
ExecutionContext executionContext = GraviteeContext.getExecutionContext();
ApplicationCriteria criteria = new ApplicationCriteria.Builder()
.ids(Sets.newHashSet(APPLICATION_IDS))
Expand All @@ -142,17 +146,18 @@ public void shouldFindByIdsWithDuplicatedIds() throws TechnicalException {
doReturn(new Page<>(Arrays.asList(app1, app2), 1, 2, 2)).when(applicationRepository).search(criteria, null);
doReturn(2).when(primaryOwners).size();

final Set<ApplicationListItem> applications = applicationService.findByIds(
final Set<ApplicationListItem> applications = applicationService.findByIdsAndStatus(
executionContext,
List.of("id-app-1", "id-app-1", "id-app-2")
List.of("id-app-1", "id-app-1", "id-app-2"),
ApplicationStatus.ACTIVE
);

assertNotNull(applications);
assertEquals(APPLICATION_IDS, applications.stream().map(ApplicationListItem::getId).collect(Collectors.toList()));
}

@Test
public void shouldFindByIdsWithNoEnvironmentCriteria() throws TechnicalException {
public void shouldFindByIdsAndStatusWithNoEnvironmentCriteria() throws TechnicalException {
ExecutionContext executionContext = new ExecutionContext("DEFAULT", null);
ApplicationCriteria criteria = new ApplicationCriteria.Builder()
.ids(Sets.newHashSet(APPLICATION_IDS))
Expand All @@ -161,17 +166,25 @@ public void shouldFindByIdsWithNoEnvironmentCriteria() throws TechnicalException
doReturn(new Page(Arrays.asList(app1, app2), 1, 2, 2)).when(applicationRepository).search(criteria, null);
doReturn(2).when(primaryOwners).size();

final Set<ApplicationListItem> applications = applicationService.findByIds(executionContext, APPLICATION_IDS);
final Set<ApplicationListItem> applications = applicationService.findByIdsAndStatus(
executionContext,
APPLICATION_IDS,
ApplicationStatus.ACTIVE
);

assertNotNull(applications);
assertEquals(APPLICATION_IDS, applications.stream().map(ApplicationListItem::getId).collect(Collectors.toList()));
}

@Test
public void shouldFindByIdsWithEmptySet() throws TechnicalException {
public void shouldFindByIdsAndStatusWithEmptySet() throws TechnicalException {
ExecutionContext executionContext = GraviteeContext.getExecutionContext();

final Set<ApplicationListItem> applications = applicationService.findByIds(executionContext, Collections.emptySet());
final Set<ApplicationListItem> applications = applicationService.findByIdsAndStatus(
executionContext,
Collections.emptySet(),
ApplicationStatus.ACTIVE
);

assertNotNull(applications);
assertTrue(applications.isEmpty());
Expand All @@ -188,7 +201,11 @@ public void shouldThrowsIfNoPrimaryOwner() throws TechnicalException {
.build();
doReturn(new Page(Arrays.asList(app1, app2), 1, 2, 2)).when(applicationRepository).search(criteria, null);

final Set<ApplicationListItem> applications = applicationService.findByIds(GraviteeContext.getExecutionContext(), APPLICATION_IDS);
final Set<ApplicationListItem> applications = applicationService.findByIdsAndStatus(
GraviteeContext.getExecutionContext(),
APPLICATION_IDS,
ApplicationStatus.ACTIVE
);

assertNotNull(applications);
assertEquals(APPLICATION_IDS, applications.stream().map(ApplicationListItem::getId).collect(Collectors.toList()));
Expand All @@ -197,6 +214,6 @@ public void shouldThrowsIfNoPrimaryOwner() throws TechnicalException {
@Test(expected = TechnicalManagementException.class)
public void shouldThrowTechnicalManagementException() throws TechnicalException {
when(applicationRepository.search(any(), any())).thenThrow(new TechnicalException());
applicationService.findByIds(GraviteeContext.getExecutionContext(), APPLICATION_IDS);
applicationService.findByIdsAndStatus(GraviteeContext.getExecutionContext(), APPLICATION_IDS, ApplicationStatus.ACTIVE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import io.gravitee.repository.management.api.SubscriptionRepository;
import io.gravitee.repository.management.model.Api;
import io.gravitee.repository.management.model.Application;
import io.gravitee.repository.management.model.ApplicationStatus;
import io.gravitee.repository.management.model.Subscription;
import io.gravitee.rest.api.model.*;
import io.gravitee.rest.api.model.application.ApplicationListItem;
Expand Down Expand Up @@ -253,7 +254,10 @@ public void shouldGetApiConsumersWithGroups() throws TechnicalException {
ApplicationListItem appListItem = new ApplicationListItem();
appListItem.setId(app.getId());
appListItem.setGroups(app.getGroups());
when(mockApplicationService.findByIds(GraviteeContext.getExecutionContext(), List.of(app.getId()))).thenReturn(Set.of(appListItem));
when(
mockApplicationService.findByIdsAndStatus(GraviteeContext.getExecutionContext(), List.of(app.getId()), ApplicationStatus.ACTIVE)
)
.thenReturn(Set.of(appListItem));

MembershipEntity membershipGroup = new MembershipEntity();
membershipGroup.setId("membership-group-id");
Expand Down
Loading

0 comments on commit d1f0f2e

Please sign in to comment.