Search in sources :

Example 11 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class QualityMetricsServiceTest method shouldScore100PercentWithManualRules.

@Test
public void shouldScore100PercentWithManualRules() {
    when(parameterService.findAsBoolean(Key.API_QUALITY_METRICS_ENABLED, ParameterReferenceType.ENVIRONMENT)).thenReturn(Boolean.TRUE);
    Map<String, List<Object>> map = new HashMap<>();
    map.put(Key.API_QUALITY_METRICS_LOGO_WEIGHT.key(), singletonList(1));
    map.put(Key.API_QUALITY_METRICS_CATEGORIES_WEIGHT.key(), singletonList(1));
    when(parameterService.findAll(anyList(), any(Function.class), any(ParameterReferenceType.class))).thenReturn(map);
    ApiEntity api = mock(ApiEntity.class);
    when(api.getId()).thenReturn("apiID");
    when(apiQualityMetricLogo.isValid(any())).thenReturn(Boolean.TRUE);
    when(apiQualityMetricCategories.isValid(any())).thenReturn(Boolean.TRUE);
    final QualityRuleEntity qualityRule = mock(QualityRuleEntity.class);
    when(qualityRule.getId()).thenReturn("1");
    when(qualityRule.getWeight()).thenReturn(1);
    when(qualityRuleService.findAll()).thenReturn(singletonList(qualityRule));
    final ApiQualityRuleEntity apiQualityRule = mock(ApiQualityRuleEntity.class);
    when(apiQualityRule.getApi()).thenReturn("apiID");
    when(apiQualityRule.getQualityRule()).thenReturn("1");
    when(apiQualityRule.isChecked()).thenReturn(true);
    when(apiQualityRuleService.findByApi("apiID")).thenReturn(singletonList(apiQualityRule));
    ApiQualityMetricsEntity metrics = srv.getMetrics(api);
    assertEquals(1, metrics.getScore(), 0);
    assertFalse(metrics.getMetricsPassed().isEmpty());
    assertTrue(metrics.getMetricsPassed().get(Key.API_QUALITY_METRICS_LOGO_WEIGHT.key()));
    assertTrue(metrics.getMetricsPassed().get(Key.API_QUALITY_METRICS_CATEGORIES_WEIGHT.key()));
    assertTrue(metrics.getMetricsPassed().get("1"));
}
Also used : Function(java.util.function.Function) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) QualityRuleEntity(io.gravitee.rest.api.model.quality.QualityRuleEntity) ApiQualityRuleEntity(io.gravitee.rest.api.model.quality.ApiQualityRuleEntity) ApiQualityMetricsEntity(io.gravitee.rest.api.model.ApiQualityMetricsEntity) Collections.singletonList(java.util.Collections.singletonList) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) ApiQualityRuleEntity(io.gravitee.rest.api.model.quality.ApiQualityRuleEntity) Test(org.junit.Test)

Example 12 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class MembershipService_GetMemberPermissionsTest method shouldGetPermissionsIfMemberOfApi.

@Test
public void shouldGetPermissionsIfMemberOfApi() throws Exception {
    ApiEntity api = mock(ApiEntity.class);
    doReturn(API_ID).when(api).getId();
    doReturn(Collections.emptySet()).when(api).getGroups();
    doReturn(api).when(apiService).findById(API_ID);
    Membership membership = mock(Membership.class);
    doReturn("API_" + ROLENAME).when(membership).getRoleId();
    doReturn(new HashSet<>(asList(membership))).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
    UserEntity userEntity = mock(UserEntity.class);
    doReturn(userEntity).when(userService).findById(USERNAME);
    RoleEntity roleEntity = mock(RoleEntity.class);
    Map<String, char[]> rolePerms = new HashMap<>();
    rolePerms.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.UPDATE.getId(), RolePermissionAction.CREATE.getId() });
    doReturn(rolePerms).when(roleEntity).getPermissions();
    doReturn(roleEntity).when(roleService).findById("API_" + ROLENAME);
    Map<String, char[]> permissions = membershipService.getUserMemberPermissions(api, USERNAME);
    assertNotNull(permissions);
    assertPermissions(rolePerms, permissions);
    verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
    verify(membershipRepository, never()).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.GROUP, GROUP_ID1);
    verify(apiService, times(1)).findById(API_ID);
    verify(userService, times(1)).findById(USERNAME);
    verify(roleService, times(1)).findById("API_" + ROLENAME);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 13 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class MembershipService_GetMemberPermissionsTest method shouldGetMergedPermissionsIfMemberOfApiAndApiGroup.

