FactoryEditValidatorImpl implements FactoryEditValidator { @Override public void validate(Factory factory) throws ForbiddenException, ServerException { final Author author = factory.getCreator(); if (author == null || author.getUserId() == null) { throw new ServerException( format( "Invalid factory without author stored. Please contact the support about the factory ID '%s'", factory.getId())); } final String userId = EnvironmentContext.getCurrent().getSubject().getUserId(); if (!author.getUserId().equals(userId)) { throw new ForbiddenException( format("You are not authorized for the factory '%s'", factory.getId())); } }  @Override void validate(Factory factory);  }
@Test(expectedExceptions = ServerException.class) public void testNoAuthor() throws ApiException { setCurrentUser(""); factoryEditValidator.validate(factory); }
@Test(expectedExceptions = ForbiddenException.class) public void testUserIsNotTheAuthor() throws ApiException { String userId = "florent"; setCurrentUser(userId); AuthorDto author = mock(AuthorDto.class); doReturn(author).when(factory).getCreator(); doReturn("john").when(author).getUserId(); factoryEditValidator.validate(factory); }
@Test public void testUserIsTheAuthor() throws ApiException { String userId = "florent"; setCurrentUser(userId); AuthorDto author = mock(AuthorDto.class); doReturn(author).when(factory).getCreator(); doReturn(userId).when(author).getUserId(); factoryEditValidator.validate(factory); }