ConsumerService { public Long getConsumerIdByToken(String token) { if (Strings.isNullOrEmpty(token)) { return null; } ConsumerToken consumerToken = consumerTokenRepository.findTopByTokenAndExpiresAfter(token, new Date()); return consumerToken == null ? null : consumerToken.getConsumerId(); } 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 testGetConsumerId() throws Exception { String someToken = "someToken"; long someConsumerId = 1; ConsumerToken someConsumerToken = new ConsumerToken(); someConsumerToken.setConsumerId(someConsumerId); when(consumerTokenRepository.findTopByTokenAndExpiresAfter(eq(someToken), any(Date.class))) .thenReturn(someConsumerToken); assertEquals(someConsumerId, consumerService.getConsumerIdByToken(someToken).longValue()); }
@Test public void testGetConsumerIdWithNullToken() throws Exception { Long consumerId = consumerService.getConsumerIdByToken(null); assertNull(consumerId); verify(consumerTokenRepository, never()).findTopByTokenAndExpiresAfter(anyString(), any(Date .class)); }