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"));
}
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);
}
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);
}
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);
}
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()));
}
Aggregations