Skip to content

Commit

Permalink
rabbitmq - rework the disabled test
Browse files Browse the repository at this point in the history
Note: with a console docker image compiled with
georchestra/georchestra#4332 the test passes.
  • Loading branch information
pmauduit committed Sep 12, 2024
1 parent f2bae43 commit 6fd609d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,40 @@
import java.util.HashSet;
import java.util.Set;

import com.google.common.annotations.VisibleForTesting;
import org.json.JSONObject;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import lombok.extern.slf4j.Slf4j;

//TODO: remove class as dead code?
@Slf4j(topic = "org.georchestra.gateway.events")
@Slf4j
public class RabbitmqEventsListener implements MessageListener {

public static final String OAUTH2_ACCOUNT_CREATION_RECEIVED = "OAUTH2-ACCOUNT-CREATION-RECEIVED";

private static Set<String> synReceivedMessageUid = Collections.synchronizedSet(new HashSet<String>());

public void onMessage(Message message) {
String messageBody = new String(message.getBody());
JSONObject jsonObj = new JSONObject(messageBody);
String uid = jsonObj.getString("uid");
String subject = jsonObj.getString("subject");
if (subject.equals(OAUTH2_ACCOUNT_CREATION_RECEIVED)
&& !synReceivedMessageUid.stream().anyMatch(s -> s.equals(uid))) {
String msg = jsonObj.getString("msg");
synReceivedMessageUid.add(uid);
log.info(msg);
try {
String messageBody = new String(message.getBody());
JSONObject jsonObj = new JSONObject(messageBody);
String uid = jsonObj.getString("uid");
String subject = jsonObj.getString("subject");
if (subject.equals(OAUTH2_ACCOUNT_CREATION_RECEIVED)
&& !synReceivedMessageUid.stream().anyMatch(s -> s.equals(uid))) {
String msg = jsonObj.getString("msg");
synReceivedMessageUid.add(uid);
log.info(msg);
}

} catch (Exception e) {
log.error("Exception caught when evaluating a message from RabbitMq, it will be silently discarded.", e);
}
}

@VisibleForTesting
public static Set<String> getSynReceivedMessageUid() {
return synReceivedMessageUid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@
import org.georchestra.ds.users.AccountDao;
import org.georchestra.gateway.accounts.admin.AccountCreated;
import org.georchestra.gateway.accounts.events.rabbitmq.RabbitmqAccountCreatedEventSender;
import org.georchestra.gateway.accounts.events.rabbitmq.RabbitmqEventsListener;
import org.georchestra.gateway.app.GeorchestraGatewayApplication;
import org.georchestra.security.model.GeorchestraUser;
import org.georchestra.testcontainers.console.GeorchestraConsoleContainer;
import org.georchestra.testcontainers.ldap.GeorchestraLdapContainer;
import org.json.JSONObject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Primary;
import org.springframework.ldap.NameNotFoundException;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.TestPropertySource;
Expand Down Expand Up @@ -54,6 +59,8 @@ public class SendMessageRabbitmqIT {
private @Autowired AccountDao accountDao;
private @Autowired OrgsDao orgsDao;

private @Autowired RabbitmqEventsListener eventsListener;

private static final int smtpPort = 25;

public static final GeorchestraLdapContainer ldap = new GeorchestraLdapContainer();
Expand Down Expand Up @@ -118,13 +125,8 @@ public class SendMessageRabbitmqIT {
user.setOAuth2Uid("123");
eventPublisher.publishEvent(new AccountCreated(user));
await().atMost(30, TimeUnit.SECONDS).until(() -> {
Account testAmqp;
try {
testAmqp = accountDao.findByUID("testamqp");
} catch (NameNotFoundException e) {
return false;
}
return testAmqp != null;
return (RabbitmqEventsListener.getSynReceivedMessageUid().size() > 0);
});
}

}

0 comments on commit 6fd609d

Please sign in to comment.