ConsumerService { public List<ConsumerRole> assignNamespaceRoleToConsumer(String token, String appId, String namespaceName) { return assignNamespaceRoleToConsumer(token, appId, namespaceName, null); } ConsumerService(
      final UserInfoHolder userInfoHolder,
      final ConsumerTokenRepository consumerTokenRepository,
      final ConsumerRepository consumerRepository,
      final ConsumerAuditRepository consumerAuditRepository,
      final ConsumerRoleRepository consumerRoleRepository,
      final PortalConfig portalConfig,
      final RolePermissionService rolePermissionService,
      final UserService userService); Consumer createConsumer(Consumer consumer); ConsumerToken generateAndSaveConsumerToken(Consumer consumer, Date expires); ConsumerToken getConsumerTokenByAppId(String appId); Long getConsumerIdByToken(String token); Consumer getConsumerByConsumerId(long consumerId); List<ConsumerRole> assignNamespaceRoleToConsumer(String token, String appId, String namespaceName); @Transactional List<ConsumerRole> assignNamespaceRoleToConsumer(String token, String appId, String namespaceName, String env); @Transactional ConsumerRole assignAppRoleToConsumer(String token, String appId); @Transactional void createConsumerAudits(Iterable<ConsumerAudit> consumerAudits); @Transactional ConsumerToken createConsumerToken(ConsumerToken entity);  }
@Test public void testAssignNamespaceRoleToConsumer() { Long consumerId = 1L; String token = "token"; doReturn(consumerId).when(consumerService).getConsumerIdByToken(token); String testNamespace = "namespace"; String modifyRoleName = RoleUtils.buildModifyNamespaceRoleName(testAppId, testNamespace); String releaseRoleName = RoleUtils.buildReleaseNamespaceRoleName(testAppId, testNamespace); String envModifyRoleName = RoleUtils.buildModifyNamespaceRoleName(testAppId, testNamespace, Env.DEV.toString()); String envReleaseRoleName = RoleUtils.buildReleaseNamespaceRoleName(testAppId, testNamespace, Env.DEV.toString()); long modifyRoleId = 1; long releaseRoleId = 2; long envModifyRoleId = 3; long envReleaseRoleId = 4; Role modifyRole = createRole(modifyRoleId, modifyRoleName); Role releaseRole = createRole(releaseRoleId, releaseRoleName); Role envModifyRole = createRole(envModifyRoleId, modifyRoleName); Role envReleaseRole = createRole(envReleaseRoleId, releaseRoleName); when(rolePermissionService.findRoleByRoleName(modifyRoleName)).thenReturn(modifyRole); when(rolePermissionService.findRoleByRoleName(releaseRoleName)).thenReturn(releaseRole); when(rolePermissionService.findRoleByRoleName(envModifyRoleName)).thenReturn(envModifyRole); when(rolePermissionService.findRoleByRoleName(envReleaseRoleName)).thenReturn(envReleaseRole); when(consumerRoleRepository.findByConsumerIdAndRoleId(consumerId, modifyRoleId)).thenReturn(null); UserInfo owner = createUser(testOwner); when(userInfoHolder.getUser()).thenReturn(owner); ConsumerRole namespaceModifyConsumerRole = createConsumerRole(consumerId, modifyRoleId); ConsumerRole namespaceEnvModifyConsumerRole = createConsumerRole(consumerId, envModifyRoleId); ConsumerRole namespaceReleaseConsumerRole = createConsumerRole(consumerId, releaseRoleId); ConsumerRole namespaceEnvReleaseConsumerRole = createConsumerRole(consumerId, envReleaseRoleId); doReturn(namespaceModifyConsumerRole).when(consumerService).createConsumerRole(consumerId, modifyRoleId, testOwner); doReturn(namespaceEnvModifyConsumerRole).when(consumerService).createConsumerRole(consumerId, envModifyRoleId, testOwner); doReturn(namespaceReleaseConsumerRole).when(consumerService).createConsumerRole(consumerId, releaseRoleId, testOwner); doReturn(namespaceEnvReleaseConsumerRole).when(consumerService).createConsumerRole(consumerId, envReleaseRoleId, testOwner); consumerService.assignNamespaceRoleToConsumer(token, testAppId, testNamespace); consumerService.assignNamespaceRoleToConsumer(token, testAppId, testNamespace, Env.DEV.toString()); verify(consumerRoleRepository).save(namespaceModifyConsumerRole); verify(consumerRoleRepository).save(namespaceEnvModifyConsumerRole); verify(consumerRoleRepository).save(namespaceReleaseConsumerRole); verify(consumerRoleRepository).save(namespaceEnvReleaseConsumerRole); }