@Test
public void shouldGetMergedPermissionsIfMemberOfApiAndApiGroup() throws Exception {
    ApiEntity api = mock(ApiEntity.class);
    doReturn(API_ID).when(api).getId();
    doReturn(Collections.singleton(GROUP_ID1)).when(api).getGroups();
    doReturn(api).when(apiService).findById(API_ID);
    Membership membershipUser = mock(Membership.class);
    doReturn("API_" + ROLENAME).when(membershipUser).getRoleId();
    doReturn(new HashSet<>(asList(membershipUser))).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
    Membership membershipGroup = mock(Membership.class);
    doReturn("API_" + ROLENAME2).when(membershipGroup).getRoleId();
    doReturn(new HashSet<>(asList(membershipGroup))).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.GROUP, GROUP_ID1);
    UserEntity userEntity = mock(UserEntity.class);
    doReturn(userEntity).when(userService).findById(USERNAME);
    RoleEntity roleEntity = mock(RoleEntity.class);
    Map<String, char[]> rolePerms = new HashMap<>();
    rolePerms.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.UPDATE.getId(), RolePermissionAction.CREATE.getId() });
    doReturn(rolePerms).when(roleEntity).getPermissions();
    doReturn(roleEntity).when(roleService).findById("API_" + ROLENAME);
    RoleEntity roleEntity2 = mock(RoleEntity.class);
    Map<String, char[]> rolePerms2 = new HashMap<>();
    rolePerms2.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.READ.getId(), RolePermissionAction.DELETE.getId() });
    rolePerms2.put(ApiPermission.PLAN.getName(), new char[] { RolePermissionAction.READ.getId() });
    doReturn(rolePerms2).when(roleEntity2).getPermissions();
    doReturn(RoleScope.API).when(roleEntity2).getScope();
    doReturn(roleEntity2).when(roleService).findById("API_" + ROLENAME2);
    Map<String, char[]> permissions = membershipService.getUserMemberPermissions(api, USERNAME);
    assertNotNull(permissions);
    Map<String, char[]> expectedPermissions = new HashMap<>();
    expectedPermissions.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.CREATE.getId(), RolePermissionAction.READ.getId(), RolePermissionAction.UPDATE.getId(), RolePermissionAction.DELETE.getId() });
    expectedPermissions.put(ApiPermission.PLAN.getName(), new char[] { RolePermissionAction.READ.getId() });
    assertPermissions(expectedPermissions, permissions);
    verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
    verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.GROUP, GROUP_ID1);
    verify(apiService, times(1)).findById(API_ID);
    verify(userService, times(1)).findById(USERNAME);
    verify(roleService, times(1)).findById("API_" + ROLENAME);
    verify(roleService, times(1)).findById("API_" + ROLENAME2);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 14 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class MembershipService_GetMemberPermissionsTest method shouldGetNoPermissionsIfNotMemberAndWithNoGroup.

@Test
public void shouldGetNoPermissionsIfNotMemberAndWithNoGroup() throws Exception {
    ApiEntity api = mock(ApiEntity.class);
    doReturn(API_ID).when(api).getId();
    doReturn(Collections.emptySet()).when(api).getGroups();
    doReturn(api).when(apiService).findById(API_ID);
    doReturn(Collections.emptySet()).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
    Map<String, char[]> permissions = membershipService.getUserMemberPermissions(api, USERNAME);
    assertNotNull(permissions);
    assertTrue(permissions.isEmpty());
    verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
    verify(apiService, times(1)).findById(API_ID);
}
Also used : ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Test(org.junit.Test)

Example 15 with ApiEntity

use of io.gravitee.rest.api.model.api.ApiEntity in project gravitee-management-rest-api by gravitee-io.

the class QualityMetricsServiceTest method shouldScore50Percent.

@Test
public void shouldScore50Percent() {
    when(parameterService.findAsBoolean(Key.API_QUALITY_METRICS_ENABLED, ParameterReferenceType.ENVIRONMENT)).thenReturn(Boolean.TRUE);
    Map<String, List<Object>> map = new HashMap<>();
    map.put(Key.API_QUALITY_METRICS_LOGO_WEIGHT.key(), singletonList(1));
    map.put(Key.API_QUALITY_METRICS_CATEGORIES_WEIGHT.key(), singletonList(1));
    when(parameterService.findAll(anyList(), any(Function.class), any(ParameterReferenceType.class))).thenReturn(map);
    ApiEntity api = mock(ApiEntity.class);
    when(apiQualityMetricLogo.isValid(any())).thenReturn(Boolean.TRUE);
    when(apiQualityMetricCategories.isValid(any())).thenReturn(Boolean.FALSE);
    ApiQualityMetricsEntity metrics = srv.getMetrics(api);
    assertEquals(0.5, metrics.getScore(), 0);
    assertFalse(metrics.getMetricsPassed().isEmpty());
    assertTrue(metrics.getMetricsPassed().get(Key.API_QUALITY_METRICS_LOGO_WEIGHT.key()));
    assertFalse(metrics.getMetricsPassed().get(Key.API_QUALITY_METRICS_CATEGORIES_WEIGHT.key()));
}
Also used : Function(java.util.function.Function) ParameterReferenceType(io.gravitee.rest.api.model.parameters.ParameterReferenceType) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) ApiQualityMetricsEntity(io.gravitee.rest.api.model.ApiQualityMetricsEntity) Collections.singletonList(java.util.Collections.singletonList) ArgumentMatchers.anyList(org.mockito.ArgumentMatchers.anyList) Test(org.junit.Test)

Aggregations

ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)273 Test (org.junit.Test)180 Response (javax.ws.rs.core.Response)65 Before (org.junit.Before)46 ApiQuery (io.gravitee.rest.api.model.api.ApiQuery)35 Error (io.gravitee.rest.api.portal.rest.model.Error)32 java.util (java.util)30 Collectors (java.util.stream.Collectors)27 Mockito.doReturn (org.mockito.Mockito.doReturn)27 Mockito (org.mockito.Mockito)25 ArgumentMatchers (org.mockito.ArgumentMatchers)24 UpdateApiEntity (io.gravitee.rest.api.model.api.UpdateApiEntity)23 Assert (org.junit.Assert)22 Collections.singletonList (java.util.Collections.singletonList)21 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)20 IOException (java.io.IOException)20 HttpStatusCode (io.gravitee.common.http.HttpStatusCode)19 Entity (javax.ws.rs.client.Entity)19 HashSet (java.util.HashSet)18 Api (io.gravitee.repository.management.model.Api)17