MachineTokenRegistry { public String getOrCreateToken(String userId, String workspaceId) throws MachineTokenException { lock.writeLock().lock(); try { final Map<String, String> wsRow = tokens.row(workspaceId); String token = wsRow.get(userId); if (token == null) { token = createToken(userId, workspaceId); } return token; } finally { lock.writeLock().unlock(); } } @Inject  MachineTokenRegistry(SignatureKeyManager signatureKeyManager, UserManager userManager); String getOrCreateToken(String userId, String workspaceId); Map<String, String> removeTokens(String workspaceId);  }
@Test public void testCreatesNewTokenWhenNoPreviouslyCreatedFound() throws Exception { final String generatedToken = tokenRegistry.getOrCreateToken(USER_ID, WORKSPACE_ID); final Claims claims = Jwts.parser().setSigningKey(keyPair.getPublic()).parseClaimsJws(generatedToken).getBody(); final SubjectImpl subject = new SubjectImpl( claims.get(USER_NAME_CLAIM, String.class), claims.get(USER_ID_CLAIM, String.class), null, false); assertEquals(subject.getUserId(), USER_ID); assertEquals(subject.getUserName(), USER_NAME); assertEquals(claims.get(WORKSPACE_ID_CLAIM, String.class), WORKSPACE_ID); verify(userManager).getById(USER_ID); verify(signatureKeyManager).getOrCreateKeyPair(anyString()); assertNotNull(generatedToken); }
@Test(expectedExceptions = MachineTokenException.class) public void testThrowsIllegalStateExceptionWhenTryToGetTokenForNonExistingUser() throws Exception { when(userManager.getById(anyString())).thenThrow(new NotFoundException("User not found")); tokenRegistry.getOrCreateToken(USER_ID, WORKSPACE_ID); }