Skip to content

Commit

Permalink
fix: throw the right error when accessing api or application
Browse files Browse the repository at this point in the history
  • Loading branch information
ytvnr committed Jul 3, 2023
1 parent 4160c9d commit 85f647d
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
Expand Up @@ -1285,10 +1285,14 @@ public MemberEntity getUserMember(
Set<String> entityGroups = new HashSet<>();
switch (referenceType) {
case API:
entityGroups = apiRepository.findById(referenceId).orElseThrow().getGroups();
entityGroups = apiRepository.findById(referenceId).orElseThrow(() -> new ApiNotFoundException(referenceId)).getGroups();
break;
case APPLICATION:
entityGroups = applicationRepository.findById(referenceId).orElseThrow().getGroups();
entityGroups =
applicationRepository
.findById(referenceId)
.orElseThrow(() -> new ApplicationNotFoundException(referenceId))
.getGroups();
break;
default:
break;
Expand Down
@@ -0,0 +1,84 @@
/**
* Copyright (C) 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.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import io.gravitee.repository.exceptions.TechnicalException;
import io.gravitee.repository.management.api.ApiRepository;
import io.gravitee.repository.management.api.ApplicationRepository;
import io.gravitee.repository.management.api.MembershipRepository;
import io.gravitee.rest.api.model.MembershipReferenceType;
import io.gravitee.rest.api.service.MembershipService;
import io.gravitee.rest.api.service.common.ExecutionContext;
import io.gravitee.rest.api.service.common.GraviteeContext;
import io.gravitee.rest.api.service.exceptions.ApiNotFoundException;
import io.gravitee.rest.api.service.exceptions.ApplicationNotFoundException;
import java.util.Optional;
import java.util.Set;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

/**
* @author Yann TAVERNIER (yann.tavernier at graviteesource.com)
* @author GraviteeSource Team
*/
@RunWith(MockitoJUnitRunner.class)
public class MembershipService_GetUserMemberTest {

@InjectMocks
private MembershipService cut = new MembershipServiceImpl();

@Mock
private MembershipRepository membershipRepository;

@Mock
private ApiRepository apiRepository;

@Mock
private ApplicationRepository applicationRepository;

@Test
public void should_throw_if_no_api_found() throws TechnicalException {
when(membershipRepository.findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(any(), any(), any(), any()))
.thenReturn(Set.of());
when(apiRepository.findById("reference-id")).thenReturn(Optional.empty());

assertThatThrownBy(() ->
cut.getUserMember(GraviteeContext.getExecutionContext(), MembershipReferenceType.API, "reference-id", "user-id")
)
.isInstanceOf(ApiNotFoundException.class);
}

@Test
public void should_throw_if_no_app_found() throws TechnicalException {
when(membershipRepository.findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(any(), any(), any(), any()))
.thenReturn(Set.of());
when(applicationRepository.findById("reference-id")).thenReturn(Optional.empty());

assertThatThrownBy(() ->
cut.getUserMember(GraviteeContext.getExecutionContext(), MembershipReferenceType.APPLICATION, "reference-id", "user-id")
)
.isInstanceOf(ApplicationNotFoundException.class);
}
}

0 comments on commit 85f647d

Please sign in to comment.