@Test void generateAndStoreToken() { User user = mock(User.class); @SuppressWarnings("unchecked") Query<User> q = mock(Query.class); when(q.eq(USERNAME, "admin")).thenReturn(q); when(q.findOne()).thenReturn(user); when(dataService.query(USER, User.class)).thenReturn(q); when(tokenGenerator.generateToken()).thenReturn("token"); assertEquals("token", tokenService.generateAndStoreToken("admin", "description")); ArgumentCaptor<Token> argumentCaptor = ArgumentCaptor.forClass(Token.class); verify(dataService).add(eq(TOKEN), argumentCaptor.capture()); Token token = argumentCaptor.getValue(); verify(token).setToken("token"); }
@Override @Transactional @RunAsSystem public String generateAndStoreToken(String username, String description) { User user = dataService.query(USER, User.class).eq(USERNAME, username).findOne(); if (user == null) { throw new IllegalArgumentException(format("Unknown username [%s]", username)); } String token = tokenGenerator.generateToken(); Token molgenisToken = tokenFactory.create(); molgenisToken.setUser(user); molgenisToken.setToken(token); molgenisToken.setDescription(description); molgenisToken.setExpirationDate(now().plus(2, HOURS)); dataService.add(TOKEN, molgenisToken); return token; }
DataServiceTokenService implements TokenService { @Override @Transactional @RunAsSystem public String generateAndStoreToken(String username, String description) { User user = dataService.query(USER, User.class).eq(USERNAME, username).findOne(); if (user == null) { throw new IllegalArgumentException(format("Unknown username [%s]", username)); } String token = tokenGenerator.generateToken(); Token molgenisToken = tokenFactory.create(); molgenisToken.setUser(user); molgenisToken.setToken(token); molgenisToken.setDescription(description); molgenisToken.setExpirationDate(now().plus(2, HOURS)); dataService.add(TOKEN, molgenisToken); return token; } }
DataServiceTokenService implements TokenService { @Override @Transactional @RunAsSystem public String generateAndStoreToken(String username, String description) { User user = dataService.query(USER, User.class).eq(USERNAME, username).findOne(); if (user == null) { throw new IllegalArgumentException(format("Unknown username [%s]", username)); } String token = tokenGenerator.generateToken(); Token molgenisToken = tokenFactory.create(); molgenisToken.setUser(user); molgenisToken.setToken(token); molgenisToken.setDescription(description); molgenisToken.setExpirationDate(now().plus(2, HOURS)); dataService.add(TOKEN, molgenisToken); return token; } DataServiceTokenService(
      TokenGenerator tokenGenerator,
      DataService dataService,
      UserDetailsService userDetailsService,
      TokenFactory tokenFactory); }
DataServiceTokenService implements TokenService { @Override @Transactional @RunAsSystem public String generateAndStoreToken(String username, String description) { User user = dataService.query(USER, User.class).eq(USERNAME, username).findOne(); if (user == null) { throw new IllegalArgumentException(format("Unknown username [%s]", username)); } String token = tokenGenerator.generateToken(); Token molgenisToken = tokenFactory.create(); molgenisToken.setUser(user); molgenisToken.setToken(token); molgenisToken.setDescription(description); molgenisToken.setExpirationDate(now().plus(2, HOURS)); dataService.add(TOKEN, molgenisToken); return token; } DataServiceTokenService(
      TokenGenerator tokenGenerator,
      DataService dataService,
      UserDetailsService userDetailsService,
      TokenFactory tokenFactory); @Override @Transactional(readOnly = true) @RunAsSystem UserDetails findUserByToken(String token); @Override @Transactional @RunAsSystem String generateAndStoreToken(String username, String description); @Override @Transactional @RunAsSystem void removeToken(String token); }
DataServiceTokenService implements TokenService { @Override @Transactional @RunAsSystem public String generateAndStoreToken(String username, String description) { User user = dataService.query(USER, User.class).eq(USERNAME, username).findOne(); if (user == null) { throw new IllegalArgumentException(format("Unknown username [%s]", username)); } String token = tokenGenerator.generateToken(); Token molgenisToken = tokenFactory.create(); molgenisToken.setUser(user); molgenisToken.setToken(token); molgenisToken.setDescription(description); molgenisToken.setExpirationDate(now().plus(2, HOURS)); dataService.add(TOKEN, molgenisToken); return token; } DataServiceTokenService(
      TokenGenerator tokenGenerator,
      DataService dataService,
      UserDetailsService userDetailsService,
      TokenFactory tokenFactory); @Override @Transactional(readOnly = true) @RunAsSystem UserDetails findUserByToken(String token); @Override @Transactional @RunAsSystem String generateAndStoreToken(String username, String description); @Override @Transactional @RunAsSystem void removeToken(String token);  }