@Test
  public void testCreateS3AttachmentToken()
      throws NumberFormatException, DatastoreException, NotFoundException, UnauthorizedException,
          InvalidModelException {
    S3AttachmentToken startToken = new S3AttachmentToken();
    startToken.setFileName("SomeFile.jpg");
    String md5 = "79054025255fb1a26e4bc422aef54eb4";
    startToken.setMd5(md5);
    Long tokenId = new Long(456);
    String entityId = "132";
    String userId = "007";
    String expectedPath =
        S3TokenManagerImpl.createAttachmentPathSlash(entityId, tokenId.toString());

    String expectePreSigneUrl = "I am a presigned url! whooot!";
    when(mocIdGenerator.generateNewId()).thenReturn(tokenId);
    Credentials mockCreds = Mockito.mock(Credentials.class);
    when(mockUuserManager.getUserInfo(any(String.class))).thenReturn(mockUser);
    when(mockPermissionsManager.hasAccess(entityId, ACCESS_TYPE.UPDATE, mockUser)).thenReturn(true);
    when(mocKLocationHelper.createFederationTokenForS3(userId, HttpMethod.PUT, expectedPath))
        .thenReturn(mockCreds);
    when(mocKLocationHelper.presignS3PUTUrl(
            any(Credentials.class), any(String.class), any(String.class), any(String.class)))
        .thenReturn(expectePreSigneUrl);
    // Make the actual call
    S3AttachmentToken endToken = manager.createS3AttachmentToken(userId, entityId, startToken);
    assertNotNull(endToken);
    assertEquals(expectePreSigneUrl, endToken.getPresignedUrl());
  }
 @Test
 public void testCreateTokenIdInvalidChars() {
   Long id = new Long(456);
   String fileName = "i have~!@#$%^&*()_+{}|/\\spaces.log";
   String tokenId = S3TokenManagerImpl.createTokenId(id, fileName);
   assertEquals("456/i_have________*_________spaces.log", tokenId);
 }
 @Test
 public void testCreateTokenIdSpaces() {
   Long id = new Long(456);
   String fileName = "i have spaces.log";
   String tokenId = S3TokenManagerImpl.createTokenId(id, fileName);
   assertEquals("456/i_have_spaces.log", tokenId);
 }
 @Test
 public void testCreateTokenIdMultiDot() {
   Long id = new Long(456);
   String fileName = "catalina.2011-05-16.log";
   String tokenId = S3TokenManagerImpl.createTokenId(id, fileName);
   assertEquals("456/catalina.2011-05-16.log", tokenId);
 }
 @Test
 public void testCreateTokenId() {
   Long id = new Long(456);
   String fileName = "image.jpg";
   String tokenId = S3TokenManagerImpl.createTokenId(id, fileName);
   assertEquals("456/image.jpg", tokenId);
 }
 @Test
 public void testGetAttachmentUrl() throws Exception {
   Long tokenId = new Long(456);
   String entityId = "132";
   String expectedPath =
       S3TokenManagerImpl.createAttachmentPathSlash(entityId, tokenId.toString());
   String expectePreSigneUrl = "I am a presigned url! whooot!";
   when(mockUuserManager.getUserInfo(userId)).thenReturn(mockUser);
   when(mockPermissionsManager.hasAccess(entityId, ACCESS_TYPE.READ, mockUser)).thenReturn(true);
   when(mocKLocationHelper.presignS3GETUrlShortLived(userId, expectedPath))
       .thenReturn(expectePreSigneUrl);
   when(mockS3Utilitiy.doesExist(any(String.class))).thenReturn(true);
   // Make the actual call
   when(mockUuserManager.getUserInfo(userId)).thenReturn(mockUser);
   PresignedUrl url = manager.getAttachmentUrl(userId, entityId, tokenId.toString());
   assertNotNull(url);
   assertEquals(expectePreSigneUrl, url.getPresignedUrl());
   assertEquals(URLStatus.READ_FOR_DOWNLOAD, url.getStatus());
 }
 @Test
 public void testValidateUpdateAccessPass()
     throws DatastoreException, NotFoundException, UnauthorizedException {
   String userId = "123456";
   String entityId = "abc";
   // return the mock user.
   when(mockUuserManager.getUserInfo(userId)).thenReturn(mockUser);
   // Say now to this
   when(mockPermissionsManager.hasAccess(entityId, ACCESS_TYPE.UPDATE, mockUser)).thenReturn(true);
   when(mockPermissionsManager.hasAccess(entityId, ACCESS_TYPE.READ, mockUser))
       .thenThrow(new IllegalArgumentException("Update and not read should have been checked"));
   manager.validateUpdateAccess(mockUser, entityId);
 }
 @Test
 public void testValidateContentTypeTxt() {
   String expectedType = "text/plain";
   String type = manager.validateContentType("SomeFile.txt");
   assertEquals(expectedType, type);
 }
 @Test
 public void testValidateContentTypeXLS() {
   String expectedType = "application/binary";
   String type = manager.validateContentType("SomeFile.xls");
   assertEquals(expectedType, type);
 }
 /**
  * This is a valid MD5
  *
  * @throws InvalidModelException
  */
 @Test
 public void testValidateMd5Valid() throws InvalidModelException {
   String md5 = "79054025255fb1a26e4bc422aef54eb4";
   manager.validateMd5(md5);
 }
 @Test(expected = InvalidModelException.class)
 public void testValidateMd5Invalid() throws InvalidModelException {
   String notAnMd5AtAll = "not an md5";
   manager.validateMd5(notAnMd5AtAll);
 }
 @Test(expected = IllegalArgumentException.class)
 public void testCreateTokenIdNullName() {
   Long id = new Long(456);
   String tokenId = S3TokenManagerImpl.createTokenId(id, null);
   assertEquals("456.jpg", tokenId);
 }
 @Test(expected = IllegalArgumentException.class)
 public void testCreateTokenIdNullId() {
   String fileName = "image.jpg";
   String tokenId = S3TokenManagerImpl.createTokenId(null, fileName);
   assertEquals("456.jpg", tokenId);
 }