-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RabbitmqEventsAutoConfigurationTest
- Loading branch information
Showing
6 changed files
with
131 additions
and
47 deletions.
There are no files selected for viewing
47 changes: 28 additions & 19 deletions
47
gateway/src/main/java/org/georchestra/gateway/events/RabbitmqEventsAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,50 @@ | ||
package org.georchestra.gateway.events; | ||
|
||
import org.georchestra.gateway.autoconfigure.security.ConditionalOnCreateLdapAccounts; | ||
import org.springframework.amqp.core.AmqpTemplate; | ||
import org.springframework.amqp.core.Queue; | ||
import org.springframework.amqp.rabbit.connection.ConnectionFactory; | ||
import org.springframework.amqp.rabbit.listener.MessageListenerContainer; | ||
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer; | ||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.cloud.gateway.config.GatewayAutoConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.DependsOn; | ||
import org.springframework.context.annotation.ImportResource; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@Profile("!test && !it") | ||
/** | ||
* {@link AutoConfiguration @AutoConfiguration} to enable sending events over | ||
* rabbitmq when it is enabled to create LDAP accounts from both/either | ||
* pre-authenticated and OIDC authentication scenarios, AND | ||
* {@literal georchestra.gateway.security.enableRabbitmqEvents = true} | ||
* | ||
* @see ConditionalOnCreateLdapAccounts | ||
*/ | ||
@AutoConfiguration | ||
@ConditionalOnCreateLdapAccounts | ||
@AutoConfigureAfter(GatewayAutoConfiguration.class) | ||
@ImportResource({ "classpath:rabbit-listener-context.xml", "classpath:rabbit-sender-context.xml" }) | ||
@ConditionalOnProperty(name = "georchestra.gateway.security.enableRabbitmqEvents", havingValue = "true", matchIfMissing = false) | ||
public class RabbitmqEventsAutoConfiguration { | ||
|
||
@Bean | ||
@DependsOn({ "eventTemplate" }) | ||
RabbitmqEventsSender eventsSender(AmqpTemplate eventTemplate) { | ||
RabbitmqEventsSender eventsSender(@Qualifier("eventTemplate") AmqpTemplate eventTemplate) { | ||
return new RabbitmqEventsSender(eventTemplate); | ||
} | ||
|
||
Queue OAuth2ReplyQueue() { | ||
return new Queue("OAuth2ReplyQueue", false); | ||
} | ||
@Bean | ||
org.springframework.amqp.rabbit.connection.CachingConnectionFactory connectionFactory(// | ||
@Value("${rabbitmqHost}") String host, // | ||
@Value("${rabbitmqPort}") int port, // | ||
@Value("${rabbitmqUser}") String user, // | ||
@Value("${rabbitmqPassword}") String pwd) { | ||
|
||
com.rabbitmq.client.ConnectionFactory fac = new com.rabbitmq.client.ConnectionFactory(); | ||
fac.setHost(host); | ||
fac.setPort(port); | ||
fac.setUsername(user); | ||
fac.setPassword(pwd); | ||
|
||
MessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) { | ||
SimpleMessageListenerContainer simpleMessageListenerContainer = new SimpleMessageListenerContainer(); | ||
simpleMessageListenerContainer.setConnectionFactory(connectionFactory); | ||
simpleMessageListenerContainer.setQueues(OAuth2ReplyQueue()); | ||
simpleMessageListenerContainer.setMessageListener(new RabbitmqEventsListener()); | ||
return simpleMessageListenerContainer; | ||
return new CachingConnectionFactory(fac); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd | ||
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"> | ||
<rabbit:connection-factory id="connectionFactory" host="${rabbitmqHost}" port="${rabbitmqPort}" username="${rabbitmqUser}" password="${rabbitmqPassword}" /> | ||
<rabbit:admin connection-factory="connectionFactory" /> | ||
<!-- Create OAuth2Queue queue --> | ||
<rabbit:queue id="OAuth2ReplyQueue" /> | ||
<!-- create OAuth2Exchange and bind OAuth2Queue with routing-gateway to the OAUTH2-EXCHANGE--> | ||
<rabbit:topic-exchange id="OAuth2Exchange" name="OAUTH2-EXCHANGE-GATEWAY"> | ||
<rabbit:bindings> | ||
<rabbit:binding queue="OAuth2ReplyQueue" pattern="routing-console"></rabbit:binding> | ||
</rabbit:bindings> | ||
</rabbit:topic-exchange> | ||
<!-- instantiate eventsListener --> | ||
<bean id="eventsListener" class="org.georchestra.gateway.events.RabbitmqEventsListener" > | ||
</bean> | ||
<!-- glue the listener and OAuth2Queue to the container--> | ||
<rabbit:listener-container connection-factory="connectionFactory"> | ||
<rabbit:listener ref="eventsListener" queues="OAuth2ReplyQueue" /></rabbit:listener-container> | ||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:rabbit="http://www.springframework.org/schema/rabbit" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> | ||
|
||
<!-- defined in RabbitmqEventsAutoConfiguration.class --> | ||
<!-- | ||
<rabbit:connection-factory id="connectionFactory" | ||
host="${rabbitmqHost}" | ||
port="${rabbitmqPort}" | ||
username="${rabbitmqUser}" | ||
password="${rabbitmqPassword}" /> | ||
--> | ||
<rabbit:admin connection-factory="connectionFactory" /> | ||
|
||
<!-- Create OAuth2Queue queue --> | ||
<rabbit:queue id="OAuth2ReplyQueue" /> | ||
|
||
<!-- create OAuth2Exchange and bind OAuth2Queue with routing-gateway to the OAUTH2-EXCHANGE --> | ||
<rabbit:topic-exchange id="OAuth2Exchange" name="OAUTH2-EXCHANGE-GATEWAY"> | ||
<rabbit:bindings> | ||
<rabbit:binding queue="OAuth2ReplyQueue" pattern="routing-console"></rabbit:binding> | ||
</rabbit:bindings> | ||
</rabbit:topic-exchange> | ||
|
||
<!-- instantiate eventsListener --> | ||
<bean id="eventsListener" class="org.georchestra.gateway.events.RabbitmqEventsListener"> | ||
</bean> | ||
|
||
<!-- glue the listener and OAuth2Queue to the container --> | ||
<rabbit:listener-container connection-factory="connectionFactory"> | ||
<rabbit:listener ref="eventsListener" queues="OAuth2ReplyQueue" /> | ||
</rabbit:listener-container> | ||
</beans> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<beans xmlns="http://www.springframework.org/schema/beans" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rabbit="http://www.springframework.org/schema/rabbit" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd | ||
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd"> | ||
<!-- obtain admin rights to create the an exchange --> | ||
<rabbit:admin connection-factory="connectionFactory" /> | ||
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:rabbit="http://www.springframework.org/schema/rabbit" | ||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | ||
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd"> | ||
|
||
<!-- create a bean which can send message to OAUTH2-EXCHANGE for the program to call --> | ||
<rabbit:template id="eventTemplate" connection-factory="connectionFactory" exchange="OAUTH2-EXCHANGE"/> | ||
<!-- obtain admin rights to create the an exchange --> | ||
<rabbit:admin connection-factory="connectionFactory" /> | ||
|
||
<!-- create a bean which can send message to OAUTH2-EXCHANGE for the program to call --> | ||
<rabbit:template id="eventTemplate" connection-factory="connectionFactory" exchange="OAUTH2-EXCHANGE" /> | ||
</beans> |
59 changes: 59 additions & 0 deletions
59
...way/src/test/java/org/georchestra/gateway/events/RabbitmqEventsAutoConfigurationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package org.georchestra.gateway.events; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; | ||
import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
|
||
/** | ||
* Application context test for {@link RabbitmqEventsAutoConfiguration} | ||
*/ | ||
class RabbitmqEventsAutoConfigurationTest { | ||
|
||
private ApplicationContextRunner runner = new ApplicationContextRunner() | ||
.withConfiguration(AutoConfigurations.of(RabbitmqEventsAutoConfiguration.class)); | ||
|
||
@Test | ||
void conditionalOnPropertyNotSet() { | ||
runner.run(context -> assertThat(context).hasNotFailed().doesNotHaveBean(RabbitmqEventsSender.class)); | ||
} | ||
|
||
@Test | ||
void conditionalOnPropertyDisabled() { | ||
runner.withPropertyValues("georchestra.gateway.security.enableRabbitmqEvents=false") | ||
.run(context -> assertThat(context).hasNotFailed().doesNotHaveBean(RabbitmqEventsSender.class)); | ||
} | ||
|
||
@Test | ||
void conditionalOnPropertyEnabled_requires_default_ldap_and_create_users_enabled() { | ||
runner.withPropertyValues("georchestra.gateway.security.createNonExistingUsersInLDAP=false", // | ||
"georchestra.gateway.security.ldap.default.enabled=", // | ||
"georchestra.gateway.security.enableRabbitmqEvents=true", // | ||
"rabbitmqHost=test.rabbit", // | ||
"rabbitmqPort=3333", // | ||
"rabbitmqUser=bunny", // | ||
"rabbitmqPassword=rabbit"// | ||
).run(context -> assertThat(context).hasNotFailed().doesNotHaveBean(RabbitmqEventsSender.class)); | ||
|
||
runner.withPropertyValues("georchestra.gateway.security.createNonExistingUsersInLDAP=true", // | ||
"georchestra.gateway.security.ldap.default.enabled=true", // | ||
"georchestra.gateway.security.enableRabbitmqEvents=true", // | ||
"rabbitmqHost=test.rabbit", // | ||
"rabbitmqPort=3333", // | ||
"rabbitmqUser=bunny", // | ||
"rabbitmqPassword=rabbit"// | ||
).run(context -> { | ||
|
||
assertThat(context).hasNotFailed().hasSingleBean(RabbitmqEventsSender.class); | ||
|
||
assertThat(context).hasBean("connectionFactory"); | ||
CachingConnectionFactory rabbitMQConnectionFactory = (CachingConnectionFactory) context | ||
.getBean("connectionFactory"); | ||
assertThat(rabbitMQConnectionFactory.getHost()).isEqualTo("test.rabbit"); | ||
assertThat(rabbitMQConnectionFactory.getPort()).isEqualTo(3333); | ||
assertThat(rabbitMQConnectionFactory.getUsername()).isEqualTo("bunny"); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ | |
import com.nimbusds.oauth2.sdk.util.JSONUtils; | ||
|
||
/** | ||
* | ||
* | ||
*/ | ||
class OpenIdConnectUserMapperTest { | ||
|
||
|