[BAEL 1382] Update Unit Test (#3630)
* [tlinh2110-BAEL1382] Add Security in SI * [tlinh2110-BAEL1382] Upgrade to Spring 5 & add Logger * [tlinh2110-BAEL-1382] Update Unit Test
This commit is contained in:
parent
3c371bea45
commit
d488e69301
@ -1,8 +1,11 @@
|
|||||||
package com.baeldung.si;
|
package com.baeldung.si;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.hamcrest.core.IsInstanceOf;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.ExpectedException;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.messaging.SubscribableChannel;
|
import org.springframework.messaging.SubscribableChannel;
|
||||||
@ -21,6 +24,9 @@ import com.baeldung.si.security.SecurityConfig;
|
|||||||
@ContextConfiguration(classes = { SecurityConfig.class, SecuredDirectChannel.class, MessageConsumer.class })
|
@ContextConfiguration(classes = { SecurityConfig.class, SecuredDirectChannel.class, MessageConsumer.class })
|
||||||
public class TestSpringIntegrationSecurity {
|
public class TestSpringIntegrationSecurity {
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public ExpectedException expectedException = ExpectedException.none();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
SubscribableChannel startDirectChannel;
|
SubscribableChannel startDirectChannel;
|
||||||
|
|
||||||
@ -34,34 +40,28 @@ public class TestSpringIntegrationSecurity {
|
|||||||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = AccessDeniedException.class)
|
@Test
|
||||||
@WithMockUser(username = "jane", roles = { "LOGGER" })
|
@WithMockUser(username = "jane", roles = { "LOGGER" })
|
||||||
public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied() throws Throwable {
|
public void givenRoleLogger_whenSendMessageToDirectChannel_thenAccessDenied() {
|
||||||
try {
|
expectedException.expectCause(IsInstanceOf.<Throwable> instanceOf(AccessDeniedException.class));
|
||||||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
|
||||||
} catch (Exception e) {
|
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||||
throw e.getCause();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = AccessDeniedException.class)
|
@Test
|
||||||
@WithMockUser(username = "jane")
|
@WithMockUser(username = "jane")
|
||||||
public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied() throws Throwable {
|
public void givenJane_whenSendMessageToDirectChannel_thenAccessDenied() {
|
||||||
try {
|
expectedException.expectCause(IsInstanceOf.<Throwable> instanceOf(AccessDeniedException.class));
|
||||||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
|
||||||
} catch (Exception e) {
|
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||||
throw e.getCause();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = AccessDeniedException.class)
|
@Test
|
||||||
@WithMockUser(roles = { "VIEWER" })
|
@WithMockUser(roles = { "VIEWER" })
|
||||||
public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied() throws Throwable {
|
public void givenRoleViewer_whenSendToDirectChannel_thenAccessDenied() {
|
||||||
try {
|
expectedException.expectCause(IsInstanceOf.<Throwable> instanceOf(AccessDeniedException.class));
|
||||||
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
|
||||||
} catch (Exception e) {
|
startDirectChannel.send(new GenericMessage<String>(DIRECT_CHANNEL_MESSAGE));
|
||||||
throw e.getCause();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -78,4 +78,4 @@ public class TestSpringIntegrationSecurity {
|
|||||||
assertEquals(DIRECT_CHANNEL_MESSAGE, messageConsumer.getMessageContent());
|
assertEquals(DIRECT_CHANNEL_MESSAGE, messageConsumer.getMessageContent());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user