[Minor] fixed broken tests due to change in privilege handling

This commit is contained in:
Robert von Burg 2014-12-31 17:00:59 +01:00
parent b70ee2d228
commit 304255fcc2
2 changed files with 21 additions and 12 deletions

View File

@ -72,6 +72,7 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa
// first check that the caller may perform this service // first check that the caller may perform this service
PrivilegeContext privilegeContext; PrivilegeContext privilegeContext;
String username = certificate == null ? "null" : certificate.getUsername();
try { try {
privilegeContext = this.privilegeHandler.getPrivilegeContext(certificate); privilegeContext = this.privilegeHandler.getPrivilegeContext(certificate);
privilegeContext.validateAction(service); privilegeContext.validateAction(service);
@ -79,7 +80,7 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa
long end = System.nanoTime(); long end = System.nanoTime();
String msg = "User {0}: Service {1} failed after {2} due to {3}"; //$NON-NLS-1$ String msg = "User {0}: Service {1} failed after {2} due to {3}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, certificate.getUsername(), service.getClass().getName(), msg = MessageFormat.format(msg, username, service.getClass().getName(),
StringHelper.formatNanoDuration(end - start), e.getMessage()); StringHelper.formatNanoDuration(end - start), e.getMessage());
logger.error(msg, e); logger.error(msg, e);
@ -114,7 +115,7 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa
// log the result // log the result
long end = System.nanoTime(); long end = System.nanoTime();
String msg = "User {0}: Service {1} took {2}"; //$NON-NLS-1$ String msg = "User {0}: Service {1} took {2}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, certificate.getUsername(), service.getClass().getName(), msg = MessageFormat.format(msg, username, service.getClass().getName(),
StringHelper.formatNanoDuration(end - start)); StringHelper.formatNanoDuration(end - start));
if (serviceResult.getState() == ServiceResultState.SUCCESS) if (serviceResult.getState() == ServiceResultState.SUCCESS)
logger.info(msg); logger.info(msg);
@ -128,7 +129,7 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa
} catch (Exception e) { } catch (Exception e) {
long end = System.nanoTime(); long end = System.nanoTime();
String msg = "User {0}: Service failed {1} after {2} due to {3}"; //$NON-NLS-1$ String msg = "User {0}: Service failed {1} after {2} due to {3}"; //$NON-NLS-1$
msg = MessageFormat.format(msg, certificate.getUsername(), service.getClass().getName(), msg = MessageFormat.format(msg, username, service.getClass().getName(),
StringHelper.formatNanoDuration(end - start), e.getMessage()); StringHelper.formatNanoDuration(end - start), e.getMessage());
logger.error(msg, e); logger.error(msg, e);
throw new StrolchException(msg, e); throw new StrolchException(msg, e);

View File

@ -15,8 +15,14 @@
*/ */
package li.strolch.service.test; package li.strolch.service.test;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
import java.util.HashSet;
import li.strolch.service.api.ServiceResult;
import li.strolch.service.test.model.GreetingResult; import li.strolch.service.test.model.GreetingResult;
import li.strolch.service.test.model.GreetingService; import li.strolch.service.test.model.GreetingService;
import li.strolch.service.test.model.GreetingService.GreetingArgument; import li.strolch.service.test.model.GreetingService.GreetingArgument;
@ -40,36 +46,38 @@ public class ServiceTest extends AbstractServiceTest {
@Test @Test
public void shouldFailNoCertificate() { public void shouldFailNoCertificate() {
this.thrown.expect(PrivilegeException.class);
TestService testService = new TestService(); TestService testService = new TestService();
getServiceHandler().doService(null, testService); ServiceResult svcResult = getServiceHandler().doService(null, testService);
assertThat(svcResult.getThrowable(), instanceOf(PrivilegeException.class));
} }
@Test @Test
public void shouldFailInvalidCertificate1() { public void shouldFailInvalidCertificate1() {
this.thrown.expect(PrivilegeException.class); this.thrown.expect(PrivilegeException.class);
TestService testService = new TestService(); TestService testService = new TestService();
getServiceHandler().doService(new Certificate(null, 0, null, null, null, null, null, null, null), testService); getServiceHandler().doService(
new Certificate(null, 0, null, null, null, null, null, new HashSet<String>(), null), testService);
} }
@Test @Test
public void shouldFailInvalidCertificate2() { public void shouldFailInvalidCertificate2() {
this.thrown.expect(AccessDeniedException.class);
TestService testService = new TestService(); TestService testService = new TestService();
Certificate badCert = new Certificate( Certificate badCert = new Certificate(
"1", System.currentTimeMillis(), "bob", "Bob", "Brown", "dsdf", null, null, null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ "1", System.currentTimeMillis(), "bob", "Bob", "Brown", "dsdf", null, new HashSet<String>(), null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
getServiceHandler().doService(badCert, testService); ServiceResult svcResult = getServiceHandler().doService(badCert, testService);
assertThat(svcResult.getThrowable(), instanceOf(AccessDeniedException.class));
} }
@Test @Test
public void shouldFailWithNoAccess() { public void shouldFailWithNoAccess() {
this.thrown.expect(AccessDeniedException.class);
this.thrown.expectMessage("User jill does not have Privilege li.strolch.service.api.Service"); //$NON-NLS-1$
Certificate certificate = runtimeMock.getPrivilegeHandler().authenticate("jill", "jill".getBytes()); //$NON-NLS-1$//$NON-NLS-2$ Certificate certificate = runtimeMock.getPrivilegeHandler().authenticate("jill", "jill".getBytes()); //$NON-NLS-1$//$NON-NLS-2$
try { try {
TestService testService = new TestService(); TestService testService = new TestService();
getServiceHandler().doService(certificate, testService); ServiceResult svcResult = getServiceHandler().doService(certificate, testService);
assertThat(svcResult.getMessage(),
containsString("User jill does not have Privilege li.strolch.service.api.Service")); //$NON-NLS-1$
assertThat(svcResult.getThrowable(), instanceOf(AccessDeniedException.class));
} finally { } finally {
runtimeMock.getPrivilegeHandler().invalidateSession(certificate); runtimeMock.getPrivilegeHandler().invalidateSession(certificate);
} }