Skip to content

Commit

Permalink
fix: get archived applications to get application info for subscriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul-tyagi08 committed Dec 6, 2024
1 parent d1f0f2e commit 950debb
Show file tree
Hide file tree
Showing 8 changed files with 229 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -497,7 +496,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.findByIdsAndStatus(executionContext, applicationIds, ApplicationStatus.ACTIVE)
applicationService.findByIds(executionContext, applicationIds)
);
applications.forEach(application ->
subscriptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,11 @@ public ApplicationEntity findById(final ExecutionContext executionContext, Strin
}
}

@Override
public Set<ApplicationListItem> findByIds(ExecutionContext executionContext, Collection<String> applicationIds) {
return findByIdsAndStatus(executionContext, applicationIds, null);
}

@Override
public Set<ApplicationListItem> findByIdsAndStatus(
final ExecutionContext executionContext,
Expand All @@ -245,9 +250,11 @@ public Set<ApplicationListItem> 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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> 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<MembershipEntity> 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<ApplicationListItem> 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<ApplicationListItem> 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<ApplicationListItem> 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<ApplicationListItem> 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<ApplicationListItem> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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)));
Expand Down

0 comments on commit 950debb

Please sign in to comment.