From d1f0f2e084b31e76095c1f8cc25286d528f7a8dc Mon Sep 17 00:00:00 2001 From: mukul-tyagi08 Date: Fri, 6 Dec 2024 10:07:22 +0530 Subject: [PATCH 1/2] refactor: rename findById to findByIdAndStatus and add status parameter --- .../api/ApiSubscriptionsResource.java | 3 +- .../api/ApiSubscriptionsResource_GetTest.java | 6 ++- .../ApiSubscriptionsResource_ListTest.java | 6 ++- .../rest/resource/ApplicationsResource.java | 7 +++- .../resource/ApplicationsResourceTest.java | 10 ++--- .../rest/api/service/ApplicationService.java | 6 ++- .../impl/ApplicationAlertServiceImpl.java | 5 +-- .../service/impl/ApplicationServiceImpl.java | 6 ++- .../api/service/impl/MessageServiceImpl.java | 3 +- .../service/impl/SubscriptionServiceImpl.java | 6 ++- ...cationService_FindByIdsAndStatusTest.java} | 41 +++++++++++++------ .../MessageService_GetRecipientIdsTest.java | 6 ++- .../service/impl/SubscriptionServiceTest.java | 15 ++++--- 13 files changed, 84 insertions(+), 36 deletions(-) rename gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/{ApplicationService_FindByIdsTest.java => ApplicationService_FindByIdsAndStatusTest.java} (86%) diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource.java index 62df156368d..a3a1d7b0c2e 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource.java @@ -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; @@ -496,7 +497,7 @@ private void expandData(List subscriptions, Set expands) { .map(subscription -> (subscription.getApplication()).getId()) .collect(Collectors.toSet()); final Collection applications = applicationMapper.mapToBaseApplicationList( - applicationService.findByIds(executionContext, applicationIds) + applicationService.findByIdsAndStatus(executionContext, applicationIds, ApplicationStatus.ACTIVE) ); applications.forEach(application -> subscriptions diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_GetTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_GetTest.java index 736781f979d..6bf6adbd4b3 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_GetTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_GetTest.java @@ -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; @@ -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())); diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_ListTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_ListTest.java index 81ea3c2067c..08d34be7993 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_ListTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_ListTest.java @@ -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; @@ -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( diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/main/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResource.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/main/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResource.java index 58ee7d2b9cc..1bdf2fa6b8a 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/main/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResource.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/main/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResource.java @@ -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; @@ -242,7 +243,11 @@ protected List transformPageContent(final ExecutionContext executio return Collections.emptyList(); } - Set applicationListItems = applicationService.findByIds(executionContext, pageContent); + Set applicationListItems = applicationService.findByIdsAndStatus( + executionContext, + pageContent, + ApplicationStatus.ACTIVE + ); Comparator orderingComparator = Comparator.comparingInt(pageContent::indexOf); return applicationListItems diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/test/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResourceTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/test/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResourceTest.java index fa4666b6220..dd95df96450 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/test/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResourceTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/test/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResourceTest.java @@ -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; @@ -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; @@ -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) @@ -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) diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApplicationService.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApplicationService.java index e9e9e2c1a2f..fd0f1422d6c 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApplicationService.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApplicationService.java @@ -43,7 +43,11 @@ public interface ApplicationService { ApplicationEntity findById(final ExecutionContext executionContext, String applicationId); - Set findByIds(final ExecutionContext executionContext, Collection applicationIds); + Set findByIdsAndStatus( + final ExecutionContext executionContext, + Collection applicationIds, + ApplicationStatus applicationStatus + ); default Set findByUser(final ExecutionContext executionContext, String username) { return findByUser(executionContext, username, null); diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationAlertServiceImpl.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationAlertServiceImpl.java index 4ab88442c6a..04f240de47a 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationAlertServiceImpl.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationAlertServiceImpl.java @@ -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; @@ -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; @@ -320,7 +319,7 @@ private void updateAlertsRecipients(ApplicationAlertMembershipEvent alertMembers // get recipients for each application final Map> recipientsByApplicationId = applicationService - .findByIds(executionContext, new ArrayList<>(applicationIds)) + .findByIdsAndStatus(executionContext, new ArrayList<>(applicationIds), ApplicationStatus.ACTIVE) .stream() .collect( Collectors.toMap( diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationServiceImpl.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationServiceImpl.java index 622dbc9958d..56e940917a1 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationServiceImpl.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationServiceImpl.java @@ -233,7 +233,11 @@ public ApplicationEntity findById(final ExecutionContext executionContext, Strin } @Override - public Set findByIds(final ExecutionContext executionContext, Collection applicationIds) { + public Set findByIdsAndStatus( + final ExecutionContext executionContext, + Collection applicationIds, + ApplicationStatus applicationStatus + ) { try { LOGGER.debug("Find application by IDs: {}", applicationIds); diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/MessageServiceImpl.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/MessageServiceImpl.java index 40ed848f190..f16b3852611 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/MessageServiceImpl.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/MessageServiceImpl.java @@ -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.*; @@ -322,7 +323,7 @@ private Set getIndirectMemberIds(ExecutionContext context, List // get all indirect members List applicationsGroups = applicationService - .findByIds(context, applicationIds) + .findByIdsAndStatus(context, applicationIds, ApplicationStatus.ACTIVE) .stream() .filter(application -> application.getGroups() != null) .flatMap((ApplicationListItem applicationListItem) -> applicationListItem.getGroups().stream()) diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/SubscriptionServiceImpl.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/SubscriptionServiceImpl.java index a8791167cdc..0663b4562f7 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/SubscriptionServiceImpl.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/SubscriptionServiceImpl.java @@ -1595,7 +1595,11 @@ public Metadata getMetadata(ExecutionContext executionContext, SubscriptionMetad .map(withApplications -> { Set 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())); }); diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApplicationService_FindByIdsTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApplicationService_FindByIdsAndStatusTest.java similarity index 86% rename from gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApplicationService_FindByIdsTest.java rename to gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApplicationService_FindByIdsAndStatusTest.java index 161f1a3f7d8..6e93450df73 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApplicationService_FindByIdsTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApplicationService_FindByIdsAndStatusTest.java @@ -53,7 +53,7 @@ * @author GraviteeSource Team */ @RunWith(MockitoJUnitRunner.class) -public class ApplicationService_FindByIdsTest { +public class ApplicationService_FindByIdsAndStatusTest { private static final List APPLICATION_IDS = Arrays.asList("id-app-1", "id-app-2"); @@ -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)) @@ -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 applications = applicationService.findByIds(executionContext, APPLICATION_IDS); + final Set 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)) @@ -142,9 +146,10 @@ 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 applications = applicationService.findByIds( + final Set 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); @@ -152,7 +157,7 @@ public void shouldFindByIdsWithDuplicatedIds() throws TechnicalException { } @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)) @@ -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 applications = applicationService.findByIds(executionContext, APPLICATION_IDS); + final Set 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 applications = applicationService.findByIds(executionContext, Collections.emptySet()); + final Set applications = applicationService.findByIdsAndStatus( + executionContext, + Collections.emptySet(), + ApplicationStatus.ACTIVE + ); assertNotNull(applications); assertTrue(applications.isEmpty()); @@ -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 applications = applicationService.findByIds(GraviteeContext.getExecutionContext(), APPLICATION_IDS); + final Set applications = applicationService.findByIdsAndStatus( + GraviteeContext.getExecutionContext(), + APPLICATION_IDS, + ApplicationStatus.ACTIVE + ); assertNotNull(applications); assertEquals(APPLICATION_IDS, applications.stream().map(ApplicationListItem::getId).collect(Collectors.toList())); @@ -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); } } diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/MessageService_GetRecipientIdsTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/MessageService_GetRecipientIdsTest.java index cbb63d35cfc..ae4104cb4c4 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/MessageService_GetRecipientIdsTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/MessageService_GetRecipientIdsTest.java @@ -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; @@ -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"); diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/SubscriptionServiceTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/SubscriptionServiceTest.java index 720fd611289..9a8475ff629 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/SubscriptionServiceTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/SubscriptionServiceTest.java @@ -58,6 +58,7 @@ import io.gravitee.repository.management.api.SubscriptionRepository; import io.gravitee.repository.management.api.search.SubscriptionCriteria; import io.gravitee.repository.management.model.ApiKey; +import io.gravitee.repository.management.model.ApplicationStatus; import io.gravitee.repository.management.model.Subscription; import io.gravitee.rest.api.idp.api.authentication.UserDetails; import io.gravitee.rest.api.model.*; @@ -72,7 +73,6 @@ import io.gravitee.rest.api.model.v4.api.GenericApiEntity; import io.gravitee.rest.api.model.v4.plan.PlanMode; import io.gravitee.rest.api.service.ApiKeyService; -import io.gravitee.rest.api.service.ApiService; import io.gravitee.rest.api.service.ApplicationService; import io.gravitee.rest.api.service.AuditService; import io.gravitee.rest.api.service.GroupService; @@ -81,7 +81,6 @@ import io.gravitee.rest.api.service.ParameterService; import io.gravitee.rest.api.service.SubscriptionService; import io.gravitee.rest.api.service.UserService; -import io.gravitee.rest.api.service.common.ExecutionContext; import io.gravitee.rest.api.service.common.GraviteeContext; import io.gravitee.rest.api.service.exceptions.ApiKeyAlreadyExistingException; import io.gravitee.rest.api.service.exceptions.PlanAlreadyClosedException; @@ -1757,7 +1756,9 @@ public void shouldGetAllMetadataWithSubscriptions() { assertFalse(metadata.toMap().isEmpty()); assertNotNull(metadata); - Mockito.verify(applicationService, times(1)).findByIds(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID))); + Mockito + .verify(applicationService, times(1)) + .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID)), ApplicationStatus.ACTIVE); Mockito .verify(apiSearchService, times(1)) .findGenericByEnvironmentAndIdIn(eq(GraviteeContext.getExecutionContext()), eq(Set.of(API_ID))); @@ -1784,7 +1785,9 @@ public void shouldGetEmptyMetadataWithSubscriptions() { Metadata metadata = subscriptionService.getMetadata(GraviteeContext.getExecutionContext(), query); assertNotNull(metadata); - Mockito.verify(applicationService, times(0)).findByIds(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID))); + Mockito + .verify(applicationService, times(0)) + .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID)), ApplicationStatus.ACTIVE); Mockito .verify(apiSearchService, times(0)) .findGenericByEnvironmentAndIdIn(eq(GraviteeContext.getExecutionContext()), eq(Set.of(API_ID))); @@ -1813,7 +1816,9 @@ public void shouldFillApiMetadataAfterService() { assertFalse(metadata.toMap().isEmpty()); assertNotNull(metadata); - Mockito.verify(applicationService, times(0)).findByIds(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID))); + Mockito + .verify(applicationService, times(0)) + .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID)), ApplicationStatus.ACTIVE); Mockito .verify(apiSearchService, times(1)) .findGenericByEnvironmentAndIdIn(eq(GraviteeContext.getExecutionContext()), eq(Set.of(API_ID))); From 950debb3dce96b8a29038bb31b8240e26f149beb Mon Sep 17 00:00:00 2001 From: mukul-tyagi08 Date: Fri, 6 Dec 2024 12:55:03 +0530 Subject: [PATCH 2/2] fix: get archived applications to get application info for subscriptions --- .../api/ApiSubscriptionsResource.java | 3 +- .../api/ApiSubscriptionsResource_GetTest.java | 6 +- .../ApiSubscriptionsResource_ListTest.java | 6 +- .../resource/ApplicationsResourceTest.java | 12 +- .../rest/api/service/ApplicationService.java | 2 + .../service/impl/ApplicationServiceImpl.java | 13 +- .../ApplicationService_FindByIdsTest.java | 201 ++++++++++++++++++ .../service/impl/SubscriptionServiceTest.java | 6 +- 8 files changed, 229 insertions(+), 20 deletions(-) create mode 100644 gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApplicationService_FindByIdsTest.java diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource.java index a3a1d7b0c2e..62df156368d 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/main/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource.java @@ -23,7 +23,6 @@ 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; @@ -497,7 +496,7 @@ private void expandData(List subscriptions, Set expands) { .map(subscription -> (subscription.getApplication()).getId()) .collect(Collectors.toSet()); final Collection applications = applicationMapper.mapToBaseApplicationList( - applicationService.findByIdsAndStatus(executionContext, applicationIds, ApplicationStatus.ACTIVE) + applicationService.findByIds(executionContext, applicationIds) ); applications.forEach(application -> subscriptions diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_GetTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_GetTest.java index 6bf6adbd4b3..736781f979d 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_GetTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_GetTest.java @@ -31,7 +31,6 @@ 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; @@ -140,10 +139,9 @@ public void should_return_subscription_with_expands() { .thenReturn(Set.of(PlanFixtures.aPlanEntityV4().toBuilder().id(PLAN).build())); when( - applicationService.findByIdsAndStatus( + applicationService.findByIds( eq(GraviteeContext.getExecutionContext()), - argThat(argument -> List.of(APPLICATION).containsAll(argument)), - ApplicationStatus.ACTIVE + argThat(argument -> List.of(APPLICATION).containsAll(argument)) ) ) .thenReturn(Set.of(ApplicationFixtures.anApplicationListItem().toBuilder().id(APPLICATION).build())); diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_ListTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_ListTest.java index 08d34be7993..81ea3c2067c 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_ListTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-management-v2/gravitee-apim-rest-api-management-v2-rest/src/test/java/io/gravitee/rest/api/management/v2/rest/resource/api/ApiSubscriptionsResource_ListTest.java @@ -33,7 +33,6 @@ 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; @@ -212,10 +211,9 @@ public void should_return_list_of_subscriptions_with_expands() { ) ); when( - applicationService.findByIdsAndStatus( + applicationService.findByIds( eq(GraviteeContext.getExecutionContext()), - argThat(argument -> List.of("application-1", "application-2").containsAll(argument)), - ApplicationStatus.ACTIVE + argThat(argument -> List.of("application-1", "application-2").containsAll(argument)) ) ) .thenReturn( diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/test/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResourceTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/test/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResourceTest.java index dd95df96450..1da1e3ee2a4 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/test/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResourceTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-portal/gravitee-apim-rest-api-portal-rest/src/test/java/io/gravitee/rest/api/portal/rest/resource/ApplicationsResourceTest.java @@ -73,14 +73,14 @@ public void init() { .findIdsByUser(eq(GraviteeContext.getExecutionContext()), any(), any()); doReturn(new HashSet<>(Arrays.asList(applicationA, applicationB))) .when(applicationService) - .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("A", "B")), ApplicationStatus.ACTIVE); + .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("A", "B")), eq(ApplicationStatus.ACTIVE)); doReturn(new HashSet<>(Arrays.asList(applicationB, applicationA))) .when(applicationService) - .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("B", "A")), ApplicationStatus.ACTIVE); + .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("B", "A")), eq(ApplicationStatus.ACTIVE)); doReturn(new HashSet<>(Arrays.asList(applicationB))) .when(applicationService) - .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(List.of("B")), ApplicationStatus.ACTIVE); + .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(List.of("B")), eq(ApplicationStatus.ACTIVE)); doReturn(new Application().id("A").name("A")) .when(applicationMapper) @@ -149,7 +149,11 @@ public void shouldGetApplicationsOrderByName() { .findIdsByUser(eq(GraviteeContext.getExecutionContext()), any(), eq(sort)); doReturn(mockApplications) .when(applicationService) - .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Arrays.asList("A", "B", "C", "D")), ApplicationStatus.ACTIVE); + .findByIdsAndStatus( + eq(GraviteeContext.getExecutionContext()), + eq(Arrays.asList("A", "B", "C", "D")), + eq(ApplicationStatus.ACTIVE) + ); doReturn(new Application().id("A").name("A")) .when(applicationMapper) diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApplicationService.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApplicationService.java index fd0f1422d6c..3006d5420db 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApplicationService.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/ApplicationService.java @@ -43,6 +43,8 @@ public interface ApplicationService { ApplicationEntity findById(final ExecutionContext executionContext, String applicationId); + Set findByIds(final ExecutionContext executionContext, Collection applicationIds); + Set findByIdsAndStatus( final ExecutionContext executionContext, Collection applicationIds, diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationServiceImpl.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationServiceImpl.java index 56e940917a1..e8f90137c1e 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationServiceImpl.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/main/java/io/gravitee/rest/api/service/impl/ApplicationServiceImpl.java @@ -232,6 +232,11 @@ public ApplicationEntity findById(final ExecutionContext executionContext, Strin } } + @Override + public Set findByIds(ExecutionContext executionContext, Collection applicationIds) { + return findByIdsAndStatus(executionContext, applicationIds, null); + } + @Override public Set findByIdsAndStatus( final ExecutionContext executionContext, @@ -245,9 +250,11 @@ public Set findByIdsAndStatus( return Collections.emptySet(); } - ApplicationCriteria.Builder criteriaBuilder = new ApplicationCriteria.Builder() - .ids(new HashSet<>(applicationIds)) - .status(ApplicationStatus.ACTIVE); + ApplicationCriteria.Builder criteriaBuilder = new ApplicationCriteria.Builder().ids(new HashSet<>(applicationIds)); + + if (applicationStatus != null) { + criteriaBuilder.status(applicationStatus); + } if (executionContext.hasEnvironmentId()) { criteriaBuilder.environmentIds(executionContext.getEnvironmentId()); diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApplicationService_FindByIdsTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApplicationService_FindByIdsTest.java new file mode 100644 index 00000000000..a9f6ff938a8 --- /dev/null +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/ApplicationService_FindByIdsTest.java @@ -0,0 +1,201 @@ +/* + * Copyright © 2015 The Gravitee team (http://gravitee.io) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.gravitee.rest.api.service.impl; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.*; + +import com.google.common.collect.Sets; +import io.gravitee.common.data.domain.Page; +import io.gravitee.repository.exceptions.TechnicalException; +import io.gravitee.repository.management.api.ApplicationRepository; +import io.gravitee.repository.management.api.search.ApplicationCriteria; +import io.gravitee.repository.management.model.ApiKeyMode; +import io.gravitee.repository.management.model.Application; +import io.gravitee.repository.management.model.ApplicationStatus; +import io.gravitee.rest.api.model.MembershipEntity; +import io.gravitee.rest.api.model.RoleEntity; +import io.gravitee.rest.api.model.application.ApplicationListItem; +import io.gravitee.rest.api.model.permissions.RoleScope; +import io.gravitee.rest.api.service.MembershipService; +import io.gravitee.rest.api.service.RoleService; +import io.gravitee.rest.api.service.UserService; +import io.gravitee.rest.api.service.common.ExecutionContext; +import io.gravitee.rest.api.service.common.GraviteeContext; +import io.gravitee.rest.api.service.configuration.application.ClientRegistrationService; +import io.gravitee.rest.api.service.exceptions.TechnicalManagementException; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; + +/** + * @author Guillaume CUSNIEUX (guillaume.cusnieux at graviteesource.com) + * @author GraviteeSource Team + */ +@RunWith(MockitoJUnitRunner.class) +public class ApplicationService_FindByIdsTest { + + private static final List APPLICATION_IDS = Arrays.asList("id-app-1", "id-app-2"); + + @InjectMocks + private ApplicationServiceImpl applicationService = new ApplicationServiceImpl(); + + @Mock + private ApplicationRepository applicationRepository; + + @Mock + private MembershipService membershipService; + + @Mock + private UserService userService; + + @Mock + private RoleService roleService; + + @Mock + private RoleEntity primaryOwnerRole; + + @Mock + private Set primaryOwners; + + @Mock + private Application app1; + + @Mock + private Application app2; + + @Mock + private ClientRegistrationService clientRegistrationService; + + @Before + public void setUp() { + GraviteeContext.setCurrentOrganization("DEFAULT"); + GraviteeContext.setCurrentEnvironment("DEFAULT"); + // One application is active + when(app1.getStatus()).thenReturn(ApplicationStatus.ACTIVE); + when(app1.getId()).thenReturn(APPLICATION_IDS.get(0)); + when(app1.getApiKeyMode()).thenReturn(ApiKeyMode.UNSPECIFIED); + // One application is archived/deleted + when(app2.getStatus()).thenReturn(ApplicationStatus.ARCHIVED); + when(app2.getId()).thenReturn(APPLICATION_IDS.get(1)); + when(app2.getApiKeyMode()).thenReturn(ApiKeyMode.UNSPECIFIED); + + doReturn(primaryOwnerRole) + .when(roleService) + .findPrimaryOwnerRoleByOrganization(GraviteeContext.getCurrentOrganization(), RoleScope.APPLICATION); + + doReturn("role-id").when(primaryOwnerRole).getId(); + + when(membershipService.getMembershipsByReferencesAndRole(any(), any(), any())).thenReturn(primaryOwners); + } + + @After + public void tearDown() { + GraviteeContext.cleanContext(); + } + + @Test + public void shouldFindByIds() throws TechnicalException { + ExecutionContext executionContext = GraviteeContext.getExecutionContext(); + ApplicationCriteria criteria = new ApplicationCriteria.Builder() + .ids(Sets.newHashSet(APPLICATION_IDS)) + .environmentIds(executionContext.getEnvironmentId()) + .build(); + // Should return both the applications by Ids irrespective of status + doReturn(new Page(Arrays.asList(app1, app2), 1, 2, 2)).when(applicationRepository).search(criteria, null); + doReturn(2).when(primaryOwners).size(); + + final Set applications = applicationService.findByIds(executionContext, APPLICATION_IDS); + + assertNotNull(applications); + assertEquals(APPLICATION_IDS, applications.stream().map(ApplicationListItem::getId).collect(Collectors.toList())); + } + + @Test + public void shouldFindByIdsWithDuplicatedIdsAndStatus() throws TechnicalException { + ExecutionContext executionContext = GraviteeContext.getExecutionContext(); + ApplicationCriteria criteria = new ApplicationCriteria.Builder() + .ids(Sets.newHashSet(APPLICATION_IDS)) + .environmentIds(executionContext.getEnvironmentId()) + .build(); + + doReturn(new Page<>(Arrays.asList(app1, app2), 1, 2, 2)).when(applicationRepository).search(criteria, null); + doReturn(2).when(primaryOwners).size(); + + final Set applications = applicationService.findByIds( + executionContext, + List.of("id-app-1", "id-app-1", "id-app-2") + ); + + assertNotNull(applications); + assertEquals(APPLICATION_IDS, applications.stream().map(ApplicationListItem::getId).collect(Collectors.toList())); + } + + @Test + public void shouldFindByIdsWithNoEnvironmentCriteria() throws TechnicalException { + ExecutionContext executionContext = new ExecutionContext("DEFAULT", null); + ApplicationCriteria criteria = new ApplicationCriteria.Builder().ids(Sets.newHashSet(APPLICATION_IDS)).build(); + doReturn(new Page(Arrays.asList(app1, app2), 1, 2, 2)).when(applicationRepository).search(criteria, null); + doReturn(2).when(primaryOwners).size(); + + final Set applications = applicationService.findByIds(executionContext, APPLICATION_IDS); + + assertNotNull(applications); + assertEquals(APPLICATION_IDS, applications.stream().map(ApplicationListItem::getId).collect(Collectors.toList())); + } + + @Test + public void shouldFindByIdsWithEmptySet() throws TechnicalException { + ExecutionContext executionContext = GraviteeContext.getExecutionContext(); + + final Set applications = applicationService.findByIds(executionContext, Collections.emptySet()); + + assertNotNull(applications); + assertTrue(applications.isEmpty()); + verify(applicationRepository, times(0)).search(any(), any()); + } + + @Test(expected = TechnicalManagementException.class) + public void shouldThrowsIfNoPrimaryOwner() throws TechnicalException { + ExecutionContext executionContext = GraviteeContext.getExecutionContext(); + ApplicationCriteria criteria = new ApplicationCriteria.Builder() + .ids(Sets.newHashSet(APPLICATION_IDS)) + .environmentIds(executionContext.getEnvironmentId()) + .build(); + doReturn(new Page(Arrays.asList(app1, app2), 1, 2, 2)).when(applicationRepository).search(criteria, null); + + final Set applications = applicationService.findByIds(GraviteeContext.getExecutionContext(), APPLICATION_IDS); + + assertNotNull(applications); + assertEquals(APPLICATION_IDS, applications.stream().map(ApplicationListItem::getId).collect(Collectors.toList())); + } + + @Test(expected = TechnicalManagementException.class) + public void shouldThrowTechnicalManagementException() throws TechnicalException { + when(applicationRepository.search(any(), any())).thenThrow(new TechnicalException()); + applicationService.findByIds(GraviteeContext.getExecutionContext(), APPLICATION_IDS); + } +} diff --git a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/SubscriptionServiceTest.java b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/SubscriptionServiceTest.java index 9a8475ff629..a2e6b5763e6 100644 --- a/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/SubscriptionServiceTest.java +++ b/gravitee-apim-rest-api/gravitee-apim-rest-api-service/src/test/java/io/gravitee/rest/api/service/impl/SubscriptionServiceTest.java @@ -1758,7 +1758,7 @@ public void shouldGetAllMetadataWithSubscriptions() { assertNotNull(metadata); Mockito .verify(applicationService, times(1)) - .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID)), ApplicationStatus.ACTIVE); + .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID)), eq(ApplicationStatus.ACTIVE)); Mockito .verify(apiSearchService, times(1)) .findGenericByEnvironmentAndIdIn(eq(GraviteeContext.getExecutionContext()), eq(Set.of(API_ID))); @@ -1787,7 +1787,7 @@ public void shouldGetEmptyMetadataWithSubscriptions() { assertNotNull(metadata); Mockito .verify(applicationService, times(0)) - .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID)), ApplicationStatus.ACTIVE); + .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID)), eq(ApplicationStatus.ACTIVE)); Mockito .verify(apiSearchService, times(0)) .findGenericByEnvironmentAndIdIn(eq(GraviteeContext.getExecutionContext()), eq(Set.of(API_ID))); @@ -1818,7 +1818,7 @@ public void shouldFillApiMetadataAfterService() { assertNotNull(metadata); Mockito .verify(applicationService, times(0)) - .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID)), ApplicationStatus.ACTIVE); + .findByIdsAndStatus(eq(GraviteeContext.getExecutionContext()), eq(Set.of(APPLICATION_ID)), eq(ApplicationStatus.ACTIVE)); Mockito .verify(apiSearchService, times(1)) .findGenericByEnvironmentAndIdIn(eq(GraviteeContext.getExecutionContext()), eq(Set.of(API_ID)));