From 304255fcc2f3b11d62a4d742b520b597adabafc9 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 31 Dec 2014 17:00:59 +0100 Subject: [PATCH] [Minor] fixed broken tests due to change in privilege handling --- .../service/api/DefaultServiceHandler.java | 7 ++--- .../li/strolch/service/test/ServiceTest.java | 26 ++++++++++++------- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/li.strolch.agent/src/main/java/li/strolch/service/api/DefaultServiceHandler.java b/li.strolch.agent/src/main/java/li/strolch/service/api/DefaultServiceHandler.java index 9900cbb8f..4990f1ba0 100644 --- a/li.strolch.agent/src/main/java/li/strolch/service/api/DefaultServiceHandler.java +++ b/li.strolch.agent/src/main/java/li/strolch/service/api/DefaultServiceHandler.java @@ -72,6 +72,7 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa // first check that the caller may perform this service PrivilegeContext privilegeContext; + String username = certificate == null ? "null" : certificate.getUsername(); try { privilegeContext = this.privilegeHandler.getPrivilegeContext(certificate); privilegeContext.validateAction(service); @@ -79,7 +80,7 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa long end = System.nanoTime(); 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()); logger.error(msg, e); @@ -114,7 +115,7 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa // log the result long end = System.nanoTime(); 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)); if (serviceResult.getState() == ServiceResultState.SUCCESS) logger.info(msg); @@ -128,7 +129,7 @@ public class DefaultServiceHandler extends StrolchComponent implements ServiceHa } catch (Exception e) { long end = System.nanoTime(); 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()); logger.error(msg, e); throw new StrolchException(msg, e); diff --git a/li.strolch.service/src/test/java/li/strolch/service/test/ServiceTest.java b/li.strolch.service/src/test/java/li/strolch/service/test/ServiceTest.java index 5c3687f5d..c66aef8e1 100644 --- a/li.strolch.service/src/test/java/li/strolch/service/test/ServiceTest.java +++ b/li.strolch.service/src/test/java/li/strolch/service/test/ServiceTest.java @@ -15,8 +15,14 @@ */ package li.strolch.service.test; +import static org.hamcrest.Matchers.containsString; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; 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.GreetingService; import li.strolch.service.test.model.GreetingService.GreetingArgument; @@ -40,36 +46,38 @@ public class ServiceTest extends AbstractServiceTest { @Test public void shouldFailNoCertificate() { - this.thrown.expect(PrivilegeException.class); TestService testService = new TestService(); - getServiceHandler().doService(null, testService); + ServiceResult svcResult = getServiceHandler().doService(null, testService); + assertThat(svcResult.getThrowable(), instanceOf(PrivilegeException.class)); } @Test public void shouldFailInvalidCertificate1() { this.thrown.expect(PrivilegeException.class); 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(), null), testService); } @Test public void shouldFailInvalidCertificate2() { - this.thrown.expect(AccessDeniedException.class); TestService testService = new TestService(); Certificate badCert = new Certificate( - "1", System.currentTimeMillis(), "bob", "Bob", "Brown", "dsdf", null, null, null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - getServiceHandler().doService(badCert, testService); + "1", System.currentTimeMillis(), "bob", "Bob", "Brown", "dsdf", null, new HashSet(), null); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + ServiceResult svcResult = getServiceHandler().doService(badCert, testService); + assertThat(svcResult.getThrowable(), instanceOf(AccessDeniedException.class)); } @Test 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$ try { 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 { runtimeMock.getPrivilegeHandler().invalidateSession(certificate); }